-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathexample2.c
139 lines (100 loc) · 3.5 KB
/
example2.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
136
137
138
139
/*
** 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"
#include "uv_send_message.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 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_sent(send_message_t *req, int status) {
if ( status < 0 ) {
printf("message send failed: %s user_data: %d\n", (char*)req->msg, (int)req->data);
} else {
printf("message sent: %s user_data: %d\n", (char*)req->msg, (int)req->data);
}
}
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 = "Yeaaah!";
send_message(client, response, strlen(response)+1, UV_MSG_STATIC, on_msg_sent, 0);
}
}
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;
}
/* we are connected! start the reading messages on this stream (asynchronously) */
uv_msg_read_start(socket, alloc_buffer, on_msg_received, free_buffer);
/* now send some messages */
msg = "Hello Mom!";
send_message(socket, msg, strlen(msg)+1, UV_MSG_STATIC, 0, 0);
msg = "Hello Dad!";
send_message(socket, msg, strlen(msg)+1, UV_MSG_TRANSIENT, 0, 0);
msg = strdup("Hello World!");
send_message(socket, msg, strlen(msg)+1, free, 0, 0);
msg = "Hello Mom!";
send_message(socket, msg, strlen(msg)+1, UV_MSG_STATIC, on_msg_sent, (void*)123);
msg = "Hello Dad!";
send_message(socket, msg, strlen(msg)+1, UV_MSG_TRANSIENT, on_msg_sent, (void*)124);
msg = strdup("Hello World!");
send_message(socket, msg, strlen(msg)+1, free, on_msg_sent, (void*)125);
msg = "Is it working?";
send_message(socket, msg, strlen(msg)+1, UV_MSG_STATIC, on_msg_sent, (void*)126);
}
#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