Debian Codegraph @

third-party/libhv-v1.3.3/examples/kcptun/smux/smux.h

raw
1 #ifndef HV_SMUX_H_
2 #define HV_SMUX_H_
3
4 /*
5 * smux: Simple MUltipleXing used by kcptun
6 * @see: https://github.com/xtaci/smux
7 *
8 */
9
10 #include <map>
11
12 #include "hplatform.h"
13 #include "hbase.h"
14 #include "hbuf.h"
15 #include "hloop.h"
16
17 typedef enum {
18 // v1
19 SMUX_CMD_SYN = 0, // stream open
20 SMUX_CMD_FIN = 1, // stream close
21 SMUX_CMD_PSH = 2, // data push
22 SMUX_CMD_NOP = 3, // no operation
23 // v2
24 SMUX_CMD_UPD = 4, // update
25 } smux_cmd_e;
26
27 typedef struct {
28 uint8_t version;
29 uint8_t cmd;
30 uint16_t length;
31 uint32_t sid;
32 } smux_head_t;
33
34 #define SMUX_HEAD_LENGTH 8
35
36 typedef struct {
37 smux_head_t head;
38 const char* data;
39 } smux_frame_t;
40
41 static inline unsigned int smux_package_length(const smux_head_t* head) {
42 return SMUX_HEAD_LENGTH + head->length;
43 }
44
45 static inline void smux_head_init(smux_head_t* head) {
46 head->version = 1;
47 head->cmd = (uint8_t)SMUX_CMD_PSH;
48 head->length = 0;
49 head->sid = 0;
50 }
51
52 static inline void smux_frame_init(smux_frame_t* frame) {
53 smux_head_init(&frame->head);
54 frame->data = NULL;
55 }
56
57 // @retval >0 package_length, <0 error
58 int smux_frame_pack(const smux_frame_t* frame, void* buf, int len);
59 // @retval >0 package_length, <0 error
60 int smux_frame_unpack(smux_frame_t* frame, const void* buf, int len);
61
62 typedef struct smux_config_s {
63 int version;
64 int keepalive_interval;
65 int keepalive_timeout;
66 int max_frame_size;
67
68 smux_config_s() {
69 version = 1;
70 keepalive_interval = 10000;
71 keepalive_timeout = 30000;
72 max_frame_size = 1024;
73 }
74 } smux_config_t;
75
76 typedef struct {
77 uint32_t stream_id;
78 smux_frame_t frame;
79 hbuf_t rbuf;
80 hbuf_t wbuf;
81 hio_t* io;
82 htimer_t* timer;
83 } smux_stream_t;
84
85 // @retval >0 package_length, <0 error, data => wbuf
86 static inline int smux_stream_output(smux_stream_t* stream, smux_frame_t* frame) {
87 return smux_frame_pack(frame, stream->wbuf.base, stream->wbuf.len);
88 }
89
90 static inline int smux_stream_output(smux_stream_t* stream, smux_cmd_e cmd) {
91 smux_frame_t frame;
92 smux_frame_init(&frame);
93 frame.head.sid = stream->stream_id;
94 frame.head.cmd = (uint8_t)cmd;
95 return smux_frame_pack(&frame, stream->wbuf.base, stream->wbuf.len);
96 }
97
98 // @retval >0 package_length, <0 error, data => frame
99 static inline int smux_stream_input(smux_stream_t* stream, const void* buf, int len) {
100 return smux_frame_unpack(&stream->frame, buf, len);
101 }
102
103 typedef struct {
104 uint32_t next_stream_id;
105 // stream_id => smux_stream_t
106 std::map<uint32_t, smux_stream_t*> streams;
107 } smux_session_t;
108
109 static inline smux_stream_t* smux_session_open_stream(smux_session_t* session, uint32_t stream_id = 0, hio_t* io = NULL) {
110 smux_stream_t* stream = NULL;
111 HV_ALLOC_SIZEOF(stream);
112 if (stream_id == 0) {
113 session->next_stream_id += 2;
114 stream_id = session->next_stream_id;
115 }
116 stream->stream_id = stream_id;
117 session->streams[stream_id] = stream;
118 stream->io = io;
119 return stream;
120 }
121
122 static inline smux_stream_t* smux_session_get_stream(smux_session_t* session, uint32_t stream_id) {
123 auto iter = session->streams.find(stream_id);
124 if (iter != session->streams.end()) {
125 return iter->second;
126 }
127 return NULL;
128 }
129
130 static inline void smux_session_close_stream(smux_session_t* session, uint32_t stream_id) {
131 auto iter = session->streams.find(stream_id);
132 if (iter != session->streams.end()) {
133 HV_FREE(iter->second);
134 session->streams.erase(iter);
135 }
136 }
137
138 #endif // HV_SMUX_H_