diff --git a/example/http_client.cpp b/example/http_client.cpp index 743bc6e7..f0b6eaac 100644 --- a/example/http_client.cpp +++ b/example/http_client.cpp @@ -4,6 +4,7 @@ #include "../poseidon/precompiled.ipp" #include "../poseidon/easy/easy_http_client.hpp" #include "../poseidon/easy/easy_timer.hpp" +#include "../poseidon/easy/enums.hpp" #include "../poseidon/utils.hpp" namespace { using namespace ::poseidon; @@ -12,13 +13,18 @@ extern Easy_HTTP_Client my_client; extern Easy_Timer my_timer; void -event_callback(shptrR session, Abstract_Fiber& /*fiber*/, Easy_Socket_Event event, HTTP_Response_Headers&& resp, linear_buffer&& data) +event_callback(shptrR session, Abstract_Fiber& /*fiber*/, + Easy_HTTP_Event event, HTTP_Response_Headers&& resp, linear_buffer&& data) { switch(event) { - case easy_socket_msg_bin: { - POSEIDON_LOG_ERROR(("HTTP client received response from `$1`: $2 $3"), - session->remote_address(), resp.status, resp.reason); + case easy_http_open: + POSEIDON_LOG_ERROR(("example HTTP client connected to server: $1"), + session->remote_address()); + break; + case easy_http_message: { + POSEIDON_LOG_ERROR(("example HTTP client received response: $1 $2"), + resp.status, resp.reason); for(const auto& pair : resp.headers) POSEIDON_LOG_ERROR((" $1 --> $2"), pair.first, pair.second); @@ -26,21 +32,18 @@ event_callback(shptrR session, Abstract_Fiber& /*fiber*/, E break; } - case easy_socket_close: - POSEIDON_LOG_ERROR(("example HTTP client shut down connection: $1"), data); + case easy_http_close: + POSEIDON_LOG_ERROR(("example HTTP client shutdown: $1"), data); break; - case easy_socket_open: - case easy_socket_stream: - case easy_socket_msg_text: - case easy_socket_pong: default: ASTERIA_TERMINATE(("shouldn't happen: event = $1"), event); } } void -timer_callback(shptrR /*timer*/, Abstract_Fiber& /*fiber*/, steady_time /*now*/) +timer_callback(shptrR /*timer*/, Abstract_Fiber& /*fiber*/, + steady_time /*now*/) { static uint32_t state; diff --git a/example/http_server.cpp b/example/http_server.cpp index c986dcac..f55dc671 100644 --- a/example/http_server.cpp +++ b/example/http_server.cpp @@ -3,6 +3,7 @@ #include "../poseidon/precompiled.ipp" #include "../poseidon/easy/easy_http_server.hpp" +#include "../poseidon/easy/enums.hpp" #include "../poseidon/utils.hpp" namespace { using namespace ::poseidon; @@ -10,14 +11,22 @@ using namespace ::poseidon; extern Easy_HTTP_Server my_server; void -event_callback(shptrR session, Abstract_Fiber& /*fiber*/, Easy_Socket_Event event, HTTP_Request_Headers&& req, linear_buffer&& data) +event_callback(shptrR session, Abstract_Fiber& /*fiber*/, + Easy_HTTP_Event event, HTTP_Request_Headers&& req, linear_buffer&& data) { switch(event) { - case easy_socket_msg_bin: { - POSEIDON_LOG_ERROR(("HTTP request --> $1 $2: $3"), req.method, req.uri_path, req.uri_query); + case easy_http_open: + POSEIDON_LOG_ERROR(("example HTTP server accepted connection: $1"), + session->remote_address()); + break; + + case easy_http_message: { + POSEIDON_LOG_ERROR(("HTTP request --> $1 $2: $3"), + req.method, req.uri_path, req.uri_query); for(const auto& r : req.headers) POSEIDON_LOG_ERROR(("HTTP header --> $1: $2"), r.first, r.second); + // send a response HTTP_Response_Headers resp; resp.status = 200; resp.headers.emplace_back(sref("Date"), system_clock::now()); @@ -30,14 +39,10 @@ event_callback(shptrR session, Abstract_Fiber& /*fiber*/, E break; } - case easy_socket_close: - POSEIDON_LOG_ERROR(("example HTTP server shut down connection: $1"), data); + case easy_http_close: + POSEIDON_LOG_ERROR(("example HTTP server shutdown: $1"), data); break; - case easy_socket_open: - case easy_socket_stream: - case easy_socket_msg_text: - case easy_socket_pong: default: ASTERIA_TERMINATE(("shouldn't happen: event = $1"), event); } @@ -47,7 +52,8 @@ int start_server() { my_server.start(sref("[::]:3804")); - POSEIDON_LOG_ERROR(("example HTTP server started: bind = $1"), my_server.local_address()); + POSEIDON_LOG_ERROR(("example HTTP server started: bind = $1"), + my_server.local_address()); return 0; } diff --git a/example/https_client.cpp b/example/https_client.cpp index cbf4b38c..9c4dc868 100644 --- a/example/https_client.cpp +++ b/example/https_client.cpp @@ -4,6 +4,7 @@ #include "../poseidon/precompiled.ipp" #include "../poseidon/easy/easy_https_client.hpp" #include "../poseidon/easy/easy_timer.hpp" +#include "../poseidon/easy/enums.hpp" #include "../poseidon/utils.hpp" namespace { using namespace ::poseidon; @@ -12,13 +13,18 @@ extern Easy_HTTPS_Client my_client; extern Easy_Timer my_timer; void -event_callback(shptrR session, Abstract_Fiber& /*fiber*/, Easy_Socket_Event event, HTTP_Response_Headers&& resp, linear_buffer&& data) +event_callback(shptrR session, Abstract_Fiber& /*fiber*/, + Easy_HTTP_Event event, HTTP_Response_Headers&& resp, linear_buffer&& data) { switch(event) { - case easy_socket_msg_bin: { - POSEIDON_LOG_WARN(("HTTPS client received response from `$1`: $2 $3"), - session->remote_address(), resp.status, resp.reason); + case easy_http_open: + POSEIDON_LOG_WARN(("example HTTPS client connected to server: $1"), + session->remote_address()); + break; + case easy_http_message: { + POSEIDON_LOG_WARN(("example HTTPS client received response: $1 $2"), + resp.status, resp.reason); for(const auto& pair : resp.headers) POSEIDON_LOG_WARN((" $1 --> $2"), pair.first, pair.second); @@ -26,21 +32,18 @@ event_callback(shptrR session, Abstract_Fiber& /*fiber*/, break; } - case easy_socket_close: - POSEIDON_LOG_WARN(("example HTTPS client shut down connection: $1"), data); + case easy_http_close: + POSEIDON_LOG_WARN(("example HTTPS client shutdown: $1"), data); break; - case easy_socket_open: - case easy_socket_stream: - case easy_socket_msg_text: - case easy_socket_pong: default: ASTERIA_TERMINATE(("shouldn't happen: event = $1"), event); } } void -timer_callback(shptrR /*timer*/, Abstract_Fiber& /*fiber*/, steady_time /*now*/) +timer_callback(shptrR /*timer*/, Abstract_Fiber& /*fiber*/, + steady_time /*now*/) { static uint32_t state; diff --git a/example/https_server.cpp b/example/https_server.cpp index f09acf63..faf3cee4 100644 --- a/example/https_server.cpp +++ b/example/https_server.cpp @@ -3,6 +3,7 @@ #include "../poseidon/precompiled.ipp" #include "../poseidon/easy/easy_https_server.hpp" +#include "../poseidon/easy/enums.hpp" #include "../poseidon/utils.hpp" namespace { using namespace ::poseidon; @@ -10,14 +11,22 @@ using namespace ::poseidon; extern Easy_HTTPS_Server my_server; void -event_callback(shptrR session, Abstract_Fiber& /*fiber*/, Easy_Socket_Event event,HTTP_Request_Headers&& req, linear_buffer&& data) +event_callback(shptrR session, Abstract_Fiber& /*fiber*/, + Easy_HTTP_Event event,HTTP_Request_Headers&& req, linear_buffer&& data) { switch(event) { - case easy_socket_msg_bin: { - POSEIDON_LOG_WARN(("HTTPS request --> $1 $2: $3"), req.method, req.uri_path, req.uri_query); + case easy_http_open: + POSEIDON_LOG_WARN(("example HTTPS server accepted connection: $1"), + session->remote_address()); + break; + + case easy_http_message: { + POSEIDON_LOG_WARN(("HTTPS request --> $1 $2: $3"), + req.method, req.uri_path, req.uri_query); for(const auto& r : req.headers) POSEIDON_LOG_WARN(("HTTPS header --> $1: $2"), r.first, r.second); + // send a response HTTP_Response_Headers resp; resp.status = 200; resp.headers.emplace_back(sref("Date"), system_clock::now()); @@ -30,14 +39,10 @@ event_callback(shptrR session, Abstract_Fiber& /*fiber*/, break; } - case easy_socket_close: + case easy_http_close: POSEIDON_LOG_WARN(("example HTTPS server shut down connection: $1"), data); break; - case easy_socket_open: - case easy_socket_stream: - case easy_socket_msg_text: - case easy_socket_pong: default: ASTERIA_TERMINATE(("shouldn't happen: event = $1"), event); } @@ -47,7 +52,8 @@ int start_server() { my_server.start(sref("[::]:3805")); - POSEIDON_LOG_WARN(("example HTTPS server started: bind = $1"), my_server.local_address()); + POSEIDON_LOG_WARN(("example HTTPS server started: bind = $1"), + my_server.local_address()); return 0; } diff --git a/example/ssl_echo_server.cpp b/example/ssl_echo_server.cpp index 7efc9260..0c7b5bb5 100644 --- a/example/ssl_echo_server.cpp +++ b/example/ssl_echo_server.cpp @@ -3,6 +3,7 @@ #include "../poseidon/precompiled.ipp" #include "../poseidon/easy/easy_ssl_server.hpp" +#include "../poseidon/easy/enums.hpp" #include "../poseidon/utils.hpp" namespace { using namespace ::poseidon; @@ -10,26 +11,27 @@ using namespace ::poseidon; extern Easy_SSL_Server my_server; void -event_callback(shptrR socket, Abstract_Fiber& /*fiber*/, Easy_Socket_Event event, linear_buffer& data, int code) +event_callback(shptrR socket, Abstract_Fiber& /*fiber*/, + Easy_Stream_Event event, linear_buffer& data, int code) { switch(event) { - case easy_socket_open: - POSEIDON_LOG_WARN(("example SSL server accepted connection: $1"), socket->remote_address()); + case easy_stream_open: + POSEIDON_LOG_WARN(("example SSL server accepted connection: $1"), + socket->remote_address()); break; - case easy_socket_stream: - POSEIDON_LOG_WARN(("example SSL server received data (eof = $1): $2"), code, data); + case easy_stream_data: + POSEIDON_LOG_WARN(("example SSL server received data (eof = $1): $2"), + code, data); socket->ssl_send(data); data.clear(); break; - case easy_socket_close: - POSEIDON_LOG_WARN(("example SSL server shut down connection: (errno = $1) $2"), code, data); + case easy_stream_close: + POSEIDON_LOG_WARN(("example SSL server shutdown: (errno = $1) $2"), + code, data); break; - case easy_socket_msg_text: - case easy_socket_msg_bin: - case easy_socket_pong: default: ASTERIA_TERMINATE(("shouldn't happen: event = $1"), event); } @@ -39,7 +41,8 @@ int start_server() { my_server.start(sref("[::]:3803")); - POSEIDON_LOG_WARN(("example SSL server started: bind = $1"), my_server.local_address()); + POSEIDON_LOG_WARN(("example SSL server started: bind = $1"), + my_server.local_address()); return 0; } diff --git a/example/tcp_echo_server.cpp b/example/tcp_echo_server.cpp index 229df0b0..2007adc1 100644 --- a/example/tcp_echo_server.cpp +++ b/example/tcp_echo_server.cpp @@ -3,6 +3,7 @@ #include "../poseidon/precompiled.ipp" #include "../poseidon/easy/easy_tcp_server.hpp" +#include "../poseidon/easy/enums.hpp" #include "../poseidon/utils.hpp" namespace { using namespace ::poseidon; @@ -10,26 +11,27 @@ using namespace ::poseidon; extern Easy_TCP_Server my_server; void -event_callback(shptrR socket, Abstract_Fiber& /*fiber*/, Easy_Socket_Event event, linear_buffer& data, int code) +event_callback(shptrR socket, Abstract_Fiber& /*fiber*/, + Easy_Stream_Event event, linear_buffer& data, int code) { switch(event) { - case easy_socket_open: - POSEIDON_LOG_ERROR(("example SSL server accepted connection: $1"), socket->remote_address()); + case easy_stream_open: + POSEIDON_LOG_ERROR(("example SSL server accepted connection: $1"), + socket->remote_address()); break; - case easy_socket_stream: - POSEIDON_LOG_ERROR(("example SSL server received data (eof = $1): $2"), code, data); + case easy_stream_data: + POSEIDON_LOG_ERROR(("example SSL server received data (eof = $1): $2"), + code, data); socket->tcp_send(data); data.clear(); break; - case easy_socket_close: - POSEIDON_LOG_ERROR(("example SSL server shut down connection: (errno = $1) $2"), code, data); + case easy_stream_close: + POSEIDON_LOG_ERROR(("example SSL server shutdown: (errno = $1) $2"), + code, data); break; - case easy_socket_msg_text: - case easy_socket_msg_bin: - case easy_socket_pong: default: ASTERIA_TERMINATE(("shouldn't happen: event = $1"), event); } @@ -39,7 +41,8 @@ int start_server() { my_server.start(sref("[::]:3802")); - POSEIDON_LOG_ERROR(("example TCP server started: bind = $1"), my_server.local_address()); + POSEIDON_LOG_ERROR(("example TCP server started: bind = $1"), + my_server.local_address()); return 0; } diff --git a/example/timer.cpp b/example/timer.cpp index 96090e26..ada6d2e9 100644 --- a/example/timer.cpp +++ b/example/timer.cpp @@ -10,7 +10,8 @@ using namespace ::poseidon; extern Easy_Timer my_timer; void -timer_callback(shptrR /*timer*/, Abstract_Fiber& /*fiber*/, steady_time now) +timer_callback(shptrR /*timer*/, Abstract_Fiber& /*fiber*/, + steady_time now) { POSEIDON_LOG_WARN(("example timer: now = $1"), now.time_since_epoch()); } diff --git a/example/ws_client.cpp b/example/ws_client.cpp index fd4c6a67..eb599679 100644 --- a/example/ws_client.cpp +++ b/example/ws_client.cpp @@ -4,6 +4,7 @@ #include "../poseidon/precompiled.ipp" #include "../poseidon/easy/easy_ws_client.hpp" #include "../poseidon/easy/easy_timer.hpp" +#include "../poseidon/easy/enums.hpp" #include "../poseidon/utils.hpp" namespace { using namespace ::poseidon; @@ -12,37 +13,39 @@ extern Easy_WS_Client my_client; extern Easy_Timer my_timer; void -event_callback(shptrR session, Abstract_Fiber& /*fiber*/, Easy_Socket_Event event, linear_buffer&& data) +event_callback(shptrR session, Abstract_Fiber& /*fiber*/, + Easy_WS_Event event, linear_buffer&& data) { switch(event) { - case easy_socket_open: - POSEIDON_LOG_WARN(("example WS client established connection to `$1`: $2"), session->remote_address(), data); + case easy_ws_open: + POSEIDON_LOG_WARN(("example WS client established connection to `$1`: $2"), + session->remote_address(), data); break; - case easy_socket_msg_text: + case easy_ws_text: POSEIDON_LOG_WARN(("example WS client received TEXT data: $1"), data); break; - case easy_socket_msg_bin: + case easy_ws_binary: POSEIDON_LOG_WARN(("example WS client received BINARY data: $1"), data); break; - case easy_socket_pong: + case easy_ws_pong: POSEIDON_LOG_WARN(("example WS client received PONG data: $1"), data); break; - case easy_socket_close: + case easy_ws_close: POSEIDON_LOG_WARN(("example WS client shut down connection: $1"), data); break; - case easy_socket_stream: default: ASTERIA_TERMINATE(("shouldn't happen: event = $1"), event); } } void -timer_callback(shptrR /*timer*/, Abstract_Fiber& /*fiber*/, steady_time /*now*/) +timer_callback(shptrR /*timer*/, Abstract_Fiber& /*fiber*/, + steady_time /*now*/) { static uint32_t state; @@ -61,26 +64,19 @@ timer_callback(shptrR /*timer*/, Abstract_Fiber& /*fiber*/, stea case 1: { const char data[] = "some text data"; - my_client.ws_send(websocket_text, data); + my_client.ws_send(easy_ws_text, data); POSEIDON_LOG_DEBUG(("example WS client sent TEXT frame: $1"), data); break; } case 2: { const char data[] = "some binary data"; - my_client.ws_send(websocket_bin, data); + my_client.ws_send(easy_ws_binary, data); POSEIDON_LOG_DEBUG(("example WS client sent BINARY frame: $1"), data); break; } case 3: { - const char data[] = "some ping data"; - my_client.ws_send(websocket_ping, data); - POSEIDON_LOG_DEBUG(("example WS client sent PING frame: $1"), data); - break; - } - - case 4: { // HACKS; DO NOT PLAY WITH THESE AT HOME. WebSocket_Frame_Header header; header.mask = 1; diff --git a/example/ws_server.cpp b/example/ws_server.cpp index e164438c..27434336 100644 --- a/example/ws_server.cpp +++ b/example/ws_server.cpp @@ -3,6 +3,7 @@ #include "../poseidon/precompiled.ipp" #include "../poseidon/easy/easy_ws_server.hpp" +#include "../poseidon/easy/enums.hpp" #include "../poseidon/utils.hpp" namespace { using namespace ::poseidon; @@ -10,32 +11,33 @@ using namespace ::poseidon; extern Easy_WS_Server my_server; void -event_callback(shptrR session, Abstract_Fiber& /*fiber*/, Easy_Socket_Event event, linear_buffer&& data) +event_callback(shptrR session, Abstract_Fiber& /*fiber*/, + Easy_WS_Event event, linear_buffer&& data) { switch(event) { - case easy_socket_open: - POSEIDON_LOG_ERROR(("example WS server accepted connection from `$1`: $2"), session->remote_address(), data); + case easy_ws_open: + POSEIDON_LOG_ERROR(("example WS server accepted connection from `$1`: $2"), + session->remote_address(), data); break; - case easy_socket_msg_text: + case easy_ws_text: POSEIDON_LOG_ERROR(("example WS server received TEXT data: $1"), data); - session->ws_send(websocket_text, data); + session->ws_send(easy_ws_text, data); break; - case easy_socket_msg_bin: + case easy_ws_binary: POSEIDON_LOG_ERROR(("example WS server received BINARY data: $1"), data); - session->ws_send(websocket_bin, data); + session->ws_send(easy_ws_binary, data); break; - case easy_socket_pong: + case easy_ws_pong: POSEIDON_LOG_ERROR(("example WS server received PONG data: $1"), data); break; - case easy_socket_close: + case easy_ws_close: POSEIDON_LOG_ERROR(("example WS server shut down connection: $1"), data); break; - case easy_socket_stream: default: ASTERIA_TERMINATE(("shouldn't happen: event = $1"), event); } @@ -45,7 +47,8 @@ int start_server() { my_server.start(sref("[::]:3806")); - POSEIDON_LOG_ERROR(("example WS server started: bind = $1"), my_server.local_address()); + POSEIDON_LOG_ERROR(("example WS server started: bind = $1"), + my_server.local_address()); return 0; } diff --git a/example/wss_client.cpp b/example/wss_client.cpp index efd7cf5f..709854b5 100644 --- a/example/wss_client.cpp +++ b/example/wss_client.cpp @@ -4,6 +4,7 @@ #include "../poseidon/precompiled.ipp" #include "../poseidon/easy/easy_wss_client.hpp" #include "../poseidon/easy/easy_timer.hpp" +#include "../poseidon/easy/enums.hpp" #include "../poseidon/utils.hpp" namespace { using namespace ::poseidon; @@ -12,37 +13,39 @@ extern Easy_WSS_Client my_client; extern Easy_Timer my_timer; void -event_callback(shptrR session, Abstract_Fiber& /*fiber*/, Easy_Socket_Event event, linear_buffer&& data) +event_callback(shptrR session, Abstract_Fiber& /*fiber*/, + Easy_WS_Event event, linear_buffer&& data) { switch(event) { - case easy_socket_open: - POSEIDON_LOG_WARN(("example WSS client established connection to `$1`: $2"), session->remote_address(), data); + case easy_ws_open: + POSEIDON_LOG_WARN(("example WSS client established connection to `$1`: $2"), + session->remote_address(), data); break; - case easy_socket_msg_text: + case easy_ws_text: POSEIDON_LOG_WARN(("example WSS client received TEXT data: $1"), data); break; - case easy_socket_msg_bin: + case easy_ws_binary: POSEIDON_LOG_WARN(("example WSS client received BINARY data: $1"), data); break; - case easy_socket_pong: + case easy_ws_pong: POSEIDON_LOG_WARN(("example WSS client received PONG data: $1"), data); break; - case easy_socket_close: + case easy_ws_close: POSEIDON_LOG_WARN(("example WSS client shut down connection: $1"), data); break; - case easy_socket_stream: default: ASTERIA_TERMINATE(("shouldn't happen: event = $1"), event); } } void -timer_callback(shptrR /*timer*/, Abstract_Fiber& /*fiber*/, steady_time /*now*/) +timer_callback(shptrR /*timer*/, Abstract_Fiber& /*fiber*/, + steady_time /*now*/) { static uint32_t state; @@ -61,26 +64,19 @@ timer_callback(shptrR /*timer*/, Abstract_Fiber& /*fiber*/, stea case 1: { const char data[] = "some text data"; - my_client.wss_send(websocket_text, data); + my_client.wss_send(easy_ws_text, data); POSEIDON_LOG_DEBUG(("example WS client sent TEXT frame: $1"), data); break; } case 2: { const char data[] = "some binary data"; - my_client.wss_send(websocket_bin, data); + my_client.wss_send(easy_ws_binary, data); POSEIDON_LOG_DEBUG(("example WS client sent BINARY frame: $1"), data); break; } case 3: { - const char data[] = "some ping data"; - my_client.wss_send(websocket_ping, data); - POSEIDON_LOG_DEBUG(("example WS client sent PING frame: $1"), data); - break; - } - - case 4: { // HACKS; DO NOT PLAY WITH THESE AT HOME. WebSocket_Frame_Header header; header.mask = 1; diff --git a/example/wss_server.cpp b/example/wss_server.cpp index e3aad59c..9f9f669c 100644 --- a/example/wss_server.cpp +++ b/example/wss_server.cpp @@ -3,6 +3,7 @@ #include "../poseidon/precompiled.ipp" #include "../poseidon/easy/easy_wss_server.hpp" +#include "../poseidon/easy/enums.hpp" #include "../poseidon/utils.hpp" namespace { using namespace ::poseidon; @@ -10,32 +11,33 @@ using namespace ::poseidon; extern Easy_WSS_Server my_server; void -event_callback(shptrR session, Abstract_Fiber& /*fiber*/, Easy_Socket_Event event, linear_buffer&& data) +event_callback(shptrR session, Abstract_Fiber& /*fiber*/, + Easy_WS_Event event, linear_buffer&& data) { switch(event) { - case easy_socket_open: - POSEIDON_LOG_ERROR(("example WSS server accepted connection from `$1`: $2"), session->remote_address(), data); + case easy_ws_open: + POSEIDON_LOG_ERROR(("example WSS server accepted connection from `$1`: $2"), + session->remote_address(), data); break; - case easy_socket_msg_text: + case easy_ws_text: POSEIDON_LOG_ERROR(("example WSS server received TEXT data: $1"), data); - session->wss_send(websocket_text, data); + session->wss_send(easy_ws_text, data); break; - case easy_socket_msg_bin: + case easy_ws_binary: POSEIDON_LOG_ERROR(("example WSS server received BINARY data: $1"), data); - session->wss_send(websocket_bin, data); + session->wss_send(easy_ws_binary, data); break; - case easy_socket_pong: + case easy_ws_pong: POSEIDON_LOG_ERROR(("example WSS server received PONG data: $1"), data); break; - case easy_socket_close: + case easy_ws_close: POSEIDON_LOG_ERROR(("example WSS server shut down connection: $1"), data); break; - case easy_socket_stream: default: ASTERIA_TERMINATE(("shouldn't happen: event = $1"), event); } @@ -45,7 +47,8 @@ int start_server() { my_server.start(sref("[::]:3807")); - POSEIDON_LOG_ERROR(("example WSS server started: bind = $1"), my_server.local_address()); + POSEIDON_LOG_ERROR(("example WSS server started: bind = $1"), + my_server.local_address()); return 0; } diff --git a/poseidon/Makefile.inc.am b/poseidon/Makefile.inc.am index 00e44efe..57cb2d63 100644 --- a/poseidon/Makefile.inc.am +++ b/poseidon/Makefile.inc.am @@ -21,6 +21,7 @@ nobase_include_HEADERS += \ %reldir%/fiber/dns_future.hpp \ %reldir%/fiber/read_file_future.hpp \ %reldir%/fiber/abstract_fiber.hpp \ + %reldir%/socket/enums.hpp \ %reldir%/socket/socket_address.hpp \ %reldir%/socket/abstract_socket.hpp \ %reldir%/socket/udp_socket.hpp \ @@ -47,6 +48,7 @@ nobase_include_HEADERS += \ %reldir%/http/websocket_frame_header.hpp \ %reldir%/http/websocket_frame_parser.hpp \ %reldir%/http/websocket_deflator.hpp \ + %reldir%/easy/enums.hpp \ %reldir%/easy/easy_deflator.hpp \ %reldir%/easy/easy_inflator.hpp \ %reldir%/easy/easy_timer.hpp \ diff --git a/poseidon/base/enums.hpp b/poseidon/base/enums.hpp new file mode 100644 index 00000000..7d2f28c5 --- /dev/null +++ b/poseidon/base/enums.hpp @@ -0,0 +1,12 @@ +// This file is part of Poseidon. +// Copyleft 2022 - 2024, LH_Mouse. All wrongs reserved. + +#ifndef POSEIDON_BASE_ENUMS_ +#define POSEIDON_BASE_ENUMS_ + +#include "../fwd.hpp" +namespace poseidon { + + +} // namespace poseidon +#endif diff --git a/poseidon/easy/easy_http_client.cpp b/poseidon/easy/easy_http_client.cpp index 63b2aed7..d7fbce23 100644 --- a/poseidon/easy/easy_http_client.cpp +++ b/poseidon/easy/easy_http_client.cpp @@ -3,6 +3,7 @@ #include "../precompiled.ipp" #include "easy_http_client.hpp" +#include "enums.hpp" #include "../static/network_driver.hpp" #include "../fiber/abstract_fiber.hpp" #include "../static/fiber_scheduler.hpp" @@ -21,7 +22,7 @@ struct Event_Queue // shared fields between threads struct Event { - Easy_Socket_Event type; + Easy_HTTP_Event type; HTTP_Response_Headers resp; linear_buffer data; bool close_now = false; @@ -138,12 +139,21 @@ struct Final_HTTP_Client_Session final : HTTP_Client_Session } } + virtual + void + do_on_tcp_connected() override + { + Event_Queue::Event event; + event.type = easy_http_open; + this->do_push_event_common(move(event)); + } + virtual void do_on_http_response_finish(HTTP_Response_Headers&& resp, linear_buffer&& data, bool close_now) override { Event_Queue::Event event; - event.type = easy_socket_msg_bin; + event.type = easy_http_message; event.resp = move(resp); event.data = move(data); event.close_now = close_now; @@ -159,7 +169,7 @@ struct Final_HTTP_Client_Session final : HTTP_Client_Session const char* err_str = ::strerror_r(err_code, sbuf, sizeof(sbuf)); Event_Queue::Event event; - event.type = easy_socket_close; + event.type = easy_http_close; event.data.puts(err_str); this->do_push_event_common(move(event)); } diff --git a/poseidon/easy/easy_http_client.hpp b/poseidon/easy/easy_http_client.hpp index 379c1773..ae0eb1e6 100644 --- a/poseidon/easy/easy_http_client.hpp +++ b/poseidon/easy/easy_http_client.hpp @@ -16,7 +16,7 @@ class Easy_HTTP_Client thunk< shptrR, // client data socket Abstract_Fiber&, // fiber for current callback - Easy_Socket_Event, // event type; see comments above constructor + Easy_HTTP_Event, // event type; see comments above constructor HTTP_Response_Headers&&, // response status code and headers linear_buffer&&>; // response payload body @@ -31,12 +31,13 @@ class Easy_HTTP_Client public: // Constructs a client. The argument shall be an invocable object taking // `(shptrR session, Abstract_Fiber& fiber, - // Easy_Socket_Event event, HTTP_Response_Headers&& resp, linear_buffer&& + // Easy_HTTP_Event event, HTTP_Response_Headers&& resp, linear_buffer&& // data)`, where `session` is a pointer to a client socket object, and if // `event` is - // 1) `easy_socket_msg_bin`, then `resp` and `data` are the headers and body + // 1) `easy_http_open`, then `data` is empty; or + // 2) `easy_http_message`, then `req` and `data` are the headers and body // of a response message, respectively; or - // 2) `easy_socket_close`, then `resp` is empty and `data` is the error + // 3) `easy_http_close`, then `resp` is empty and `data` is the error // description. // This client object stores a copy of the callback object, which is invoked // accordingly in the main thread. The callback object is never copied, and diff --git a/poseidon/easy/easy_http_server.cpp b/poseidon/easy/easy_http_server.cpp index f463abbf..77a7fc17 100644 --- a/poseidon/easy/easy_http_server.cpp +++ b/poseidon/easy/easy_http_server.cpp @@ -3,6 +3,7 @@ #include "../precompiled.ipp" #include "easy_http_server.hpp" +#include "enums.hpp" #include "../socket/listen_socket.hpp" #include "../static/network_driver.hpp" #include "../fiber/abstract_fiber.hpp" @@ -22,7 +23,7 @@ struct Client_Table // shared fields between threads struct Event { - Easy_Socket_Event type; + Easy_HTTP_Event type; HTTP_Request_Headers req; linear_buffer data; bool close_now = false; @@ -85,7 +86,7 @@ struct Final_Fiber final : Abstract_Fiber auto event = move(queue->events.front()); queue->events.pop_front(); - if(ROCKET_UNEXPECT(event.type == easy_socket_close)) { + if(ROCKET_UNEXPECT(event.type == easy_http_close)) { // This will be the last event on this session. queue = nullptr; table->client_map.erase(client_iter); @@ -94,7 +95,7 @@ struct Final_Fiber final : Abstract_Fiber lock.unlock(); try { - if(event.type == easy_socket_pong) { + if(event.status != 0) { // Send a bad request response. HTTP_Response_Headers resp; resp.status = event.status; @@ -174,12 +175,21 @@ struct Final_HTTP_Server_Session final : HTTP_Server_Session } } + virtual + void + do_on_tcp_connected() override + { + Client_Table::Event_Queue::Event event; + event.type = easy_http_open; + this->do_push_event_common(move(event)); + } + virtual void do_on_http_request_finish(HTTP_Request_Headers&& req, linear_buffer&& data, bool close_now) override { Client_Table::Event_Queue::Event event; - event.type = easy_socket_msg_bin; + event.type = easy_http_message; event.req = move(req); event.data = move(data); event.close_now = close_now; @@ -191,7 +201,6 @@ struct Final_HTTP_Server_Session final : HTTP_Server_Session do_on_http_request_error(uint32_t status) override { Client_Table::Event_Queue::Event event; - event.type = easy_socket_pong; event.status = status; event.close_now = true; this->do_push_event_common(move(event)); @@ -206,7 +215,7 @@ struct Final_HTTP_Server_Session final : HTTP_Server_Session const char* err_str = ::strerror_r(err_code, sbuf, sizeof(sbuf)); Client_Table::Event_Queue::Event event; - event.type = easy_socket_close; + event.type = easy_http_close; event.data.puts(err_str); this->do_push_event_common(move(event)); } diff --git a/poseidon/easy/easy_http_server.hpp b/poseidon/easy/easy_http_server.hpp index d409f584..d0ce72e6 100644 --- a/poseidon/easy/easy_http_server.hpp +++ b/poseidon/easy/easy_http_server.hpp @@ -16,7 +16,7 @@ class Easy_HTTP_Server thunk< shptrR, // server data socket Abstract_Fiber&, // fiber for current callback - Easy_Socket_Event, // event type; see comments above constructor + Easy_HTTP_Event, // event type; see comments above constructor HTTP_Request_Headers&&, // request method, URI, and headers linear_buffer&&>; // request payload body @@ -30,12 +30,13 @@ class Easy_HTTP_Server public: // Constructs a server. The argument shall be an invocable object taking // `(shptrR session, Abstract_Fiber& fiber, - // Easy_Socket_Event event, HTTP_Request_Headers&& req, linear_buffer&& + // Easy_HTTP_Event event, HTTP_Request_Headers&& req, linear_buffer&& // data)`, where `session` is a pointer to a client session object, and if // `event` is - // 1) `easy_socket_msg_bin`, then `req` and `data` are the headers and body + // 1) `easy_http_open`, then `data` is empty; or + // 2) `easy_http_message`, then `req` and `data` are the headers and body // of a request message, respectively; or - // 2) `easy_socket_close`, then `resp` is empty and `data` is the error + // 3) `easy_http_close`, then `resp` is empty and `data` is the error // description. // The server object owns all client session objects. As a recommendation, // applications should store only `wkptr`s to client sessions, and call diff --git a/poseidon/easy/easy_https_client.cpp b/poseidon/easy/easy_https_client.cpp index d2ecb1c2..089c9629 100644 --- a/poseidon/easy/easy_https_client.cpp +++ b/poseidon/easy/easy_https_client.cpp @@ -3,6 +3,7 @@ #include "../precompiled.ipp" #include "easy_https_client.hpp" +#include "enums.hpp" #include "../static/network_driver.hpp" #include "../fiber/abstract_fiber.hpp" #include "../static/fiber_scheduler.hpp" @@ -21,7 +22,7 @@ struct Event_Queue // shared fields between threads struct Event { - Easy_Socket_Event type; + Easy_HTTP_Event type; HTTP_Response_Headers resp; linear_buffer data; bool close_now = false; @@ -138,12 +139,21 @@ struct Final_HTTPS_Client_Session final : HTTPS_Client_Session } } + virtual + void + do_on_ssl_connected() override + { + Event_Queue::Event event; + event.type = easy_http_open; + this->do_push_event_common(move(event)); + } + virtual void do_on_https_response_finish(HTTP_Response_Headers&& resp, linear_buffer&& data, bool close_now) override { Event_Queue::Event event; - event.type = easy_socket_msg_bin; + event.type = easy_http_message; event.resp = move(resp); event.data = move(data); event.close_now = close_now; @@ -159,7 +169,7 @@ struct Final_HTTPS_Client_Session final : HTTPS_Client_Session const char* err_str = ::strerror_r(err_code, sbuf, sizeof(sbuf)); Event_Queue::Event event; - event.type = easy_socket_close; + event.type = easy_http_close; event.data.puts(err_str); this->do_push_event_common(move(event)); } diff --git a/poseidon/easy/easy_https_client.hpp b/poseidon/easy/easy_https_client.hpp index f4f79e1a..ae21eed7 100644 --- a/poseidon/easy/easy_https_client.hpp +++ b/poseidon/easy/easy_https_client.hpp @@ -16,7 +16,7 @@ class Easy_HTTPS_Client thunk< shptrR, // client data socket Abstract_Fiber&, // fiber for current callback - Easy_Socket_Event, // event type; see comments above constructor + Easy_HTTP_Event, // event type; see comments above constructor HTTP_Response_Headers&&, // response status code and headers linear_buffer&&>; // response payload body @@ -31,12 +31,13 @@ class Easy_HTTPS_Client public: // Constructs a client. The argument shall be an invocable object taking // `(shptrR session, Abstract_Fiber& fiber, - // Easy_Socket_Event event, HTTP_Response_Headers&& resp, linear_buffer&& + // Easy_HTTP_Event event, HTTP_Response_Headers&& resp, linear_buffer&& // data)`, where `session` is a pointer to a client socket object, and if // `event` is - // 1) `easy_socket_msg_bin`, then `resp` and `data` are the headers and body + // 1) `easy_http_open`, then `data` is empty; or + // 2) `easy_http_message`, then `req` and `data` are the headers and body // of a response message, respectively; or - // 2) `easy_socket_close`, then `resp` is empty and `data` is the error + // 3) `easy_http_close`, then `resp` is empty and `data` is the error // description. // This client object stores a copy of the callback object, which is invoked // accordingly in the main thread. The callback object is never copied, and diff --git a/poseidon/easy/easy_https_server.cpp b/poseidon/easy/easy_https_server.cpp index 5029486d..88e34972 100644 --- a/poseidon/easy/easy_https_server.cpp +++ b/poseidon/easy/easy_https_server.cpp @@ -3,6 +3,7 @@ #include "../precompiled.ipp" #include "easy_https_server.hpp" +#include "enums.hpp" #include "../socket/listen_socket.hpp" #include "../static/network_driver.hpp" #include "../fiber/abstract_fiber.hpp" @@ -22,7 +23,7 @@ struct Client_Table // shared fields between threads struct Event { - Easy_Socket_Event type; + Easy_HTTP_Event type; HTTP_Request_Headers req; linear_buffer data; bool close_now = false; @@ -85,7 +86,7 @@ struct Final_Fiber final : Abstract_Fiber auto event = move(queue->events.front()); queue->events.pop_front(); - if(ROCKET_UNEXPECT(event.type == easy_socket_close)) { + if(ROCKET_UNEXPECT(event.type == easy_http_close)) { // This will be the last event on this session. queue = nullptr; table->client_map.erase(client_iter); @@ -94,7 +95,7 @@ struct Final_Fiber final : Abstract_Fiber lock.unlock(); try { - if(event.type == easy_socket_pong) { + if(event.status != 0) { // Send a bad request response. HTTP_Response_Headers resp; resp.status = event.status; @@ -174,12 +175,21 @@ struct Final_HTTPS_Server_Session final : HTTPS_Server_Session } } + virtual + void + do_on_ssl_connected() override + { + Client_Table::Event_Queue::Event event; + event.type = easy_http_open; + this->do_push_event_common(move(event)); + } + virtual void do_on_https_request_finish(HTTP_Request_Headers&& req, linear_buffer&& data, bool close_now) override { Client_Table::Event_Queue::Event event; - event.type = easy_socket_msg_bin; + event.type = easy_http_message; event.req = move(req); event.data = move(data); event.close_now = close_now; @@ -191,7 +201,6 @@ struct Final_HTTPS_Server_Session final : HTTPS_Server_Session do_on_https_request_error(uint32_t status) override { Client_Table::Event_Queue::Event event; - event.type = easy_socket_pong; event.status = status; event.close_now = true; this->do_push_event_common(move(event)); @@ -206,7 +215,7 @@ struct Final_HTTPS_Server_Session final : HTTPS_Server_Session const char* err_str = ::strerror_r(err_code, sbuf, sizeof(sbuf)); Client_Table::Event_Queue::Event event; - event.type = easy_socket_close; + event.type = easy_http_close; event.data.puts(err_str); this->do_push_event_common(move(event)); } diff --git a/poseidon/easy/easy_https_server.hpp b/poseidon/easy/easy_https_server.hpp index 1dd147c4..43d87e30 100644 --- a/poseidon/easy/easy_https_server.hpp +++ b/poseidon/easy/easy_https_server.hpp @@ -16,7 +16,7 @@ class Easy_HTTPS_Server thunk< shptrR, // server data socket Abstract_Fiber&, // fiber for current callback - Easy_Socket_Event, // event type; see comments above constructor + Easy_HTTP_Event, // event type; see comments above constructor HTTP_Request_Headers&&, // request method, URI, and headers linear_buffer&&>; // request payload body @@ -30,12 +30,13 @@ class Easy_HTTPS_Server public: // Constructs a server. The argument shall be an invocable object taking // `(shptrR session, Abstract_Fiber& fiber, - // Easy_Socket_Event event, HTTP_Request_Headers&& req, linear_buffer&& + // Easy_HTTP_Event event, HTTP_Request_Headers&& req, linear_buffer&& // data)`, where `session` is a pointer to a client session object, and if // `event` is - // 1) `easy_socket_msg_bin`, then `req` and `data` are the headers and body + // 1) `easy_http_open`, then `data` is empty; or + // 2) `easy_http_message`, then `req` and `data` are the headers and body // of a request message, respectively; or - // 2) `easy_socket_close`, then `resp` is empty and `data` is the error + // 3) `easy_http_close`, then `resp` is empty and `data` is the error // description. // The server object owns all client session objects. As a recommendation, // applications should store only `wkptr`s to client sessions, and call diff --git a/poseidon/easy/easy_ssl_client.cpp b/poseidon/easy/easy_ssl_client.cpp index 66c3ef69..e258dcce 100644 --- a/poseidon/easy/easy_ssl_client.cpp +++ b/poseidon/easy/easy_ssl_client.cpp @@ -3,6 +3,7 @@ #include "../precompiled.ipp" #include "easy_ssl_client.hpp" +#include "enums.hpp" #include "../static/network_driver.hpp" #include "../fiber/abstract_fiber.hpp" #include "../static/fiber_scheduler.hpp" @@ -25,7 +26,7 @@ struct Event_Queue // shared fields between threads struct Event { - Easy_Socket_Event type; + Easy_Stream_Event type; linear_buffer data; int code = 0; }; @@ -78,11 +79,11 @@ struct Final_Fiber final : Abstract_Fiber lock.unlock(); try { - // `easy_socket_stream` is really special. We append new data to + // `easy_stream_data` is really special. We append new data to // `data_stream` which is passed to the callback instead of // `event.data`. `data_stream` may be consumed partially by user code, // and shall be preserved across callbacks. - if(event.type == easy_socket_stream) + if(event.type == easy_stream_data) this->m_thunk(socket, *this, event.type, splice_buffers(queue->data_stream, move(event.data)), event.code); else this->m_thunk(socket, *this, event.type, event.data, event.code); @@ -147,7 +148,7 @@ struct Final_SSL_Socket final : SSL_Socket do_on_ssl_connected() override { Event_Queue::Event event; - event.type = easy_socket_open; + event.type = easy_stream_open; this->do_push_event_common(move(event)); } @@ -156,7 +157,7 @@ struct Final_SSL_Socket final : SSL_Socket do_on_ssl_stream(linear_buffer& data, bool eof) override { Event_Queue::Event event; - event.type = easy_socket_stream; + event.type = easy_stream_data; event.data.swap(data); event.code = eof; this->do_push_event_common(move(event)); @@ -171,7 +172,7 @@ struct Final_SSL_Socket final : SSL_Socket const char* err_str = ::strerror_r(err_code, sbuf, sizeof(sbuf)); Event_Queue::Event event; - event.type = easy_socket_close; + event.type = easy_stream_close; event.data.puts(err_str); event.code = err_code; this->do_push_event_common(move(event)); diff --git a/poseidon/easy/easy_ssl_client.hpp b/poseidon/easy/easy_ssl_client.hpp index 660032a4..08daaf67 100644 --- a/poseidon/easy/easy_ssl_client.hpp +++ b/poseidon/easy/easy_ssl_client.hpp @@ -16,7 +16,7 @@ class Easy_SSL_Client thunk< shptrR, // client data socket Abstract_Fiber&, // fiber for current callback - Easy_Socket_Event, // event type; see comments above constructor + Easy_Stream_Event, // event type; see comments above constructor linear_buffer&, // accumulative data that have been received int>; // event code; see comments above constructor @@ -30,16 +30,16 @@ class Easy_SSL_Client public: // Constructs a client. The argument shall be an invocable object taking - // `(shptrR socket, Abstract_Fiber& fiber, Easy_Socket_Event + // `(shptrR socket, Abstract_Fiber& fiber, Easy_Stream_Event // event, linear_buffer& data, int code)`, where `socket` is a pointer to // a client socket object, and if `event` is - // 1) `easy_socket_open`, then `data` is empty; or - // 2) `easy_socket_stream`, then `data` contains all data that have been + // 1) `easy_stream_open`, then `data` is empty; or + // 2) `easy_stream_datahen `data` contains all data that have been // received and have not been removed so far (this callback shall // `.discard()` processed data from `data`, otherwise they will remain // there for the next call); `code` is non-zero if the remote peer has // closed the connection; or - // 3) `easy_socket_closed`, then `data` is the error description and `code` + // 3) `easy_stream_closed`, then `data` is the error description and `code` // is the system error number. // This client object stores a copy of the callback, which is invoked // accordingly in the main thread. The callback object is never copied, diff --git a/poseidon/easy/easy_ssl_server.cpp b/poseidon/easy/easy_ssl_server.cpp index 92dc1a2b..a5ab9fd4 100644 --- a/poseidon/easy/easy_ssl_server.cpp +++ b/poseidon/easy/easy_ssl_server.cpp @@ -3,6 +3,7 @@ #include "../precompiled.ipp" #include "easy_ssl_server.hpp" +#include "enums.hpp" #include "../socket/listen_socket.hpp" #include "../static/network_driver.hpp" #include "../fiber/abstract_fiber.hpp" @@ -26,7 +27,7 @@ struct Client_Table // shared fields between threads struct Event { - Easy_Socket_Event type; + Easy_Stream_Event type; linear_buffer data; int code = 0; }; @@ -87,7 +88,7 @@ struct Final_Fiber final : Abstract_Fiber auto event = move(queue->events.front()); queue->events.pop_front(); - if(ROCKET_UNEXPECT(event.type == easy_socket_close)) { + if(ROCKET_UNEXPECT(event.type == easy_stream_close)) { // This will be the last event on this socket. queue = nullptr; table->client_map.erase(client_iter); @@ -96,11 +97,11 @@ struct Final_Fiber final : Abstract_Fiber lock.unlock(); try { - // `easy_socket_stream` is really special. We append new data to + // `easy_stream_data` is really special. We append new data to // `data_stream` which is passed to the callback instead of // `event.data`. `data_stream` may be consumed partially by user code, // and shall be preserved across callbacks. - if(event.type == easy_socket_stream) + if(event.type == easy_stream_data) this->m_thunk(socket, *this, event.type, splice_buffers(queue->data_stream, move(event.data)), event.code); else this->m_thunk(socket, *this, event.type, event.data, event.code); @@ -171,7 +172,7 @@ struct Final_SSL_Socket final : SSL_Socket do_on_ssl_connected() override { Client_Table::Event_Queue::Event event; - event.type = easy_socket_open; + event.type = easy_stream_open; this->do_push_event_common(move(event)); } @@ -180,7 +181,7 @@ struct Final_SSL_Socket final : SSL_Socket do_on_ssl_stream(linear_buffer& data, bool eof) override { Client_Table::Event_Queue::Event event; - event.type = easy_socket_stream; + event.type = easy_stream_data; event.data.swap(data); event.code = eof; this->do_push_event_common(move(event)); @@ -195,7 +196,7 @@ struct Final_SSL_Socket final : SSL_Socket const char* err_str = ::strerror_r(err_code, sbuf, sizeof(sbuf)); Client_Table::Event_Queue::Event event; - event.type = easy_socket_close; + event.type = easy_stream_close; event.data.puts(err_str); event.code = err_code; this->do_push_event_common(move(event)); diff --git a/poseidon/easy/easy_ssl_server.hpp b/poseidon/easy/easy_ssl_server.hpp index ad5922e8..7e88d5e0 100644 --- a/poseidon/easy/easy_ssl_server.hpp +++ b/poseidon/easy/easy_ssl_server.hpp @@ -16,7 +16,7 @@ class Easy_SSL_Server thunk< shptrR, // server data socket Abstract_Fiber&, // fiber for current callback - Easy_Socket_Event, // event type; see comments above constructor + Easy_Stream_Event, // event type; see comments above constructor linear_buffer&, // accumulative data that have been received int>; // event code; see comments above constructor @@ -29,16 +29,16 @@ class Easy_SSL_Server public: // Constructs a server. The argument shall be an invocable object taking - // `(shptrR socket, Abstract_Fiber& fiber, Easy_Socket_Event + // `(shptrR socket, Abstract_Fiber& fiber, Easy_Stream_Event // event, linear_buffer& data, int code)`, where `socket` is a pointer to // a client socket object, and if `event` is - // 1) `easy_socket_open`, then `data` is empty; or - // 2) `easy_socket_stream`, then `data` contains all data that have been + // 1) `easy_stream_open`, then `data` is empty; or + // 2) `easy_stream_data`, then `data` contains all data that have been // received and have not been removed so far (this callback shall // `.discard()` processed data from `data`, otherwise they will remain // there for the next call); `code` is non-zero if the remote peer has // closed the connection; or - // 3) `easy_socket_closed`, then `data` is the error description and `code` + // 3) `easy_stream_closed`, then `data` is the error description and `code` // is the system error number. // The server object owns all client socket objects. As a recommendation, // applications should store only `wkptr`s to client sockets, and call diff --git a/poseidon/easy/easy_tcp_client.cpp b/poseidon/easy/easy_tcp_client.cpp index 2d38f97b..58c40394 100644 --- a/poseidon/easy/easy_tcp_client.cpp +++ b/poseidon/easy/easy_tcp_client.cpp @@ -3,6 +3,7 @@ #include "../precompiled.ipp" #include "easy_tcp_client.hpp" +#include "enums.hpp" #include "../static/network_driver.hpp" #include "../fiber/abstract_fiber.hpp" #include "../static/fiber_scheduler.hpp" @@ -25,7 +26,7 @@ struct Event_Queue // shared fields between threads struct Event { - Easy_Socket_Event type; + Easy_Stream_Event type; linear_buffer data; int code = 0; }; @@ -78,11 +79,11 @@ struct Final_Fiber final : Abstract_Fiber lock.unlock(); try { - // `easy_socket_stream` is really special. We append new data to + // `easy_stream_data` is really special. We append new data to // `data_stream` which is passed to the callback instead of // `event.data`. `data_stream` may be consumed partially by user code, // and shall be preserved across callbacks. - if(event.type == easy_socket_stream) + if(event.type == easy_stream_data) this->m_thunk(socket, *this, event.type, splice_buffers(queue->data_stream, move(event.data)), event.code); else this->m_thunk(socket, *this, event.type, event.data, event.code); @@ -147,7 +148,7 @@ struct Final_TCP_Socket final : TCP_Socket do_on_tcp_connected() override { Event_Queue::Event event; - event.type = easy_socket_open; + event.type = easy_stream_open; this->do_push_event_common(move(event)); } @@ -156,7 +157,7 @@ struct Final_TCP_Socket final : TCP_Socket do_on_tcp_stream(linear_buffer& data, bool eof) override { Event_Queue::Event event; - event.type = easy_socket_stream; + event.type = easy_stream_data; event.data.swap(data); event.code = eof; this->do_push_event_common(move(event)); @@ -171,7 +172,7 @@ struct Final_TCP_Socket final : TCP_Socket const char* err_str = ::strerror_r(err_code, sbuf, sizeof(sbuf)); Event_Queue::Event event; - event.type = easy_socket_close; + event.type = easy_stream_close; event.data.puts(err_str); event.code = err_code; this->do_push_event_common(move(event)); diff --git a/poseidon/easy/easy_tcp_client.hpp b/poseidon/easy/easy_tcp_client.hpp index 569eddc3..7c159a65 100644 --- a/poseidon/easy/easy_tcp_client.hpp +++ b/poseidon/easy/easy_tcp_client.hpp @@ -16,7 +16,7 @@ class Easy_TCP_Client thunk< shptrR, // client data socket Abstract_Fiber&, // fiber for current callback - Easy_Socket_Event, // event type; see comments above constructor + Easy_Stream_Event, // event type; see comments above constructor linear_buffer&, // accumulative data that have been received int>; // event code; see comments above constructor @@ -30,16 +30,16 @@ class Easy_TCP_Client public: // Constructs a client. The argument shall be an invocable object taking - // `(shptrR socket, Abstract_Fiber& fiber, Easy_Socket_Event + // `(shptrR socket, Abstract_Fiber& fiber, Easy_Stream_Event // event, linear_buffer& data, int code)`, where `socket` is a pointer to // a client socket object, and if `event` is - // 1) `easy_socket_open`, then `data` is empty; or - // 2) `easy_socket_stream`, then `data` contains all data that have been + // 1) `easy_stream_open`, then `data` is empty; or + // 2) `easy_stream_data`, then `data` contains all data that have been // received and have not been removed so far (this callback shall // `.discard()` processed data from `data`, otherwise they will remain // there for the next call); `code` is non-zero if the remote peer has // closed the connection; or - // 3) `easy_socket_closed`, then `data` is the error description and `code` + // 3) `easy_stream_closed`, then `data` is the error description and `code` // is the system error number. // This client object stores a copy of the callback, which is invoked // accordingly in the main thread. The callback object is never copied, diff --git a/poseidon/easy/easy_tcp_server.cpp b/poseidon/easy/easy_tcp_server.cpp index 5388e71c..0fbb0252 100644 --- a/poseidon/easy/easy_tcp_server.cpp +++ b/poseidon/easy/easy_tcp_server.cpp @@ -3,6 +3,7 @@ #include "../precompiled.ipp" #include "easy_tcp_server.hpp" +#include "enums.hpp" #include "../socket/listen_socket.hpp" #include "../static/network_driver.hpp" #include "../fiber/abstract_fiber.hpp" @@ -26,7 +27,7 @@ struct Client_Table // shared fields between threads struct Event { - Easy_Socket_Event type; + Easy_Stream_Event type; linear_buffer data; int code = 0; }; @@ -87,7 +88,7 @@ struct Final_Fiber final : Abstract_Fiber auto event = move(queue->events.front()); queue->events.pop_front(); - if(ROCKET_UNEXPECT(event.type == easy_socket_close)) { + if(ROCKET_UNEXPECT(event.type == easy_stream_close)) { // This will be the last event on this socket. queue = nullptr; table->client_map.erase(client_iter); @@ -96,11 +97,11 @@ struct Final_Fiber final : Abstract_Fiber lock.unlock(); try { - // `easy_socket_stream` is really special. We append new data to + // `easy_stream_data` is really special. We append new data to // `data_stream` which is passed to the callback instead of // `event.data`. `data_stream` may be consumed partially by user code, // and shall be preserved across callbacks. - if(event.type == easy_socket_stream) + if(event.type == easy_stream_data) this->m_thunk(socket, *this, event.type, splice_buffers(queue->data_stream, move(event.data)), event.code); else this->m_thunk(socket, *this, event.type, event.data, event.code); @@ -171,7 +172,7 @@ struct Final_TCP_Socket final : TCP_Socket do_on_tcp_connected() override { Client_Table::Event_Queue::Event event; - event.type = easy_socket_open; + event.type = easy_stream_open; this->do_push_event_common(move(event)); } @@ -180,7 +181,7 @@ struct Final_TCP_Socket final : TCP_Socket do_on_tcp_stream(linear_buffer& data, bool eof) override { Client_Table::Event_Queue::Event event; - event.type = easy_socket_stream; + event.type = easy_stream_data; event.data.swap(data); event.code = eof; this->do_push_event_common(move(event)); @@ -195,7 +196,7 @@ struct Final_TCP_Socket final : TCP_Socket const char* err_str = ::strerror_r(err_code, sbuf, sizeof(sbuf)); Client_Table::Event_Queue::Event event; - event.type = easy_socket_close; + event.type = easy_stream_close; event.data.puts(err_str); event.code = err_code; this->do_push_event_common(move(event)); diff --git a/poseidon/easy/easy_tcp_server.hpp b/poseidon/easy/easy_tcp_server.hpp index cda4305a..93fa47a6 100644 --- a/poseidon/easy/easy_tcp_server.hpp +++ b/poseidon/easy/easy_tcp_server.hpp @@ -16,7 +16,7 @@ class Easy_TCP_Server thunk< shptrR, // server data socket Abstract_Fiber&, // fiber for current callback - Easy_Socket_Event, // event type; see comments above constructor + Easy_Stream_Event, // event type; see comments above constructor linear_buffer&, // accumulative data that have been received int>; // event code; see comments above constructor @@ -29,16 +29,16 @@ class Easy_TCP_Server public: // Constructs a server. The argument shall be an invocable object taking - // `(shptrR socket, Abstract_Fiber& fiber, Easy_Socket_Event + // `(shptrR socket, Abstract_Fiber& fiber, Easy_Stream_Event // event, linear_buffer& data, int code)`, where `socket` is a pointer to // a client socket object, and if `event` is - // 1) `easy_socket_open`, then `data` is empty; or - // 2) `easy_socket_stream`, then `data` contains all data that have been + // 1) `easy_stream_open`, then `data` is empty; or + // 2) `easy_stream_data`, then `data` contains all data that have been // received and have not been removed so far (this callback shall // `.discard()` processed data from `data`, otherwise they will remain // there for the next call); `code` is non-zero if the remote peer has // closed the connection; or - // 3) `easy_socket_close`, then `data` is the error description and `code` + // 3) `easy_stream_close`, then `data` is the error description and `code` // is the system error number. // The server object owns all client socket objects. As a recommendation, // applications should store only `wkptr`s to client sockets, and call diff --git a/poseidon/easy/easy_ws_client.cpp b/poseidon/easy/easy_ws_client.cpp index 70f6a22b..35c3b7fe 100644 --- a/poseidon/easy/easy_ws_client.cpp +++ b/poseidon/easy/easy_ws_client.cpp @@ -3,10 +3,12 @@ #include "../precompiled.ipp" #include "easy_ws_client.hpp" +#include "enums.hpp" #include "../static/network_driver.hpp" #include "../fiber/abstract_fiber.hpp" #include "../static/fiber_scheduler.hpp" #include "../socket/async_connect.hpp" +#include "../socket/enums.hpp" #include "../static/async_task_executor.hpp" #include "../utils.hpp" namespace poseidon { @@ -25,7 +27,7 @@ struct Event_Queue // shared fields between threads struct Event { - Easy_Socket_Event type; + Easy_WS_Event type; linear_buffer data; }; @@ -139,7 +141,7 @@ struct Final_WS_Client_Session final : WS_Client_Session do_on_ws_connected(cow_string&& caddr) override { Event_Queue::Event event; - event.type = easy_socket_open; + event.type = easy_ws_open; event.data.putn(caddr.data(), caddr.size()); this->do_push_event_common(move(event)); } @@ -151,11 +153,11 @@ struct Final_WS_Client_Session final : WS_Client_Session Event_Queue::Event event; if(opcode == websocket_text) - event.type = easy_socket_msg_text; - else if(opcode == websocket_bin) - event.type = easy_socket_msg_bin; + event.type = easy_ws_text; + else if(opcode == websocket_binary) + event.type = easy_ws_binary; else if(opcode == websocket_pong) - event.type = easy_socket_pong; + event.type = easy_ws_pong; else return; @@ -168,7 +170,7 @@ struct Final_WS_Client_Session final : WS_Client_Session do_on_ws_close(uint16_t status, chars_view reason) override { Event_Queue::Event event; - event.type = easy_socket_close; + event.type = easy_ws_close; tinyfmt_ln fmt; fmt << status << ": " << reason; @@ -250,7 +252,7 @@ remote_address() const noexcept bool Easy_WS_Client:: -ws_send(WebSocket_OpCode opcode, chars_view data) +ws_send(Easy_WS_Event opcode, chars_view data) { if(!this->m_session) return false; diff --git a/poseidon/easy/easy_ws_client.hpp b/poseidon/easy/easy_ws_client.hpp index fdd5a0bb..bc86b619 100644 --- a/poseidon/easy/easy_ws_client.hpp +++ b/poseidon/easy/easy_ws_client.hpp @@ -16,7 +16,7 @@ class Easy_WS_Client thunk< shptrR, // client data socket Abstract_Fiber&, // fiber for current callback - Easy_Socket_Event, // event type; see comments above constructor + Easy_WS_Event, // event type; see comments above constructor linear_buffer&&>; // message payload private: @@ -30,16 +30,16 @@ class Easy_WS_Client public: // Constructs a client. The argument shall be an invocable object taking // `(shptrR session, Abstract_Fiber& fiber, - // Easy_Socket_Event event, linear_buffer&& data)`, where `session` is a + // Easy_WS_Event event, linear_buffer&& data)`, where `session` is a // pointer to a client session object, and if `event` is - // 1) `easy_socket_open`, then `data` is the request URI; or - // 2) `easy_socket_msg_text`, then `data` is a complete text message that - // has been received; or - // 3) `easy_socket_msg_bin`, then `data` is a complete binary message that - // has been received; or - // 4) `easy_socket_pong`, then `data` is a PONG notification that has been + // 1) `easy_ws_open`, then `data` is the request URI; or + // 2) `easy_ws_msg_text`, then `data` is a complete text message that has + // been received; or + // 3) `easy_ws_msg_bin`, then `data` is a complete binary message that has + // been received; or + // 4) `easy_ws_pong`, then `data` is a PONG notification that has been // received, usually a copy of a previous PING notification; or - // 5) `easy_socket_close`, then `data` is a string about the reason, such + // 5) `easy_ws_close`, then `data` is a string about the reason, such // as `"1002: invalid opcode"`. // This client object stores a copy of the callback, which is invoked // accordingly in the main thread. The callback object is never copied, and @@ -97,7 +97,7 @@ class Easy_WS_Client // If this function throws an exception, there is no effect. // This function is thread-safe. bool - ws_send(WebSocket_OpCode opcode, chars_view data); + ws_send(Easy_WS_Event opcode, chars_view data); // Sends a CLOSE frame with an optional error message, then shuts down the // connection. The reason string will be truncated to 123 bytes if it's too diff --git a/poseidon/easy/easy_ws_server.cpp b/poseidon/easy/easy_ws_server.cpp index 423c0243..fc83c016 100644 --- a/poseidon/easy/easy_ws_server.cpp +++ b/poseidon/easy/easy_ws_server.cpp @@ -3,7 +3,9 @@ #include "../precompiled.ipp" #include "easy_ws_server.hpp" +#include "enums.hpp" #include "../socket/listen_socket.hpp" +#include "../socket/enums.hpp" #include "../static/network_driver.hpp" #include "../fiber/abstract_fiber.hpp" #include "../static/fiber_scheduler.hpp" @@ -22,7 +24,7 @@ struct Client_Table // shared fields between threads struct Event { - Easy_Socket_Event type; + Easy_WS_Event type; linear_buffer data; }; @@ -82,7 +84,7 @@ struct Final_Fiber final : Abstract_Fiber auto event = move(queue->events.front()); queue->events.pop_front(); - if(ROCKET_UNEXPECT(event.type == easy_socket_close)) { + if(ROCKET_UNEXPECT(event.type == easy_ws_close)) { // This will be the last event on this session. queue = nullptr; table->client_map.erase(client_iter); @@ -158,7 +160,7 @@ struct Final_WS_Server_Session final : WS_Server_Session do_on_ws_accepted(cow_string&& caddr) override { Client_Table::Event_Queue::Event event; - event.type = easy_socket_open; + event.type = easy_ws_open; event.data.putn(caddr.data(), caddr.size()); this->do_push_event_common(move(event)); } @@ -170,11 +172,11 @@ struct Final_WS_Server_Session final : WS_Server_Session Client_Table::Event_Queue::Event event; if(opcode == websocket_text) - event.type = easy_socket_msg_text; - else if(opcode == websocket_bin) - event.type = easy_socket_msg_bin; + event.type = easy_ws_text; + else if(opcode == websocket_binary) + event.type = easy_ws_binary; else if(opcode == websocket_pong) - event.type = easy_socket_pong; + event.type = easy_ws_pong; else return; @@ -187,7 +189,7 @@ struct Final_WS_Server_Session final : WS_Server_Session do_on_ws_close(uint16_t status, chars_view reason) override { Client_Table::Event_Queue::Event event; - event.type = easy_socket_close; + event.type = easy_ws_close; tinyfmt_ln fmt; fmt << status << ": " << reason; diff --git a/poseidon/easy/easy_ws_server.hpp b/poseidon/easy/easy_ws_server.hpp index 3690f605..639bdcaf 100644 --- a/poseidon/easy/easy_ws_server.hpp +++ b/poseidon/easy/easy_ws_server.hpp @@ -16,7 +16,7 @@ class Easy_WS_Server thunk< shptrR, // server data socket Abstract_Fiber&, // fiber for current callback - Easy_Socket_Event, // event type; see comments above constructor + Easy_WS_Event, // event type; see comments above constructor linear_buffer&&>; // message payload private: @@ -29,17 +29,17 @@ class Easy_WS_Server public: // Constructs a server. The argument shall be an invocable object taking // `(shptrR session, Abstract_Fiber& fiber, - // Easy_Socket_Event event, linear_buffer&& data)`, where `session` is a + // Easy_WS_Event event, linear_buffer&& data)`, where `session` is a // pointer to a client session object, and if `event` is - // 1) `easy_socket_open`, then `data` is the request URI; or - // 2) `easy_socket_msg_text`, then `data` is a complete text message that - // has been received; or - // 3) `easy_socket_msg_bin`, then `data` is a complete binary message that - // has been received; or - // 4) `easy_socket_pong`, then `data` is a PONG notification that has been + // 1) `easy_ws_open`, then `data` is the request URI; or + // 2) `easy_ws_msg_text`, then `data` is a complete text message that has + // been received; or + // 3) `easy_ws_msg_bin`, then `data` is a complete binary message that has + // been received; or + // 4) `easy_ws_pong`, then `data` is a PONG notification that has been // received, usually a copy of a previous PING notification; or - // 5) `easy_socket_close`, then `data` is a string about the reason, such - // as `"1002: invalid opcode"`. + // 5) `easy_ws_close`, then `data` is a string about the reason, such as + // `"1002: invalid opcode"`. // The server object owns all client socket objects. As a recommendation, // applications should store only `wkptr`s to client sockets, and call // `.lock()` as needed. This server object stores a copy of the callback, diff --git a/poseidon/easy/easy_wss_client.cpp b/poseidon/easy/easy_wss_client.cpp index 5a96bf29..ca041667 100644 --- a/poseidon/easy/easy_wss_client.cpp +++ b/poseidon/easy/easy_wss_client.cpp @@ -3,10 +3,12 @@ #include "../precompiled.ipp" #include "easy_wss_client.hpp" +#include "enums.hpp" #include "../static/network_driver.hpp" #include "../fiber/abstract_fiber.hpp" #include "../static/fiber_scheduler.hpp" #include "../socket/async_connect.hpp" +#include "../socket/enums.hpp" #include "../static/async_task_executor.hpp" #include "../utils.hpp" namespace poseidon { @@ -25,7 +27,7 @@ struct Event_Queue // shared fields between threads struct Event { - Easy_Socket_Event type; + Easy_WS_Event type; linear_buffer data; }; @@ -139,7 +141,7 @@ struct Final_WSS_Client_Session final : WSS_Client_Session do_on_wss_connected(cow_string&& caddr) override { Event_Queue::Event event; - event.type = easy_socket_open; + event.type = easy_ws_open; event.data.putn(caddr.data(), caddr.size()); this->do_push_event_common(move(event)); } @@ -151,11 +153,11 @@ struct Final_WSS_Client_Session final : WSS_Client_Session Event_Queue::Event event; if(opcode == websocket_text) - event.type = easy_socket_msg_text; - else if(opcode == websocket_bin) - event.type = easy_socket_msg_bin; + event.type = easy_ws_text; + else if(opcode == websocket_binary) + event.type = easy_ws_binary; else if(opcode == websocket_pong) - event.type = easy_socket_pong; + event.type = easy_ws_pong; else return; @@ -168,7 +170,7 @@ struct Final_WSS_Client_Session final : WSS_Client_Session do_on_wss_close(uint16_t status, chars_view reason) override { Event_Queue::Event event; - event.type = easy_socket_close; + event.type = easy_ws_close; tinyfmt_ln fmt; fmt << status << ": " << reason; @@ -250,7 +252,7 @@ remote_address() const noexcept bool Easy_WSS_Client:: -wss_send(WebSocket_OpCode opcode, chars_view data) +wss_send(Easy_WS_Event opcode, chars_view data) { if(!this->m_session) return false; diff --git a/poseidon/easy/easy_wss_client.hpp b/poseidon/easy/easy_wss_client.hpp index 9aca531f..ba2e3489 100644 --- a/poseidon/easy/easy_wss_client.hpp +++ b/poseidon/easy/easy_wss_client.hpp @@ -16,7 +16,7 @@ class Easy_WSS_Client thunk< shptrR, // client data socket Abstract_Fiber&, // fiber for current callback - Easy_Socket_Event, // event type; see comments above constructor + Easy_WS_Event, // event type; see comments above constructor linear_buffer&&>; // message payload private: @@ -30,16 +30,16 @@ class Easy_WSS_Client public: // Constructs a client. The argument shall be an invocable object taking // `(shptrR session, Abstract_Fiber& fiber, - // Easy_Socket_Event event, linear_buffer&& data)`, where `session` is a + // Easy_WS_Event event, linear_buffer&& data)`, where `session` is a // pointer to a client session object, and if `event` is - // 1) `easy_socket_open`, then `data` is the request URI; or - // 2) `easy_socket_msg_text`, then `data` is a complete text message that - // has been received; or - // 3) `easy_socket_msg_bin`, then `data` is a complete binary message that - // has been received; or - // 4) `easy_socket_pong`, then `data` is a PONG notification that has been + // 1) `easy_ws_open`, then `data` is the request URI; or + // 2) `easy_ws_msg_text`, then `data` is a complete text message that has + // been received; or + // 3) `easy_ws_msg_bin`, then `data` is a complete binary message that has + // been received; or + // 4) `easy_ws_pong`, then `data` is a PONG notification that has been // received, usually a copy of a previous PING notification; or - // 5) `easy_socket_close`, then `data` is a string about the reason, such + // 5) `easy_ws_close`, then `data` is a string about the reason, such // as `"1002: invalid opcode"`. // This client object stores a copy of the callback, which is invoked // accordingly in the main thread. The callback object is never copied, and @@ -97,7 +97,7 @@ class Easy_WSS_Client // If this function throws an exception, there is no effect. // This function is thread-safe. bool - wss_send(WebSocket_OpCode opcode, chars_view data); + wss_send(Easy_WS_Event opcode, chars_view data); // Sends a CLOSE frame with an optional error message, then shuts down the // connection. The reason string will be truncated to 123 bytes if it's too diff --git a/poseidon/easy/easy_wss_server.cpp b/poseidon/easy/easy_wss_server.cpp index 380e8fa5..1493951e 100644 --- a/poseidon/easy/easy_wss_server.cpp +++ b/poseidon/easy/easy_wss_server.cpp @@ -3,7 +3,9 @@ #include "../precompiled.ipp" #include "easy_wss_server.hpp" +#include "enums.hpp" #include "../socket/listen_socket.hpp" +#include "../socket/enums.hpp" #include "../static/network_driver.hpp" #include "../fiber/abstract_fiber.hpp" #include "../static/fiber_scheduler.hpp" @@ -22,7 +24,7 @@ struct Client_Table // shared fields between threads struct Event { - Easy_Socket_Event type; + Easy_WS_Event type; linear_buffer data; }; @@ -82,7 +84,7 @@ struct Final_Fiber final : Abstract_Fiber auto event = move(queue->events.front()); queue->events.pop_front(); - if(ROCKET_UNEXPECT(event.type == easy_socket_close)) { + if(ROCKET_UNEXPECT(event.type == easy_ws_close)) { // This will be the last event on this session. queue = nullptr; table->client_map.erase(client_iter); @@ -159,7 +161,7 @@ struct Final_WSS_Server_Session final : WSS_Server_Session do_on_wss_accepted(cow_string&& caddr) override { Client_Table::Event_Queue::Event event; - event.type = easy_socket_open; + event.type = easy_ws_open; event.data.putn(caddr.data(), caddr.size()); this->do_push_event_common(move(event)); } @@ -171,11 +173,11 @@ struct Final_WSS_Server_Session final : WSS_Server_Session Client_Table::Event_Queue::Event event; if(opcode == websocket_text) - event.type = easy_socket_msg_text; - else if(opcode == websocket_bin) - event.type = easy_socket_msg_bin; + event.type = easy_ws_text; + else if(opcode == websocket_binary) + event.type = easy_ws_binary; else if(opcode == websocket_pong) - event.type = easy_socket_pong; + event.type = easy_ws_pong; else return; @@ -188,7 +190,7 @@ struct Final_WSS_Server_Session final : WSS_Server_Session do_on_wss_close(uint16_t status, chars_view reason) override { Client_Table::Event_Queue::Event event; - event.type = easy_socket_close; + event.type = easy_ws_close; tinyfmt_ln fmt; fmt << status << ": " << reason; diff --git a/poseidon/easy/easy_wss_server.hpp b/poseidon/easy/easy_wss_server.hpp index af343d92..85b0b04b 100644 --- a/poseidon/easy/easy_wss_server.hpp +++ b/poseidon/easy/easy_wss_server.hpp @@ -16,7 +16,7 @@ class Easy_WSS_Server thunk< shptrR, // server data socket Abstract_Fiber&, // fiber for current callback - Easy_Socket_Event, // event type; see comments above constructor + Easy_WS_Event, // event type; see comments above constructor linear_buffer&&>; // message payload private: @@ -29,17 +29,17 @@ class Easy_WSS_Server public: // Constructs a server. The argument shall be an invocable object taking // `(shptrR session, Abstract_Fiber& fiber, - // Easy_Socket_Event event, linear_buffer&& data)`, where `session` is a + // Easy_WS_Event event, linear_buffer&& data)`, where `session` is a // pointer to a client session object, and if `event` is - // 1) `easy_socket_open`, then `data` is the request URI; or - // 2) `easy_socket_msg_text`, then `data` is a complete text message that - // has been received; or - // 3) `easy_socket_msg_bin`, then `data` is a complete binary message that - // has been received; or - // 4) `easy_socket_pong`, then `data` is a PONG notification that has been + // 1) `easy_ws_open`, then `data` is the request URI; or + // 2) `easy_ws_msg_text`, then `data` is a complete text message that has + // been received; or + // 3) `easy_ws_msg_bin`, then `data` is a complete binary message that has + // been received; or + // 4) `easy_ws_pong`, then `data` is a PONG notification that has been // received, usually a copy of a previous PING notification; or - // 5) `easy_socket_close`, then `data` is a string about the reason, such - // as `"1002: invalid opcode"`. + // 5) `easy_ws_close`, then `data` is a string about the reason, such as + // `"1002: invalid opcode"`. // The server object owns all client socket objects. As a recommendation, // applications should store only `wkptr`s to client sockets, and call // `.lock()` as needed. This server object stores a copy of the callback, diff --git a/poseidon/easy/enums.hpp b/poseidon/easy/enums.hpp new file mode 100644 index 00000000..0551bfc4 --- /dev/null +++ b/poseidon/easy/enums.hpp @@ -0,0 +1,35 @@ +// This file is part of Poseidon. +// Copyleft 2022 - 2024, LH_Mouse. All wrongs reserved. + +#ifndef POSEIDON_EASY_ENUMS_ +#define POSEIDON_EASY_ENUMS_ + +#include "../fwd.hpp" +namespace poseidon { + +enum Easy_Stream_Event : uint8_t + { + easy_stream_open = 11, // connection established + easy_stream_data = 12, // data received + easy_stream_close = 13, // connection closed + }; + +enum Easy_HTTP_Event : uint8_t + { + easy_http_open = 21, // connection established + easy_http_message = 22, // message received + easy_http_close = 23, // connection closed + }; + +enum Easy_WS_Event : uint8_t + { + easy_ws_open = 1, // connection established + easy_ws_text = 2, // text message received + easy_ws_binary = 3, // binary message received + easy_ws_ping = easy_ws_open, + easy_ws_pong = 4, // pong notification received + easy_ws_close = 5, // closure notification closed + }; + +} // namespace poseidon +#endif diff --git a/poseidon/fwd.hpp b/poseidon/fwd.hpp index 3c16f912..b25ddaf8 100644 --- a/poseidon/fwd.hpp +++ b/poseidon/fwd.hpp @@ -174,6 +174,7 @@ template using vfptr = void (*)(T...); template using char_array = array; template using uchar_array = array; +// utilities struct cacheline_barrier { static constexpr size_t align = alignof(max_align_t); @@ -477,10 +478,25 @@ shptr::type> new_sh(ValueT&& value) { return ::std::make_shared::type>(forward(value)); } +// Constants +enum zlib_Format : uint8_t + { + zlib_deflate = 0, + zlib_raw = 1, + zlib_gzip = 2, + }; + +enum Async_State : uint8_t + { + async_pending = 0, + async_suspended = 1, + async_running = 2, + async_finished = 3, + }; + // Base types class UUID; using GUID = UUID; - class Config_File; class Abstract_Timer; class Abstract_Async_Task; @@ -494,6 +510,10 @@ class Read_File_Future; class Abstract_Fiber; // Socket types +enum IP_Address_Class : uint8_t; +enum Socket_State : uint8_t; +enum HTTP_Payload_Type : uint8_t; +enum WebSocket_OpCode : uint8_t; class Socket_Address; class Abstract_Socket; class Listen_Socket; @@ -526,6 +546,9 @@ class WebSocket_Deflator; // Easy types // Being 'easy' means all callbacks are invoked in fibers and can perform // async/await operations. These are suitable for agile development. +enum Easy_Stream_Event : uint8_t; // TCP, SSL +enum Easy_HTTP_Event : uint8_t; // HTTP, HTTPS +enum Easy_WS_Event : uint8_t; // WS, WSS class Easy_Deflator; class Easy_Inflator; class Easy_Timer; @@ -544,67 +567,6 @@ class Easy_WS_Client; class Easy_WSS_Server; class Easy_WSS_Client; -// Constants -enum zlib_Format : uint8_t - { - zlib_deflate = 0, - zlib_raw = 1, - zlib_gzip = 2, - }; - -enum Async_State : uint8_t - { - async_pending = 0, - async_suspended = 1, - async_running = 2, - async_finished = 3, - }; - -enum IP_Address_Class : uint8_t - { - ip_address_unspecified = 0, // all zeroes - ip_address_reserved = 1, - ip_address_public = 2, - ip_address_loopback = 3, - ip_address_private = 4, - ip_address_link_local = 5, - ip_address_multicast = 6, - ip_address_broadcast = 7, // IPv4 only - }; - -enum Socket_State : uint8_t - { - socket_pending = 0, - socket_established = 1, - socket_closing = 2, - socket_closed = 3, - }; - -enum HTTP_Payload_Type : uint8_t - { - http_payload_normal = 0, - http_payload_empty = 1, - http_payload_connect = 2, - }; - -enum WebSocket_OpCode : uint8_t - { - websocket_text = 1, - websocket_bin = 2, - websocket_ping = 9, - websocket_pong = 10, - }; - -enum Easy_Socket_Event : uint8_t - { - easy_socket_open = 0, // TCP WS - easy_socket_stream = 1, // TCP - easy_socket_close = 2, // TCP HTTP WS - easy_socket_msg_text = 3, // WS - easy_socket_msg_bin = 4, // HTTP WS - easy_socket_pong = 5, // WS - }; - // Singletons extern atomic_relaxed exit_signal; extern class Main_Config& main_config; diff --git a/poseidon/http/websocket_deflator.cpp b/poseidon/http/websocket_deflator.cpp index e0a6f416..93a0ceb9 100644 --- a/poseidon/http/websocket_deflator.cpp +++ b/poseidon/http/websocket_deflator.cpp @@ -4,6 +4,7 @@ #include "../precompiled.ipp" #include "websocket_deflator.hpp" #include "websocket_frame_parser.hpp" +#include "../base/enums.hpp" #include "../utils.hpp" namespace poseidon { diff --git a/poseidon/socket/abstract_socket.cpp b/poseidon/socket/abstract_socket.cpp index d3620d02..e0bdabc0 100644 --- a/poseidon/socket/abstract_socket.cpp +++ b/poseidon/socket/abstract_socket.cpp @@ -3,6 +3,7 @@ #include "../precompiled.ipp" #include "abstract_socket.hpp" +#include "enums.hpp" #include "../utils.hpp" #include #include diff --git a/poseidon/socket/enums.hpp b/poseidon/socket/enums.hpp new file mode 100644 index 00000000..dc879740 --- /dev/null +++ b/poseidon/socket/enums.hpp @@ -0,0 +1,46 @@ +// This file is part of Poseidon. +// Copyleft 2022 - 2024, LH_Mouse. All wrongs reserved. + +#ifndef POSEIDON_SOCKET_ENUMS_ +#define POSEIDON_SOCKET_ENUMS_ + +#include "../fwd.hpp" +namespace poseidon { + +enum IP_Address_Class : uint8_t + { + ip_address_unspecified = 0, // all zeroes + ip_address_reserved = 1, + ip_address_public = 2, + ip_address_loopback = 3, + ip_address_private = 4, + ip_address_link_local = 5, + ip_address_multicast = 6, + ip_address_broadcast = 7, // IPv4 only + }; + +enum Socket_State : uint8_t + { + socket_pending = 0, + socket_established = 1, + socket_closing = 2, + socket_closed = 3, + }; + +enum HTTP_Payload_Type : uint8_t + { + http_payload_normal = 0, + http_payload_empty = 1, + http_payload_connect = 2, + }; + +enum WebSocket_OpCode : uint8_t + { + websocket_text = 1, + websocket_binary = 2, + websocket_ping = 9, + websocket_pong = 10, + }; + +} // namespace poseidon +#endif diff --git a/poseidon/socket/http_client_session.cpp b/poseidon/socket/http_client_session.cpp index 02251be3..ce750d13 100644 --- a/poseidon/socket/http_client_session.cpp +++ b/poseidon/socket/http_client_session.cpp @@ -3,6 +3,7 @@ #include "../precompiled.ipp" #include "http_client_session.hpp" +#include "enums.hpp" #include "../base/config_file.hpp" #include "../static/main_config.hpp" #include "../utils.hpp" diff --git a/poseidon/socket/http_server_session.cpp b/poseidon/socket/http_server_session.cpp index f81fa1a4..e63d75c1 100644 --- a/poseidon/socket/http_server_session.cpp +++ b/poseidon/socket/http_server_session.cpp @@ -3,6 +3,7 @@ #include "../precompiled.ipp" #include "http_server_session.hpp" +#include "enums.hpp" #include "../http/http_header_parser.hpp" #include "../base/config_file.hpp" #include "../static/main_config.hpp" diff --git a/poseidon/socket/https_client_session.cpp b/poseidon/socket/https_client_session.cpp index 5ac9027f..141ed5b6 100644 --- a/poseidon/socket/https_client_session.cpp +++ b/poseidon/socket/https_client_session.cpp @@ -3,6 +3,7 @@ #include "../precompiled.ipp" #include "https_client_session.hpp" +#include "enums.hpp" #include "../base/config_file.hpp" #include "../static/main_config.hpp" #include "../utils.hpp" diff --git a/poseidon/socket/https_server_session.cpp b/poseidon/socket/https_server_session.cpp index 7b61e256..99ac2fd5 100644 --- a/poseidon/socket/https_server_session.cpp +++ b/poseidon/socket/https_server_session.cpp @@ -3,6 +3,7 @@ #include "../precompiled.ipp" #include "https_server_session.hpp" +#include "enums.hpp" #include "../http/http_header_parser.hpp" #include "../base/config_file.hpp" #include "../static/main_config.hpp" diff --git a/poseidon/socket/socket_address.cpp b/poseidon/socket/socket_address.cpp index 4883ddee..9e97f9a5 100644 --- a/poseidon/socket/socket_address.cpp +++ b/poseidon/socket/socket_address.cpp @@ -3,6 +3,7 @@ #include "../precompiled.ipp" #include "socket_address.hpp" +#include "enums.hpp" #include "../utils.hpp" #include namespace poseidon { diff --git a/poseidon/socket/ssl_socket.cpp b/poseidon/socket/ssl_socket.cpp index 61582f2d..d0cae07a 100644 --- a/poseidon/socket/ssl_socket.cpp +++ b/poseidon/socket/ssl_socket.cpp @@ -3,6 +3,7 @@ #include "../precompiled.ipp" #include "ssl_socket.hpp" +#include "enums.hpp" #include "../static/network_driver.hpp" #include "../utils.hpp" #include diff --git a/poseidon/socket/tcp_socket.cpp b/poseidon/socket/tcp_socket.cpp index 57da8fb3..eeb23e44 100644 --- a/poseidon/socket/tcp_socket.cpp +++ b/poseidon/socket/tcp_socket.cpp @@ -3,6 +3,7 @@ #include "../precompiled.ipp" #include "tcp_socket.hpp" +#include "enums.hpp" #include "../utils.hpp" #include #include diff --git a/poseidon/socket/udp_socket.cpp b/poseidon/socket/udp_socket.cpp index 1b917eb8..8a37bc91 100644 --- a/poseidon/socket/udp_socket.cpp +++ b/poseidon/socket/udp_socket.cpp @@ -3,6 +3,7 @@ #include "../precompiled.ipp" #include "udp_socket.hpp" +#include "enums.hpp" #include "../utils.hpp" #include #include diff --git a/poseidon/socket/ws_client_session.cpp b/poseidon/socket/ws_client_session.cpp index 525c02b6..54864056 100644 --- a/poseidon/socket/ws_client_session.cpp +++ b/poseidon/socket/ws_client_session.cpp @@ -3,6 +3,8 @@ #include "../precompiled.ipp" #include "ws_client_session.hpp" +#include "enums.hpp" +#include "../easy/enums.hpp" #include "../http/websocket_deflator.hpp" #include "../base/config_file.hpp" #include "../static/main_config.hpp" @@ -167,7 +169,7 @@ do_on_http_upgraded_stream(linear_buffer& data, bool eof) // If this is a data frame or continuation, its payload is part of a // (potentially fragmented) data message, so combine it. auto opcode = static_cast(this->m_parser.message_opcode()); - ROCKET_ASSERT(is_any_of(opcode, { websocket_text, websocket_bin })); + ROCKET_ASSERT(is_any_of(opcode, { websocket_text, websocket_binary })); this->do_on_ws_message_data_stream(opcode, splice_buffers(this->m_msg, move(payload))); } @@ -182,7 +184,7 @@ do_on_http_upgraded_stream(linear_buffer& data, bool eof) case 1: // TEXT case 2: { // BINARY auto opcode = static_cast(this->m_parser.message_opcode()); - ROCKET_ASSERT(is_any_of(opcode, { websocket_text, websocket_bin })); + ROCKET_ASSERT(is_any_of(opcode, { websocket_text, websocket_binary })); this->do_on_ws_message_finish(opcode, move(this->m_msg)); break; } @@ -254,7 +256,7 @@ do_on_ws_message_data_stream(WebSocket_OpCode /*opcode*/, linear_buffer& data) if(data.size() > (uint64_t) max_websocket_message_length) POSEIDON_THROW(( "WebSocket text data message too large: `$3` > `$4`", - "[WebSocket server session `$1` (class `$2`)]"), + "[WebSocket client session `$1` (class `$2`)]"), this, typeid(*this), data.size(), max_websocket_message_length); } @@ -296,7 +298,7 @@ ws_send(WebSocket_OpCode opcode, chars_view data) if(!this->do_has_upgraded()) POSEIDON_THROW(( "WebSocket handshake not complete yet", - "[WebSocket server session `$1` (class `$2`)]"), + "[WebSocket client session `$1` (class `$2`)]"), this, typeid(*this)); switch(opcode) { @@ -343,7 +345,7 @@ ws_send(WebSocket_OpCode opcode, chars_view data) if(data.n > 125) POSEIDON_THROW(( "Control frame too large: `$3` > `125`", - "[WebSocket server session `$1` (class `$2`)]"), + "[WebSocket client session `$1` (class `$2`)]"), this, typeid(*this), data.n); // Control messages can't be compressed, so send it as is. @@ -353,7 +355,7 @@ ws_send(WebSocket_OpCode opcode, chars_view data) default: POSEIDON_THROW(( "WebSocket opcode `$3` not supported", - "[WebSocket server session `$1` (class `$2`)]"), + "[WebSocket client session `$1` (class `$2`)]"), this, typeid(*this), opcode); } } @@ -390,4 +392,32 @@ ws_shut_down(uint16_t status, chars_view reason) noexcept return succ; } +bool +WS_Client_Session:: +ws_send(Easy_WS_Event opcode, chars_view data) + { + switch(opcode) { + case easy_ws_open: + return this->ws_send(websocket_ping, data); + + case easy_ws_text: + return this->ws_send(websocket_text, data); + + case easy_ws_binary: + return this->ws_send(websocket_binary, data); + + case easy_ws_pong: + return this->ws_send(websocket_pong, data); + + case easy_ws_close: + return this->ws_shut_down(1000, data); + + default: + POSEIDON_THROW(( + "Easy WebSocket event code `$3` not supported", + "[WebSocket client session `$1` (class `$2`)]"), + this, typeid(*this), opcode); + } + } + } // namespace poseidon diff --git a/poseidon/socket/ws_client_session.hpp b/poseidon/socket/ws_client_session.hpp index 0490c94b..24262e27 100644 --- a/poseidon/socket/ws_client_session.hpp +++ b/poseidon/socket/ws_client_session.hpp @@ -62,7 +62,7 @@ class WS_Client_Session // This callback is invoked by the network thread for each fragment of a data // message. `opcode` indicates the type of the message, which can be either - // `websocket_text` or `websocket_bin`; control frames are never notified via + // `websocket_text` or `websocket_binary`; control frames are never notified via // this callback. As with `TCP_Connection::do_on_tcp_stream()`, the argument // buffer contains all data that have been accumulated so far and callees are // supposed to remove bytes that have been processed. @@ -75,7 +75,7 @@ class WS_Client_Session do_on_ws_message_data_stream(WebSocket_OpCode opcode, linear_buffer& data); // This callback is invoked by the network thread at the end of a data message - // or a control frame. `opcode` may be `websocket_text`, `websocket_bin`, + // or a control frame. `opcode` may be `websocket_text`, `websocket_binary`, // `websocket_ping` or `websocket_pong`. virtual void @@ -112,6 +112,10 @@ class WS_Client_Session // This function is thread-safe. bool ws_shut_down(uint16_t status = 1000, chars_view reason = "") noexcept; + + // Provides a helper function for the sake of Easy classes. + bool + ws_send(Easy_WS_Event opcode, chars_view data); }; } // namespace poseidon diff --git a/poseidon/socket/ws_server_session.cpp b/poseidon/socket/ws_server_session.cpp index 24cd9778..3a9e109b 100644 --- a/poseidon/socket/ws_server_session.cpp +++ b/poseidon/socket/ws_server_session.cpp @@ -3,6 +3,8 @@ #include "../precompiled.ipp" #include "ws_server_session.hpp" +#include "enums.hpp" +#include "../easy/enums.hpp" #include "../http/websocket_deflator.hpp" #include "../base/config_file.hpp" #include "../static/main_config.hpp" @@ -206,7 +208,7 @@ do_on_http_upgraded_stream(linear_buffer& data, bool eof) // If this is a data frame or continuation, its payload is part of a // (potentially fragmented) data message, so combine it. auto opcode = static_cast(this->m_parser.message_opcode()); - ROCKET_ASSERT(is_any_of(opcode, { websocket_text, websocket_bin })); + ROCKET_ASSERT(is_any_of(opcode, { websocket_text, websocket_binary })); this->do_on_ws_message_data_stream(opcode, splice_buffers(this->m_msg, move(payload))); } @@ -221,7 +223,7 @@ do_on_http_upgraded_stream(linear_buffer& data, bool eof) case 1: // TEXT case 2: { // BINARY auto opcode = static_cast(this->m_parser.message_opcode()); - ROCKET_ASSERT(is_any_of(opcode, { websocket_text, websocket_bin })); + ROCKET_ASSERT(is_any_of(opcode, { websocket_text, websocket_binary })); this->do_on_ws_message_finish(opcode, move(this->m_msg)); break; } @@ -424,4 +426,32 @@ ws_shut_down(uint16_t status, chars_view reason) noexcept return succ; } +bool +WS_Server_Session:: +ws_send(Easy_WS_Event opcode, chars_view data) + { + switch(opcode) { + case easy_ws_open: + return this->ws_send(websocket_ping, data); + + case easy_ws_text: + return this->ws_send(websocket_text, data); + + case easy_ws_binary: + return this->ws_send(websocket_binary, data); + + case easy_ws_pong: + return this->ws_send(websocket_pong, data); + + case easy_ws_close: + return this->ws_shut_down(1000, data); + + default: + POSEIDON_THROW(( + "Easy WebSocket event code `$3` not supported", + "[WebSocket server session `$1` (class `$2`)]"), + this, typeid(*this), opcode); + } + } + } // namespace poseidon diff --git a/poseidon/socket/ws_server_session.hpp b/poseidon/socket/ws_server_session.hpp index a25e1450..1ca10b6b 100644 --- a/poseidon/socket/ws_server_session.hpp +++ b/poseidon/socket/ws_server_session.hpp @@ -63,7 +63,7 @@ class WS_Server_Session // This callback is invoked by the network thread for each fragment of a data // message. `opcode` indicates the type of the message, which can be either - // `websocket_text` or `websocket_bin`; control frames are never notified via + // `websocket_text` or `websocket_binary`; control frames are never notified via // this callback. As with `TCP_Connection::do_on_tcp_stream()`, the argument // buffer contains all data that have been accumulated so far and callees are // supposed to remove bytes that have been processed. @@ -76,7 +76,7 @@ class WS_Server_Session do_on_ws_message_data_stream(WebSocket_OpCode opcode, linear_buffer& data); // This callback is invoked by the network thread at the end of a data message - // or a control frame. `opcode` may be `websocket_text`, `websocket_bin`, + // or a control frame. `opcode` may be `websocket_text`, `websocket_binary`, // `websocket_ping` or `websocket_pong`. virtual void @@ -112,6 +112,10 @@ class WS_Server_Session // This function is thread-safe. bool ws_shut_down(uint16_t status = 1000, chars_view reason = "") noexcept; + + // Provides a helper function for the sake of Easy classes. + bool + ws_send(Easy_WS_Event opcode, chars_view data); }; } // namespace poseidon diff --git a/poseidon/socket/wss_client_session.cpp b/poseidon/socket/wss_client_session.cpp index 726a7183..d8bc94bd 100644 --- a/poseidon/socket/wss_client_session.cpp +++ b/poseidon/socket/wss_client_session.cpp @@ -3,6 +3,8 @@ #include "../precompiled.ipp" #include "wss_client_session.hpp" +#include "enums.hpp" +#include "../easy/enums.hpp" #include "../http/websocket_deflator.hpp" #include "../base/config_file.hpp" #include "../static/main_config.hpp" @@ -167,7 +169,7 @@ do_on_https_upgraded_stream(linear_buffer& data, bool eof) // If this is a data frame or continuation, its payload is part of a // (potentially fragmented) data message, so combine it. auto opcode = static_cast(this->m_parser.message_opcode()); - ROCKET_ASSERT(is_any_of(opcode, { websocket_text, websocket_bin })); + ROCKET_ASSERT(is_any_of(opcode, { websocket_text, websocket_binary })); this->do_on_wss_message_data_stream(opcode, splice_buffers(this->m_msg, move(payload))); } @@ -182,7 +184,7 @@ do_on_https_upgraded_stream(linear_buffer& data, bool eof) case 1: // TEXT case 2: { // BINARY auto opcode = static_cast(this->m_parser.message_opcode()); - ROCKET_ASSERT(is_any_of(opcode, { websocket_text, websocket_bin })); + ROCKET_ASSERT(is_any_of(opcode, { websocket_text, websocket_binary })); this->do_on_wss_message_finish(opcode, move(this->m_msg)); break; } @@ -254,7 +256,7 @@ do_on_wss_message_data_stream(WebSocket_OpCode /*opcode*/, linear_buffer& data) if(data.size() > (uint64_t) max_websocket_message_length) POSEIDON_THROW(( "WebSocket text data message too large: `$3` > `$4`", - "[WebSocket server session `$1` (class `$2`)]"), + "[WebSocket client session `$1` (class `$2`)]"), this, typeid(*this), data.size(), max_websocket_message_length); } @@ -296,7 +298,7 @@ wss_send(WebSocket_OpCode opcode, chars_view data) if(!this->do_has_upgraded()) POSEIDON_THROW(( "WebSocket handshake not complete yet", - "[WebSocket server session `$1` (class `$2`)]"), + "[WebSocket client session `$1` (class `$2`)]"), this, typeid(*this)); switch(opcode) { @@ -343,7 +345,7 @@ wss_send(WebSocket_OpCode opcode, chars_view data) if(data.n > 125) POSEIDON_THROW(( "Control frame too large: `$3` > `125`", - "[WebSocket server session `$1` (class `$2`)]"), + "[WebSocket client session `$1` (class `$2`)]"), this, typeid(*this), data.n); // Control messages can't be compressed, so send it as is. @@ -353,7 +355,7 @@ wss_send(WebSocket_OpCode opcode, chars_view data) default: POSEIDON_THROW(( "WebSocket opcode `$3` not supported", - "[WebSocket server session `$1` (class `$2`)]"), + "[WebSocket client session `$1` (class `$2`)]"), this, typeid(*this), opcode); } } @@ -390,4 +392,32 @@ wss_shut_down(uint16_t status, chars_view reason) noexcept return succ; } +bool +WSS_Client_Session:: +wss_send(Easy_WS_Event opcode, chars_view data) + { + switch(opcode) { + case easy_ws_open: + return this->wss_send(websocket_ping, data); + + case easy_ws_text: + return this->wss_send(websocket_text, data); + + case easy_ws_binary: + return this->wss_send(websocket_binary, data); + + case easy_ws_pong: + return this->wss_send(websocket_pong, data); + + case easy_ws_close: + return this->wss_shut_down(1000, data); + + default: + POSEIDON_THROW(( + "Easy WebSocket event code `$3` not supported", + "[WebSocket client session `$1` (class `$2`)]"), + this, typeid(*this), opcode); + } + } + } // namespace poseidon diff --git a/poseidon/socket/wss_client_session.hpp b/poseidon/socket/wss_client_session.hpp index 652153dd..f8f27efa 100644 --- a/poseidon/socket/wss_client_session.hpp +++ b/poseidon/socket/wss_client_session.hpp @@ -62,7 +62,7 @@ class WSS_Client_Session // This callback is invoked by the network thread for each fragment of a data // message. `opcode` indicates the type of the message, which can be either - // `websocket_text` or `websocket_bin`; control frames are never notified via + // `websocket_text` or `websocket_binary`; control frames are never notified via // this callback. As with `TCP_Connection::do_on_tcp_stream()`, the argument // buffer contains all data that have been accumulated so far and callees are // supposed to remove bytes that have been processed. @@ -75,7 +75,7 @@ class WSS_Client_Session do_on_wss_message_data_stream(WebSocket_OpCode opcode, linear_buffer& data); // This callback is invoked by the network thread at the end of a data message - // or a control frame. `opcode` may be `websocket_text`, `websocket_bin`, + // or a control frame. `opcode` may be `websocket_text`, `websocket_binary`, // `websocket_ping` or `websocket_pong`. virtual void @@ -112,6 +112,10 @@ class WSS_Client_Session // This function is thread-safe. bool wss_shut_down(uint16_t status = 1000, chars_view reason = "") noexcept; + + // Provides a helper function for the sake of Easy classes. + bool + wss_send(Easy_WS_Event opcode, chars_view data); }; } // namespace poseidon diff --git a/poseidon/socket/wss_server_session.cpp b/poseidon/socket/wss_server_session.cpp index 8254b1a7..179eb25a 100644 --- a/poseidon/socket/wss_server_session.cpp +++ b/poseidon/socket/wss_server_session.cpp @@ -3,6 +3,8 @@ #include "../precompiled.ipp" #include "wss_server_session.hpp" +#include "enums.hpp" +#include "../easy/enums.hpp" #include "../http/websocket_deflator.hpp" #include "../base/config_file.hpp" #include "../static/main_config.hpp" @@ -206,7 +208,7 @@ do_on_https_upgraded_stream(linear_buffer& data, bool eof) // If this is a data frame or continuation, its payload is part of a // (potentially fragmented) data message, so combine it. auto opcode = static_cast(this->m_parser.message_opcode()); - ROCKET_ASSERT(is_any_of(opcode, { websocket_text, websocket_bin })); + ROCKET_ASSERT(is_any_of(opcode, { websocket_text, websocket_binary })); this->do_on_wss_message_data_stream(opcode, splice_buffers(this->m_msg, move(payload))); } @@ -221,7 +223,7 @@ do_on_https_upgraded_stream(linear_buffer& data, bool eof) case 1: // TEXT case 2: { // BINARY auto opcode = static_cast(this->m_parser.message_opcode()); - ROCKET_ASSERT(is_any_of(opcode, { websocket_text, websocket_bin })); + ROCKET_ASSERT(is_any_of(opcode, { websocket_text, websocket_binary })); this->do_on_wss_message_finish(opcode, move(this->m_msg)); break; } @@ -424,4 +426,32 @@ wss_shut_down(uint16_t status, chars_view reason) noexcept return succ; } +bool +WSS_Server_Session:: +wss_send(Easy_WS_Event opcode, chars_view data) + { + switch(opcode) { + case easy_ws_open: + return this->wss_send(websocket_ping, data); + + case easy_ws_text: + return this->wss_send(websocket_text, data); + + case easy_ws_binary: + return this->wss_send(websocket_binary, data); + + case easy_ws_pong: + return this->wss_send(websocket_pong, data); + + case easy_ws_close: + return this->wss_shut_down(1000, data); + + default: + POSEIDON_THROW(( + "Easy WebSocket event code `$3` not supported", + "[WebSocket client session `$1` (class `$2`)]"), + this, typeid(*this), opcode); + } + } + } // namespace poseidon diff --git a/poseidon/socket/wss_server_session.hpp b/poseidon/socket/wss_server_session.hpp index 1af9e1c4..44a027eb 100644 --- a/poseidon/socket/wss_server_session.hpp +++ b/poseidon/socket/wss_server_session.hpp @@ -63,7 +63,7 @@ class WSS_Server_Session // This callback is invoked by the network thread for each fragment of a data // message. `opcode` indicates the type of the message, which can be either - // `websocket_text` or `websocket_bin`; control frames are never notified via + // `websocket_text` or `websocket_binary`; control frames are never notified via // this callback. As with `TCP_Connection::do_on_tcp_stream()`, the argument // buffer contains all data that have been accumulated so far and callees are // supposed to remove bytes that have been processed. @@ -76,7 +76,7 @@ class WSS_Server_Session do_on_wss_message_data_stream(WebSocket_OpCode opcode, linear_buffer& data); // This callback is invoked by the network thread at the end of a data message - // or a control frame. `opcode` may be `websocket_text`, `websocket_bin`, + // or a control frame. `opcode` may be `websocket_text`, `websocket_binary`, // `websocket_ping` or `websocket_pong`. virtual void @@ -112,6 +112,10 @@ class WSS_Server_Session // This function is thread-safe. bool wss_shut_down(uint16_t status = 1000, chars_view reason = "") noexcept; + + // Provides a helper function for the sake of Easy classes. + bool + wss_send(Easy_WS_Event opcode, chars_view data); }; } // namespace poseidon diff --git a/poseidon/static/async_task_executor.cpp b/poseidon/static/async_task_executor.cpp index f585d0bf..abe90433 100644 --- a/poseidon/static/async_task_executor.cpp +++ b/poseidon/static/async_task_executor.cpp @@ -4,6 +4,7 @@ #include "../precompiled.ipp" #include "async_task_executor.hpp" #include "../base/abstract_async_task.hpp" +#include "../base/enums.hpp" #include "../utils.hpp" namespace poseidon { diff --git a/poseidon/static/fiber_scheduler.cpp b/poseidon/static/fiber_scheduler.cpp index f7a66e22..ae49e82d 100644 --- a/poseidon/static/fiber_scheduler.cpp +++ b/poseidon/static/fiber_scheduler.cpp @@ -4,6 +4,7 @@ #include "../precompiled.ipp" #include "fiber_scheduler.hpp" #include "../base/config_file.hpp" +#include "../base/enums.hpp" #include "../fiber/abstract_fiber.hpp" #include "../fiber/abstract_future.hpp" #include "../third/asan_fwd.hpp" diff --git a/poseidon/static/network_driver.cpp b/poseidon/static/network_driver.cpp index 92a77f88..e5fe6f04 100644 --- a/poseidon/static/network_driver.cpp +++ b/poseidon/static/network_driver.cpp @@ -5,6 +5,7 @@ #include "network_driver.hpp" #include "../socket/abstract_socket.hpp" #include "../socket/ssl_socket.hpp" +#include "../socket/enums.hpp" #include "../base/config_file.hpp" #include "../utils.hpp" #include diff --git a/poseidon/static/timer_driver.cpp b/poseidon/static/timer_driver.cpp index b4a769d0..60d53300 100644 --- a/poseidon/static/timer_driver.cpp +++ b/poseidon/static/timer_driver.cpp @@ -4,6 +4,7 @@ #include "../precompiled.ipp" #include "timer_driver.hpp" #include "../base/abstract_timer.hpp" +#include "../base/enums.hpp" #include "../utils.hpp" namespace poseidon { namespace { diff --git a/poseidon/third/zlib_fwd.hpp b/poseidon/third/zlib_fwd.hpp index 44f81caf..d187dcb2 100644 --- a/poseidon/third/zlib_fwd.hpp +++ b/poseidon/third/zlib_fwd.hpp @@ -33,10 +33,10 @@ class deflate_Stream int fmt_wbits; if(fmt == zlib_deflate) fmt_wbits = wbits; - else if(fmt == zlib_gzip) - fmt_wbits = wbits + 16; else if(fmt == zlib_raw) fmt_wbits = -wbits; + else if(fmt == zlib_gzip) + fmt_wbits = wbits + 16; else ::rocket::sprintf_and_throw<::std::invalid_argument>( "deflate_Stream: format `%d` not valid", @@ -122,10 +122,10 @@ class inflate_Stream int fmt_wbits; if(fmt == zlib_deflate) fmt_wbits = wbits; - else if(fmt == zlib_gzip) - fmt_wbits = wbits + 16; else if(fmt == zlib_raw) fmt_wbits = -wbits; + else if(fmt == zlib_gzip) + fmt_wbits = wbits + 16; else ::rocket::sprintf_and_throw<::std::invalid_argument>( "inflate_Stream: format `%d` not valid", diff --git a/test/socket_address.cpp b/test/socket_address.cpp index fd9f9f65..1ef3e61a 100644 --- a/test/socket_address.cpp +++ b/test/socket_address.cpp @@ -3,6 +3,7 @@ #include "utils.hpp" #include "../poseidon/socket/socket_address.hpp" +#include "../poseidon/socket/enums.hpp" using namespace ::poseidon; int