Skip to content

Commit ee1ac3e

Browse files
committed
refactor: improve code formatting and add documentation for TCP MUX structures and functions
Signed-off-by: Dengfeng Liu <liudf0716@gmail.com>
1 parent b238a1b commit ee1ac3e

File tree

1 file changed

+245
-68
lines changed

1 file changed

+245
-68
lines changed

tcpmux.h

+245-68
Original file line numberDiff line numberDiff line change
@@ -24,140 +24,317 @@
2424
@author Copyright (C) 2016 Dengfeng Liu <liu_df@qq.com>
2525
*/
2626

27-
#ifndef __TCP_MUX__
28-
#define __TCP_MUX__
27+
#ifndef TCPMUX_H
28+
#define TCPMUX_H
2929

3030
#include "uthash.h"
31+
#include <stdint.h>
3132

32-
#define MAX_STREAM_WINDOW_SIZE 256*1024
33-
#define RBUF_SIZE 32*1024
34-
#define WBUF_SIZE 32*1024
35-
33+
#define MAX_STREAM_WINDOW_SIZE (256 * 1024)
34+
#define RBUF_SIZE (32 * 1024)
35+
#define WBUF_SIZE (32 * 1024)
3636

3737
struct ring_buffer {
38-
uint32_t cur;
39-
uint32_t end;
40-
uint32_t sz;
41-
uint8_t data[RBUF_SIZE];
38+
uint32_t cur;
39+
uint32_t end;
40+
uint32_t sz;
41+
uint8_t data[RBUF_SIZE];
4242
};
4343

4444
enum go_away_type {
45-
NORMAL,
46-
PROTO_ERR,
47-
INTERNAL_ERR,
45+
NORMAL,
46+
PROTO_ERR,
47+
INTERNAL_ERR,
4848
};
4949

5050
enum tcp_mux_type {
51-
DATA,
52-
WINDOW_UPDATE,
53-
PING,
54-
GO_AWAY,
51+
DATA,
52+
WINDOW_UPDATE,
53+
PING,
54+
GO_AWAY,
5555
};
5656

5757
struct tcp_mux_type_desc {
58-
enum tcp_mux_type type;
59-
char *desc;
58+
enum tcp_mux_type type;
59+
char *desc;
6060
};
6161

6262
enum tcp_mux_flag {
63-
ZERO,
64-
SYN,
65-
ACK = 1<<1,
66-
FIN = 1<<2,
67-
RST = 1<<3,
63+
ZERO,
64+
SYN,
65+
ACK = 1 << 1,
66+
FIN = 1 << 2,
67+
RST = 1 << 3,
6868
};
6969

7070
struct __attribute__((__packed__)) tcp_mux_header {
71-
uint8_t version;
72-
uint8_t type;
73-
uint16_t flags;
74-
uint32_t stream_id;
75-
uint32_t length;
71+
uint8_t version;
72+
uint8_t type;
73+
uint16_t flags;
74+
uint32_t stream_id;
75+
uint32_t length;
7676
};
7777

7878
struct tcp_mux_flag_desc {
79-
enum tcp_mux_flag flag;
80-
char *desc;
79+
enum tcp_mux_flag flag;
80+
char *desc;
8181
};
8282

8383
enum tcp_mux_state {
84-
INIT = 0,
85-
SYN_SEND,
86-
SYN_RECEIVED,
87-
ESTABLISHED,
88-
LOCAL_CLOSE,
89-
REMOTE_CLOSE,
90-
CLOSED,
91-
RESET
84+
INIT = 0,
85+
SYN_SEND,
86+
SYN_RECEIVED,
87+
ESTABLISHED,
88+
LOCAL_CLOSE,
89+
REMOTE_CLOSE,
90+
CLOSED,
91+
RESET
9292
};
9393

9494
struct tmux_stream {
95-
uint32_t id;
96-
uint32_t recv_window;
97-
uint32_t send_window;
98-
enum tcp_mux_state state;
99-
struct ring_buffer tx_ring;
100-
struct ring_buffer rx_ring;
101-
102-
// private arguments
103-
UT_hash_handle hh;
95+
uint32_t id;
96+
uint32_t recv_window;
97+
uint32_t send_window;
98+
enum tcp_mux_state state;
99+
struct ring_buffer tx_ring;
100+
struct ring_buffer rx_ring;
101+
102+
// private arguments
103+
UT_hash_handle hh;
104104
};
105105

106106
typedef void (*handle_data_fn_t)(uint8_t *, int, void *);
107107

108-
void init_tmux_stream(struct tmux_stream *stream, uint32_t id, enum tcp_mux_state state);
109-
108+
/**
109+
* @brief Initializes TCP MUX stream.
110+
*
111+
* @param stream Pointer to the tmux_stream structure.
112+
* @param id Stream ID of the TCP MUX message.
113+
* @param state State of the TCP MUX message.
114+
*/
115+
void init_tmux_stream(struct tmux_stream *stream, uint32_t id,
116+
enum tcp_mux_state state);
117+
118+
/**
119+
* @brief Validates a TCP MUX protocol.
120+
*
121+
* @param tmux_hdr Pointer to the TCP MUX header.
122+
* @return Status of the operation.
123+
*/
110124
int validate_tcp_mux_protocol(struct tcp_mux_header *tmux_hdr);
111125

