-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebSocketServer.h
More file actions
89 lines (77 loc) · 2.9 KB
/
WebSocketServer.h
File metadata and controls
89 lines (77 loc) · 2.9 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
/** --------------------------------------------------------------------------
* WebSocketServer.h
*
* Base class that WebSocket implementations must inherit from. Handles the
* client connections and calls the child class callbacks for connection
* events like onConnect, onMessage, and onDisconnect.
*
* Author : Jason Kruse <jason@jasonkruse.com> or @mnisjk
* Copyright : 2014
* License : BSD (see LICENSE)
* --------------------------------------------------------------------------
**/
#ifndef _WEBSOCKETSERVER_H
#define _WEBSOCKETSERVER_H
#include <stdint.h>
#include <map>
#include <vector>
#include <string>
#include <list>
#include <stdio.h>
#include <ctime>
//#include <sys/time.h>
#include <iostream>
#include <sstream>
#include "include\libwebsockets.h"
#include "WriteByteArray.h"
using namespace std;
#define WS_RESERVE_RECEIVE_BUFFER_SIZE (4096)
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
/// WebSocketServer
/// ---------
class WebSocketServer
{
public:
// Represents a client connection
struct Connection
{
list<WriteByteArray*> buffer; // Ordered list of pending messages to flush out when socket is writable
map<string,string> keyValueMap;
time_t createTime;
};
// Manages connections. Unfortunately this is public because static callback for
// libwebsockets is defined outside the instance and needs access to it.
map<int,Connection*> connections;
// Constructor / Destructor
WebSocketServer( int port, const string certPath = "", const string& keyPath = "" );
~WebSocketServer( );
void run( uint64_t timeout = 50 );
void wait( uint64_t timeout = 50 );
void send(int socketID, WriteByteArray* wb);
void broadcast(WriteByteArray* wb);
// Key => value storage for each connection
string getValue( int socketID, const string& name );
void setValue( int socketID, const string& name, const string& value );
int getNumberOfConnections( );
// Overridden by children
virtual void onConnect( int socketID ) = 0;
virtual void onMessage(struct lws *wsi, void* in, int len) = 0;
virtual void onDisconnect( int socketID ) = 0;
virtual void onError( int socketID, const string& message ) = 0;
// Wrappers, so we can take care of some maintenance
void onConnectWrapper( int socketID );
void onDisconnectWrapper( int socketID );
void onErrorWrapper( int socketID, const string& message );
protected:
// Nothing, yet.
std::vector<char> _receivedData;
private:
int _port;
string _keyPath;
string _certPath;
struct lws_context *_context;
void _removeConnection( int socketID );
};
// WebSocketServer.h
#endif