sio_packet.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. //
  2. // sio_packet.h
  3. //
  4. // Created by Melo Yao on 3/19/15.
  5. //
  6. #ifndef SIO_PACKET_H
  7. #define SIO_PACKET_H
  8. #include <sstream>
  9. #include "../sio_message.h"
  10. #include <functional>
  11. namespace sio
  12. {
  13. using namespace std;
  14. class packet
  15. {
  16. public:
  17. enum frame_type
  18. {
  19. frame_open = 0,
  20. frame_close = 1,
  21. frame_ping = 2,
  22. frame_pong = 3,
  23. frame_message = 4,
  24. frame_upgrade = 5,
  25. frame_noop = 6
  26. };
  27. enum type
  28. {
  29. type_min = 0,
  30. type_connect = 0,
  31. type_disconnect = 1,
  32. type_event = 2,
  33. type_ack = 3,
  34. type_error = 4,
  35. type_binary_event = 5,
  36. type_binary_ack = 6,
  37. type_max = 6,
  38. type_undetermined = 0x10 //undetermined mask bit
  39. };
  40. private:
  41. frame_type _frame;
  42. int _type;
  43. string _nsp;
  44. int _pack_id;
  45. message::ptr _message;
  46. unsigned _pending_buffers;
  47. vector<shared_ptr<const string> > _buffers;
  48. public:
  49. packet(string const& nsp,message::ptr const& msg,int pack_id = -1,bool isAck = false);//message type constructor.
  50. packet(frame_type frame);
  51. packet(type type,string const& nsp= string(),message::ptr const& msg = message::ptr());//other message types constructor.
  52. //empty constructor for parse.
  53. packet();
  54. frame_type get_frame() const;
  55. type get_type() const;
  56. bool parse(string const& payload_ptr);//return true if need to parse buffer.
  57. bool parse_buffer(string const& buf_payload);
  58. bool accept(string& payload_ptr, vector<shared_ptr<const string> >&buffers); //return true if has binary buffers.
  59. string const& get_nsp() const;
  60. message::ptr const& get_message() const;
  61. unsigned get_pack_id() const;
  62. static bool is_message(string const& payload_ptr);
  63. static bool is_text_message(string const& payload_ptr);
  64. static bool is_binary_message(string const& payload_ptr);
  65. };
  66. class packet_manager
  67. {
  68. public:
  69. typedef function<void (bool,shared_ptr<const string> const&)> encode_callback_function;
  70. typedef function<void (packet const&)> decode_callback_function;
  71. void set_decode_callback(decode_callback_function const& decode_callback);
  72. void set_encode_callback(encode_callback_function const& encode_callback);
  73. void encode(packet& pack,encode_callback_function const& override_encode_callback = encode_callback_function()) const;
  74. void put_payload(string const& payload);
  75. void reset();
  76. private:
  77. decode_callback_function m_decode_callback;
  78. encode_callback_function m_encode_callback;
  79. std::unique_ptr<packet> m_partial_packet;
  80. };
  81. }
  82. #endif