drogon
C++14/17-based HTTP application framework
Loading...
Searching...
No Matches
WebSocketConnection.h
Go to the documentation of this file.
1
14
15#pragma once
16
17#include <json/value.h>
18#include <memory>
19#include <string>
20#include <drogon/HttpTypes.h>
21#include <string_view>
22#include <trantor/net/InetAddress.h>
23#include <trantor/utils/NonCopyable.h>
24
25namespace drogon
26{
27enum class CloseCode
28{
29 /*1000 indicates a normal closure, meaning that the purpose for which the
30 connection was established has been fulfilled.*/
31 kNormalClosure = 1000,
32 /*1001 indicates that an endpoint is "going away", such as a server going
33 down or a browser having navigated away from a page.*/
34 kEndpointGone = 1001,
35 /*1002 indicates that an endpoint is terminating the connection due to a
36 protocol error.*/
37 kProtocolError = 1002,
38 /*1003 indicates that an endpoint is terminating the connection because it
39 has received a type of data it cannot accept (e.g., an endpoint that
40 understands only text data MAY send this if it receives a binary
41 message).*/
42 kInvalidMessage = 1003,
43 /*1005 is a reserved value and MUST NOT be set as a status code in a Close
44 control frame by an endpoint. It is designated for use in applications
45 expecting a status code to indicate that no status code was actually
46 present.*/
47 kNone = 1005,
48 /*1006 is a reserved value and MUST NOT be set as a status code in a Close
49 control frame by an endpoint. It is designated for use in applications
50 expecting a status code to indicate that the connection was closed
51 abnormally, e.g., without sending or receiving a Close control frame.
52 */
53 kAbnormally = 1006,
54 /*1007 indicates that an endpoint is terminating the connection because it
55 has received data within a message that was not consistent with the type
56 of the message (e.g., non-UTF-8 [RFC3629] data within a text message).*/
57 kWrongMessageContent = 1007,
58 /*1008 indicates that an endpoint is terminating the connection because it
59 has received a message that violates its policy. This is a generic
60 status code that can be returned when there is no other more suitable
61 status code (e.g., 1003 or 1009) or if there is a need to hide specific
62 details about the policy.
63 */
64 kViolation = 1008,
65 /*1009 indicates that an endpoint is terminating the connection because it
66 has received a message that is too big for it to process.*/
67 kMessageTooBig = 1009,
68 /*1010 indicates that an endpoint (client) is terminating the connection
69 because it has expected the server to negotiate one or more extension,
70 but the server didn't return them in the response message of the
71 WebSocket handshake. The list of extensions that are needed SHOULD
72 appear in the /reason/ part of the Close frame. Note that this status
73 code is not used by the server, because it can fail the WebSocket
74 handshake instead.*/
75 kNeedMoreExtensions = 1010,
76 /*1011 indicates that a server is terminating the connection because it
77 encountered an unexpected condition that prevented it from fulfilling the
78 request.*/
79 kUnexpectedCondition = 1011,
80 /*1015 is a reserved value and MUST NOT be set as a status code in a Close
81 control frame by an endpoint. It is designated for use in applications
82 expecting a status code to indicate that the connection was closed due to
83 a failure to perform a TLS handshake (e.g., the server certificate can't
84 be verified).*/
85 kTLSFailed = 1015
86};
87
92class WebSocketConnection
93{
94 public:
95 WebSocketConnection() = default;
96 virtual ~WebSocketConnection(){};
97
105 virtual void send(
106 const char *msg,
107 uint64_t len,
108 const WebSocketMessageType type = WebSocketMessageType::Text) = 0;
109
116 virtual void send(
117 std::string_view msg,
118 const WebSocketMessageType type = WebSocketMessageType::Text) = 0;
119
126 virtual void sendJson(
127 const Json::Value &json,
128 const WebSocketMessageType type = WebSocketMessageType::Text) = 0;
129
131 virtual const trantor::InetAddress &localAddr() const = 0;
132
134 virtual const trantor::InetAddress &peerAddr() const = 0;
135
137 virtual bool connected() const = 0;
138
140 virtual bool disconnected() const = 0;
141
149 virtual void shutdown(const CloseCode code = CloseCode::kNormalClosure,
150 const std::string &reason = "") = 0;
151
153 virtual void forceClose() = 0;
154
160 void setContext(const std::shared_ptr<void> &context)
161 {
162 contextPtr_ = context;
163 }
164
170 void setContext(std::shared_ptr<void> &&context)
171 {
172 contextPtr_ = std::move(context);
173 }
174
181 template <typename T>
182 std::shared_ptr<T> getContext() const
183 {
184 return std::static_pointer_cast<T>(contextPtr_);
185 }
186
193 template <typename T>
194 T &getContextRef() const
195 {
196 return *(static_cast<T *>(contextPtr_.get()));
197 }
198
201 {
202 return (bool)contextPtr_;
203 }
204
207 {
208 contextPtr_.reset();
209 }
210
222 virtual void setPingMessage(
223 const std::string &message,
224 const std::chrono::duration<double> &interval) = 0;
225
229 virtual void disablePing() = 0;
230
231 private:
232 std::shared_ptr<void> contextPtr_;
233};
234
235using WebSocketConnectionPtr = std::shared_ptr<WebSocketConnection>;
236} // namespace drogon
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