诸暨麻将添加redis
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

169 lines
4.8 KiB

  1. #ifndef ZL_WEBSOCKET_H
  2. #define ZL_WEBSOCKET_H
  3. #include <string>
  4. #include <stdint.h>
  5. namespace zl{
  6. namespace net{
  7. namespace ws
  8. {
  9. const char* const kWebSocketMagicString = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
  10. const char* const kSecWebSocketKeyHeader = "Sec-WebSocket-Key";
  11. const char* const kSecWebSocketVersionHeader = "Sec-WebSocket-Version";
  12. const char* const kUpgradeHeader = "Upgrade";
  13. const char* const kConnectionHeader = "Connection";
  14. const char* const kSecWebSocketProtocolHeader = "Sec-WebSocket-Protocol";
  15. const char* const kSecWebSocketAccept = "Sec-WebSocket-Accept";
  16. #define WS_FRAGMENT_FIN (1 << 7)
  17. enum WsOpcode
  18. {
  19. WS_OPCODE_CONTINUE = 0x0,
  20. WS_OPCODE_TEXT = 0x1,
  21. WS_OPCODE_BINARY = 0x2,
  22. WS_OPCODE_CLOSE = 0x8,
  23. WS_OPCODE_PING = 0x9,
  24. WS_OPCODE_PONG = 0xa
  25. };
  26. enum WsFrameType
  27. {
  28. WS_ERROR_FRAME = 0xFF00,
  29. WS_INCOMPLETE_FRAME = 0xFE00,
  30. WS_OPENING_FRAME = 0x3300,
  31. WS_CLOSING_FRAME = 0x3400,
  32. WS_INCOMPLETE_TEXT_FRAME = 0x01,
  33. WS_INCOMPLETE_BINARY_FRAME = 0x02,
  34. WS_TEXT_FRAME = 0x81, // 128 + 1 == WS_FRAGMENT_FIN | WS_OPCODE_TEXT
  35. WS_BINARY_FRAME = 0x82, // 128 + 2
  36. WS_RSV3_FRAME = 0x83, // 128 + 3
  37. WS_RSV4_FRAME = 0x84,
  38. WS_RSV5_FRAME = 0x85,
  39. WS_RSV6_FRAME = 0x86,
  40. WS_RSV7_FRAME = 0x87,
  41. WS_CLOSE_FRAME = 0x88,
  42. WS_PING_FRAME = 0x89,
  43. WS_PONG_FRAME = 0x8A,
  44. };
  45. enum WsCloseReason
  46. {
  47. WS_CLOSE_NORMAL = 1000,
  48. WS_CLOSE_GOING_AWAY = 1001,
  49. WS_CLOSE_PROTOCOL_ERROR = 1002,
  50. WS_CLOSE_NOT_ALLOWED = 1003,
  51. WS_CLOSE_RESERVED = 1004,
  52. WS_CLOSE_NO_CODE = 1005,
  53. WS_CLOSE_DIRTY = 1006,
  54. WS_CLOSE_WRONG_TYPE = 1007,
  55. WS_CLOSE_POLICY_VIOLATION = 1008,
  56. WS_CLOSE_MESSAGE_TOO_BIG = 1009,
  57. WS_CLOSE_UNEXPECTED_ERROR = 1011
  58. };
  59. #define STATE_SHOULD_CLOSE (1 << 0)
  60. #define STATE_SENT_CLOSE_FRAME (1 << 1)
  61. #define STATE_CONNECTING (1 << 2)
  62. #define STATE_IS_SSL (1 << 3)
  63. #define STATE_CONNECTED (1 << 4)
  64. #define STATE_SENDING_FRAGMENT (1 << 5)
  65. #define STATE_RECEIVING_FRAGMENT (1 << 6)
  66. #define STATE_RECEIVED_CLOSE_FRAME (1 << 7)
  67. #define STATE_FAILING_CONNECTION (1 << 8)
  68. enum WsConnState
  69. {
  70. WS_CONN_CONNECTING = 0,
  71. WS_CONN_OPEN = 1,
  72. WS_CONN_CLOSING = 2,
  73. WS_COOO_CLOSED = 3
  74. };
  75. enum
  76. {
  77. /// Maximum size of a basic WebSocket payload
  78. PAYLOAD_SIZE_BASIC = 125, // 2^7 -1
  79. /// Maximum size of an extended WebSocket payload (basic payload = 126)
  80. PAYLOAD_SIZE_EXTENDED = 0xFFFF, // 2^16 - 1, 65535
  81. /// Maximum size of a jumbo WebSocket payload (basic payload = 127)
  82. PAYLOAD_SIZE_JUMBO = 0x7FFFFFFF //2^63 -1
  83. };
  84. enum WsHeaderFlag
  85. {
  86. WS_FLAG_NULL = 0x0,
  87. WS_FLAG_UPGRAGE = 0x0001,
  88. WS_FLAG_CONNECTION = 0x0002,
  89. WS_FLAG_HOST = 0x0004,
  90. WS_FLAG_ORIGIN = 0x0008,
  91. WS_FLAG_SEC_ORIGIN = 0x0010,
  92. WS_FLAG_SEC_KEY = 0x0020,
  93. WS_FLAG_SEC_VERSION = 0x0040,
  94. WS_FLAG_SEC_KEY1 = 0x0080,
  95. WS_FLAG_SEC_KEY2 = 0x0100,
  96. WS_FLAG_SEC_PROTOCOL = 0x0200,
  97. };
  98. class WsConnection
  99. {
  100. public:
  101. WsConnection()
  102. {
  103. handShaked_ = false;
  104. state_ = WS_CONN_CONNECTING;
  105. }
  106. ~WsConnection()
  107. {
  108. }
  109. public:
  110. void setHandshaked(bool yes) { handShaked_ = yes; }
  111. bool handshaked() const { return handShaked_; }
  112. void setConnState(WsConnState state) { state_ = state; }
  113. WsConnState connState() const { return state_; }
  114. void setPath(const std::string& s) { path_ = s; }
  115. const std::string& path() const { return path_; }
  116. void setProtocol(const std::string& s) { protocol_ = s; }
  117. const std::string& protocol() const { return protocol_; }
  118. private:
  119. bool handShaked_;
  120. WsConnState state_;
  121. std::string path_;
  122. std::string protocol_;
  123. };
  124. uint16_t ntoh16(uint16_t);
  125. uint16_t hton16(uint16_t);
  126. uint64_t ntoh64(uint64_t);
  127. uint64_t hton64(uint64_t);
  128. /// 设置返回给client的握手响应
  129. /// seckey : client的"Sec-WebSocket-Key"对应值
  130. std::string makeHandshakeResponse(const char* seckey, std::string protocol);
  131. /// 将从client发过来的数据帧进行解码
  132. /// inbuf : 接收到的client发送的数据
  133. /// insize : 接收到的数据大小
  134. /// outbuf : 解码缓冲区
  135. /// return : WsFrameType
  136. WsFrameType decodeFrame(const char* inbuf, int insize, char* outbuf, int* outlen, int* frameSize);
  137. /// 将发回给client的数据进行编码
  138. /// msg : 发回给client的数据
  139. /// msglen : 发回的数据大小
  140. /// outbuf : 编码缓冲区
  141. /// outsize : 编码缓冲区大小(建议outsize > insize + 10)
  142. int encodeFrame(WsFrameType frame_type, const char* msg, int msgsize, char* outbuf, int outsize);
  143. } // namespace zl { namespace net { namespace ws {
  144. }
  145. }
  146. #endif /* ZL_WEBSOCKET_UTIL_H */