fastdo  0.6.16
ws_websocket.hpp
浏览该文件的文档.
1 #pragma once
2 
62 namespace ws
63 {
66 {
68  dataText = 0x1U,
69  dataBinary = 0x2U,
70  dataRsv3 = 0x3U,
71  dataRsv4 = 0x4U,
72  dataRsv5 = 0x5U,
73  dataRsv6 = 0x6U,
74  dataRsv7 = 0x7U,
75  ctrlClose = 0x8U,
76  ctrlPing = 0x9U,
77  ctrlPong = 0xAU,
78  ctrlRsvB = 0xBU,
79  ctrlRsvC = 0xCU,
80  ctrlRsvD = 0xDU,
81  ctrlRsvE = 0xEU,
82  ctrlRsvF = 0xFU,
83 };
84 
87 {
90  //errCnnUpgradeFail,
91 };
92 
95 {
96  ftBasic = 0b0010,
97  ftBasicMasked = 0b0011,
98  ftExtended16 = 0b0100,
100  ftExtended64 = 0b1000,
102 };
103 
106 {
112 };
113 
114 #pragma pack(push, 1)
115 
116 struct FrameBase
117 {
123 
126 
128  {
129  memset( this, 0, sizeof(*this) );
130  }
131 };
132 
133 // 基本帧 payload len < 126, mask == 0
135 {
137  {
138  memset( this, 0, sizeof(*this) );
139  }
140 
141  template < typename _Ty >
142  _Ty * data()
143  {
144  return reinterpret_cast<_Ty*>( this + 1 );
145  }
146 };
147 #pragma pack(pop)
148 
149 
150 #pragma pack(push, 2)
151 // 基本掩码帧 payload len < 126, mask == 1
153 {
154  winux::uint8 maskingKey[4];
155 
157  {
158  memset( this, 0, sizeof(*this) );
159  }
160 
161  template < typename _Ty >
162  _Ty * data()
163  {
164  return reinterpret_cast<_Ty*>( this + 1 );
165  }
166 };
167 
168 // 16位数据扩展帧 payload len == 126, mask == 0
170 {
172 
174  {
175  memset( this, 0, sizeof(*this) );
176  }
177 
178  template < typename _Ty >
179  _Ty * data()
180  {
181  return reinterpret_cast<_Ty*>( this + 1 );
182  }
183 };
184 
185 // 16位数据扩展掩码帧 payload len == 126, mask == 1
187 {
189  winux::uint8 maskingKey[4];
190 
192  {
193  memset( this, 0, sizeof(*this) );
194  }
195 
196  template < typename _Ty >
197  _Ty * data()
198  {
199  return reinterpret_cast<_Ty*>( this + 1 );
200  }
201 };
202 
203 // 64位数据扩展帧 payload len == 127, mask == 0
205 {
207 
209  {
210  memset( this, 0, sizeof(*this) );
211  }
212 
213  template < typename _Ty >
214  _Ty * data()
215  {
216  return reinterpret_cast<_Ty*>( this + 1 );
217  }
218 };
219 
220 // 64位数据扩展掩码帧 payload len == 127, mask == 1
222 {
224  winux::uint8 maskingKey[4];
225 
227  {
228  memset( this, 0, sizeof(*this) );
229  }
230 
231  template < typename _Ty >
232  _Ty * data()
233  {
234  return reinterpret_cast<_Ty*>( this + 1 );
235  }
236 };
237 
238 // 关闭帧载荷数据
240 {
242  template < typename _Ty >
243  _Ty * data()
244  {
245  return reinterpret_cast<_Ty*>( this + 1 );
246  }
247 };
248 #pragma pack(pop)
249 
250 
251 // 构建WebSocket帧
252 HTTPWRAPPER_FUNC_DECL(winux::Buffer) BuildFrame( OpCode opcode, bool fin, bool mask, winux::byte * payloadData = nullptr, size_t payloadDataSize = 0 );
253 // 构建WebSocket数据。指定perFrameMaxPayloadSize将自动分片成多个帧
254 HTTPWRAPPER_FUNC_DECL(std::vector<winux::Buffer>) BuildFramesFromData( OpCode opcode, bool mask, winux::Buffer payloadData = winux::Buffer(), size_t perFrameMaxPayloadSize = -1 );
255 
256 
257 class WsHttpServer;
260 {
261 public:
262  WsHttpRequestCtx( WsHttpServer * server, winux::uint64 clientId, winux::String const & clientEpStr, winux::SharedPointer<eiennet::ip::tcp::Socket> clientSockPtr );
263 
265  enum TaskType
266  {
267  ttRecvWebSocketFrameBase = http::HttpRequestCtx::ttNextStart,
269  ttDoProcessWebSocketFrameTask
270  };
271 
272 private:
273  winux::AnsiString _message; // websocket消息缓冲区,用于接收多帧数据(一条消息可能分片)
274  int _messageType; // 消息类型,0没有;1文本;2二进制
275  ReadyState _state; // WebSocket连接状态
276 
277  friend class WsHttpServer;
278 };
279 
282 {
283 public:
284  using HttpServer::HttpServer;
285 
286 protected:
288  virtual bool _beginRouting( winux::SharedPointer<http::HttpRequestCtx> requestCtxPtr, http::UrlPathPartData const & urlData, http::Response & rsp ) override;
289 
290  DEFINE_CUSTOM_EVENT( Open, ( winux::SharedPointer<WsHttpRequestCtx> requestCtxPtr ), ( requestCtxPtr ) )
291  DEFINE_CUSTOM_EVENT( Message, ( winux::SharedPointer<WsHttpRequestCtx> requestCtxPtr, winux::AnsiString const & data, int messageType ), ( requestCtxPtr, data, messageType ) )
292  DEFINE_CUSTOM_EVENT( Close, ( winux::SharedPointer<WsHttpRequestCtx> requestCtxPtr, winux::uint16 errCode, winux::AnsiString const & errStr ), ( requestCtxPtr, errCode, errStr ) )
293  DEFINE_CUSTOM_EVENT( Error, ( winux::SharedPointer<WsHttpRequestCtx> requestCtxPtr, WebSocketErrorCode ec ), ( requestCtxPtr, ec ) )
294 };
295 
296 
297 } // namespace ws
XString< char > AnsiString
Definition: utilities.hpp:257
#define HTTPWRAPPER_FUNC_DECL(ret)
Definition: http_base.hpp:28
ping frame
#define HTTPWRAPPER_DLL
Definition: http_base.hpp:24
close frame
处理URL路径部分时的一些中间变量
Definition: http_misc.hpp:12
WebSocket客户请求场景
winux::uint16 extendedPayloadLen
XString< tchar > String
Definition: utilities.hpp:261
winux::uint64 extendedPayloadLen
text frame
HTTP服务器
winux::uint8 rsv3
没有状态,尚未触发WS功能
std::vector< winux::Buffer > BuildFramesFromData(OpCode opcode, bool mask, winux::Buffer payloadData=winux::Buffer(), size_t perFrameMaxPayloadSize=-1)
winux::uint16 extendedPayloadLen
FrameType
数据帧类型
winux::uint8 payloadLen
已打开
引用计数共享指针
Definition: smartptr.hpp:456
缓冲区,表示内存中一块二进制数据(利用malloc/realloc进行内存分配)
Definition: utilities.hpp:906
winux::Buffer BuildFrame(OpCode opcode, bool fin, bool mask, winux::byte *payloadData=nullptr, size_t payloadDataSize=0)
unsigned int uint
Definition: utilities.hpp:215
ReadyState
就绪状态
无错误
pong frame
winux::uint8 fin
unsigned char byte
Definition: utilities.hpp:249
winux::uint8 rsv1
#define DEFINE_CUSTOM_EVENT(evtname, paramtypes, calledparams)
Definition: utilities.hpp:182
WebSocketErrorCode
WebSocket错误码
HTTP响应
unsigned short uint16
Definition: utilities.hpp:218
连接意外无效
winux::uint8 opcode
winux::uint8 rsv2
HTTP客户请求场景
#define U(s)
Definition: strings.hpp:1141
OpCode
操作码
unsigned __int64 uint64
Definition: utilities.hpp:230
WS帧基类
winux::uint8 mask
WebSocket封装
unsigned char uint8
Definition: utilities.hpp:220
用于下一个任务类型的起始枚举值
binary frame
WebSocket服务器
winux::uint64 extendedPayloadLen
continuation frame
跨平台基础功能库
Definition: archives.hpp:7