From 993191ec51d0499c571edbc452b26af66a174724 Mon Sep 17 00:00:00 2001 From: Joshua Scott Date: Fri, 20 Apr 2018 18:53:23 +0100 Subject: [PATCH] net: Add error codes to Session::close --- include/ki/protocol/net/Session.h | 18 +++++++++++++++++- src/protocol/net/ClientSession.cpp | 12 ++++++------ src/protocol/net/ServerSession.cpp | 12 ++++++------ src/protocol/net/Session.cpp | 4 ++-- 4 files changed, 31 insertions(+), 15 deletions(-) diff --git a/include/ki/protocol/net/Session.h b/include/ki/protocol/net/Session.h index f3b7682..e877319 100644 --- a/include/ki/protocol/net/Session.h +++ b/include/ki/protocol/net/Session.h @@ -17,6 +17,22 @@ namespace protocol { namespace net { + enum class SessionCloseErrorCode + { + NONE, + APPLICATION_ERROR, + + INVALID_FRAMING_START_SIGNAL, + INVALID_FRAMING_SIZE_EXCEEDS_MAXIMUM, + + UNHANDLED_CONTROL_MESSAGE, + UNHANDLED_APPLICATION_MESSAGE, + INVALID_MESSAGE, + + SESSION_OFFER_TIMED_OUT, + SESSION_DIED + }; + enum class ReceiveState { // Waiting for the 0xF00D start signal. @@ -106,7 +122,7 @@ namespace net /* Low-level socket methods */ virtual void send_packet_data(const char *data, const size_t size) = 0; - virtual void close() = 0; + virtual void close(SessionCloseErrorCode error) = 0; private: /* Low-level networking members */ uint16_t m_maximum_packet_size; diff --git a/src/protocol/net/ClientSession.cpp b/src/protocol/net/ClientSession.cpp index db70956..9232a5a 100644 --- a/src/protocol/net/ClientSession.cpp +++ b/src/protocol/net/ClientSession.cpp @@ -72,7 +72,7 @@ namespace net break; default: - close(); + close(SessionCloseErrorCode::UNHANDLED_CONTROL_MESSAGE); break; } } @@ -87,9 +87,9 @@ namespace net } catch (parse_error &e) { - // The SESSION_ACCEPT wasn't valid... + // The SESSION_OFFER wasn't valid... // Close the session - close(); + close(SessionCloseErrorCode::INVALID_MESSAGE); return; } @@ -98,7 +98,7 @@ namespace net std::chrono::steady_clock::now() - m_connection_time ).count() > KI_CONNECTION_TIMEOUT) { - close(); + close(SessionCloseErrorCode::SESSION_OFFER_TIMED_OUT); return; } @@ -136,7 +136,7 @@ namespace net { // The KEEP_ALIVE wasn't valid... // Close the session - close(); + close(SessionCloseErrorCode::INVALID_MESSAGE); return; } @@ -158,7 +158,7 @@ namespace net { // The KEEP_ALIVE_RSP wasn't valid... // Close the session - close(); + close(SessionCloseErrorCode::INVALID_MESSAGE); return; } diff --git a/src/protocol/net/ServerSession.cpp b/src/protocol/net/ServerSession.cpp index 5da2d09..72efe98 100644 --- a/src/protocol/net/ServerSession.cpp +++ b/src/protocol/net/ServerSession.cpp @@ -78,7 +78,7 @@ namespace net break; default: - close(); + close(SessionCloseErrorCode::UNHANDLED_CONTROL_MESSAGE); break; } } @@ -95,7 +95,7 @@ namespace net { // The SESSION_ACCEPT wasn't valid... // Close the session - close(); + close(SessionCloseErrorCode::INVALID_MESSAGE); return; } @@ -104,14 +104,14 @@ namespace net std::chrono::steady_clock::now() - m_connection_time ).count() > KI_CONNECTION_TIMEOUT) { - close(); + close(SessionCloseErrorCode::SESSION_OFFER_TIMED_OUT); return; } // Make sure they're accepting this session if (accept.get_session_id() != m_id) { - close(); + close(SessionCloseErrorCode::INVALID_MESSAGE); return; } @@ -134,7 +134,7 @@ namespace net { // The KEEP_ALIVE wasn't valid... // Close the session - close(); + close(SessionCloseErrorCode::INVALID_MESSAGE); return; } @@ -156,7 +156,7 @@ namespace net { // The KEEP_ALIVE_RSP wasn't valid... // Close the session - close(); + close(SessionCloseErrorCode::INVALID_MESSAGE); return; } diff --git a/src/protocol/net/Session.cpp b/src/protocol/net/Session.cpp index 6a995c8..9807773 100644 --- a/src/protocol/net/Session.cpp +++ b/src/protocol/net/Session.cpp @@ -104,7 +104,7 @@ namespace net // correctly. if (m_start_signal != KI_START_SIGNAL) { - close(); + close(SessionCloseErrorCode::INVALID_FRAMING_START_SIGNAL); return; } @@ -126,7 +126,7 @@ namespace net // stop processing data. if (m_incoming_packet_size > m_maximum_packet_size) { - close(); + close(SessionCloseErrorCode::INVALID_FRAMING_SIZE_EXCEEDS_MAXIMUM); return; }