21 #ifndef UWebSocketGate_H_
22 #define UWebSocketGate_H_
27 #include <condition_variable>
30 #include <sigc++/sigc++.h>
31 #include <Poco/JSON/Object.h>
32 #include <Poco/Net/WebSocket.h>
33 #include "UniSetTypes.h"
34 #include "LogAgregator.h"
35 #include "UniSetObject.h"
36 #include "DebugStream.h"
37 #include "SharedMemory.h"
38 #include "SMInterface.h"
39 #include "EventLoopServer.h"
40 #include "UTCPStream.h"
41 #include "UHttpRequestHandler.h"
42 #include "UHttpServer.h"
190 #ifndef DISABLE_REST_API
191 ,
public Poco::Net::HTTPRequestHandler
197 ,
const std::shared_ptr<SharedMemory>& ic =
nullptr
198 ,
const std::string& prefix =
"-ws" );
203 static std::shared_ptr<UWebSocketGate>
init_wsgate(
int argc,
const char*
const* argv
205 ,
const std::shared_ptr<SharedMemory>& ic =
nullptr
206 ,
const std::string& prefix =
"ws-" );
211 inline std::shared_ptr<DebugStream> log()
215 inline std::shared_ptr<uniset::LogAgregator> logAgregator() noexcept
220 #ifndef DISABLE_REST_API
221 virtual void handleRequest( Poco::Net::HTTPServerRequest& req, Poco::Net::HTTPServerResponse& resp )
override;
222 void onWebSocketSession( Poco::Net::HTTPServerRequest& req, Poco::Net::HTTPServerResponse& resp );
232 void run(
bool async );
233 virtual void evfinish()
override;
234 virtual void evprepare()
override;
235 void onCheckBuffer( ev::timer& t,
int revents );
236 void onActivate( ev::async& watcher,
int revents ) ;
237 void onCommand( ev::async& watcher,
int revents );
239 #ifndef DISABLE_REST_API
240 void httpWebSocketPage( std::ostream& out, Poco::Net::HTTPServerRequest& req, Poco::Net::HTTPServerResponse& resp );
241 void httpWebSocketConnectPage(std::ostream& out, Poco::Net::HTTPServerRequest& req,
242 Poco::Net::HTTPServerResponse& resp,
const std::string& params );
244 std::shared_ptr<UWebSocket> newWebSocket(Poco::Net::HTTPServerRequest* req, Poco::Net::HTTPServerResponse* resp,
const Poco::URI::QueryParameters& qp );
245 void delWebSocket( std::shared_ptr<UWebSocket>& ws );
247 Poco::JSON::Object::Ptr respError( Poco::Net::HTTPServerResponse& resp, Poco::Net::HTTPResponse::HTTPStatus s,
const std::string& message );
248 void makeResponseAccessHeader( Poco::Net::HTTPServerResponse& resp );
252 ev::async wsactivate;
253 std::shared_ptr<ev::async> wscmd;
255 void checkMessages( ev::timer& t,
int revents );
259 double check_sec = { 0.05 };
260 int maxMessagesProcessing = { 100 };
262 std::shared_ptr<DebugStream> mylog;
263 std::shared_ptr<uniset::LogAgregator> loga;
264 std::shared_ptr<SMInterface> shm;
265 std::unique_ptr<uniset::RunLock> runlock;
267 std::shared_ptr<uniset::LogServer> logserv;
268 std::string logserv_host = {
""};
269 int logserv_port = {0};
271 #ifndef DISABLE_REST_API
272 std::shared_ptr<Poco::Net::HTTPServer> httpserv;
273 std::string httpHost = {
"" };
274 int httpPort = { 0 };
275 std::string httpCORS_allow = {
"*" };
277 double wsHeartbeatTime_sec = { 3.0 };
278 double wsSendTime_sec = { 0.5 };
279 size_t wsMaxSend = { 5000 };
280 size_t wsMaxCmd = { 200 };
283 static Poco::JSON::Object::Ptr error_to_json(
const std::string& err );
293 public Poco::Net::WebSocket
296 UWebSocket( Poco::Net::HTTPServerRequest* req,
297 Poco::Net::HTTPServerResponse* resp);
301 std::string getInfo()
const noexcept;
304 void set( ev::dynamic_loop& loop, std::shared_ptr<ev::async> a );
306 void send( ev::timer& t,
int revents );
307 void ping( ev::timer& t,
int revents );
308 void read( ev::io& io,
int revents );
314 std::string cmd =
"";
324 void doCommand(
const std::shared_ptr<SMInterface>&
ui );
325 static Poco::JSON::Object::Ptr to_short_json(
sinfo* si );
329 void waitCompletion();
332 void setHearbeatTime(
const double& sec );
333 void setSendPeriod(
const double& sec );
334 void setMaxSendCount(
size_t val );
335 void setMaxCmdCount(
size_t val );
337 std::shared_ptr<DebugStream> mylog;
342 void sendResponse(
sinfo& si );
343 void sendShortResponse(
sinfo& si );
344 void onCommand(
const std::string& cmd );
345 void sendError(
const std::string& message );
348 double send_sec = { 0.5 };
349 size_t maxsend = { 5000 };
350 size_t maxcmd = { 200 };
351 const int Kbuf = { 10 };
354 double ping_sec = { 3.0 };
355 static const std::string ping_str;
358 char rbuf[32 * 1024];
360 std::shared_ptr<ev::async> cmdsignal;
362 std::mutex finishmut;
363 std::condition_variable finish;
365 std::atomic_bool cancelled = {
false };
367 std::unordered_map<uniset::ObjectId, sinfo> smap;
368 std::queue<sinfo> qcmd;
370 Poco::Net::HTTPServerRequest* req;
371 Poco::Net::HTTPServerResponse* resp;
374 std::queue<Poco::JSON::Object::Ptr> jbuf;
377 std::queue<uniset::UTCPCore::Buffer*> wbuf;
390 wsgate->delWebSocket(ws);
395 std::shared_ptr<UWebSocket> ws;
401 std::list<std::shared_ptr<UWebSocket>> wsocks;
403 size_t maxwsocks = { 50 };
407 public Poco::Net::HTTPRequestHandlerFactory
413 virtual Poco::Net::HTTPRequestHandler* createRequestHandler(
const Poco::Net::HTTPServerRequest& req )
override;
The EventLoopServer class Реализация общей части всех процессов использующих libev....
Definition: EventLoopServer.h:18
Definition: MessageType.h:127
Definition: MessageType.h:171
Definition: UWebSocketGate.h:408
Definition: UWebSocketGate.h:382
Definition: UWebSocketGate.h:294
timeout_t recvTimeout
Definition: UWebSocketGate.h:359
Definition: UWebSocketGate.h:193
virtual bool activateObject() override
Активизация объекта (переопределяется для необходимых действий после активизации)
Definition: UWebSocketGate.cc:631
static std::shared_ptr< UWebSocketGate > init_wsgate(int argc, const char *const *argv, uniset::ObjectId shmID, const std::shared_ptr< SharedMemory > &ic=nullptr, const std::string &prefix="ws-")
Definition: UWebSocketGate.cc:317
virtual bool deactivateObject() override
Деактивация объекта (переопределяется для необходимых действий при завершении работы)
Definition: UWebSocketGate.cc:621
static void help_print()
Definition: UWebSocketGate.cc:333
Definition: UniSetObject.h:80
std::shared_ptr< UInterface > ui
Definition: UniSetObject.h:136
Definition: CommonEventLoop.h:15
const ObjectId DefaultObjectId
Definition: UniSetTypes.h:70
long ObjectId
Definition: UniSetTypes_i.idl:30
Definition: UniSetTypes_i.idl:65
Definition: UWebSocketGate.h:311