/* filename: pipe_server.c */
#define UNICODE
#include <windows.h>
#include <stdio.h>
int main() {
wchar_t buf[1024];
int numBytes = 0;
BOOL writeGood = FALSE;
HANDLE hPipe = NULL;
hPipe = CreateNamedPipe(L"\\\\.\\pipe\\purpose",
PIPE_ACCESS_DUPLEX,
PIPE_TYPE_BYTE,
1,
512, /* out-buf size */
512, /* in-buf size */
0,
NULL);
if (hPipe == INVALID_HANDLE_VALUE) {
_putws(L"hPipe error.\n");
return 0;
}
puts("server is listening...");
if (ConnectNamedPipe(hPipe, NULL) == TRUE) {
writeGood = WriteFile(hPipe, L"^_^", 8, &numBytes, NULL);
printf("numBytes for writefile = %d\n", numBytes);
if (writeGood == FALSE) return 0;
_putws(L"sleeping...");
Sleep(8000);
writeGood = WriteFile(hPipe, L">_<", 8, &numBytes, NULL);
printf("numBytes for writefile = %d\n", numBytes);
if (writeGood == FALSE) return 0;
ReadFile(hPipe,
buf,
1024 * sizeof(wchar_t),
&numBytes,
NULL);
printf("numBytes for readfile = %d\n", numBytes);
_putws(buf);
}
FlushFileBuffers(hPipe);
DisconnectNamedPipe(hPipe);
CloseHandle(hPipe);
return 0;
}
LyogZmlsZW5hbWU6IHBpcGVfc2VydmVyLmMgKi8KCiNkZWZpbmUgVU5JQ09ERQoKI2luY2x1ZGUgPHdpbmRvd3MuaD4KI2luY2x1ZGUgPHN0ZGlvLmg+CgppbnQgbWFpbigpIHsKICB3Y2hhcl90IGJ1ZlsxMDI0XTsKICBpbnQgbnVtQnl0ZXMgPSAwOwogIEJPT0wgd3JpdGVHb29kID0gRkFMU0U7CgogIEhBTkRMRSBoUGlwZSA9IE5VTEw7CiAgaFBpcGUgPSBDcmVhdGVOYW1lZFBpcGUoTCJcXFxcLlxccGlwZVxccHVycG9zZSIsCiAgICAgICAgICAgICAgICAgICAgICAgICAgUElQRV9BQ0NFU1NfRFVQTEVYLAogICAgICAgICAgICAgICAgICAgICAgICAgIFBJUEVfVFlQRV9CWVRFLAogICAgICAgICAgICAgICAgICAgICAgICAgIDEsCiAgICAgICAgICAgICAgICAgICAgICAgICAgNTEyLCAvKiBvdXQtYnVmIHNpemUgKi8KICAgICAgICAgICAgICAgICAgICAgICAgICA1MTIsIC8qIGluLWJ1ZiBzaXplICovCiAgICAgICAgICAgICAgICAgICAgICAgICAgMCwKICAgICAgICAgICAgICAgICAgICAgICAgICBOVUxMKTsKCiAgaWYgKGhQaXBlID09IElOVkFMSURfSEFORExFX1ZBTFVFKSB7CiAgICBfcHV0d3MoTCJoUGlwZSBlcnJvci5cbiIpOwogICAgcmV0dXJuIDA7CiAgfQoKICBwdXRzKCJzZXJ2ZXIgaXMgbGlzdGVuaW5nLi4uIik7CiAgaWYgKENvbm5lY3ROYW1lZFBpcGUoaFBpcGUsIE5VTEwpID09IFRSVUUpIHsKICAgIHdyaXRlR29vZCA9IFdyaXRlRmlsZShoUGlwZSwgTCJeX14iLCA4LCAmbnVtQnl0ZXMsIE5VTEwpOwogICAgcHJpbnRmKCJudW1CeXRlcyBmb3Igd3JpdGVmaWxlID0gJWRcbiIsIG51bUJ5dGVzKTsKICAgIGlmICh3cml0ZUdvb2QgPT0gRkFMU0UpICByZXR1cm4gMDsKCiAgICBfcHV0d3MoTCJzbGVlcGluZy4uLiIpOwogICAgU2xlZXAoODAwMCk7CgogICAgd3JpdGVHb29kID0gV3JpdGVGaWxlKGhQaXBlLCBMIj5fPCIsIDgsICZudW1CeXRlcywgTlVMTCk7CiAgICBwcmludGYoIm51bUJ5dGVzIGZvciB3cml0ZWZpbGUgPSAlZFxuIiwgbnVtQnl0ZXMpOwogICAgaWYgKHdyaXRlR29vZCA9PSBGQUxTRSkgIHJldHVybiAwOwoKICAgIFJlYWRGaWxlKGhQaXBlLCAKICAgICAgICAgICAgIGJ1ZiwgCiAgICAgICAgICAgICAxMDI0ICogc2l6ZW9mKHdjaGFyX3QpLAogICAgICAgICAgICAgJm51bUJ5dGVzLAogICAgICAgICAgICAgTlVMTCk7CiAgICBwcmludGYoIm51bUJ5dGVzIGZvciByZWFkZmlsZSA9ICVkXG4iLCBudW1CeXRlcyk7CiAgICBfcHV0d3MoYnVmKTsKICB9CgogIEZsdXNoRmlsZUJ1ZmZlcnMoaFBpcGUpOwogIERpc2Nvbm5lY3ROYW1lZFBpcGUoaFBpcGUpOyAKICBDbG9zZUhhbmRsZShoUGlwZSk7CiAgcmV0dXJuIDA7Cn0=