فهرست منبع

Add some comments and rename some variables in the test

bel 8 سال پیش
والد
کامیت
b3f8f87cbd
3فایلهای تغییر یافته به همراه37 افزوده شده و 19 حذف شده
  1. 18 3
      src/civetweb.c
  2. 1 1
      src/mod_lua.inl
  3. 18 15
      test/public_server.c

+ 18 - 3
src/civetweb.c

@@ -1507,7 +1507,9 @@ struct mg_context {
 	char *config[NUM_OPTIONS];     /* Civetweb configuration parameters */
 	struct mg_callbacks callbacks; /* User-defined callback function */
 	void *user_data;               /* User-defined data */
-	int context_type;              /* 1 = server context, 2 = client context */
+	int context_type;              /* 1 = server context,
+	                                * 2 = ws/wss client context,
+	                                */
 
 	struct socket *listening_sockets;
 	struct pollfd *listening_socket_fds;
@@ -12182,24 +12184,33 @@ mg_close_connection(struct mg_connection *conn)
 	}
 
 	if (conn->ctx->context_type == 2) {
+		/* ws/wss client */
 		client_ctx = conn->ctx;
+
 		/* client context: loops must end */
 		conn->ctx->stop_flag = 1;
+
+		/* get client out of recv function */
+		// shutdown(conn->client.sock, SHUTDOWN_RD);
 	}
 
+
 #ifndef NO_SSL
 	if (conn->client_ssl_ctx != NULL) {
 		SSL_CTX_free((SSL_CTX *)conn->client_ssl_ctx);
 	}
 #endif
+
 	close_connection(conn);
+
 	if (client_ctx != NULL) {
-		/* join worker thread and free context */
+		/* join worker thread */
 		for (i = 0; i < client_ctx->cfg_worker_threads; i++) {
 			if (client_ctx->workerthreadids[i] != 0) {
 				mg_join_thread(client_ctx->workerthreadids[i]);
 			}
 		}
+		/* free context */
 		mg_free(client_ctx->workerthreadids);
 		mg_free(client_ctx);
 		(void)pthread_mutex_destroy(&conn->mutex);
@@ -12823,6 +12834,10 @@ websocket_client_thread(void *data)
 		cdata->close_handler(cdata->conn, cdata->callback_data);
 	}
 
+	/* The websocket_client context has only this thread. If it runs out,
+	   set the stop_flag to 2 (= "stopped"). */
+	cdata->conn->ctx->stop_flag = 2;
+
 	mg_free((void *)cdata);
 
 #ifdef _WIN32
@@ -12909,7 +12924,7 @@ mg_connect_websocket_client(const char *host,
 	newctx = (struct mg_context *)mg_malloc(sizeof(struct mg_context));
 	memcpy(newctx, conn->ctx, sizeof(struct mg_context));
 	newctx->user_data = user_data;
-	newctx->context_type = 2;       /* client context type */
+	newctx->context_type = 2;       /* ws/wss client context type */
 	newctx->cfg_worker_threads = 1; /* one worker thread will be created */
 	newctx->workerthreadids =
 	    (pthread_t *)mg_calloc(newctx->cfg_worker_threads, sizeof(pthread_t));

+ 1 - 1
src/mod_lua.inl

@@ -289,7 +289,7 @@ lsp_connect(lua_State *L)
 			return luaL_error(L, ebuf);
 		} else {
 			lua_newtable(L);
-			reg_lstring(L, "sock", (const char*)&sock, sizeof(SOCKET));
+			reg_lstring(L, "sock", (const char *)&sock, sizeof(SOCKET));
 			reg_string(L, "host", lua_tostring(L, -4));
 			luaL_getmetatable(L, LUASOCKET);
 			lua_setmetatable(L, -2);

+ 18 - 15
test/public_server.c

@@ -962,9 +962,11 @@ START_TEST(test_request_handlers)
 	struct tclient_data ws_client1_data = {NULL, 0, 0};
 	struct tclient_data ws_client2_data = {NULL, 0, 0};
 	struct tclient_data ws_client3_data = {NULL, 0, 0};
+	struct tclient_data ws_client4_data = {NULL, 0, 0};
 	struct mg_connection *ws_client1_conn = NULL;
 	struct mg_connection *ws_client2_conn = NULL;
 	struct mg_connection *ws_client3_conn = NULL;
+	struct mg_connection *ws_client4_conn = NULL;
 #endif
 
 	char cmd_buf[256];
@@ -1700,10 +1702,12 @@ START_TEST(test_request_handlers)
 	ws_client3_data.len = 0;
 
 	/* Disconnect client 3 */
+	ck_assert(ws_client3_data.closed == 0);
 	mg_close_connection(ws_client3_conn);
+	ck_assert(ws_client3_data.closed == 1);
 
-	/* Connect client 3 again */
-	ws_client3_conn =
+	/* Connect client 4 */
+	ws_client4_conn =
 	    mg_connect_websocket_client("localhost",
 #if defined(NO_SSL)
 	                                ipv4_port,
@@ -1718,7 +1722,7 @@ START_TEST(test_request_handlers)
 	                                NULL,
 	                                websocket_client_data_handler,
 	                                websocket_client_close_handler,
-	                                &ws_client3_data);
+	                                &ws_client4_data);
 
 	ck_assert(ws_client3_conn != NULL);
 
@@ -1726,19 +1730,18 @@ START_TEST(test_request_handlers)
 	    &(ws_client3_data.data)); /* Wait for the websocket welcome message */
 	ck_assert(ws_client1_data.closed == 1);
 	ck_assert(ws_client2_data.closed == 1);
-	ck_assert(ws_client3_data.closed == 0);
-	ck_assert(ws_client1_data.data == NULL);
-	ck_assert(ws_client1_data.len == 0);
-	ck_assert(ws_client2_data.data == NULL);
-	ck_assert(ws_client2_data.len == 0);
-	ck_assert(ws_client3_data.data != NULL);
-	ck_assert(ws_client3_data.len == websocket_welcome_msg_len);
-	ck_assert(!memcmp(ws_client3_data.data,
+	ck_assert(ws_client3_data.closed == 1);
+	ck_assert(ws_client4_data.closed == 0);
+	ck_assert(ws_client4_data.data != NULL);
+	ck_assert(ws_client4_data.len == websocket_welcome_msg_len);
+	ck_assert(!memcmp(ws_client4_data.data,
 	                  websocket_welcome_msg,
 	                  websocket_welcome_msg_len));
-	free(ws_client3_data.data);
-	ws_client3_data.data = NULL;
-	ws_client3_data.len = 0;
+	free(ws_client4_data.data);
+	ws_client4_data.data = NULL;
+	ws_client4_data.len = 0;
+
+/* stop the server without closing this connection */
 
 #endif
 
@@ -1756,7 +1759,7 @@ START_TEST(test_request_handlers)
 		}
 	}
 
-	ck_assert_int_eq(ws_client3_data.closed, 1);
+	ck_assert_int_eq(ws_client4_data.closed, 1);
 #endif
 }
 END_TEST