一、tcpsenddata_nb
tcpsenddata_nb()是fastdfs中主要的用于发送数据的函数。注意:本例sock的非阻塞的。
int tcpsenddata_nb(int sock, void* data, const int size, const int timeout)
{
int left_bytes;
int write_bytes;
int result;
unsigned char* p;
#ifdef USE_SELECT
fd_set write_set;
struct timeval t;
#else
struct pollfd pollfds;
#endif
#ifdef USE_SELECT
FD_ZERO(&write_set);
FD_SET(sock, &write_set);
#else
pollfds.fd = sock;
pollfds.events = POLLOUT;
#endif
p = (unsigned char*)data;
left_bytes = size;
while (left_bytes > 0)
{
write_bytes = send(sock, p, left_bytes, 0);
if (write_bytes < 0)
{
if (!(errno == EAGAIN || errno == EWOULDBLOCK)) //如果是这两个错误,则问题不太,重新调用send()即可。
{
return errno != 0 ? errno : EINTR;
}
}
else
{
left_bytes -= write_bytes;
p += write_bytes;
continue;
}
//以下在write_bytes<0时执行。
#ifdef USE_SELECT
if