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

Function mt_udpsendrcv

adapter/micro_thread/mt_api.cpp:33–85  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

31namespace NS_MICRO_THREAD {
32
33int mt_udpsendrcv(struct sockaddr_in* dst, void* pkg, int len, void* rcv_buf, int& buf_size, int timeout)
34{
35 int ret = 0;
36 int rc = 0;
37 int flags = 1;
38 struct sockaddr_in from_addr = {0};
39 int addr_len = sizeof(from_addr);
40
41 if(len<1 || buf_size<1 ||!dst || !pkg || !rcv_buf)
42 {
43 MTLOG_ERROR("mt_udpsendrcv input params invalid, dst[%p], pkg[%p], rcv_buf[%p], len[%d], buf_size[%d]",
44 dst, pkg, rcv_buf, len, buf_size);
45 return -10;
46 }
47
48 int sock = socket(PF_INET, SOCK_DGRAM, 0);
49 if ((sock < 0) || (ioctl(sock, FIONBIO, &flags) < 0))
50 {
51 MT_ATTR_API(320842, 1);
52 MTLOG_ERROR("mt_udpsendrcv new sock failed, sock: %d, errno: %d (%m)", sock, errno);
53 ret = -1;
54 goto EXIT_LABEL;
55 }
56
57 rc = MtFrame::sendto(sock, pkg, len, 0, (struct sockaddr*)dst, (int)sizeof(*dst), timeout);
58 if (rc < 0)
59 {
60 MT_ATTR_API(320844, 1);
61 MTLOG_ERROR("mt_udpsendrcv send failed, rc: %d, errno: %d (%m)", rc, errno);
62 ret = -2;
63 goto EXIT_LABEL;
64 }
65
66 rc = MtFrame::recvfrom(sock, rcv_buf, buf_size, 0, (struct sockaddr*)&from_addr, (socklen_t*)&addr_len, timeout);
67 if (rc < 0)
68 {
69 MT_ATTR_API(320845, 1);
70 MTLOG_ERROR("mt_udpsendrcv recv failed, rc: %d, errno: %d (%m)", rc, errno);
71 ret = -3;
72 goto EXIT_LABEL;
73 }
74 buf_size = rc;
75
76EXIT_LABEL:
77
78 if (sock > 0)
79 {
80 close(sock);
81 sock = -1;
82 }
83
84 return ret;
85}
86
87int mt_tcp_create_sock(void)
88{

Callers

nothing calls this directly

Calls 5

recvfromFunction · 0.85
socketFunction · 0.70
ioctlFunction · 0.70
sendtoFunction · 0.70
closeFunction · 0.70

Tested by

no test coverage detected