-
Notifications
You must be signed in to change notification settings - Fork 12
/
WebSocket.h
executable file
·151 lines (121 loc) · 5.55 KB
/
WebSocket.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
146
147
148
149
150
151
//#include "GenericTypeDefs.h"
#define WebSocketKeyLength 24
#define MaxFrameLength 248
#define ShortExtendLength 2
#define LongExtendLength 4
#define MaskLength 4
#define ShortReplyAddition 2
typedef struct {
unsigned char Opcode : 4;
unsigned char RSV : 3;
unsigned char FIN : 1;
unsigned char PayloadLen : 7;
unsigned char Mask : 1;
BYTE data[MaxFrameLength + 6];//allow room for additional
} WebSocketFrame;
typedef enum {
ContinuationFrame, TextFrame, BinaryFrame, ConnectionClose = 0x08, WSPing, WSPong
} OPCODES;
extern int CreateHandShake(TCP_SOCKET MySocket,unsigned char* WebSocketKey);
extern int UnMaskFrame(WebSocketFrame *Frame);
extern void CreateFrame(WebSocketFrame *OutFrame, BYTE* Data, int Length, int Opcode);
extern int WebSocketEventRequest[MAX_HTTP_CONNECTIONS];//for requesting an data out event
/****************************************************************************
Section:
User-Implemented Callback Function Prototypes
***************************************************************************/
/*****************************************************************************
Function:
void WebSocketNewFrame(BYTE* DATA, int Len, int SocketID, int Opcode)
Summary:
Processes Incoming WebSocket Data
Description:
This function is implemented by the application developer in
CustomHTTPApp.c. Its purpose is to parse the data received from
a WebSocket Frame and perform any
application-specific tasks in response to these inputs.
If data buffer space associated
with this connection is required, curHTTP.data may be overwritten
here once the application is done with the values. Any data placed
there will be available to future callbacks for this connection,
including HTTPExecutePost and any HTTPPrint_varname dynamic
substitutions.
Precondition:
None
Parameters:
DATA - The data stream from the target WebSocket Frame.
Len - The length of the Data from the Frame.
SocketID - the Unique ID of the current TCP Socket
Opcode - The opcode for the type of data contained in the frame.
Return Values:
None
Remarks:
This function is only called if data is recieved over an active WebSocket.
This function may NOT write to the TCP buffer.
This function may service multiple HTTP requests simultaneously.
Exercise caution when using global or static variables inside this
routine. Use curHTTP.callbackPos or curHTTP.data for storage associated
with individual requests.
***************************************************************************/
extern void WebSocketNewFrame(BYTE* DATA, int Len, int SocketID, int Opcode);
/*****************************************************************************
Function:
void WebSocketGetFrame(BYTE* Stream, int* Length, int* Opcode, int SocketID)
Summary:
Provides data to be sent out in a new WebSocket Frame.
Description:
This function is implemented by the application developer in
CustomHTTPApp.c. Its purpose is to provide data to be sent to the websocket
client in a new frame.
If data buffer space associated
with this connection is required, curHTTP.data may be overwritten
here once the application is done with the values. Any data placed
there will be available to future callbacks for this connection,
including HTTPExecutePost and any HTTPPrint_varname dynamic
substitutions.
Precondition:
None
Parameters:
Stream(OUT) - Pointer to the Stream where any data to be sent should be written.
Length(OUT) - The length of the Data written to the Stream.
SocketID - the Unique ID of the current TCP Socket
Opcode(OUT) - The opcode for the type of data written to the stream.
Return Values:
None
Remarks:
This function is only called if WebSocketEventRequest[SocketID] flag has been set.
This function may service multiple HTTP requests simultaneously.
Exercise caution when using global or static variables inside this
routine. Use curHTTP.callbackPos or curHTTP.data for storage associated
with individual requests.
***************************************************************************/
extern void WebSocketGetFrame(BYTE* Stream, int* Length, int* Opcode, int SocketID);
/*****************************************************************************
Function:
void HTTPExecuteWebSocket(int SocketID);
Summary:
Processes IO Data for WebSocket requests.
Description:
This function is implemented by the application developer in
CustomHTTPApp.c. Its purpose is to handle any data recieved from
a websocket frame and process any corresponding IO accordingly.
It must also set the WebSocketEventRequest[SocketID] flag when/if there
is response data to be sent back to the client.
If data buffer space associated with this connection is required,
curHTTP.data may be overwritten here once the application is done
with the values. Any data placed there will be available to future
callbacks for this connection, including HTTPExecutePost and any
HTTPPrint_varname dynamic substitutions.
Precondition:
None
Parameters:
SocketID - The unique identifier for the current TCP socket.
Return Values:
None.
Remarks:
This function may service multiple HTTP requests simultaneously.
Exercise caution when using global or static variables inside this
routine. Use curHTTP.callbackPos or curHTTP.data for storage associated
with individual requests.
***************************************************************************/
extern void HTTPExecuteWebSocket(int SocketID);