| 3 |
void MsToTimeval(const int ms, struct timeval *tv) |
void MsToTimeval(const int ms, struct timeval *tv) |
| 4 |
{ |
{ |
| 5 |
tv->tv_sec = ms / TIME_CNV_RATIO; |
tv->tv_sec = ms / TIME_CNV_RATIO; |
| 6 |
tv->tv_usec = (ms - tv->tv_sec * TIME_CNV_RATIO) * TIME_CNV_RATIO; |
tv->tv_usec = ms % TIME_CNV_RATIO * TIME_CNV_RATIO; |
| 7 |
} |
} |
| 8 |
|
|
| 9 |
int TimevalToMs(const struct timeval *tv) |
long int TimevalToMs(const struct timeval *tv) |
| 10 |
{ |
{ |
| 11 |
return (tv->tv_sec * TIME_CNV_RATIO + tv->tv_usec / TIME_CNV_RATIO); |
return (tv->tv_sec * TIME_CNV_RATIO + tv->tv_usec / TIME_CNV_RATIO); |
| 12 |
} |
} |
| 143 |
return 0; /* 连接服务器成功 */ |
return 0; /* 连接服务器成功 */ |
| 144 |
} |
} |
| 145 |
|
|
| 146 |
int DoConnect(const char *ip, const int port, const int msec) |
int DoConnect(const char *ip, const in_port_t port, const int msec) |
| 147 |
{ |
{ |
| 148 |
struct sockaddr_in sa; |
struct sockaddr_in sa; |
| 149 |
int conn_fd; |
int conn_fd; |
| 241 |
return conn_fd; |
return conn_fd; |
| 242 |
} |
} |
| 243 |
|
|
| 244 |
int DoSendData(int sockfd, const void *buf, const size_t len, int msec) |
long int DoSendData(int sockfd, const void *buf, const size_t len, int msec) |
| 245 |
{ |
{ |
| 246 |
struct timeval tv; |
struct timeval tv; |
| 247 |
fd_set wset; |
fd_set wset; |
| 248 |
int rv; |
int rv; |
| 249 |
int wc; |
ssize_t wc; |
| 250 |
|
|
| 251 |
MsToTimeval(msec, &tv); |
MsToTimeval(msec, &tv); |
| 252 |
FD_ZERO(&wset); |
FD_ZERO(&wset); |
| 267 |
return 0; /* nothing had sent */ |
return 0; /* nothing had sent */ |
| 268 |
} |
} |
| 269 |
|
|
| 270 |
int DoRecvData(int sockfd, void *buf, const size_t len, int msec) |
long int DoRecvData(int sockfd, void *buf, const size_t len, int msec) |
| 271 |
{ |
{ |
| 272 |
struct timeval tv; |
struct timeval tv; |
| 273 |
fd_set rset; |
fd_set rset; |
| 274 |
int rv; |
int rv; |
| 275 |
int rc; |
ssize_t rc; |
| 276 |
|
|
| 277 |
MsToTimeval(msec, &tv); |
MsToTimeval(msec, &tv); |
| 278 |
FD_ZERO(&rset); |
FD_ZERO(&rset); |