192#ifndef DISABLE_REST_API
193 ,
public Poco::Net::HTTPRequestHandler
199 ,
const std::shared_ptr<SharedMemory>& ic =
nullptr
200 ,
const std::string& prefix =
"-ws" );
205 static std::shared_ptr<UWebSocketGate>
init_wsgate(
int argc,
const char*
const* argv
207 ,
const std::shared_ptr<SharedMemory>& ic =
nullptr
208 ,
const std::string& prefix =
"ws-" );
213 inline std::shared_ptr<DebugStream> log()
217 inline std::shared_ptr<uniset::LogAgregator> logAgregator()
noexcept
222#ifndef DISABLE_REST_API
223 virtual void handleRequest( Poco::Net::HTTPServerRequest& req, Poco::Net::HTTPServerResponse& resp )
override;
224 void onWebSocketSession( Poco::Net::HTTPServerRequest& req, Poco::Net::HTTPServerResponse& resp );
227 static Poco::JSON::Object::Ptr error_to_json( std::string_view err );
228 static void fill_error_json( Poco::JSON::Object::Ptr& p, std::string_view err );
237 void run(
bool async );
238 virtual void evfinish()
override;
239 virtual void evprepare()
override;
240 void onCheckBuffer( ev::timer& t,
int revents );
241 void onActivate( ev::async& watcher,
int revents ) ;
242 void onCommand( ev::async& watcher,
int revents );
244#ifndef DISABLE_REST_API
245 void httpWebSocketPage( std::ostream& out, Poco::Net::HTTPServerRequest& req, Poco::Net::HTTPServerResponse& resp );
246 void httpWebSocketConnectPage(std::ostream& out, Poco::Net::HTTPServerRequest& req,
247 Poco::Net::HTTPServerResponse& resp,
const std::string& params );
249 std::shared_ptr<UWebSocket> newWebSocket(Poco::Net::HTTPServerRequest* req, Poco::Net::HTTPServerResponse* resp,
const Poco::URI::QueryParameters& qp );
250 void delWebSocket( std::shared_ptr<UWebSocket>& ws );
252 Poco::JSON::Object::Ptr respError( Poco::Net::HTTPServerResponse& resp, Poco::Net::HTTPResponse::HTTPStatus s,
const std::string& message );
253 void makeResponseAccessHeader( Poco::Net::HTTPServerResponse& resp );
257 ev::async wsactivate;
258 std::shared_ptr<ev::async> wscmd;
260 void checkMessages( ev::timer& t,
int revents );
264 double check_sec = { 0.05 };
265 int maxMessagesProcessing = { 200 };
267 std::shared_ptr<DebugStream> mylog;
268 std::shared_ptr<uniset::LogAgregator> loga;
269 std::shared_ptr<SMInterface> shm;
270 std::unique_ptr<uniset::RunLock> runlock;
272 std::shared_ptr<uniset::LogServer> logserv;
273 std::string logserv_host = {
""};
274 int logserv_port = { 0 };
276#ifndef DISABLE_REST_API
277 std::shared_ptr<Poco::Net::HTTPServer> httpserv;
278 std::string httpHost = {
"" };
279 int httpPort = { 0 };
280 std::string httpCORS_allow = {
"*" };
282 double wsHeartbeatTime_sec = { 3.0 };
283 double wsSendTime_sec = { 0.2 };
284 size_t wsMaxSend = { 5000 };
285 size_t wsMaxCmd = { 200 };
286 double wsPongTimeout_sec = {5.0 };
288 int jpoolCapacity = { 200 };
289 int jpoolPeakCapacity = { 5000 };
299 public Poco::Net::WebSocket
302 UWebSocket( Poco::Net::HTTPServerRequest* req,
303 Poco::Net::HTTPServerResponse* resp,
304 int jpoolCapacity = 100,
305 int jpoolPeakCapacity = 500 );
309 std::string getInfo()
const noexcept;
312 void set( ev::dynamic_loop& loop, std::shared_ptr<ev::async> a );
314 void send( ev::timer& t,
int revents );
315 void ping( ev::timer& t,
int revents );
316 void read( ev::io& io,
int revents );
317 void pong( ev::timer& t,
int revents );
325 std::string cmd =
"";
338 void doCommand(
const std::shared_ptr<SMInterface>&
ui );
339 static Poco::JSON::Object::Ptr to_short_json(
const std::shared_ptr<sinfo>& si );
340 static Poco::JSON::Object::Ptr to_json(
const uniset::SensorMessage* sm,
const std::shared_ptr<sinfo>& si );
341 static void fill_short_json( Poco::JSON::Object::Ptr& p,
const std::shared_ptr<sinfo>& si );
342 static void fill_json( Poco::JSON::Object::Ptr& p,
const uniset::SensorMessage* sm,
const std::shared_ptr<sinfo>& si );
345 void waitCompletion();
348 void setHearbeatTime(
const double& sec );
349 void setSendPeriod(
const double& sec );
350 void setMaxSendCount(
size_t val );
351 void setMaxCmdCount(
size_t val );
352 void setPongTimeout(
const double& set );
354 std::shared_ptr<DebugStream> mylog;
359 void sendResponse(
const std::shared_ptr<sinfo>& si );
360 void sendShortResponse(
const std::shared_ptr<sinfo>& si );
361 void onCommand( std::string_view cmd );
362 void sendError( std::string_view message );
363 void returnObjectToPool( Poco::JSON::Object::Ptr& json );
366 double send_sec = { 0.5 };
367 size_t maxsend = { 5000 };
368 size_t maxcmd = { 200 };
369 const int Kbuf = { 10 };
370 static const size_t sbufLen = 100 * 1024;
374 double ping_sec = { 3.0 };
375 static const std::string ping_str;
377 double pongTimeout_sec = { 5.0 };
378 size_t pongCounter = { 0 };
381 char rbuf[64 * 1024];
383 std::shared_ptr<ev::async> cmdsignal;
385 std::mutex finishmut;
386 std::condition_variable finish;
388 std::atomic_bool cancelled = {
false };
390 std::unordered_map<uniset::ObjectId, std::shared_ptr<sinfo> > smap;
391 std::queue< std::shared_ptr<sinfo> > qcmd;
393 Poco::Net::HTTPServerRequest* req;
394 Poco::Net::HTTPServerResponse* resp;
397 std::queue<Poco::JSON::Object::Ptr> jbuf;
398 std::unique_ptr<Poco::ObjectPool< Poco::JSON::Object, Poco::JSON::Object::Ptr >> jpoolSM;
399 std::unique_ptr<Poco::ObjectPool< Poco::JSON::Object, Poco::JSON::Object::Ptr >> jpoolErr;
400 std::unique_ptr<Poco::ObjectPool< Poco::JSON::Object, Poco::JSON::Object::Ptr >> jpoolShortSM;
403 std::unique_ptr<Poco::ObjectPool< uniset::UTCPCore::Buffer >> wbufpool;
404 std::queue<uniset::UTCPCore::Buffer*> wbuf;
417 wsgate->delWebSocket(ws);
421 std::shared_ptr<UWebSocket> ws;
427 std::list<std::shared_ptr<UWebSocket>> wsocks;
429 size_t maxwsocks = { 50 };
432 public Poco::Net::HTTPRequestHandlerFactory
438 virtual Poco::Net::HTTPRequestHandler* createRequestHandler(
const Poco::Net::HTTPServerRequest& req )
override;
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:373