17#include <json/value.h>
22#include <trantor/net/InetAddress.h>
23#include <trantor/utils/NonCopyable.h>
31 kNormalClosure = 1000,
37 kProtocolError = 1002,
42 kInvalidMessage = 1003,
57 kWrongMessageContent = 1007,
67 kMessageTooBig = 1009,
75 kNeedMoreExtensions = 1010,
79 kUnexpectedCondition = 1011,
92class WebSocketConnection
95 WebSocketConnection() =
default;
96 virtual ~WebSocketConnection(){};
108 const WebSocketMessageType type = WebSocketMessageType::Text) = 0;
117 std::string_view msg,
118 const WebSocketMessageType type = WebSocketMessageType::Text) = 0;
127 const Json::Value &json,
128 const WebSocketMessageType type = WebSocketMessageType::Text) = 0;
131 virtual const trantor::InetAddress &
localAddr()
const = 0;
134 virtual const trantor::InetAddress &
peerAddr()
const = 0;
149 virtual void shutdown(
const CloseCode code = CloseCode::kNormalClosure,
150 const std::string &reason =
"") = 0;
162 contextPtr_ = context;
172 contextPtr_ = std::move(context);
181 template <
typename T>
184 return std::static_pointer_cast<T>(contextPtr_);
193 template <
typename T>
196 return *(
static_cast<T *
>(contextPtr_.get()));
202 return (
bool)contextPtr_;
223 const std::string &message,
224 const std::chrono::duration<double> &interval) = 0;
232 std::shared_ptr<void> contextPtr_;
235using WebSocketConnectionPtr = std::shared_ptr<WebSocketConnection>;
virtual void sendJson(const Json::Value &json, const WebSocketMessageType type=WebSocketMessageType::Text)=0
Send a message to the peer.
virtual void shutdown(const CloseCode code=CloseCode::kNormalClosure, const std::string &reason="")=0
Shut down the write direction, which means that further send operations are disabled.
T & getContextRef() const
Get the custom data reference from the connection.
Definition WebSocketConnection.h:194
void setContext(std::shared_ptr< void > &&context)
Set custom data on the connection.
Definition WebSocketConnection.h:170
void setContext(const std::shared_ptr< void > &context)
Set custom data on the connection.
Definition WebSocketConnection.h:160
void clearContext()
Clear the context.
Definition WebSocketConnection.h:206
virtual void setPingMessage(const std::string &message, const std::chrono::duration< double > &interval)=0
Set the heartbeat(ping) message sent to the peer.
bool hasContext()
Return true if the context is set by user.
Definition WebSocketConnection.h:200
virtual void send(std::string_view msg, const WebSocketMessageType type=WebSocketMessageType::Text)=0
Send a message to the peer.
virtual const trantor::InetAddress & peerAddr() const =0
Return the remote IP address and port number of the connection.
virtual void disablePing()=0
Disable sending ping messages to the peer.
virtual bool connected() const =0
Return true if the connection is open.
virtual void send(const char *msg, uint64_t len, const WebSocketMessageType type=WebSocketMessageType::Text)=0
Send a message to the peer.
virtual void forceClose()=0
Close the connection.
std::shared_ptr< T > getContext() const
Get custom data from the connection.
Definition WebSocketConnection.h:182
virtual bool disconnected() const =0
Return true if the connection is closed.
virtual const trantor::InetAddress & localAddr() const =0
Return the local IP address and port number of the connection.
Drogon Test is a minimal effort test framework developed because the major C++ test frameworks doesn'...
Definition Attribute.h:23