WebSocketChannel.h 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. {}
  15. ~WebSocketChannel() {}
  16. // isConnected, send, close
  17. int send(const std::string& msg, enum ws_opcode opcode = WS_OPCODE_TEXT, bool fin = true) {
  18. return send(msg.c_str(), msg.size(), opcode, fin);
  19. }
  20. int send(const char* buf, int len, enum ws_opcode opcode = WS_OPCODE_BINARY, bool fin = true);
  21. // websocket fragment
  22. int send(const char* buf, int len, int fragment, enum ws_opcode opcode = WS_OPCODE_BINARY);
  23. int sendPing();
  24. int sendPong();
  25. int close() {
  26. return SocketChannel::close(type == WS_SERVER);
  27. }
  28. protected:
  29. int sendFrame(const char* buf, int len, enum ws_opcode opcode = WS_OPCODE_BINARY, bool fin = true);
  30. public:
  31. enum ws_opcode opcode;
  32. private:
  33. Buffer sendbuf_;
  34. std::mutex mutex_;
  35. };
  36. }
  37. typedef std::shared_ptr<hv::WebSocketChannel> WebSocketChannelPtr;
  38. #endif // HV_WEBSOCKET_CHANNEL_H_