112-
void send_window_update(struct bufferevent *bout, struct tmux_stream *stream, uint32_t length);
113-
126+
/**
127+
* @brief Sends window update message.
128+
*
129+
* @param bout The bufferevent to send the window update message.
130+
* @param stream Pointer to the tmux_stream structure.
131+
* @param length Length of the window update message.
132+
*/
133+
void send_window_update(struct bufferevent *bout, struct tmux_stream *stream,
134+
uint32_t length);
135+
136+
/**
137+
* @brief Sends a TCP MUX window update message with SYN flag.
138+
*
139+
* @param bout The bufferevent to send the window update message.
140+
* @param stream_id Stream ID of the TCP MUX message.
141+
*/
114142
void tcp_mux_send_win_update_syn(struct bufferevent *bout, uint32_t stream_id);
115143

116-
void tcp_mux_send_win_update_ack(struct bufferevent *bout, uint32_t stream_id, uint32_t delta);
117-
144+
/**
145+
* @brief Sends a TCP MUX window update message with ACK flag.
146+
*
147+
* @param bout The bufferevent to send the window update message.
148+
* @param stream_id Stream ID of the TCP MUX message.
149+
* @param delta Delta of the TCP MUX message.
150+
*/
151+
void tcp_mux_send_win_update_ack(struct bufferevent *bout, uint32_t stream_id,
152+
uint32_t delta);
153+
154+
/**
155+
* @brief Sends a TCP MUX window update message with FIN flag.
156+
*
157+
* @param bout The bufferevent to send the window update message.
158+
* @param stream_id Stream ID of the TCP MUX message.
159+
*/
118160
void tcp_mux_send_win_update_fin(struct bufferevent *bout, uint32_t stream_id);
119161

162+
/**
163+
* @brief Sends a TCP MUX window update message with RST flag.
164+
*
165+
* @param bout The bufferevent to send the window update message.
166+
* @param stream_id Stream ID of the TCP MUX message.
167+
*/
120168
void tcp_mux_send_win_update_rst(struct bufferevent *bout, uint32_t stream_id);
121169

122-
void tcp_mux_send_data(struct bufferevent *bout, uint16_t flags, uint32_t stream_id, uint32_t length);
123-
170+
/**
171+
* @brief Sends a TCP MUX data message.
172+
*
173+
* @param bout The bufferevent to send the data message.
174+
* @param flags Flags of the TCP MUX message.
175+
* @param stream_id Stream ID of the TCP MUX message.
176+
* @param length Length of the TCP MUX message.
177+
*/
178+
void tcp_mux_send_data(struct bufferevent *bout, uint16_t flags,
179+
uint32_t stream_id, uint32_t length);
180+
181+
/**
182+
* @brief Sends a TCP MUX ping message.
183+
*
184+
* @param bout The bufferevent to send the ping message.
185+
* @param ping_id The ping ID to send.
186+
*/
124187
void tcp_mux_send_ping(struct bufferevent *bout, uint32_t ping_id);
125188

189+
/**
190+
* @brief get the next session ID.
191+
*/
126192
uint32_t get_next_session_id();
127193

128-
void tcp_mux_encode(enum tcp_mux_type type, enum tcp_mux_flag flags,
129-
uint32_t stream_id, uint32_t length, struct tcp_mux_header *tmux_hdr);
130-
194+
/**
195+
* @brief Encodes a TCP MUX header.
196+
*
197+
* @param type Type of the TCP MUX message.
198+
* @param flags Flags of the TCP MUX message.
199+
* @param stream_id Stream ID of the TCP MUX message.
200+
* @param length Length of the TCP MUX message.
201+
* @param tmux_hdr Pointer to the TCP MUX header.
202+
*/
203+
void tcp_mux_encode(enum tcp_mux_type type, enum tcp_mux_flag flags,
204+
uint32_t stream_id, uint32_t length,
205+
struct tcp_mux_header *tmux_hdr);
206+
207+
/**
208+
* @brief Handles a TCP MUX data stream.
209+
*
210+
* @param tmux_hdr Pointer to the TCP MUX header.
211+
* @param fn Function pointer to handle the data.
212+
* @return Status of the operation.
213+
*/
131214
int handle_tcp_mux_stream(struct tcp_mux_header *tmux_hdr, handle_data_fn_t fn);
132215

216+
/**
217+
* @brief Handles a TCP MUX ping message.
218+
*
219+
* @param tmux_hdr Pointer to the TCP MUX header.
220+
*/
133221
void handle_tcp_mux_ping(struct tcp_mux_header *tmux_hdr);
134222

