#if _WIN32 || _WIN64
const char *pipename = "\\\\.\\pipe\\my.pipe";
#else
cosnt char *pipename = "my.pipe";
#endif
void echo_read(uv_stream_t *client, ssize_t nread, const uv_buf_t *buf) {
if (nread < 0) {
if (nread != UV_EOF)
fprintf(stderr, "Read error %s\n", uv_err_name(nread));
uv_close((uv_handle_t*) client, NULL);
return;
}
fprintf(stdout, "Received message: %s\n", buf->base);
free(buf->base);
}
void on_new_connection(uv_stream_t *server, int status) {
if (status == -1) {
// error!
return;
}
uv_pipe_t *client = (uv_pipe_t*) malloc(sizeof(uv_pipe_t));
uv_pipe_init(loop, client, 0);
if (uv_accept(server, (uv_stream_t*) client) == 0) {
uv_read_start((uv_stream_t*) client, alloc_buffer, echo_read);
}
else {
uv_close((uv_handle_t*) client, NULL);
}
}
void main {
loop = uv_default_loop();
uv_pipe_t server;
uv_pipe_init(loop, &server, 0);
signal(SIGINT, remove_sock);
char *args[3];
char *y = new char[processPath.length()+1];
std::strcpy(y, processPath.c_str());
char pidBuffer[19];
itoa(getProcessID(), pidBuffer, 10);
args[0] = y;
args[1] = pidBuffer;
args[2] = NULL;
// startup the listener
fprintf(stdout, "Starting Listener...\n");
int q;
if ((q = uv_pipe_bind(&server, pipename))) {
fprintf(stderr, "Bind error %s\n", uv_err_name(q));
return false;
}
if ((q = uv_listen((uv_stream_t*) &server, 16, on_new_connection))) {
fprintf(stderr, "Listen error %s\n", uv_err_name(q));
return false;
}
return (uv_run(loop, UV_RUN_DEFAULT) == 0);
}