-
Notifications
You must be signed in to change notification settings - Fork 9
/
example.c
135 lines (98 loc) · 3.14 KB
/
example.c
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
** This example code must be run with the echo-server running.
** It is in this same folder.
*/
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <uv.h>
#include "uv_msg_framing.c"
#define DEFAULT_PORT 7000
#ifdef _WIN32
# define PIPENAME "\\\\?\\pipe\\some.name"
#elif defined (__android__)
# define PIPENAME "/data/local/tmp/some.name"
#else
# define PIPENAME "/tmp/some.name"
#endif
/****************************************************************************/
void send_message(uv_msg_t* socket, char *msg, int size, uv_write_cb write_cb) {
uv_msg_send_t *req = malloc(sizeof(uv_msg_send_t));
/* save the data pointer to release on completion */
req->data = msg;
uv_msg_send(req, socket, msg, size, write_cb);
}
void on_msg_sent(uv_write_t *req, int status) {
if ( status < 0 ) {
puts("message write failed");
} else {
puts("message sent");
}
/* release the message data */
free(req->data);
/* release the write request */
free(req);
}
/* buffer allocation and release. used with the message reading */
void alloc_buffer(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf) {
buf->base = (char*) malloc(suggested_size);
buf->len = suggested_size;
}
void free_buffer(uv_handle_t* handle, void* ptr) {
free(ptr);
}
void on_msg_received(uv_msg_t *client, void *msg, int size) {
if (size < 0) {
if (size != UV_EOF) {
fprintf(stderr, "Read error: %s\n", uv_err_name(size));
}
uv_close((uv_handle_t*) client, NULL);
return;
}
printf("new message received (%d bytes): %s\n", size, (char*)msg);
if (strcmp(msg, "Is it working?") == 0) {
char *response = strdup("Yeaaah!");
send_message(client, response, strlen(response)+1, on_msg_sent);
}
}
void on_connect(uv_connect_t *connect, int status) {
uv_msg_t* socket = (uv_msg_t*) connect->handle;
char *msg;
free(connect);
if (status < 0) {
fprintf(stderr, "Connection error: %s\n", uv_strerror(status));
return;
}
uv_msg_read_start(socket, alloc_buffer, on_msg_received, free_buffer);
msg = strdup("Hello World!");
send_message(socket, msg, strlen(msg)+1, on_msg_sent);
msg = strdup("Is it working?");
send_message(socket, msg, strlen(msg)+1, on_msg_sent);
}
#ifdef USE_PIPE_EXAMPLE
int main() {
int rc;
uv_loop_t *loop = uv_default_loop();
uv_msg_t* socket = malloc(sizeof(uv_msg_t));
rc = uv_msg_init(loop, socket, UV_NAMED_PIPE);
uv_connect_t* connect = malloc(sizeof(uv_connect_t));
uv_pipe_connect(connect, (uv_pipe_t*)socket, PIPENAME, on_connect);
return uv_run(loop, UV_RUN_DEFAULT);
}
#else
int main() {
int rc;
uv_loop_t *loop = uv_default_loop();
uv_msg_t* socket = malloc(sizeof(uv_msg_t));
rc = uv_msg_init(loop, socket, UV_TCP);
struct sockaddr_in dest;
uv_ip4_addr("127.0.0.1", DEFAULT_PORT, &dest);
uv_connect_t* connect = malloc(sizeof(uv_connect_t));
rc = uv_tcp_connect(connect, (uv_tcp_t*)socket, (const struct sockaddr*)&dest, on_connect);
if (rc) {
fprintf(stderr, "Connect error: %s\n", uv_strerror(rc));
return 1;
}
return uv_run(loop, UV_RUN_DEFAULT);
}
#endif