-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathwebsock.h
173 lines (131 loc) · 4.81 KB
/
websock.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
//***************************************************************************
// Web Socket Interface
// File websock.h
// This code is distributed under the terms and conditions of the
// GNU GENERAL PUBLIC LICENSE. See the file LICENSE for details.
// Date 16.04.2020 - Jörg Wendel
//***************************************************************************
#pragma once
#include <queue>
#include <jansson.h>
#include <libwebsockets.h>
#include "webservice.h"
//***************************************************************************
// Class Web Interface
//***************************************************************************
class cWebInterface : public cWebService
{
public:
virtual const char* myName() = 0;
virtual int pushInMessage(const char* data) = 0;
};
//***************************************************************************
// Class cWebSock
//***************************************************************************
class cWebSock : public cWebService
{
public:
enum MsgType
{
mtNone = na,
mtPing, // 0
mtData // 1
};
enum Protokoll
{
sizeLwsPreFrame = LWS_SEND_BUFFER_PRE_PADDING,
sizeLwsPostFrame = LWS_SEND_BUFFER_POST_PADDING,
sizeLwsFrame = sizeLwsPreFrame + sizeLwsPostFrame
};
struct SessionData
{
char* buffer;
int bufferSize;
int payloadSize;
int dataPending;
};
struct Client
{
~Client() { free(msgBuffer); }
ClientType type;
int tftprio;
std::queue<std::string> messagesOut;
cMyMutex messagesOutMutex;
void* wsi;
// buffer to send the payload in chunks
unsigned char* msgBuffer {};
int msgBufferSize {0};
int msgBufferPayloadSize {0};
int msgBufferSendOffset {0};
bool msgBufferDataPending() { return msgBufferSendOffset < msgBufferPayloadSize; }
// push next message
void pushMessage(const char* p)
{
cMyMutexLock lock(&messagesOutMutex);
messagesOut.push(p);
}
void cleanupMessageQueue()
{
cMyMutexLock lock(&messagesOutMutex);
// just in case client is not connected and wasted messages are pending
tell(eloAlways, "Info: Flushing (%zu) old 'wasted' messages of client (%p)", messagesOut.size(), wsi);
while (!messagesOut.empty())
messagesOut.pop();
}
std::string buffer; // for chunked messages
};
cWebSock(cWebInterface* aProcess, const char* aHttpPath);
virtual ~cWebSock();
int init(int aPort, int aTimeout, const char* confDir, bool ssl = false);
int exit();
int performData(MsgType type);
// static interface
void pushOutMessage(const char* p, lws* wsi = 0);
void setClientType(lws* wsi, ClientType type);
private:
// callback methods
static int wsLogLevel;
static int callbackHttp(lws* wsi, lws_callback_reasons reason, void* user, void* in, size_t len);
static int callbackWs(lws* wsi, lws_callback_reasons reason, void* user, void* in, size_t len);
// static interface for callbacks
static void atLogin(lws* wsi, const char* message, const char* clientInfo, json_t* object);
static void atLogout(lws* wsi, const char* message, const char* clientInfo);
static int getClientCount();
static void writeLog(int level, const char* line);
static int serveFile(lws* wsi, const char* path);
static int dispatchDataRequest(lws* wsi, SessionData* sessionData, const char* url);
static const char* methodOf(const char* url);
static const char* getStrParameter(lws* wsi, const char* name, const char* def = 0);
static int getIntParameter(lws* wsi, const char* name, int def = na);
//
int port {na};
char* certFile {};
char* certKeyFile {};
lws_protocols protocols[3];
lws_http_mount mounts[1];
#if defined (LWS_LIBRARY_VERSION_MAJOR) && (LWS_LIBRARY_VERSION_MAJOR >= 4)
lws_retry_bo_t retry;
#endif
// sync thread stuff
struct ThreadControl
{
bool active {false};
bool close {false};
cWebSock* webSock {};
int timeout {60};
};
pthread_t syncThread {0};
ThreadControl threadCtl;
static void* syncFct(void* user);
static int service(ThreadControl* threadCtl);
// statics
static lws_context* context;
static cWebInterface* singleton;
static char* httpPath;
static char* epgImagePath;
static int timeout;
static std::map<void*,Client> clients;
static cMyMutex clientsMutex;
static MsgType msgType;
static std::map<std::string, std::string> htmlTemplates;
};