-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathminimal_client.c
116 lines (89 loc) · 2.47 KB
/
minimal_client.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
#include <neat.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#define QUOTE(...) #__VA_ARGS__
/*
* Note: Do not edit this file without updating the line numbers in the tutorial
* in the documentation.
*/
static neat_error_code
on_readable(struct neat_flow_operations *ops)
{
uint32_t bytes_read = 0;
unsigned char buffer[32];
if (neat_read(ops->ctx, ops->flow, buffer, 31, &bytes_read, NULL, 0) == NEAT_OK) {
buffer[bytes_read] = 0;
fprintf(stdout, "Read %u bytes:\n%s", bytes_read, buffer);
}
neat_close(ops->ctx, ops->flow);
return NEAT_OK;
}
static neat_error_code
on_close(struct neat_flow_operations *ops) {
neat_stop_event_loop(ops->ctx);
return NEAT_OK;
}
static neat_error_code
on_writable(struct neat_flow_operations *ops)
{
const unsigned char message[] = "Hi!";
neat_write(ops->ctx, ops->flow, message, 3, NULL, 0);
return NEAT_OK;
}
static neat_error_code
on_all_written(struct neat_flow_operations *ops)
{
ops->on_readable = on_readable;
ops->on_writable = NULL;
neat_set_operations(ops->ctx, ops->flow, ops);
return NEAT_OK;
}
static neat_error_code
on_connected(struct neat_flow_operations *ops)
{
ops->on_writable = on_writable;
ops->on_all_written = on_all_written;
neat_set_operations(ops->ctx, ops->flow, ops);
return NEAT_OK;
}
static char *properties = QUOTE(
{"transport": {
"value": ["SCTP", "TCP", "SCTP/UDP"],
"precedence": 2}
}
);
int
main(int argc, char *argv[])
{
struct neat_ctx *ctx;
struct neat_flow *flow;
struct neat_flow_operations ops;
ctx = neat_init_ctx();
if (!ctx) {
fprintf(stderr, "neat_init_ctx failed\n");
return EXIT_FAILURE;
}
flow = neat_new_flow(ctx);
if (!flow) {
fprintf(stderr, "neat_new_flow failed\n");
return EXIT_FAILURE;
}
memset(&ops, 0, sizeof(ops));
ops.on_connected = on_connected;
ops.on_close = on_close;
neat_log_level(ctx, NEAT_LOG_INFO);
neat_set_operations(ctx, flow, &ops);
if (neat_set_property(ctx, flow, properties) != NEAT_OK) {
fprintf(stderr, "neat_set_property failed\n");
return EXIT_FAILURE;
}
if (neat_open(ctx, flow, "127.0.0.1", 5000, NULL, 0)) {
fprintf(stderr, "neat_open failed\n");
return EXIT_FAILURE;
}
neat_start_event_loop(ctx, NEAT_RUN_DEFAULT);
neat_free_ctx(ctx);
return EXIT_SUCCESS;
}