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 */ 23 24 #ifndef __MT_API_H__ 25 #define __MT_API_H__ 26 27 #include <netinet/in.h> 28 #include <vector> 29 30 using std::vector; 31 32 namespace NS_MICRO_THREAD { 33 34 int mt_udpsendrcv(struct sockaddr_in* dst, void* pkg, int len, void* rcv_buf, int& buf_size, int timeout); 35 36 typedef int (*MtFuncTcpMsgLen)(void* buf, int len); 37 38 int mt_tcpsendrcv(struct sockaddr_in* dst, void* pkg, int len, void* rcv_buf, int& buf_size, 39 int timeout, MtFuncTcpMsgLen chek_func); 40 41 42 enum MT_TCP_CONN_TYPE 43 { 44 MT_TCP_SHORT = 1, 45 MT_TCP_LONG = 2, 46 MT_TCP_SHORT_SNDONLY = 3, 47 MT_TCP_LONG_SNDONLY = 4, 48 MT_TCP_BUTT 49 }; 50 51 int mt_tcpsendrcv_ex(struct sockaddr_in* dst, void* pkg, int len, void* rcv_buf, int* buf_size, 52 int timeout, MtFuncTcpMsgLen func, MT_TCP_CONN_TYPE type = MT_TCP_LONG); 53 54 typedef int (*MtFuncTcpMsgChecker)(void* buf, int len, bool closed, void* msg_ctx, bool &msg_len_detected); 55 56 57 int mt_tcpsendrcv_ex(struct sockaddr_in* dst, void* pkg, int len, void*& rcv_buf, int& recv_pkg_size, 58 int timeout, MtFuncTcpMsgChecker check_func, void* msg_ctx=NULL, 59 MT_TCP_CONN_TYPE type = MT_TCP_LONG, bool keep_rcv_buf=false); 60 61 62 int mt_tcpsendrcv(struct sockaddr_in* dst, void* pkg, int len, void*& rcv_buf, int& recv_pkg_size, 63 int timeout, MtFuncTcpMsgChecker check_func, void* msg_ctx=NULL, bool keep_rcv_buf=false); 64 65 66 class IMtTask 67 { 68 public: 69 Process()70 virtual int Process() { return -1; }; 71 SetResult(int rc)72 void SetResult(int rc) 73 { 74 _result = rc; 75 } 76 GetResult(void)77 int GetResult(void) 78 { 79 return _result; 80 } 81 SetTaskType(int type)82 void SetTaskType(int type) 83 { 84 _type = type; 85 } 86 GetTaskType(void)87 int GetTaskType(void) 88 { 89 return _type; 90 } 91 IMtTask()92 IMtTask() {}; ~IMtTask()93 virtual ~IMtTask() {}; 94 95 protected: 96 97 int _type; 98 int _result; 99 }; 100 101 typedef vector<IMtTask*> IMtTaskList; 102 103 int mt_exec_all_task(IMtTaskList& req_list); 104 105 void mt_sleep(int ms); 106 107 unsigned long long mt_time_ms(void); 108 109 void mt_set_msg_private(void *data); 110 111 void* mt_get_msg_private(); 112 113 bool mt_init_frame(int argc=0, char * const argv[]=NULL); 114 115 void mt_set_stack_size(unsigned int bytes); 116 117 int mt_recvfrom(int fd, void *buf, int len, int flags, struct sockaddr *from, socklen_t *fromlen, int timeout); 118 119 int mt_sendto(int fd, const void *msg, int len, int flags, const struct sockaddr *to, int tolen, int timeout); 120 121 int mt_connect(int fd, const struct sockaddr *addr, int addrlen, int timeout); 122 123 int mt_accept(int fd, struct sockaddr *addr, socklen_t *addrlen, int timeout); 124 125 ssize_t mt_read(int fd, void *buf, size_t nbyte, int timeout); 126 127 ssize_t mt_write(int fd, const void *buf, size_t nbyte, int timeout); 128 129 ssize_t mt_recv(int fd, void *buf, int len, int flags, int timeout); 130 131 ssize_t mt_send(int fd, const void *buf, size_t nbyte, int flags, int timeout); 132 133 int mt_wait_events(int fd, int events, int timeout); 134 135 void* mt_start_thread(void* entry, void* args); 136 137 void* mt_active_thread(); 138 139 void mt_thread_wait(int ms); 140 141 void mt_thread_wakeup_wait(void * thread_p); 142 143 void mt_swap_thread(); 144 145 } 146 147 #endif 148 149 150