forked from zackxue/ipc
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathsmtp_sock.c
46 lines (39 loc) · 955 Bytes
/
smtp_sock.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
ssize_t SMTP_SOCK_send(int sock, const void* buf, ssize_t size, int timeout_s)
{
int ret = 0;
int ret_errno = 0;
time_t const begin_time = time(NULL);
time_t current_time = begin_time;
uint8_t* buf_ptr = buf;
ssize_t send_len = 0;
while(1)
{
assert(send_len <= size);//, "send_len=%d, size=%d", send_len, size);
if(size == send_len){
// send completed
return send_len;
}
if(current_time - begin_time >= timeout_s){
// send timeout
return send_len;
}
// TRACE_DEBUG("waitting_size=%d", waitting_size);
ret = send(sock, _ctx->buf + send_len, size - send_len, 0);
ret_errno = errno;
current_time = time(NULL);
if(ret < 0){
if(EAGAIN == ret_errno || EINTR == ret_errno){
// retry
continue;
}
printf("send failed errno = %d\r\n", ret_errno);
return -1;
}else if(ret >= 0){
send_len += ret;
if(0 == ret){
printf("ret is zero, errno_cpy=%d\r\n", ret_errno);
}
}
}
return -1;
}