-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWebSocket.h
More file actions
122 lines (101 loc) · 2.87 KB
/
WebSocket.h
File metadata and controls
122 lines (101 loc) · 2.87 KB
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
#ifndef _MONGOOSE_WEBSOCKET_H
#define _MONGOOSE_WEBSOCKET_H
#include <vector>
#include <iostream>
#include <mongoose.h>
#include "Request.h"
#include "Mutex.h"
using namespace std;
#define WEBSOCKET_FIN 0x80
enum {
WEBSOCKET_OPCODE_CONTINUATION = 0x0,
WEBSOCKET_OPCODE_TEXT = 0x1,
WEBSOCKET_OPCODE_BINARY = 0x2,
WEBSOCKET_OPCODE_CONNECTION_CLOSE = 0x8,
WEBSOCKET_OPCODE_PING = 0x9,
WEBSOCKET_OPCODE_PONG = 0xa
};
namespace Mongoose
{
class WebSockets;
class WebSocket
{
public:
WebSocket(struct mg_connection *connection_);
/**
* Sends data through the web socket
*
* @param string the data to send
*/
void send(string data, int opcode = WEBSOCKET_OPCODE_TEXT);
/**
* Returns the connection request
*
* @return Request& the request
*/
Request &getRequest();
/**
* Closes the connection
*/
void close();
/**
* Is the connection closed ?
*
* @return bool true if the connection is closed
*/
bool isClosed();
/**
* Append data to the internal receive buffer
*
* @param string data
*/
void appendData(string data);
/**
* Gets the internal receive buffer and clear it
*
* @return string data
*/
string flushData();
/**
* Gets the internal mg connection
*
* @return struct mg_connection* the connection
*/
struct mg_connection *getConnection();
/**
* Adding this websocket in a container
*
* @param WebSockets* the container
*/
void addContainer(WebSockets *websockets);
/**
* Removing this websocket from a container
*/
void removeContainer(WebSockets *websockets);
/**
* Notify all the containers that the websocket is now closed and unusable
*/
void notifyContainers();
/**
* Sets the identifier of this websocket
*
* @param int the websocket identifier
*/
void setId(int id_);
/**
* Gets the websocket identifier
*
* @return int the identifier of this websocket
*/
int getId();
protected:
int id;
Mutex mutex;
string data;
Request request;
struct mg_connection *connection;
bool closed;
vector<WebSockets *> containers;
};
}
#endif