@@ -205,6 +205,13 @@ void ZorroWebsocketProxy::handleClientMessage(Message& msg) {
205205 case Message::Type::WsRequest:
206206 sendWsRequest (msg);
207207 break ;
208+ case Message::Type::Subscribe:
209+ handleSubscribe (msg);
210+ break ;
211+
212+ case Message::Type::Unsubscribe:
213+ handleUnsubscribe (msg);
214+ break ;
208215
209216 case Message::Type::WsData:
210217 case Message::Type::WsError:
@@ -284,7 +291,7 @@ void ZorroWebsocketProxy::openWs(Message& msg) {
284291 req->id = id;
285292 req->new_connection = (state == Websocket::Status::CONNECTING);
286293 onWsOpened (id, msg.pid );
287- lwsl_user (" Websocket %s already opened. id=%d, new=%d\n " , req->url , id, req->new_connection );
294+ lwsl_user (" Websocket %s already opened. id=%d, new=%d, client=%d \n " , req->url , id, req->new_connection , msg. pid );
288295 msg.status .store (Message::Status::SUCCESS, std::memory_order_release);
289296 return ;
290297 }
@@ -299,7 +306,7 @@ void ZorroWebsocketProxy::openWs(Message& msg) {
299306}
300307
301308void ZorroWebsocketProxy::openNewWs (Message& msg, WsOpen* req) {
302- lwsl_user (" Opening ws %s\n " , req->url );
309+ lwsl_user (" Opening ws %s, clinet=%d \n " , req->url , msg. pid );
303310 req->new_connection = true ;
304311 auto websocket = std::make_shared<Websocket>(this , pid_ * 10000 + (++websocket_id_), req->url );
305312 auto b = websocket->open (msg.pid );
@@ -343,6 +350,70 @@ void ZorroWebsocketProxy::closeWs(uint32_t id, DWORD pid) {
343350 lwsl_user (" Close ws. socket not found id=%d\n " , id);
344351 }
345352}
353+ void ZorroWebsocketProxy::handleSubscribe (Message& msg) {
354+ auto req = reinterpret_cast <WsSubscription*>(msg.data );
355+ auto client = getClient (msg.pid );
356+ if (client) {
357+ lwsl_user (" Subscribe %s client=%d ws_id=%d\n " , req->symbol , msg.pid , req->id );
358+ auto it = websocketsById_.find (req->id );
359+ if (it != websocketsById_.end ()) {
360+ auto & subscriptions = it->second ->subscriptions_ ;
361+ auto sub_it = subscriptions.find (req->symbol );
362+ if (sub_it == subscriptions.end ()) {
363+ subscriptions.emplace (req->symbol , 1 );
364+ if (it->second ->send (req->request , req->request_len )) {
365+ msg.status .store (Message::Status::SUCCESS, std::memory_order_release);
366+ return ;
367+ }
368+ }
369+ else {
370+ ++sub_it->second ;
371+ req->existing = true ;
372+ msg.status .store (Message::Status::SUCCESS, std::memory_order_release);
373+ return ;
374+ }
375+ }
376+ else {
377+ lwsl_user (" Websocket not found. id=\n " , req->id );
378+ }
379+ }
380+ else {
381+ lwsl_user (" Client not found. pid=\n " , msg.pid );
382+ }
383+ msg.status .store (Message::Status::FAILED, std::memory_order_release);
384+ }
385+
386+ void ZorroWebsocketProxy::handleUnsubscribe (Message& msg) {
387+ auto req = reinterpret_cast <WsSubscription*>(msg.data );
388+ auto client = getClient (msg.pid );
389+ if (client) {
390+ lwsl_user (" Unsubscribe %s client=%d ws_id=%d\n " , req->symbol , msg.pid , req->id );
391+ auto it = websocketsById_.find (req->id );
392+ if (it != websocketsById_.end ()) {
393+ auto & subscriptions = it->second ->subscriptions_ ;
394+ auto sub_it = subscriptions.find (req->symbol );
395+ if (sub_it != subscriptions.end ()) {
396+ if (--sub_it->second == 0 ) {
397+ subscriptions.erase (sub_it);
398+ if (!it->second ->send (req->request , req->request_len )) {
399+ msg.status .store (Message::Status::FAILED, std::memory_order_release);
400+ return ;
401+ }
402+ }
403+ }
404+ else {
405+ lwsl_user (" Subscription not find. symbol=%s ws_id=%d\n " , req->symbol , req->id );
406+ }
407+ }
408+ else {
409+ lwsl_user (" Websocket not found. id=\n " , req->id );
410+ }
411+ }
412+ else {
413+ lwsl_user (" Client not found. pid=\n " , msg.pid );
414+ }
415+ msg.status .store (Message::Status::SUCCESS, std::memory_order_release);
416+ }
346417
347418void ZorroWebsocketProxy::sendWsRequest (Message& msg) {
348419 auto req = reinterpret_cast <WsRequest*>(msg.data );
@@ -352,6 +423,9 @@ void ZorroWebsocketProxy::sendWsRequest(Message& msg) {
352423 auto it = websocketsById_.find (req->id );
353424 if (it != websocketsById_.end ()) {
354425 if (it->second ->send (req->data , req->len )) {
426+ #ifdef _DEBUG
427+ lwsl_user (" --> %.*s\n " , req->len , req->data );
428+ #endif
355429 msg.status .store (Message::Status::SUCCESS, std::memory_order_release);
356430 return ;
357431 }
@@ -413,6 +487,7 @@ bool ZorroWebsocketProxy::sendHeartbeat(uint64_t now) {
413487 if ((now - last_heartbeat_time_) > HEARTBEAT_INTERVAL) {
414488 auto [msg, index, size] = reserveMessage ();
415489 msg->type = Message::Type::Heartbeat;
490+ lwsl_user (" .\n " );
416491 sendMessage (index, size, now);
417492 return true ;
418493 }
@@ -481,5 +556,7 @@ void ZorroWebsocketProxy::onWsData(uint32_t id, const char* data, size_t len, si
481556 memcpy (d->data , data, len);
482557 }
483558 sendMessage (index, size);
484- // lwsl_user("<-- %.*s\n", len, data);
559+ #ifdef _DEBUG
560+ lwsl_user (" <-- %.*s\n " , len, data);
561+ #endif
485562}
0 commit comments