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" 189 #ifndef DISABLE_REST_API 190 ,
public Poco::Net::HTTPRequestHandler
196 ,
const std::shared_ptr<SharedMemory>& ic =
nullptr 197 ,
const std::string& prefix =
"-ws" );
202 static std::shared_ptr<UWebSocketGate>
init_wsgate(
int argc,
const char*
const* argv
204 ,
const std::shared_ptr<SharedMemory>& ic =
nullptr 205 ,
const std::string& prefix =
"ws-" );
210 inline std::shared_ptr<DebugStream> log()
215 #ifndef DISABLE_REST_API 216 virtual void handleRequest( Poco::Net::HTTPServerRequest& req, Poco::Net::HTTPServerResponse& resp )
override;
217 void onWebSocketSession( Poco::Net::HTTPServerRequest& req, Poco::Net::HTTPServerResponse& resp );
225 void run(
bool async );
226 virtual void evfinish()
override;
227 virtual void evprepare()
override;
228 void onCheckBuffer( ev::timer& t,
int revents );
229 void onActivate( ev::async& watcher,
int revents ) ;
230 void onCommand( ev::async& watcher,
int revents );
232 #ifndef DISABLE_REST_API 233 void httpWebSocketPage( std::ostream& out, Poco::Net::HTTPServerRequest& req, Poco::Net::HTTPServerResponse& resp );
234 void httpWebSocketConnectPage(std::ostream& out, Poco::Net::HTTPServerRequest& req,
235 Poco::Net::HTTPServerResponse& resp,
const std::string& params );
237 std::shared_ptr<UWebSocket> newWebSocket(Poco::Net::HTTPServerRequest* req, Poco::Net::HTTPServerResponse* resp,
const Poco::URI::QueryParameters& qp );
238 void delWebSocket( std::shared_ptr<UWebSocket>& ws );
240 Poco::JSON::Object::Ptr respError( Poco::Net::HTTPServerResponse& resp, Poco::Net::HTTPResponse::HTTPStatus s,
const std::string& message );
241 void makeResponseAccessHeader( Poco::Net::HTTPServerResponse& resp );
246 void onTerminate( ev::sig& evsig,
int revents );
248 ev::async wsactivate;
249 std::shared_ptr<ev::async> wscmd;
251 void checkMessages( ev::timer& t,
int revents );
255 double check_sec = { 0.05 };
256 int maxMessagesProcessing = { 100 };
258 std::shared_ptr<DebugStream> mylog;
259 std::shared_ptr<SMInterface> shm;
261 #ifndef DISABLE_REST_API 262 std::shared_ptr<Poco::Net::HTTPServer> httpserv;
263 std::string httpHost = {
"" };
264 int httpPort = { 0 };
265 std::string httpCORS_allow = {
"*" };
267 double wsHeartbeatTime_sec = { 3.0 };
268 double wsSendTime_sec = { 0.5 };
269 size_t wsMaxSend = { 5000 };
270 size_t wsMaxCmd = { 200 };
273 static Poco::JSON::Object::Ptr error_to_json(
const std::string& err );
283 public Poco::Net::WebSocket
286 UWebSocket( Poco::Net::HTTPServerRequest* req,
287 Poco::Net::HTTPServerResponse* resp);
291 std::string getInfo()
const noexcept;
294 void set( ev::dynamic_loop& loop, std::shared_ptr<ev::async> a );
296 void send( ev::timer& t,
int revents );
297 void ping( ev::timer& t,
int revents );
298 void read( ev::io& io,
int revents );
304 std::string cmd =
"";
314 void doCommand(
const std::shared_ptr<SMInterface>&
ui );
315 static Poco::JSON::Object::Ptr to_short_json(
sinfo* si );
319 void waitCompletion();
322 void setHearbeatTime(
const double& sec );
323 void setSendPeriod(
const double& sec );
324 void setMaxSendCount(
size_t val );
325 void setMaxCmdCount(
size_t val );
327 std::shared_ptr<DebugStream> mylog;
332 void sendResponse(
sinfo& si );
333 void sendShortResponse(
sinfo& si );
334 void onCommand(
const std::string& cmd );
335 void sendError(
const std::string& message );
338 double send_sec = { 0.5 };
339 size_t maxsend = { 5000 };
340 size_t maxcmd = { 200 };
341 const int Kbuf = { 10 };
344 double ping_sec = { 3.0 };
345 static const std::string ping_str;
348 char rbuf[32 * 1024];
350 std::shared_ptr<ev::async> cmdsignal;
352 std::mutex finishmut;
353 std::condition_variable finish;
355 std::atomic_bool cancelled = {
false };
357 std::unordered_map<uniset::ObjectId, sinfo> smap;
358 std::queue<sinfo> qcmd;
360 Poco::Net::HTTPServerRequest* req;
361 Poco::Net::HTTPServerResponse* resp;
364 std::queue<Poco::JSON::Object::Ptr> jbuf;
367 std::queue<uniset::UTCPCore::Buffer*> wbuf;
380 wsgate->delWebSocket(ws);
385 std::shared_ptr<UWebSocket> ws;
391 std::list<std::shared_ptr<UWebSocket>> wsocks;
393 size_t maxwsocks = { 50 };
397 public Poco::Net::HTTPRequestHandlerFactory
403 virtual Poco::Net::HTTPRequestHandler* createRequestHandler(
const Poco::Net::HTTPServerRequest& req )
override;
Definition: CommonEventLoop.h:14
timeout_t recvTimeout
Definition: UWebSocketGate.h:349
Definition: UniSetObject.h:73
std::shared_ptr< UInterface > ui
Definition: UniSetObject.h:136
Definition: UWebSocketGate.h:396
const ObjectId DefaultObjectId
Definition: UniSetTypes.h:69
Definition: UWebSocketGate.h:186
Definition: MessageType.h:126
virtual bool activateObject() override
Активизация объекта (переопределяется для необходимых действий после активизации)
Definition: UWebSocketGate.cc:566
Definition: UWebSocketGate.h:300
Definition: UWebSocketGate.h:371
The EventLoopServer class Реализация общей части всех процессов использующих libev....
Definition: EventLoopServer.h:17
static void help_print()
Definition: UWebSocketGate.cc:281
Definition: UniSetTypes_i.idl:64
Definition: UWebSocketGate.h:282
long ObjectId
Definition: UniSetTypes_i.idl:30
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:265