-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwebsock.h
236 lines (184 loc) · 6.22 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
//***************************************************************************
// 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>
//***************************************************************************
// Class OSD Service
//***************************************************************************
class cOsdService
{
public:
enum ObjectShape
{
osText = 0x01,
osSmall = 0x02,
osLarge = 0x04
};
enum Event
{
evUnknown,
evTakeFocus,
evLeaveFocus,
evKeyPress,
evChannels,
evMaxLines,
evLogin,
evLogout,
evCommand,
evCount
};
enum ClientType
{
ctInactive = na,
ctInteractive, // = 0 - for interactive browser session
ctView, // = 1 - e.g. for graphtft display
ctFB // = 2 - pure FB (remote control) clients
};
static const char* toName(Event event);
static Event toEvent(const char* name);
static const char* events[];
struct cCutMark
{
int position;
std::string comment;
};
struct cCuttingMarks
{
cMutex mutex;
std::queue<cCutMark> queue;
int isSet;
};
};
//***************************************************************************
// 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 cOsdService
{
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 {0};
int payloadSize {0};
int dataPending {no};
};
struct Client
{
~Client() { free(msgBuffer); }
ClientType type {};
int tftprio {0};
std::queue<std::string> messagesOut;
cMutex 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)
{
cMutexLock lock(&messagesOutMutex);
messagesOut.push(p);
}
void cleanupMessageQueue()
{
cMutexLock lock(&messagesOutMutex);
// just in case client is not connected and wasted messages are pending
tell(0, "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);
int exit();
// 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);
int performData(MsgType type);
// status 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
static void activateAvailableClient();
static int isHighestViewClient(lws* wsi);
static void atLogin(lws* wsi, const char* message, const char* clientInfo);
static void atLogout(lws* wsi, const char* message, const char* clientInfo);
static int getClientCount();
static void pushOutMessage(const char* p, lws* wsi = 0);
static void writeLog(int level, const char* line);
private:
static int serveFile(lws* wsi, const char* path);
static int dispatchDataRequest(lws* wsi, SessionData* sessionData, const char* url);
static int doEventImg(lws* wsi);
static int doRecordingImg(lws* wsi);
static int doChannelLogo(lws* wsi);
static int doEnvironment(lws* wsi, SessionData* sessionData);
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
// statics
static lws_context* context;
static cWebInterface* singleton;
static char* httpPath;
static char* epgImagePath;
static int timeout;
static void* activeClient;
static std::map<void*,Client> clients;
static cMutex clientsMutex;
static MsgType msgType;
// only used in callback
// static char* msgBuffer;
// static int msgBufferSize;
};