This repository has been archived by the owner on Feb 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcs_example.c
106 lines (84 loc) · 2.75 KB
/
cs_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
#include <assert.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include "deps/dragonnet/listen.h"
#include "dnet-types.h"
static DragonnetListener *l;
static void connect_func(DragonnetPeer *p)
{
char *str_addr = dragonnet_addr_str(p->raddr);
printf("new connection from %s\n", str_addr);
free(str_addr);
}
static void handle_pingpacket(DragonnetPeer *p, PingPacket *ping)
{
Blob blob = ping->compr_blob->blob;
printf("PingPacket siz: 0x%08x\n", blob->siz);
printf("PingPacket compr_siz: 0x%08x\n", ping->compr_blob->siz);
printf("PingPacket data: 0x%02x%02x\n", blob->data[0], blob->data[1]);
free(blob->data);
free(blob);
dragonnet_peer_send_PongPacket(p, &(PongPacket) {
.number = 0xdba,
.num_array = {0xd, 0xba},
.nested_array = {
{0xd, 0xba},
{0x13, 0x37}
}
});
dragonnet_listener_close(l);
dragonnet_listener_delete(l);
l = NULL;
dragonnet_peer_close(p);
dragonnet_peer_delete(p);
}
static void handle_pongpacket(DragonnetPeer *p, PongPacket *pong)
{
printf("PongPacket number: 0x%08x\n", pong->number);
printf("PongPacket num_array[0]: 0x%02x\n", pong->num_array[0]);
printf("PongPacket num_array[1]: 0x%02x\n", pong->num_array[1]);
printf("PongPacket nested_array[0][0]: 0x%02x\n", pong->nested_array[0][0]);
printf("PongPacket nested_array[0][1]: 0x%02x\n", pong->nested_array[0][1]);
printf("PongPacket nested_array[1][0]: 0x%02x\n", pong->nested_array[1][0]);
printf("PongPacket nested_array[1][1]: 0x%02x\n", pong->nested_array[1][1]);
dragonnet_peer_close(p);
dragonnet_peer_delete(p);
}
static void *srv_func(__attribute((unused)) void *unused)
{
l = dragonnet_listener_new("[::1]:50000", &connect_func);
assert(l != NULL);
dragonnet_listener_set_recv_hook(l, DRAGONNET_TYPE_PINGPACKET,
(void (*)(DragonnetPeer *, void *)) &handle_pingpacket);
dragonnet_listener_run(l);
return NULL;
}
static void *clt_func(__attribute((unused)) void *unused)
{
while (l == NULL);
DragonnetPeer *p = dragonnet_connect("[::1]:50000");
assert(p != NULL);
dragonnet_peer_set_recv_hook(p, DRAGONNET_TYPE_PONGPACKET,
(void (*)(DragonnetPeer *, void *)) &handle_pongpacket);
dragonnet_peer_run(p);
CompressedBlob compr_blob = malloc(sizeof *compr_blob);
compr_blob->blob = malloc(sizeof *compr_blob->blob);
compr_blob->blob->siz = 2;
compr_blob->blob->data = (u8 *) "\x0d\xba";
dragonnet_peer_send_PingPacket(p, &(PingPacket) {
.compr_blob = compr_blob
});
free(compr_blob->blob);
free(compr_blob);
return NULL;
}
int main(__attribute((unused)) int argc, __attribute((unused)) char **argv)
{
pthread_t srv_thread, clt_thread;
pthread_create(&srv_thread, NULL, &srv_func, NULL);
pthread_create(&clt_thread, NULL, &clt_func, NULL);
pthread_join(clt_thread, NULL);
pthread_join(srv_thread, NULL);
while (true);
}