Disconnect clients when server DP is closed 14/260014/4
authorSeonah Moon <seonah1.moon@samsung.com>
Thu, 17 Jun 2021 10:33:09 +0000 (19:33 +0900)
committerSeonah Moon <seonah1.moon@samsung.com>
Mon, 21 Jun 2021 02:56:18 +0000 (11:56 +0900)
Change-Id: Ibe90ac217e7ca431e0a69f9277e99a294c76c55b

plugins/libwebsockets/libwebsockets-plugin.cpp

index ed0c6fe..54cb6df 100755 (executable)
@@ -102,15 +102,17 @@ static vine_dp_plugin_callbacks g_callbacks = {
 static map<int, struct lws_pollfd *> g_pollfds;
 static struct lws_context *g_context = NULL;
 
+#define VINE_SERVICE_NAME_HEADER "vine-service-name:"
+#define VINE_SERVICE_NAME_HEADER_LEN 18
+#define VINE_PROTOCOL_NAME "vine-websocket-protocol"
+
 static int _websocket_protocol_cb(struct lws *wsi,
                enum lws_callback_reasons reason, void *user, void *in, size_t len);
 static struct lws_protocols protocols[] = {
-       {"vine-websocket-protocol", _websocket_protocol_cb, sizeof(websocket_s), 0, 0, NULL, 0},
+       {VINE_PROTOCOL_NAME, _websocket_protocol_cb, sizeof(websocket_s), 0, 0, NULL, 0},
        { NULL, NULL, 0, 0 } /* terminator */
 };
 
-#define VINE_SERVICE_NAME_HEADER "vine-service-name:"
-#define VINE_SERVICE_NAME_HEADER_LEN 18
 
 static void websocket_process_event(int fd, int events);
 static void _connect_server(websocket_s *ws,
@@ -546,6 +548,15 @@ static int _websocket_protocol_cb(struct lws *wsi,
                        g_callbacks.terminated_cb(ws->user);
                break;
 
+       case LWS_CALLBACK_USER:
+               VINE_LOGI("vhost[%p] is closed. disconnect a client. wsi[%p] ws[%p]",
+                               vhd->vh, wsi, ws);
+               if (ws && ws->wsi) {
+                       ws->close_requested = true;
+                       lws_callback_on_writable(ws->wsi);
+               }
+               return -1;
+
        /* --- client callbacks --- */
        case LWS_CALLBACK_CLIENT_CONNECTION_ERROR:
                VINE_LOGI("Failed connection request to server.");
@@ -977,7 +988,12 @@ static int websocket_close(vine_dp_plugin_h handle)
 
        ws->close_requested = true;
        if (ws->is_server) {
-               VINE_LOGE("vh destroy[%p]", ws->vh);
+               const struct lws_protocols *protocol = lws_vhost_name_to_protocol(ws->vh,
+                               VINE_PROTOCOL_NAME);
+               if (!protocol)
+                       return VINE_DATA_PATH_ERROR_OPERATION_FAILED;
+
+               lws_callback_all_protocol_vhost_args(ws->vh, protocol, LWS_CALLBACK_USER, NULL, 0);
                listen_vh_list.erase(ws->vh);
                lws_vhost_destroy(ws->vh);
                ws->vh = NULL;
@@ -1089,6 +1105,7 @@ static int websocket_get_local_address_info(vine_dp_plugin_h handle,
 static int websocket_set_token(vine_dp_plugin_h handle, const char *token)
 {
        RET_VAL_IF(handle == NULL, VINE_DATA_PATH_ERROR_INVALID_PARAMETER, "handle is NULL");
+       RET_VAL_IF(token == NULL, VINE_DATA_PATH_ERROR_INVALID_PARAMETER, "token is NULL");
 
        websocket_s *ws = (websocket_s *)handle;
        int len = strlen(token);
@@ -1104,6 +1121,7 @@ static int websocket_set_token(vine_dp_plugin_h handle, const char *token)
 static int websocket_get_token(vine_dp_plugin_h handle, char **token)
 {
        RET_VAL_IF(handle == NULL, VINE_DATA_PATH_ERROR_INVALID_PARAMETER, "handle is NULL");
+       RET_VAL_IF(token == NULL, VINE_DATA_PATH_ERROR_INVALID_PARAMETER, "token is NULL");
 
        websocket_s *ws = (websocket_s *)handle;
        if (!ws->token || ws->token_len <= 0) {