1
0

WebSocketChannel.h 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #ifndef HV_WEBSOCKET_CHANNEL_H_
  2. #define HV_WEBSOCKET_CHANNEL_H_
  3. #include <mutex>
  4. #include "Channel.h"
  5. #include "wsdef.h"
  6. #include "hmath.h"
  7. namespace hv {
  8. class HV_EXPORT WebSocketChannel : public SocketChannel {
  9. public:
  10. ws_session_type type;
  11. WebSocketChannel(hio_t* io, ws_session_type type = WS_CLIENT)
  12. : SocketChannel(io)
  13. , type(type)
  14. , opcode(WS_OPCODE_CLOSE)
  15. {}
  16. ~WebSocketChannel() {}
  17. // isConnected, send, close
  18. int send(const std::string& msg, enum ws_opcode opcode = WS_OPCODE_TEXT, bool fin = true) {
  19. return send(msg.c_str(), msg.size(), opcode, fin);
  20. }
  21. int send(const char* buf, int len, enum ws_opcode opcode = WS_OPCODE_BINARY, bool fin = true);
  22. // websocket fragment
  23. int send(const char* buf, int len, int fragment, enum ws_opcode opcode = WS_OPCODE_BINARY);
  24. int sendPing();
  25. int sendPong();
  26. int close() {
  27. return SocketChannel::close(type == WS_SERVER);
  28. }
  29. protected:
  30. int sendFrame(const char* buf, int len, enum ws_opcode opcode = WS_OPCODE_BINARY, bool fin = true);
  31. public:
  32. enum ws_opcode opcode;
  33. private:
  34. Buffer sendbuf_;
  35. std::mutex mutex_;
  36. };
  37. }
  38. typedef std::shared_ptr<hv::WebSocketChannel> WebSocketChannelPtr;
  39. #endif // HV_WEBSOCKET_CHANNEL_H_