diff --git a/lib/client-handshake.c b/lib/client-handshake.c index a95eb52d..b805cd15 100644 --- a/lib/client-handshake.c +++ b/lib/client-handshake.c @@ -534,7 +534,7 @@ check_accept: this->fds[this->fds_count++].events = POLLIN; wsi->state = WSI_STATE_ESTABLISHED; - wsi->client_mode = 1; + wsi->mode = LWS_CONNMODE_WS_CLIENT; fprintf(stderr, "handshake OK for protocol %s\n", wsi->protocol->name); diff --git a/lib/handshake.c b/lib/handshake.c index d126809f..ab2f09e6 100644 --- a/lib/handshake.c +++ b/lib/handshake.c @@ -463,12 +463,17 @@ libwebsocket_read(struct libwebsocket *wsi, unsigned char * buf, size_t len) fwrite(buf, 1, len, stderr); #endif - if (wsi->client_mode) { + switch (wsi->mode) { + case LWS_CONNMODE_WS_CLIENT: for (n = 0; n < len; n++) libwebsocket_client_rx_sm(wsi, *buf++); return 0; + default: + break; } + + /* LWS_CONNMODE_WS_SERVING */ for (n = 0; n < len; n++) libwebsocket_parse(wsi, *buf++); @@ -557,13 +562,18 @@ libwebsocket_read(struct libwebsocket *wsi, unsigned char * buf, size_t len) break; case WSI_STATE_ESTABLISHED: - if (wsi->client_mode) { + switch (wsi->mode) { + case LWS_CONNMODE_WS_CLIENT: for (n = 0; n < len; n++) libwebsocket_client_rx_sm(wsi, *buf++); return 0; + default: + break; } + /* LWS_CONNMODE_WS_SERVING */ + if (libwebsocket_interpret_incoming_packet(wsi, buf, len) < 0) goto bail; diff --git a/lib/libwebsockets.c b/lib/libwebsockets.c index 7f111644..6a74a77f 100644 --- a/lib/libwebsockets.c +++ b/lib/libwebsockets.c @@ -142,7 +142,7 @@ libwebsocket_poll_connections(struct libwebsocket_context *this) /* only to clients connected to us */ - if (wsi->client_mode) + if (wsi->mode != LWS_CONNMODE_WS_SERVING) continue; /* @@ -223,10 +223,14 @@ libwebsocket_context_destroy(struct libwebsocket_context *this) /* close listening skt and per-protocol broadcast sockets */ for (client = this->count_protocols + 1; client < this->fds_count; client++) - if (this->wsi[client]->client_mode) - libwebsocket_client_close(this->wsi[client]); - else + switch (this->wsi[client]->mode) { + case LWS_CONNMODE_WS_SERVING: libwebsocket_close_and_free_session(this->wsi[client]); + break; + case LWS_CONNMODE_WS_CLIENT: + libwebsocket_client_close(this->wsi[client]); + break; + } #ifdef LWS_OPENSSL_SUPPORT if (this->ssl_ctx) @@ -397,7 +401,7 @@ libwebsocket_service(struct libwebsocket_context *this, int timeout_ms) this->wsi[this->fds_count]->sock = fd; this->wsi[this->fds_count]->state = WSI_STATE_HTTP; this->wsi[this->fds_count]->name_buffer_pos = 0; - this->wsi[this->fds_count]->client_mode = 0; + this->wsi[this->fds_count]->mode = LWS_CONNMODE_WS_SERVING; for (n = 0; n < WSI_TOKEN_COUNT; n++) { this->wsi[this->fds_count]-> diff --git a/lib/parsers.c b/lib/parsers.c index b1c45a0b..304c5743 100644 --- a/lib/parsers.c +++ b/lib/parsers.c @@ -1138,7 +1138,8 @@ int libwebsocket_write(struct libwebsocket *wsi, unsigned char *buf, * Deal with masking if appropriate */ - if (wsi->client_mode && wsi->ietf_spec_revision == 4) { + if (wsi->mode == LWS_CONNMODE_WS_CLIENT && + wsi->ietf_spec_revision == 4) { /* * this is only useful for security tests where it's required diff --git a/lib/private-libwebsockets.h b/lib/private-libwebsockets.h index c2010f94..8028b948 100644 --- a/lib/private-libwebsockets.h +++ b/lib/private-libwebsockets.h @@ -182,6 +182,11 @@ struct libwebsocket_context { int count_protocols; }; +enum connection_mode { + LWS_CONNMODE_WS_SERVING, + LWS_CONNMODE_WS_CLIENT, +}; + /* * This is totally opaque to code using the library. It's exported as a @@ -222,7 +227,7 @@ struct libwebsocket { /* client support */ char initial_handshake_hash_base64[30]; - int client_mode; + enum connection_mode mode; #ifdef LWS_OPENSSL_SUPPORT SSL *ssl;