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

Function mt_tcpsendrcv_short

adapter/micro_thread/mt_api.cpp:274–332  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

272}
273
274int mt_tcpsendrcv_short(struct sockaddr_in* dst, void* pkg, int len, void* rcv_buf, int& buf_size, int timeout, MtFuncTcpMsgLen func)
275{
276 int ret = 0, rc = 0;
277 int addr_len = sizeof(struct sockaddr_in);
278 utime64_t start_ms = MtFrame::Instance()->GetLastClock();
279 utime64_t cost_time = 0;
280 int time_left = timeout;
281
282 if (!dst || !pkg || !rcv_buf || !func || len<1 || buf_size<1)
283 {
284 MTLOG_ERROR("input params invalid, dst[%p], pkg[%p], rcv_buf[%p], fun[%p], len[%d], buf_size[%d]",
285 dst, pkg, rcv_buf, func, len, buf_size);
286 return -10;
287 }
288
289 int sock;
290 sock = mt_tcp_create_sock();
291 if (sock < 0)
292 {
293 MTLOG_ERROR("create tcp socket failed, ret: %d", sock);
294 return -1;
295 }
296
297 rc = MtFrame::connect(sock, (struct sockaddr *)dst, addr_len, time_left);
298 if (rc < 0)
299 {
300 MTLOG_ERROR("socket[%d] connect failed, ret[%d][%m]", sock, rc);
301 ret = -4;
302 goto EXIT_LABEL;
303 }
304
305 cost_time = MtFrame::Instance()->GetLastClock() - start_ms;
306 time_left = (timeout > (int)cost_time) ? (timeout - (int)cost_time) : 0;
307 rc = MtFrame::send(sock, pkg, len, 0, time_left);
308 if (rc < 0)
309 {
310 MTLOG_ERROR("socket[%d] send failed, ret[%d][%m]", sock, rc);
311 ret = -2;
312 goto EXIT_LABEL;
313 }
314
315 cost_time = MtFrame::Instance()->GetLastClock() - start_ms;
316 time_left = (timeout > (int)cost_time) ? (timeout - (int)cost_time) : 0;
317 rc = mt_tcp_check_recv(sock, (char*)rcv_buf, buf_size, 0, time_left, func);
318 if (rc < 0)
319 {
320 MTLOG_ERROR("socket[%d] rcv failed, ret[%d][%m]", sock, rc);
321 ret = rc;
322 goto EXIT_LABEL;
323 }
324
325 ret = 0;
326
327EXIT_LABEL:
328 if (sock >= 0)
329 ::close(sock);
330
331 return ret;

Callers 1

mt_tcpsendrcv_exFunction · 0.85

Calls 6

mt_tcp_create_sockFunction · 0.85
mt_tcp_check_recvFunction · 0.85
GetLastClockMethod · 0.80
connectFunction · 0.70
sendFunction · 0.70
closeFunction · 0.70

Tested by

no test coverage detected