forked from mortzdk/Websocket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Datastructures.h
145 lines (129 loc) · 3.63 KB
/
Datastructures.h
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
/******************************************************************************
Copyright (c) 2013 Morten Houmøller Nygaard - www.mortz.dk - admin@mortz.dk
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
******************************************************************************/
#ifndef _DATASTRUCTURES_H
#define _DATASTRUCTURES_H
#include "Includes.h"
typedef enum {
CONTINUE,
CLOSE_NORMAL=1000, /* The connection */
CLOSE_SHUTDOWN=1001, /* Server is shutting down */
CLOSE_PROTOCOL=1002, /* Some error in the protocol has happened */
CLOSE_TYPE=1003, /* The type (text, binary) was not supported */
CLOSE_UTF8=1007, /* The message wasn't in UTF8 */
CLOSE_POLICY=1008, /* The policy of the server has been broken */
CLOSE_BIG=1009, /* The messages received is too big */
CLOSE_UNEXPECTED=1011 /* Unexpected happened */
} ws_connection_close;
typedef enum {
UNKNOWN,
RFC6455,
HYBI10,
HYBI07,
HYBI00,
HIXIE75,
HTTP
} ws_type;
typedef enum {
NONE,
CHAT,
ECHO
} ws_protocol;
typedef struct {
char *host;
char *connection;
char *key;
char *key1;
char *key2;
char *key3;
char *origin;
char *upgrade;
char *get;
char *accept;
char *extension;
char *resourcename;
char *protocol_string;
int version;
int host_len;
int protocol_len;
int origin_len;
int upgrade_len;
int accept_len;
int extension_len;
int get_len;
int resourcename_len;
ws_type type;
ws_protocol protocol;
} ws_header;
typedef struct {
char opcode[1];
char mask[4];
uint64_t len;
uint64_t enc_len;
uint64_t next_len;
char *msg;
char *next;
char *enc;
char *hybi00;
} ws_message;
typedef struct ws_client_n {
int socket_id;
char *client_ip;
char *string;
pthread_t thread_id;
ws_header *headers;
ws_message *message;
struct ws_client_n *next;
} ws_client;
typedef struct {
int len;
ws_client *first;
ws_client *last;
pthread_mutex_t lock;
} ws_list;
/**
* List functions.
*/
ws_list *list_new(void);
ws_client *list_get(ws_list *l, char *addr, int socket);
void list_free(ws_list *l);
void list_add(ws_list *l, ws_client *n);
void list_remove(ws_list *l, ws_client *r);
void list_remove_all(ws_list *l);
void list_print(ws_list *l);
void list_multicast(ws_list *l, ws_client *n);
void list_multicast_one(ws_list *l, ws_client *n, ws_message *m);
void list_multicast_all(ws_list *l, ws_message *m);
/**
* Websocket functions.
*/
void ws_closeframe(ws_client *n, ws_connection_close c);
void ws_send(ws_client *n, ws_message *m);
/**
* New structures.
*/
ws_client *client_new(int sock, char *addr);
ws_header *header_new();
ws_message *message_new();
/**
* Free structures
*/
void header_free(ws_header *h);
void message_free(ws_message *m);
void client_free(ws_client *n);
#endif