From 50efb8f996cd487f2931de0b365a61e06be936be Mon Sep 17 00:00:00 2001 From: Peter Thorson Date: Fri, 18 Nov 2011 08:46:05 -0600 Subject: [PATCH] moves public connection/session interface into the appropriate places --- src/connection.hpp | 86 ++++++++++++++++++++++++++++++++++++++++++-- src/roles/server.hpp | 37 +++++++++++++++++-- 2 files changed, 119 insertions(+), 4 deletions(-) diff --git a/src/connection.hpp b/src/connection.hpp index 33573e6556..20fd322a3e 100644 --- a/src/connection.hpp +++ b/src/connection.hpp @@ -40,9 +40,65 @@ #include #include // temporary? +#include +#include namespace websocketpp { +// types to use for utf8 string and binary messages +typedef std::vector binary_string; +typedef boost::shared_ptr binary_string_ptr; + +typedef std::string utf8_string; +typedef boost::shared_ptr utf8_string_ptr; + +// Universal close status codes. Might be better somewhere else. +namespace close { +namespace status { + enum value { + INVALID_END = 999, + NORMAL = 1000, + GOING_AWAY = 1001, + PROTOCOL_ERROR = 1002, + UNSUPPORTED_DATA = 1003, + RSV_ADHOC_1 = 1004, + NO_STATUS = 1005, + ABNORMAL_CLOSE = 1006, + INVALID_PAYLOAD = 1007, + POLICY_VIOLATION = 1008, + MESSAGE_TOO_BIG = 1009, + EXTENSION_REQUIRE = 1010, + RSV_START = 1011, + RSV_END = 2999, + INVALID_START = 5000 + }; + + inline bool reserved(value s) { + return ((s >= RSV_START && s <= RSV_END) || + s == RSV_ADHOC_1); + } + + inline bool invalid(value s) { + return ((s <= INVALID_END || s >= INVALID_START) || + s == NO_STATUS || + s == ABNORMAL_CLOSE); + } + + // TODO functions for application ranges? +} +} + +namespace session { +namespace state { + enum value { + CONNECTING = 0, + OPEN = 1, + CLOSING = 2, + CLOSED = 3 + }; +} +} + template struct connection_traits; @@ -69,6 +125,8 @@ public: // protected instead of public. // friend typename endpoint_traits::role_type; + + connection(endpoint_type& e) : role_type(e), socket_type(e), @@ -84,6 +142,29 @@ public: ) ); } + + // Valid always + session::state::value get_state() const { + return m_state; + } + + // Valid for OPEN state + void send(utf8_string_ptr msg) {} + void send(binary_string_ptr data) {} + void close(close::status::value code, const utf8_string& reason) {} + void ping(binary_string_ptr data) {} + void pong(binary_string_ptr data) {} + + uint64_t buffered_amount() const; + + // Valid for CLOSED state + close::status::value get_local_close_code() const {}; + utf8_string get_local_close_reason() const {}; + close::status::value get_remote_close_code() const {}; + utf8_string get_remote_close_reason() const {}; + bool failed_by_me() const {}; + bool dropped_by_me() const {}; + bool closed_by_me() const {}; protected: void handle_socket_init(const boost::system::error_code& error) { if (error) { @@ -151,8 +232,9 @@ protected: endpoint_type& m_endpoint; char m_data[512]; // temporary - http::parser::request m_request; - http::parser::response m_response; + boost::asio::streambuf m_buf; + + session::state::value m_state; }; // connection related types that it and its policy classes need. diff --git a/src/roles/server.hpp b/src/roles/server.hpp index ed95570164..e6d3f83bd9 100644 --- a/src/roles/server.hpp +++ b/src/roles/server.hpp @@ -43,13 +43,46 @@ template class server { public: // Connection specific details - template + template class connection { public: - connection(server& e) {} + // Valid always + std::string get_request_header(const std::string& key) const {} + std::string get_origin() const {} + + // Valid for CONNECTING state + void add_response_header(const std::string& key, const std::string& value) {}; + void replace_response_header(const std::string& key, const std::string& e) {}; + const std::vector& get_subprotocols() const {}; + const std::vector& get_extensions() const {}; + void select_subprotocol(const std::string& value) {}; + void select_extension(const std::string& value) {}; + protected: + //connection(server& e) : m_endpoint(e) {} + connection(endpoint& e) : m_endpoint(e) {} + + // initializes the websocket connection + void async_init() { + /*boost::asio::async_read_until( + m_endpoint.socket(), + m_buf, + "\r\n\r\n", + boost::bind( + &connection_type::handle_read_request, + connection_type::shared_from_this(), + boost::asio::placeholders::error, + boost::asio::placeholders::bytes_transferred + ) + );*/ + } + + private: + endpoint& m_endpoint; + http::parser::request m_request; + http::parser::response m_response; }; // handler interface callback class