xref: /f-stack/app/micro_thread/mt_api.h (revision a02c88d6)
1 
2 /**
3  * Tencent is pleased to support the open source community by making MSEC available.
4  *
5  * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.
6  *
7  * Licensed under the GNU General Public License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License. You may
9  * obtain a copy of the License at
10  *
11  *     https://opensource.org/licenses/GPL-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software distributed under the
14  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
15  * either express or implied. See the License for the specific language governing permissions
16  * and limitations under the License.
17  */
18 
19 
20 /**
21  *  @filename mt_api.h
22  *  @info  ΢�̷߳�װϵͳapi, ͬ������΢�߳�API��ʵ���첽����
23  */
24 
25 #ifndef __MT_API_H__
26 #define __MT_API_H__
27 
28 #include <netinet/in.h>
29 #include <vector>
30 
31 using std::vector;
32 
33 namespace NS_MICRO_THREAD {
34 
35 /******************************************************************************/
36 /*  ΢�߳��û��ӿڶ���: UDP�������շ��ӿ�                                     */
37 /******************************************************************************/
38 
39 /**
40  * @brief ��������˿ڵ�socket�շ��ӿ�, ��socket������������, ҵ������֤������
41  *        [ע��] UDP����buff, ���Ƽ�static����, ��һ�����յ��������Ĵ���[��Ҫ]
42  * @param dst -�����͵�Ŀ�ĵ�ַ
43  * @param pkg -�������װ�İ���
44  * @param len -�������װ�İ��峤��
45  * @param rcv_buf -����Ӧ�����buff
46  * @param buf_size -modify-����Ӧ�����buff��С, �ɹ�����ʱ, �޸�ΪӦ�������
47  * @param timeout -��ʱʱ��, ��λms
48  * @return  0 �ɹ�, -1 ��socketʧ��, -2 ��������ʧ��, -3 ����Ӧ��ʧ��, �ɴ�ӡerrno
49  */
50 int mt_udpsendrcv(struct sockaddr_in* dst, void* pkg, int len, void* rcv_buf, int& buf_size, int timeout);
51 
52 
53 /******************************************************************************/
54 /*  ΢�߳��û��ӿڶ���: TCP���ӳ��շ��ӿ�                                     */
55 /******************************************************************************/
56 
57 /**
58  * @brief TCP��ⱨ���Ƿ���������Ļص���������
59  * @param buf ���ı��滺����
60  * @param len �Ѿ����յij���
61  * @return >0 ʵ�ʵı��ij���; 0 ����Ҫ�ȴ�����; <0 �����쳣
62  */
63 typedef int (*MtFuncTcpMsgLen)(void* buf, int len);
64 
65 /**
66  * @brief TCP��������ӳصķ�ʽ����IP/PORT����, ���ӱ���Ĭ��10����
67  *        [ע��] tcp���շ���buff, ��������static����, ����������Ĵ��� [��Ҫ]
68  * @param dst -�����͵�Ŀ�ĵ�ַ
69  * @param pkg -�������װ�İ���
70  * @param len -�������װ�İ��峤��
71  * @param rcv_buf -����Ӧ�����buff
72  * @param buf_size -modify-����Ӧ�����buff��С, �ɹ�����ʱ, �޸�ΪӦ�������
73  * @param timeout -��ʱʱ��, ��λms
74  * @param check_func -��ⱨ���Ƿ�ɹ����ﺯ��
75  * @return  0 �ɹ�, -1 ��socketʧ��, -2 ��������ʧ��, -3 ����Ӧ��ʧ��,
76  *          -4 ����ʧ��, -5 ��ⱨ��ʧ��, -6 ���տռ䲻��, -7 ��������ر����ӣ�-10 ������Ч
77  */
78 int mt_tcpsendrcv(struct sockaddr_in* dst, void* pkg, int len, void* rcv_buf, int& buf_size,
79                   int timeout, MtFuncTcpMsgLen chek_func);
80 
81 
82 enum MT_TCP_CONN_TYPE
83 {
84     MT_TCP_SHORT         = 1, /// ������
85     MT_TCP_LONG          = 2, /// ������
86     MT_TCP_SHORT_SNDONLY = 3, /// ������ֻ��
87     MT_TCP_LONG_SNDONLY  = 4, /// ������ֻ��
88     MT_TCP_BUTT
89 };
90 
91 /**
92  * @brief TCP�շ��ӿڣ�����ѡ���˱������ӻ��߶�����
93  *        [ע��] tcp���շ���buff, ��������static����, ����������Ĵ��� [��Ҫ]
94  * @param dst -�����͵�Ŀ�ĵ�ַ
95  * @param pkg -�������װ�İ���
96  * @param len -�������װ�İ��峤��
97  * @param rcv_buf -����Ӧ�����buff��ֻ�����տ�������ΪNULL
98  * @param buf_size -modify-����Ӧ�����buff��С, �ɹ�����ʱ, �޸�ΪӦ������ȣ�ֻ�����գ�����ΪNULL
99  * @param timeout -��ʱʱ��, ��λms
100  * @param check_func -��ⱨ���Ƿ�ɹ����ﺯ��
101  * @param type - ��������
102  *               MT_TCP_SHORT: һ��һ�������ӣ�
103  *               MT_TCP_LONG : һ��һ�ճ����ӣ�
104  *               MT_TCP_LONG_SNDONLY : ֻ�����ճ����ӣ�
105  *               MT_TCP_SHORT_SNDONLY: ֻ�����ն����ӣ�
106  * @return  0 �ɹ�, -1 ��socketʧ��, -2 ��������ʧ��, -3 ����Ӧ��ʧ��,
107  *          -4 ����ʧ��, -5 ��ⱨ��ʧ��, -6 ���տռ䲻��, -7 ��������ر�����, -10 ������Ч
108  */
109 int mt_tcpsendrcv_ex(struct sockaddr_in* dst, void* pkg, int len, void* rcv_buf, int* buf_size,
110                      int timeout, MtFuncTcpMsgLen func, MT_TCP_CONN_TYPE type = MT_TCP_LONG);
111 
112  /**
113  * @brief TCP��ⱨ���Ƿ���������Ļص�����
114  * @param buf ���ı��滺����
115  * @param len �Ѿ����յij���
116  * @param closed Զ�˹ر����ӣ�����ͨ�����ӹر���ȷ�����ij��ȵ�ҵ��
117  * @param msg_ctx    �������ĵ������ı�����ͨ����ֵ��ҵ�����ʵ�ֱ��Ľ���״̬���ȣ������ظ����
118  *               ҵ����mt_tcpsendrcv�д���ò�������ܻص�ʱ���ݸ�ҵ���ñ�������Ϊ����߽ṹ�壬ҵ���Զ��塣
119  *               ���Ľ�������������ڱ����У�mt_tcpsendrcv���غ�ֱ��ʹ��
120  * ��ע�⡿ �ûص����������bufָ����ܱ仯�����Ա��Ľ���״̬�����λ����Ϣ��ʹ��ƫ����
121  * @param msg_len_detected �������� ���ҵ��ȷ���˱��ij��ȣ���������Ϊtrue������Ϊfalse������ʱΪfalse��
122  *         ��ע�⡿�����ֵ����true������Ժ�������ֵ��Ϊ���ij��ȣ�>0�������հ������հ����ǰ�����ٵ��ø�MtFuncTcpMsgChecker���������Ľ�����ɺ�����MtFuncTcpMsgChecker�������һ�α��ġ������������հ��������Ч���á�
123  * @return >0 ����Ľ��ջ����С; 0 ����Ҫ�ȴ�����; <0 �����쳣
124  *      ��ע�⡿
125  *            ��������������Է��ش���0:
126  *               1��ҵ��ȷ�����ij���,���ر��ij���
127  *               2��ҵ��ȷ�����ij��ȣ���������buf�Ľ��鳤�ȣ�������ȴ�������buf���ȣ���ܸ��ݸ�ֵrealloc buf��
128  *
129  *           ����ֵΪ0����ʾҵ���޷�ȷ�����ij��ȣ���Ҫ�������ա����buf��������ܽ�realloc buf��buf��С����һ��
130  *           ����ֵС��0����ʾҵ��ȷ�����Ĵ���
131  */
132 typedef int (*MtFuncTcpMsgChecker)(void* buf, int len, bool closed, void* msg_ctx, bool &msg_len_detected);
133 
134 
135 
136 /**
137  * @brief TCP�շ��ӿڣ�����ѡ���˱������ӻ��߶�����
138  *        [ע��] tcp���շ���buff, ��������static����, ����������Ĵ��� [��Ҫ]
139  * @param dst -�����͵�Ŀ�ĵ�ַ
140  * @param pkg -�������װ�İ���
141  * @param len -�������װ�İ��峤��
142  * @param rcv_buf -���������������ο����� keep_rcv_buf��ҵ����Ҫmalloc���ڴ档void* rcv_buf=NULL: ������ܼ���
143  * @param recv_pkg_size -����Ӧ�����buff�ij�ʼ��С����������0ʱ�����ر���ʵ�ʳ��ȡ�
144  * @param timeout -��ʱʱ��, ��λms
145  * @param check_func -��ⱨ���Ƿ�ɹ����ﺯ��
146  * @param msg_ctx -�������ĵ������ı�����
147  *
148  * @param type - ��������
149  *               MT_TCP_SHORT: һ��һ�������ӣ�
150  *               MT_TCP_LONG : һ��һ�ճ����ӣ�
151  *               MT_TCP_LONG_SNDONLY : ֻ�����ճ����ӣ�
152  *               MT_TCP_SHORT_SNDONLY: ֻ�����ն����ӣ�
153  * @param keep_rcv_buf -true,���óɹ�����ܽ�����rcv_buf��ֵ������������ҵ�����ͷŸ�buf���������ڵ��ý���ǰ�ͷŸ�buf����ע�⡿
154  *               ҵ����Ҫ�Լ���msg_ctx�б��������Ϣ�������ͨ��malloc�����ڴ桿  Ĭ�Ͽ���Զ��ͷŸ�buf��ҵ����Ҫ��ctx������Ϣ
155  * @return  0 �ɹ�, -1 ��socketʧ��, -2 ��������ʧ��, -3 ����Ӧ��ʧ��,
156  *          -4 ����ʧ��, -5 ��ⱨ��ʧ��, -6 ���տռ䲻��, -7 ��������ر�����, -10 ������Ч, -11,�������bufʧ��
157  */
158 int mt_tcpsendrcv_ex(struct sockaddr_in* dst, void* pkg, int len, void*& rcv_buf, int& recv_pkg_size,
159                      int timeout, MtFuncTcpMsgChecker check_func, void* msg_ctx=NULL,
160                      MT_TCP_CONN_TYPE type = MT_TCP_LONG, bool keep_rcv_buf=false);
161 
162 /**
163  * @brief TCP��������ӳصķ�ʽ����IP/PORT����, ���ӱ���Ĭ��10����
164  *        [ע��] tcp���շ���buff, ��������static����, ����������Ĵ��� [��Ҫ]
165  * @param dst -�����͵�Ŀ�ĵ�ַ
166  * @param pkg -�������װ�İ���
167  * @param len -�������װ�İ��峤��
168  * @param rcv_buf -���������������ο����� keep_rcv_buf��
169  * @param recv_pkg_size -����Ӧ�����buff�ij�ʼ��С����������0ʱ�����ر���ʵ�ʳ��ȡ�
170  * @param timeout -��ʱʱ��, ��λms
171  * @param check_func -��ⱨ���Ƿ�ɹ����ﺯ��
172  * @param msg_ctx -�������ĵ������ı�����
173  * @param keep_rcv_buf -true,��ܽ�����rcv_buf��ֵ������������ҵ�����ͷŸ�buf���������ڵ��ý���ǰ�ͷŸ�buf����ע�⡿
174  *               ҵ����Ҫ�Լ���msg_ctx�б��������Ϣ�������ͨ��malloc�����ڴ桿
175  * @return  0 �ɹ�, -1 ��socketʧ��, -2 ��������ʧ��, -3 ����Ӧ��ʧ��,
176  *          -4 ����ʧ��, -5 ��ⱨ��ʧ��, -6 ���տռ䲻��, -7 ��������ر�����, -10 ������Ч
177  */
178 int mt_tcpsendrcv(struct sockaddr_in* dst, void* pkg, int len, void*& rcv_buf, int& recv_pkg_size,
179                      int timeout, MtFuncTcpMsgChecker check_func, void* msg_ctx=NULL, bool keep_rcv_buf=false);
180 
181 
182 /******************************************************************************/
183 /*  ΢�߳��û��ӿڶ���: ΢�߳�Task��·����ģ�ͽӿڶ���                        */
184 /******************************************************************************/
185 
186 /**
187  * @brief  ΢�߳������������
188  */
189 class IMtTask
190 {
191 public:
192 
193     /**
194      * @brief  ΢�߳�������Ĵ���������ں���
195      * @return 0 -�ɹ�, < 0 ʧ��
196      */
197     virtual int Process() { return -1; };
198 
199     /**
200      * @brief ����taskִ�н��
201      * @info  ��Process����ֵ
202      */
203     void SetResult(int rc)
204     {
205         _result = rc;
206     }
207 
208     /**
209      * @brief ��ȡtaskִ�н��
210      * @info  ��Process����ֵ
211      */
212     int GetResult(void)
213     {
214         return _result;
215     }
216 
217     /**
218      * @brief ����task����
219      */
220     void SetTaskType(int type)
221     {
222         _type = type;
223     }
224 
225     /**
226      * @brief  ��ȡtask����
227      * @info   ���ҵ���ж���task������ʹ�ø��ֶ����ֲ�ͬ��task����
228      * @return ��ȡtask����
229      */
230     int GetTaskType(void)
231     {
232         return _type;
233     }
234 
235     /**
236      * @brief  ΢�߳�������๹��������
237      */
238     IMtTask() {};
239     virtual ~IMtTask() {};
240 
241 protected:
242 
243     int _type;      // task���ͣ���������task��ҵ������Զ������ͣ�����ӻ���ת��
244     int _result;    // taskִ�н������Process����ֵ
245 };
246 
247 typedef vector<IMtTask*>  IMtTaskList;
248 
249 /**
250  * @brief ��·IO����, Task-fork-waitģʽ�ӿ�
251  * @param req_list -task list ��װ����api��task�б�
252  * @return  0 �ɹ�, -1 �������߳�ʧ��
253  */
254 int mt_exec_all_task(IMtTaskList& req_list);
255 
256 
257 /******************************************************************************/
258 /*  ΢�߳��û��ӿڶ���: ΢�̷߳�װϵͳ�ӿ�                                    */
259 /******************************************************************************/
260 
261 /**
262  * @brief ΢�߳�����sleep�ӿ�, ��λms
263  * @info  ҵ����Ҫ�����ó�CPUʱʹ��
264  */
265 void mt_sleep(int ms);
266 
267 /**
268  * @brief ΢�̻߳�ȡϵͳʱ�䣬��λms
269  */
270 unsigned long long mt_time_ms(void);
271 
272 /******************************************************************************/
273 /*  ΢�߳��û��ӿڶ���: ΢�߳��û�˽�����ݽӿ�                                */
274 /******************************************************************************/
275 
276 /**
277  * @brief ���õ�ǰIMtMsg��˽�б���
278  * @info  ֻ����ָ�룬�ڴ���Ҫҵ�����
279  */
280 void mt_set_msg_private(void *data);
281 
282 /**
283  * @brief  ��ȡ��ǰIMtMsg��˽�б���
284  * @return ˽�б���ָ��
285  */
286 void* mt_get_msg_private();
287 
288 
289 /******************************************************************************/
290 /*  ΢�߳��û��ӿڶ���: ΢�̷߳�װϵͳ�ӿ�(���Ƽ�ʹ��)                        */
291 /******************************************************************************/
292 
293 /**
294  * @brief  ΢�߳̿�ܳ�ʼ��
295  * @info   ҵ��ʹ��spp������΢�̣߳���Ҫ���øú�����ʼ����ܣ�
296  *         ʹ��spp��ֱ�ӵ���SyncFrame�Ŀ�ܳ�ʼ����������
297  * @return false:��ʼ��ʧ��  true:��ʼ���ɹ�
298  */
299 bool mt_init_frame(int argc=0, char * const argv[]=NULL);
300 
301 /**
302  * @brief ����΢�̶߳���ջ�ռ��С
303  * @info  �DZ������ã�Ĭ�ϴ�СΪ128K
304  */
305 void mt_set_stack_size(unsigned int bytes);
306 
307 /**
308  * @brief ΢�̰߳�����ϵͳIO���� recvfrom
309  * @param fd ϵͳsocket��Ϣ
310  * @param buf ������Ϣ������ָ��
311  * @param len ������Ϣ����������
312  * @param from ��Դ��ַ��ָ��
313  * @param fromlen ��Դ��ַ�Ľṹ����
314  * @param timeout ��ȴ�ʱ��, ����
315  * @return >0 �ɹ����ճ���, <0 ʧ��
316  */
317 int mt_recvfrom(int fd, void *buf, int len, int flags, struct sockaddr *from, socklen_t *fromlen, int timeout);
318 
319 /**
320  * @brief ΢�̰߳�����ϵͳIO���� sendto
321  * @param fd ϵͳsocket��Ϣ
322  * @param msg �����͵���Ϣָ��
323  * @param len �����͵���Ϣ����
324  * @param to Ŀ�ĵ�ַ��ָ��
325  * @param tolen Ŀ�ĵ�ַ�Ľṹ����
326  * @param timeout ��ȴ�ʱ��, ����
327  * @return >0 �ɹ����ͳ���, <0 ʧ��
328  */
329 int mt_sendto(int fd, const void *msg, int len, int flags, const struct sockaddr *to, int tolen, int timeout);
330 
331 
332 /**
333  * @brief ΢�̰߳�����ϵͳIO���� connect
334  * @param fd ϵͳsocket��Ϣ
335  * @param addr ָ��server��Ŀ�ĵ�ַ
336  * @param addrlen ��ַ�ij���
337  * @param timeout ��ȴ�ʱ��, ����
338  * @return >0 �ɹ����ͳ���, <0 ʧ��
339  */
340 int mt_connect(int fd, const struct sockaddr *addr, int addrlen, int timeout);
341 
342 /**
343  * @brief ΢�̰߳�����ϵͳIO���� accept
344  * @param fd �����׽���
345  * @param addr �ͻ��˵�ַ
346  * @param addrlen ��ַ�ij���
347  * @param timeout ��ȴ�ʱ��, ����
348  * @return >=0 accept��socket������, <0 ʧ��
349  */
350 int mt_accept(int fd, struct sockaddr *addr, socklen_t *addrlen, int timeout);
351 
352 /**
353  * @brief ΢�̰߳�����ϵͳIO���� read
354  * @param fd ϵͳsocket��Ϣ
355  * @param buf ������Ϣ������ָ��
356  * @param nbyte ������Ϣ����������
357  * @param timeout ��ȴ�ʱ��, ����
358  * @return >0 �ɹ����ճ���, <0 ʧ��
359  */
360 ssize_t mt_read(int fd, void *buf, size_t nbyte, int timeout);
361 
362 /**
363  * @brief ΢�̰߳�����ϵͳIO���� write
364  * @param fd ϵͳsocket��Ϣ
365  * @param buf �����͵���Ϣָ��
366  * @param nbyte �����͵���Ϣ����
367  * @param timeout ��ȴ�ʱ��, ����
368  * @return >0 �ɹ����ͳ���, <0 ʧ��
369  */
370 ssize_t mt_write(int fd, const void *buf, size_t nbyte, int timeout);
371 
372 /**
373  * @brief ΢�̰߳�����ϵͳIO���� recv
374  * @param fd ϵͳsocket��Ϣ
375  * @param buf ������Ϣ������ָ��
376  * @param len ������Ϣ����������
377  * @param timeout ��ȴ�ʱ��, ����
378  * @return >0 �ɹ����ճ���, <0 ʧ��
379  */
380 ssize_t mt_recv(int fd, void *buf, int len, int flags, int timeout);
381 
382 /**
383  * @brief ΢�̰߳�����ϵͳIO���� send
384  * @param fd ϵͳsocket��Ϣ
385  * @param buf �����͵���Ϣָ��
386  * @param nbyte �����͵���Ϣ����
387  * @param timeout ��ȴ�ʱ��, ����
388  * @return >0 �ɹ����ͳ���, <0 ʧ��
389  */
390 ssize_t mt_send(int fd, const void *buf, size_t nbyte, int flags, int timeout);
391 
392 
393 /**
394  * @brief ΢�̵߳ȴ�epoll�¼��İ�������
395  * @param fd ϵͳsocket��Ϣ
396  * @param events �ȴ����¼� IN/OUT
397  * @param timeout ��ȴ�ʱ��, ����
398  * @return >0 ������¼�, <0 ʧ��
399  */
400 int mt_wait_events(int fd, int events, int timeout);
401 
402 
403 void* mt_start_thread(void* entry, void* args);
404 
405 
406 }
407 
408 #endif
409 
410 
411