MCPcopy Create free account
hub / github.com/F-Stack/f-stack / mt_tcpsendrcv

Function mt_tcpsendrcv

adapter/micro_thread/mt_api.cpp:210–272  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

208}
209
210int mt_tcpsendrcv(struct sockaddr_in* dst, void* pkg, int len, void* rcv_buf, int& buf_size, int timeout, MtFuncTcpMsgLen func)
211{
212 if (!dst || !pkg || !rcv_buf || !func || len<1 || buf_size<1)
213 {
214 MTLOG_ERROR("input params invalid, dst[%p], pkg[%p], rcv_buf[%p], fun[%p], len[%d], buf_size[%d]",
215 dst, pkg, rcv_buf, func, len, buf_size);
216 return -10;
217 }
218
219 int ret = 0, rc = 0;
220 int addr_len = sizeof(struct sockaddr_in);
221 utime64_t start_ms = MtFrame::Instance()->GetLastClock();
222 utime64_t cost_time = 0;
223 int time_left = timeout;
224
225 int sock = -1;
226 TcpKeepConn* conn = mt_tcp_get_keep_conn(dst, sock);
227 if ((conn == NULL) || (sock < 0))
228 {
229 MTLOG_ERROR("socket[%d] get conn failed, ret[%m]", sock);
230 ret = -1;
231 goto EXIT_LABEL;
232 }
233
234 rc = MtFrame::connect(sock, (struct sockaddr *)dst, addr_len, time_left);
235 if (rc < 0)
236 {
237 MTLOG_ERROR("socket[%d] connect failed, ret[%d][%m]", sock, rc);
238 ret = -4;
239 goto EXIT_LABEL;
240 }
241
242 cost_time = MtFrame::Instance()->GetLastClock() - start_ms;
243 time_left = (timeout > (int)cost_time) ? (timeout - (int)cost_time) : 0;
244 rc = MtFrame::send(sock, pkg, len, 0, time_left);
245 if (rc < 0)
246 {
247 MTLOG_ERROR("socket[%d] send failed, ret[%d][%m]", sock, rc);
248 ret = -2;
249 goto EXIT_LABEL;
250 }
251
252 cost_time = MtFrame::Instance()->GetLastClock() - start_ms;
253 time_left = (timeout > (int)cost_time) ? (timeout - (int)cost_time) : 0;
254 rc = mt_tcp_check_recv(sock, (char*)rcv_buf, buf_size, 0, time_left, func);
255 if (rc < 0)
256 {
257 MTLOG_ERROR("socket[%d] rcv failed, ret[%d][%m]", sock, rc);
258 ret = rc;
259 goto EXIT_LABEL;
260 }
261
262 ret = 0;
263
264EXIT_LABEL:
265
266 if (conn != NULL)
267 {

Callers 1

mt_tcpsendrcv_exFunction · 0.85

Calls 6

mt_tcp_get_keep_connFunction · 0.85
mt_tcp_check_recvFunction · 0.85
GetLastClockMethod · 0.80
FreeConnectionMethod · 0.80
connectFunction · 0.70
sendFunction · 0.70

Tested by

no test coverage detected