MetaSoul

Loading...
Searching...
No Matches
sio_packet.h
Go to the documentation of this file.
1// Modifications Copyright 2022-current EmoShape. All Rights Reserved
2//
3// sio_packet.h
4//
5// Created by Melo Yao on 3/19/15.
6//
7
8#ifndef EMOSHAPEIO_PACKET_H
9#define EMOSHAPEIO_PACKET_H
10#include <sstream>
11#include <functional>
12
13#include "EmoShapeSubSystem/EmoShapeIOLib/Public/sio_message.h"
14
15 namespace emoshapeio
16{
17 using namespace std;
18
19 class packet
20 {
21 public:
22 enum frame_type
23 {
24 frame_open = 0,
25 frame_close = 1,
26 frame_ping = 2,
27 frame_pong = 3,
28 frame_message = 4,
29 frame_upgrade = 5,
30 frame_noop = 6
31 };
32
33 enum type
34 {
35 type_min = 0,
36 type_connect = 0,
37 type_disconnect = 1,
38 type_event = 2,
39 type_ack = 3,
40 type_error = 4,
41 type_binary_event = 5,
42 type_binary_ack = 6,
43 type_max = 6,
44 type_undetermined = 0x10 //undetermined mask bit
45 };
46 private:
47 frame_type _frame;
48 int _type;
49 string _nsp;
50 int _pack_id;
51 message::ptr _message;
52 unsigned _pending_buffers;
53 vector<shared_ptr<const string> > _buffers;
54 public:
55 packet(string const& nsp, message::ptr const& msg, int pack_id = -1, bool isAck = false);//message type constructor.
56
57 packet(frame_type frame);
58
59 packet(type type, string const& nsp = string(), message::ptr const& msg = message::ptr());//other message types constructor.
60 //empty constructor for parse.
61 packet();
62
63 frame_type get_frame() const;
64
65 type get_type() const;
66
67 bool parse(string const& payload_ptr);//return true if need to parse buffer.
68
69 bool parse_buffer(string const& buf_payload);
70
71 bool accept(string& payload_ptr, vector<shared_ptr<const string> >& buffers); //return true if has binary buffers.
72
73 string const& get_nsp() const;
74
75 message::ptr const& get_message() const;
76
77 unsigned get_pack_id() const;
78
79 static bool is_message(string const& payload_ptr);
80 static bool is_text_message(string const& payload_ptr);
81 static bool is_binary_message(string const& payload_ptr);
82 };
83
84 class packet_manager
85 {
86 public:
87 typedef function<void(bool, shared_ptr<const string> const&)> encode_callback_function;
88 typedef function<void(packet const&)> decode_callback_function;
89
90 void set_decode_callback(decode_callback_function const& decode_callback);
91
92 void set_encode_callback(encode_callback_function const& encode_callback);
93
94 void encode(packet& pack, encode_callback_function const& override_encode_callback = encode_callback_function()) const;
95
96 void put_payload(string const& payload);
97
98 void reset();
99
100 private:
101 decode_callback_function m_decode_callback;
102
103 encode_callback_function m_encode_callback;
104
105 std::unique_ptr<packet> m_partial_packet;
106 };
107}
108#endif
emoshapeio::client
Definition sio_client.h:13
emoshapeio::message::ptr
std::shared_ptr< message > ptr
Definition sio_message.h:37
emoshapeio::packet_manager
Definition sio_packet.h:85
emoshapeio::packet_manager::set_decode_callback
void set_decode_callback(decode_callback_function const &decode_callback)
Definition sio_packet.cpp:444
emoshapeio::packet_manager::reset
void reset()
Definition sio_packet.cpp:454
emoshapeio::packet_manager::set_encode_callback
void set_encode_callback(encode_callback_function const &encode_callback)
Definition sio_packet.cpp:449
emoshapeio::packet_manager::encode
void encode(packet &pack, encode_callback_function const &override_encode_callback=encode_callback_function()) const
Definition sio_packet.cpp:459
emoshapeio::packet_manager::put_payload
void put_payload(string const &payload)
Definition sio_packet.cpp:491
emoshapeio::packet_manager::encode_callback_function
function< void(bool, shared_ptr< const string > const &) encode_callback_function)
Definition sio_packet.h:87
emoshapeio::packet_manager::decode_callback_function
function< void(packet const &) decode_callback_function)
Definition sio_packet.h:88
emoshapeio::packet
Definition sio_packet.h:20
emoshapeio::packet::packet
packet()
Definition sio_packet.cpp:245
emoshapeio::packet::get_frame
frame_type get_frame() const
Definition sio_packet.cpp:417
emoshapeio::packet::parse_buffer
bool parse_buffer(string const &buf_payload)
Definition sio_packet.cpp:269
emoshapeio::packet::parse
bool parse(string const &payload_ptr)
Definition sio_packet.cpp:289
emoshapeio::packet::accept
bool accept(string &payload_ptr, vector< shared_ptr< const string > > &buffers)
Definition sio_packet.cpp:364
emoshapeio::packet::get_nsp
string const & get_nsp() const
Definition sio_packet.cpp:428
emoshapeio::packet::frame_type
frame_type
Definition sio_packet.h:23
emoshapeio::packet::frame_upgrade
@ frame_upgrade
Definition sio_packet.h:29
emoshapeio::packet::frame_close
@ frame_close
Definition sio_packet.h:25
emoshapeio::packet::frame_message
@ frame_message
Definition sio_packet.h:28
emoshapeio::packet::frame_pong
@ frame_pong
Definition sio_packet.h:27
emoshapeio::packet::frame_ping
@ frame_ping
Definition sio_packet.h:26
emoshapeio::packet::frame_noop
@ frame_noop
Definition sio_packet.h:30
emoshapeio::packet::frame_open
@ frame_open
Definition sio_packet.h:24
emoshapeio::packet::get_message
message::ptr const & get_message() const
Definition sio_packet.cpp:433
emoshapeio::packet::get_pack_id
unsigned get_pack_id() const
Definition sio_packet.cpp:438
emoshapeio::packet::get_type
type get_type() const
Definition sio_packet.cpp:422
emoshapeio::packet::is_text_message
static bool is_text_message(string const &payload_ptr)
Definition sio_packet.cpp:259
emoshapeio::packet::is_message
static bool is_message(string const &payload_ptr)
Definition sio_packet.cpp:264
emoshapeio::packet::is_binary_message
static bool is_binary_message(string const &payload_ptr)
Definition sio_packet.cpp:254
emoshapeio::packet::type
type
Definition sio_packet.h:34
emoshapeio::packet::type_connect
@ type_connect
Definition sio_packet.h:36
emoshapeio::packet::type_binary_ack
@ type_binary_ack
Definition sio_packet.h:42
emoshapeio::packet::type_min
@ type_min
Definition sio_packet.h:35
emoshapeio::packet::type_event
@ type_event
Definition sio_packet.h:38
emoshapeio::packet::type_ack
@ type_ack
Definition sio_packet.h:39
emoshapeio::packet::type_disconnect
@ type_disconnect
Definition sio_packet.h:37
emoshapeio::packet::type_max
@ type_max
Definition sio_packet.h:43
emoshapeio::packet::type_error
@ type_error
Definition sio_packet.h:40
emoshapeio::packet::type_binary_event
@ type_binary_event
Definition sio_packet.h:41
emoshapeio::packet::type_undetermined
@ type_undetermined
Definition sio_packet.h:44
emoshapeio
Definition sio_client_impl.cpp:51
sio_message.h