-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.c
228 lines (196 loc) · 4.7 KB
/
server.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include <stdio.h>
#include <stdlib.h>
#include <libwebsockets.h>
#include <jansson.h>
int parseWsMessage(char *msg, int len);
#define WS_MSG_MAX_SIZE 512
static void *wsMsgBuffer = NULL;
static struct lws_context *clientContext = NULL;
static struct lws *clientWebsocket = NULL;
static int callback_http(
struct lws *wsi,
enum lws_callback_reasons reason,
void *user,
void *input,
size_t len)
{
switch (reason)
{
case LWS_CALLBACK_ESTABLISHED:
printf("New connection from Client \n");
break;
case LWS_CALLBACK_RECEIVE:
printf("Received: %s\n", (char *)input);
parseWsMessage((char *)input, len);
break;
case LWS_CALLBACK_CLOSED:
printf("Connection Closed\n");
break;
case LWS_CALLBACK_GET_THREAD_ID:
break;
default:
printf ("Callback http %d\n", reason);
break;
}
return 0;
}
static int callback_client_http(
struct lws *wsi,
enum lws_callback_reasons reason,
void *user,
void *input,
size_t len)
{
switch (reason)
{
case LWS_CALLBACK_ESTABLISHED:
printf("Client New connection Established \n");
break;
case LWS_CALLBACK_CLIENT_RECEIVE:
printf("Client Received: %s\n", (char *)input);
parseWsMessage((char *)input, len);
break;
case LWS_CALLBACK_CLOSED:
case LWS_CALLBACK_CLIENT_CONNECTION_ERROR:
printf("Client Connection Closed\n");
break;
case LWS_CALLBACK_CLIENT_WRITEABLE:
printf("Got a request to send something to the other user.\n");
unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 256 + LWS_SEND_BUFFER_POST_PADDING];
unsigned char *text = &buf[LWS_SEND_BUFFER_PRE_PADDING];
int size = sprintf(text, "Hello From Proxy!");
lws_write(clientWebsocket, text, size, LWS_WRITE_TEXT);
case LWS_CALLBACK_GET_THREAD_ID:
break;
default:
printf ("Client default reason %d\n", reason);
break;
}
return 0;
}
static struct lws_protocols serverProtocols[] =
{
{
"http-only",
&callback_http,
0,
0
},
{
NULL, NULL, 0, 0
}
};
static struct lws_protocols clientProtocols[] =
{
{
"http-only",
&callback_client_http,
0,
0
},
{
NULL, NULL, 0, 0
}
};
int main()
{
printf("Starting WebSocket Server\n");
struct lws_context_creation_info contextInfo;
memset(&contextInfo, 0, sizeof(contextInfo));
contextInfo.port = 8010;
contextInfo.iface = NULL;
contextInfo.protocols = serverProtocols;
contextInfo.extensions = NULL;
contextInfo.token_limits = NULL;
contextInfo.ssl_private_key_password = NULL;
contextInfo.ssl_cert_filepath = NULL;
contextInfo.ssl_private_key_filepath = NULL;
contextInfo.ssl_ca_filepath = NULL;
contextInfo.ssl_cipher_list = NULL;
contextInfo.http_proxy_address = NULL;
contextInfo.http_proxy_port = 0;
contextInfo.gid = -1;
contextInfo.uid = -1;
contextInfo.options = 0;
contextInfo.user = NULL;
contextInfo.ka_time = 900;
contextInfo.ka_probes = 5;
contextInfo.ka_interval = 30;
wsMsgBuffer = malloc(WS_MSG_MAX_SIZE*sizeof(char));
struct lws_context *context = lws_create_context(&contextInfo);
while(1)
{
lws_service(context, 250);
lws_service(clientContext,250);
usleep(50000);
}
return 0;
}
int parseWsMessage(char *msg, int len)
{
int ret = 0;
json_error_t error;
if (len >= WS_MSG_MAX_SIZE)
{
printf("Message too big %d\n", len);
ret = -1;
}
else
{
memset(wsMsgBuffer, 0, WS_MSG_MAX_SIZE*sizeof(char));
memcpy(wsMsgBuffer, msg, len);
}
json_t *root = json_loads(wsMsgBuffer, 0, &error);
if (!root)
{
printf("Error on parsing JSON line: %d, text: %s \n", error.line, error.text);
}
else
{
json_t *type = json_object_get(root, "type");
const char *type_text = NULL;
int type_int;
if (type != NULL)
{
type_int = json_integer_value(type);
printf("Type of command : %d\n", type_int);
if (type_int == 12)
{
startWsClient("127.0.0.1", 8011);
}
else
{
lws_callback_on_writable(clientWebsocket);
}
}
else
{
printf("Cannot find type in %s\n", wsMsgBuffer);
}
}
json_decref(root);
return ret;
}
int startWsClient(const char *serverIp, int serverPort)
{
struct lws_context_creation_info info;
memset(&info, 0, sizeof(info));
info.port = CONTEXT_PORT_NO_LISTEN;
info.protocols = clientProtocols;
info.gid = -1;
info.uid = -1;
printf("Creating Client Context\n");
clientContext = lws_create_context(&info);
//Connect to server
struct lws_client_connect_info connectInfo;
memset(&connectInfo, 0, sizeof(connectInfo));
connectInfo.context = clientContext;
connectInfo.address = serverIp;
connectInfo.port = 8010;
connectInfo.path = "/";
connectInfo.host = lws_canonical_hostname(clientContext);
connectInfo.origin = "origin";
connectInfo.protocol = clientProtocols[0].name;
printf("Connecting Client Websocket\n");
clientWebsocket = lws_client_connect_via_info(&connectInfo);
}