53#ifndef DISABLE_REST_API
54 ,
public Poco::Net::HTTPRequestHandler
60 ,
const std::shared_ptr<SharedMemory>& ic =
nullptr
61 ,
const std::string& prefix =
"-ws" );
66 static std::shared_ptr<UWebSocketGate>
init_wsgate(
int argc,
const char*
const* argv
68 ,
const std::shared_ptr<SharedMemory>& ic =
nullptr
69 ,
const std::string& prefix =
"ws-" );
74 inline std::shared_ptr<DebugStream> log()
78 inline std::shared_ptr<uniset::LogAgregator> logAgregator()
noexcept
83#ifndef DISABLE_REST_API
84 virtual void handleRequest( Poco::Net::HTTPServerRequest& req, Poco::Net::HTTPServerResponse& resp )
override;
85 void onWebSocketSession( Poco::Net::HTTPServerRequest& req, Poco::Net::HTTPServerResponse& resp );
86 Poco::JSON::Object::Ptr httpStatus();
87 Poco::JSON::Object::Ptr httpList();
88 Poco::JSON::Object::Ptr httpHelpApi();
91 static Poco::JSON::Object::Ptr error_to_json( std::string_view err );
92 static void fill_error_json( Poco::JSON::Object::Ptr& p, std::string_view err );
101 void run(
bool async );
102 virtual void evfinish()
override;
103 virtual void evprepare()
override;
104 void onCheckBuffer( ev::timer& t,
int revents );
105 void onActivate( ev::async& watcher,
int revents ) ;
106 void onCommand( ev::async& watcher,
int revents );
108#ifndef DISABLE_REST_API
109 void httpWebSocketPage( std::ostream& out, Poco::Net::HTTPServerRequest& req, Poco::Net::HTTPServerResponse& resp );
110 void httpWebSocketConnectPage(std::ostream& out, Poco::Net::HTTPServerRequest& req,
111 Poco::Net::HTTPServerResponse& resp,
const std::string& params );
113 std::shared_ptr<UWebSocket> newWebSocket(Poco::Net::HTTPServerRequest* req, Poco::Net::HTTPServerResponse* resp,
const Poco::URI::QueryParameters& qp );
114 void delWebSocket( std::shared_ptr<UWebSocket>&
ws );
116 Poco::JSON::Object::Ptr respError( Poco::Net::HTTPServerResponse& resp, Poco::Net::HTTPResponse::HTTPStatus s,
const std::string& message );
117 void makeResponseAccessHeader( Poco::Net::HTTPServerResponse& resp );
121 ev::async wsactivate;
122 std::shared_ptr<ev::async> wscmd;
124 void checkMessages( ev::timer& t,
int revents );
128 double check_sec = { 0.05 };
129 int maxMessagesProcessing = { 200 };
131 std::shared_ptr<DebugStream> mylog;
132 std::shared_ptr<uniset::LogAgregator> loga;
133 std::shared_ptr<SMInterface> shm;
134 std::unique_ptr<uniset::RunLock> runlock;
136 std::shared_ptr<uniset::LogServer> logserv;
137 std::string logserv_host = {
""};
138 int logserv_port = { 0 };
140#ifndef DISABLE_REST_API
141 std::shared_ptr<Poco::Net::HTTPServer> httpserv;
142 std::string httpHost = {
"" };
143 int httpPort = { 0 };
144 std::string httpCORS_allow = {
"*" };
146 double wsHeartbeatTime_sec = { 3.0 };
147 double wsSendTime_sec = { 0.2 };
148 size_t wsMaxSend = { 5000 };
149 size_t wsMaxCmd = { 200 };
150 double wsPongTimeout_sec = { 5.0 };
151 double wsMaxLifetime_sec = { 0 };
153 int jpoolCapacity = { 200 };
154 int jpoolPeakCapacity = { 5000 };
164 public Poco::Net::WebSocket
167 UWebSocket( Poco::Net::HTTPServerRequest* req,
168 Poco::Net::HTTPServerResponse* resp,
169 int jpoolCapacity = 100,
170 int jpoolPeakCapacity = 500 );
174 std::string getInfo()
const noexcept;
177 void set( ev::dynamic_loop& loop, std::shared_ptr<ev::async> a );
179 void send( ev::timer& t,
int revents );
180 void ping( ev::timer& t,
int revents );
181 void read( ev::io& io,
int revents );
182 void pong( ev::timer& t,
int revents );
183 void onLifetimeExpired( ev::timer& t,
int revents );
191 std::string cmd =
"";
205 void doCommand(
const std::shared_ptr<SMInterface>&
ui );
206 static Poco::JSON::Object::Ptr to_short_json(
const std::shared_ptr<sinfo>& si );
207 static Poco::JSON::Object::Ptr to_json(
const uniset::SensorMessage* sm,
const std::shared_ptr<sinfo>& si );
208 static void fill_short_json( Poco::JSON::Object::Ptr& p,
const std::shared_ptr<sinfo>& si );
209 static void fill_json( Poco::JSON::Object::Ptr& p,
const uniset::SensorMessage* sm,
const std::shared_ptr<sinfo>& si );
212 void waitCompletion();
215 void setHearbeatTime(
const double& sec );
216 void setSendPeriod(
const double& sec );
217 void setMaxSendCount(
size_t val );
218 void setMaxCmdCount(
size_t val );
219 void setPongTimeout(
const double& sec );
220 void setMaxLifetime(
const double& sec );
222 std::shared_ptr<DebugStream> mylog;
227 void sendResponse(
const std::shared_ptr<sinfo>& si );
228 void sendShortResponse(
const std::shared_ptr<sinfo>& si );
229 void onCommand( std::string_view cmd );
230 void sendError( std::string_view message );
231 void returnObjectToPool( Poco::JSON::Object::Ptr& json );
234 double send_sec = { 0.5 };
235 size_t maxsend = { 5000 };
236 size_t maxcmd = { 200 };
237 const int Kbuf = { 10 };
238 static const size_t sbufLen = 100 * 1024;
241 static const size_t sbufLim = (size_t)(0.8 * sbufLen);
245 double ping_sec = { 3.0 };
246 static const std::string ping_str;
248 double pongTimeout_sec = { 5.0 };
249 size_t pongCounter = { 0 };
251 ev::timer iolifetime;
252 double maxLifetime_sec = { 0 };
253 std::chrono::steady_clock::time_point sessionStart;
256 char rbuf[64 * 1024];
258 std::shared_ptr<ev::async> cmdsignal;
260 std::mutex finishmut;
261 std::condition_variable finish;
262 std::mutex dataMutex;
264 std::atomic_bool cancelled = {
false };
266 std::unordered_map<uniset::ObjectId, std::shared_ptr<sinfo> > smap;
267 std::queue< std::shared_ptr<sinfo> > qcmd;
269 Poco::Net::HTTPServerRequest* req;
270 Poco::Net::HTTPServerResponse* resp;
273 std::queue<Poco::JSON::Object::Ptr> jbuf;
274 std::unique_ptr<Poco::ObjectPool< Poco::JSON::Object, Poco::JSON::Object::Ptr >> jpoolSM;
275 std::unique_ptr<Poco::ObjectPool< Poco::JSON::Object, Poco::JSON::Object::Ptr >> jpoolErr;
276 std::unique_ptr<Poco::ObjectPool< Poco::JSON::Object, Poco::JSON::Object::Ptr >> jpoolShortSM;
279 std::unique_ptr<Poco::ObjectPool< uniset::UTCPCore::Buffer >> wbufpool;
280 std::queue<uniset::UTCPCore::Buffer*> wbuf;
293 wsgate->delWebSocket(
ws);
297 std::shared_ptr<UWebSocket>
ws;
303 std::list<std::shared_ptr<UWebSocket>> wsocks;
305 size_t maxwsocks = { 50 };
308 public Poco::Net::HTTPRequestHandlerFactory
314 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:414