-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathServer.h
More file actions
150 lines (129 loc) · 3.92 KB
/
Server.h
File metadata and controls
150 lines (129 loc) · 3.92 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
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
#ifndef _MONGOOSE_SERVER_H
#define _MONGOOSE_SERVER_H
#include <vector>
#include <iostream>
#include <mongoose.h>
#include "Request.h"
#include "Response.h"
#include "Controller.h"
#ifndef NO_WEBSOCKET
#include "WebSocket.h"
#include "WebSockets.h"
#endif
#include "Mutex.h"
#include "Sessions.h"
using namespace std;
/**
* Wrapper for the Mongoose server
*/
namespace Mongoose
{
class Server
{
public:
/**
* Constructs the server
*
* @param int the number of the port to listen to
* @param string documentRoot the root that should be used for static files
*/
Server(int port = 80, const char *documentRoot = "www");
virtual ~Server();
/**
* Runs the Mongoose server
*/
void start();
/**
* Stops the Mongoose server
*/
void stop();
/**
* Register a new controller on the server
*
* @param Controller* a pointer to a controller
*/
void registerController(Controller *);
/**
* Internally used to process a request
*
* @param struct mg_connection* the mongoose connection
*/
int _handleRequest(struct mg_connection *conn);
/**
* Internally used to process a file upload
*
* @param struct mg_connection* the mongoose conneciton
* @param const char * the name of the uploaded file
*/
void _upload(struct mg_connection *conn, const char *fileName);
/**
* Handles a web socket connection
*
* @param struct mg_connection* the mongoose connection with the client
*/
void _webSocketReady(struct mg_connection *conn);
/**
* Handles web sockets data
*
* @param struct mg_connection* the mongoose connection
* @param int flags the data flags
* @param char* the data
* @param size the data size
*
* @return int if we have to keep the connection opened
*/
int _webSocketData(struct mg_connection *conn, string data);
/**
* Process the request by controllers
*
* @param Request the request
*
* @return Response the response if one of the controllers can handle it,
* NULL else
*/
Response *handleRequest(Request &request);
/**
* Sets a mongoose extra option
*
* @param string the name of the option
* @param string the value of the option
*/
void setOption(string key, string value);
#ifndef NO_WEBSOCKET
/**
* Returns the WebSockets container
*
* @return WebSockets the web sockets container
*/
WebSockets &getWebSockets();
#endif
/**
* Print statistics
*/
void printStats();
/**
* Polls the server
*/
void poll();
/**
* Does the server handles url?
*/
bool handles(string method, string url);
protected:
volatile bool stopped;
volatile bool destroyed;
Sessions sessions;
Mutex mutex;
map<string, string> optionsMap;
map<struct mg_connection*, Request *> currentRequests;
struct mg_server *server;
#ifndef NO_WEBSOCKET
WebSockets websockets;
#endif
vector<Controller *> controllers;
// Statistics
int requests;
int startTime;
};
}
#endif