223+
/**
224+
* @brief Handles a TCP MUX go away message.
225+
*
226+
* @param tmux_hdr Pointer to the TCP MUX header.
227+
*/
135228
void handle_tcp_mux_go_away(struct tcp_mux_header *tmux_hdr);
136229

137-
uint32_t tmux_stream_write(struct bufferevent *bev, uint8_t *data, uint32_t length, struct tmux_stream *stream);
138-
139-
uint32_t tmux_stream_read(struct bufferevent *bev, struct tmux_stream *stream, uint32_t len);
140-
230+
/**
231+
* @brief Writes data to a tmux stream.
232+
*
233+
* @param bev The bufferevent to write data to.
234+
* @param data Pointer to the data buffer to write.
235+
* @param length Length of the data to write.
236+
* @param stream Pointer to the tmux_stream structure.
237+
* @return Number of bytes written.
238+
*/
239+
uint32_t tmux_stream_write(struct bufferevent *bev, uint8_t *data,
240+
uint32_t length, struct tmux_stream *stream);
241+
242+
/**
243+
* @brief Reads data from a tmux stream.
244+
*
245+
* @param bev The bufferevent to read data from.
246+
* @param stream Pointer to the tmux_stream structure.
247+
* @param len Maximum number of bytes to read.
248+
* @return Number of bytes read.
249+
*/
250+
uint32_t tmux_stream_read(struct bufferevent *bev, struct tmux_stream *stream,
251+
uint32_t len);
252+
253+
/**
254+
* @brief Resets the session ID counter.
255+
*/
141256
void reset_session_id();
142257

258+
/**
259+
* @brief Retrieves the current tmux stream.
260+
*
261+
* @return Pointer to the current tmux_stream.
262+
*/
143263
struct tmux_stream *get_cur_stream();
144264

265+
/**
266+
* @brief Sets the current tmux stream.
267+
*
268+
* @param stream Pointer to the tmux_stream to set as current.
269+
*/
145270
void set_cur_stream(struct tmux_stream *stream);
146271

272+
/**
273+
* @brief Adds a tmux stream to the stream list.
274+
*
275+
* @param stream Pointer to the tmux_stream to add.
276+
*/
147277
void add_stream(struct tmux_stream *stream);
148278

279+
/**
280+
* @brief Deletes a tmux stream by its ID.
281+
*
282+
* @param stream_id ID of the tmux_stream to delete.
283+
*/
149284
void del_stream(uint32_t stream_id);
150285

286+
/**
287+
* @brief Clears all tmux streams from the stream list.
288+
*/
151289
void clear_stream();
152290

153-
struct tmux_stream* get_stream_by_id(uint32_t id);
154-
291+
/**
292+
* @brief Retrieves a tmux stream by its ID.
293+
*
294+
* @param id ID of the tmux_stream to retrieve.
295+
* @return Pointer to the tmux_stream if found, NULL otherwise.
296+
*/
297+
struct tmux_stream *get_stream_by_id(uint32_t id);
298+
299+
/**
300+
* @brief Closes a tmux stream.
301+
*
302+
* @param bev The bufferevent associated with the stream.
303+
* @param stream Pointer to the tmux_stream to close.
304+
* @return 0 on success, negative value on error.
305+
*/
155306
int tmux_stream_close(struct bufferevent *bout, struct tmux_stream *stream);
156307

308+
/**
309+
* @brief Pops data from the receive ring buffer.
310+
*
311+
* @param ring Pointer to the ring_buffer structure.
312+
* @param data Buffer to store the popped data.
313+
* @param len Maximum number of bytes to pop.
314+
* @return Number of bytes popped.
315+
*/
157316
int rx_ring_buffer_pop(struct ring_buffer *ring, uint8_t *data, uint32_t len);
158317

159-
uint32_t rx_ring_buffer_read(struct bufferevent *bev, struct ring_buffer *ring, uint32_t len);
160-
161-
uint32_t tx_ring_buffer_write(struct bufferevent *bev, struct ring_buffer *ring, uint32_t len);
318+
/**
319+
* @brief Reads data from a bufferevent into the receive ring buffer.
320+
*
321+
* @param bev The bufferevent to read data from.
322+
* @param ring Pointer to the ring_buffer to store data.
323+
* @param len Maximum number of bytes to read.
324+
* @return Number of bytes read.
325+
*/
326+
uint32_t rx_ring_buffer_read(struct bufferevent *bev, struct ring_buffer *ring,
327+
uint32_t len);
328+
329+
/**
330+
* @brief Writes data from the transmit ring buffer to a bufferevent.
331+
*
332+
* @param bev The bufferevent to write data to.
333+
* @param ring Pointer to the ring_buffer containing data to send.
334+
* @param len Maximum number of bytes to write.
335+
* @return Number of bytes written.
336+
*/
337+
uint32_t tx_ring_buffer_write(struct bufferevent *bev, struct ring_buffer *ring,
338+
uint32_t len);
162339

163340
#endif

0 commit comments

Comments
 (0)