00001 #ifndef CPP_WEB_SERVER_WEBSOCKET_CONNECTION_HPP 00002 #define CPP_WEB_SERVER_WEBSOCKET_CONNECTION_HPP 00003 00004 #include <boost/asio.hpp> 00005 #include <boost/noncopyable.hpp> 00006 #include <boost/shared_ptr.hpp> 00007 #include <boost/enable_shared_from_this.hpp> 00008 #include <boost/thread/mutex.hpp> 00009 #include "async_web_server_cpp/http_connection.hpp" 00010 #include "async_web_server_cpp/websocket_message.hpp" 00011 00012 namespace async_web_server_cpp 00013 { 00014 00015 class WebsocketHttpRequestHandler; 00016 00017 class WebsocketConnection; 00018 typedef boost::shared_ptr<WebsocketConnection> WebsocketConnectionPtr; 00019 typedef boost::weak_ptr<WebsocketConnection> WebsocketConnectionWeakPtr; 00020 00025 class WebsocketConnection : public boost::enable_shared_from_this<WebsocketConnection>, 00026 private boost::noncopyable 00027 { 00028 public: 00029 explicit WebsocketConnection(HttpConnectionPtr connection); 00030 00031 typedef boost::function<void(const WebsocketMessage& message)> MessageHandler; 00032 00033 bool sendTextMessage(const std::string& content); 00034 bool sendPingMessage(const std::string& content = ""); 00035 00036 bool sendMessage(const WebsocketMessage& message); 00037 bool sendFrame(WebsocketFrame& frame); 00038 00039 private: 00040 static void static_handle_read(WebsocketConnectionWeakPtr weak_this, const char* begin, const char* end); 00041 void handle_read(const char* begin, const char* end); 00042 HttpConnectionPtr connection_; 00043 00044 void set_message_handler(MessageHandler& handler); 00045 MessageHandler handler_; 00046 00047 WebsocketFrame frame_; 00048 WebsocketMessage message_; 00049 WebsocketFrameParser frame_parser_; 00050 WebsocketFrameBuffer frame_buffer_; 00051 00052 friend class WebsocketHttpRequestHandler; 00053 }; 00054 00055 } 00056 00057 #endif