aboutsummaryrefslogtreecommitdiff
path: root/source/tools/pipe_client.c
blob: f00bc92d9c67071c8c521baed04422375665ad9e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <windows.h>
#include <stdio.h>

#include "xor_strings.h"

#define BUFSIZE 512

int main(int argc, char** argv)
{
    HANDLE hPipe;
    LPSTR  lpvMessage = "Default message from client.";
    BOOL   fSuccess = FALSE;
    DWORD  cbToWrite, cbWritten;
    LPCSTR  lpszPipename = MILLER_MSGPIPE;

    if(argc > 1) {
        lpvMessage = argv[1];
    }
 
    // Try to open a named pipe; wait for it, if necessary.
    while (1) {
        hPipe = CreateFile(
            lpszPipename,   // pipe name
            GENERIC_WRITE,
            0,              // no sharing
            NULL,           // default security attributes
            OPEN_EXISTING,  // opens existing pipe
            0,              // default attributes
            NULL);          // no template file
        // Break if the pipe handle is valid.
        if (hPipe != INVALID_HANDLE_VALUE) {
            break;
        }
        // Exit if an error other than ERROR_PIPE_BUSY occurs.
        if (GetLastError() != ERROR_PIPE_BUSY) {
            printf("Could not open pipe. (ERROR: %lu)\n", GetLastError());
            return -1;
        }
        // All pipe instances are busy, so wait for 20 seconds.
        if (!WaitNamedPipe(lpszPipename, 20000)) {
            printf("Could not open pipe: 20 second wait timed out.\n");
            return -1;
        }
    }

    printf("Pipe opened: %s\n", lpszPipename);
    // Send a message to the pipe server.
    cbToWrite = (lstrlen(lpvMessage)+1)*sizeof(char);

    char line[1024];
    size_t sent = 0;
    do {
        printf("Sending %lu byte message: \"%s\"\n", cbToWrite, lpvMessage);
        fSuccess = WriteFile( 
            hPipe,       // pipe handle
            lpvMessage,  // message
            cbToWrite,   // message length
            &cbWritten,  // bytes written
            NULL);       // not overlapped
        if (!fSuccess) {
            printf("WriteFile to pipe failed. (ERROR: %lu)\n", GetLastError());
            return -1;
        }
        sent++;
        if (argc == 1) {
            memset(&line[0], '\0', sizeof(line));
            printf("Input: ");
            if (fgets(line, sizeof(line), stdin) == NULL) {
                break;
            }
            lpvMessage = &line[0];
            cbToWrite = strnlen(lpvMessage, sizeof(line));
            if (lpvMessage[cbToWrite-1] == '\n') {
                lpvMessage[cbToWrite-1] = '\0';
                cbToWrite--;
            }
        } else break;
    } while (1);

    printf("%u messages sent to server\n", sent);
    CloseHandle(hPipe);
    return 0;
}