-
Notifications
You must be signed in to change notification settings - Fork 1
/
ws_msgs.c
67 lines (53 loc) · 1.54 KB
/
ws_msgs.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
#include <uuid/uuid.h>
#include <cjson/cJSON.h>
#include "ws_msgs.h"
int lws_send_json(struct lws *wsi, char *message)
{
char buf[LWS_PRE + 256];
memset(&buf[LWS_PRE], 0, 256);
lws_strncpy(&buf[LWS_PRE], message, strlen(message)+1);
lwsl_user("sending: %s\n", &buf[LWS_PRE]);
return lws_write(wsi, (unsigned char*)&buf[LWS_PRE], strlen(message), LWS_WRITE_TEXT);
}
///////////////////////////////////////////////////////////
static uuid_t binuuid;
static char uuid[UUID_STR_LEN];
char *generate_hello_msg()
{
static char msg[256];
uuid_generate_random(binuuid);
uuid_unparse_lower(binuuid, uuid);
sprintf(msg, "{ \"id\": \"%s\", \"timeout\": 5000, \"type\": \"hello\" }", uuid);
return msg;
}
char *generate_offer_msg()
{
static char msg[1024];
cJSON *json = cJSON_Parse(WS_OFFER_TEMPLATE);
char *str = cJSON_Print(json);
strcpy(msg, str);
lwsl_user("OFFER RX: \n%s\n", msg);
return msg;
}
////////////////////////////////////////////////////////////
#define OUTGOING_WS_MAX 20
static char outgoing_ws_buf[OUTGOING_WS_MAX][256];
static int outgoing_ws_count;
int outgoing_ws_push(char *msg)
{
if(outgoing_ws_count>=OUTGOING_WS_MAX)
return -1;
strcpy(outgoing_ws_buf[outgoing_ws_count], msg);
outgoing_ws_count++;
return 0;
}
int outgoing_ws_shift(char *buf)
{
if(outgoing_ws_count<=0)
return -1;
strcpy(buf, outgoing_ws_buf[0]);
memmove(outgoing_ws_buf[0],outgoing_ws_buf[1],256*(OUTGOING_WS_MAX-1));
outgoing_ws_count--;
return 0;
}
///////////////////////////////////////////////////////////////