-
Notifications
You must be signed in to change notification settings - Fork 0
/
websocket.cpp
178 lines (146 loc) · 5.17 KB
/
websocket.cpp
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <sys/time.h>
#include <libwebsockets.h>
//---------
int close_testing = 1;
int force_exit = 0;
struct lws_context *context;
//---------
enum demo_protocols {
/* always first */
PROTOCOL_DUMB_INCREMENT = 0,
/* always last */
DEMO_PROTOCOL_COUNT
};
//-----------
struct per_session_data__dumb_increment {
int number;
};
//********************
int callback_dumb_increment(struct lws *wsi, enum lws_callback_reasons reason,
void *user, void *in, size_t len)
{
unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 512];
struct per_session_data__dumb_increment *pss =
(struct per_session_data__dumb_increment *)user;
unsigned char *p = &buf[LWS_SEND_BUFFER_PRE_PADDING];
int n, m;
switch (reason) {
case LWS_CALLBACK_ESTABLISHED:
pss->number = 0;
break;
case LWS_CALLBACK_SERVER_WRITEABLE:
n = sprintf((char *)p, "%d", pss->number++);
m = lws_write(wsi, p, n, LWS_WRITE_TEXT);
if (m < n) {
lwsl_err("ERROR %d writing to di socket\n", n);
return -1;
}
if (close_testing && pss->number == 50) {
lwsl_info("close tesing limit, closing\n");
return -1;
}
break;
case LWS_CALLBACK_RECEIVE:
if (len < 6)
break;
if (strcmp((const char *)in, "reset\n") == 0)
pss->number = 0;
if (strcmp((const char *)in, "closeme\n") == 0) {
lwsl_notice("dumb_inc: closing as requested\n");
lws_close_reason(wsi, LWS_CLOSE_STATUS_GOINGAWAY,
(unsigned char *)"seeya", 5);
return -1;
}
break;
case LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION:
break;
/*
* this just demonstrates how to handle
* LWS_CALLBACK_WS_PEER_INITIATED_CLOSE and extract the peer's close
* code and auxiliary data. You can just not handle it if you don't
* have a use for this.
*/
case LWS_CALLBACK_WS_PEER_INITIATED_CLOSE:
lwsl_notice("LWS_CALLBACK_WS_PEER_INITIATED_CLOSE: len %d\n",
len);
for (n = 0; n < (int)len; n++)
lwsl_notice(" %d: 0x%02X\n", n,
((unsigned char *)in)[n]);
break;
default:
break;
}
return 0;
}//callback_dumb_increment
//-----------
static struct lws_protocols protocols[] = {
/* first protocol must always be HTTP handler */
{
"dumb-increment-protocol",
callback_dumb_increment,
sizeof(struct per_session_data__dumb_increment),
10,
},
{ NULL, NULL, 0, 0 } /* terminator */
};//protocols
//**************************
void sighandler(int sig)
{
force_exit = 1;
lws_cancel_service(context);
}
//*********************************************************************
int main(int argc, char **argv)
{
struct lws_context_creation_info info;
int opts = 0;
int n = 0;
unsigned int ms, oldms = 0;
memset(&info, 0, sizeof info);
info.port = 9999;
info.protocols = protocols;
info.ssl_cert_filepath = NULL;
info.ssl_private_key_filepath = NULL;
info.extensions = lws_get_internal_extensions();
info.gid = -1;
info.uid = -1;
info.max_http_header_pool = 1;
info.options = opts;
signal(SIGINT, sighandler);
context = lws_create_context(&info);
if (context == NULL) {
lwsl_err("libwebsocket init failed\n");
return -1;
}
n = 0;
while (n >= 0 && !force_exit) {
struct timeval tv;
gettimeofday(&tv, NULL);
/*
* This provokes the LWS_CALLBACK_SERVER_WRITEABLE for every
* live websocket connection using the DUMB_INCREMENT protocol,
* as soon as it can take more packets (usually immediately)
*/
ms = (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
if ((ms - oldms) > 50) {
lws_callback_on_writable_all_protocol(context,
&protocols[PROTOCOL_DUMB_INCREMENT]);
oldms = ms;
}
/*
* If libwebsockets sockets are all we care about,
* you can use this api which takes care of the poll()
* and looping through finding who needed service.
*
* If no socket needs service, it'll return anyway after
* the number of ms in the second argument.
*/
n = lws_service(context, 50);
}//while n>=0
lws_context_destroy(context);
return 0;
}//main