16 非阻塞网络函数封装不一致
ACE的非阻塞网络函数参数设计有不合理的地方。ACE_SOCK_Stream和ACE_SOCK_Connector在非阻塞的的调用的接口对于ACE_Time_Value *timeout参数的使用不一致,一个要使用NULL,一个却要使用ACE_Time_Value::zero。
ACE_SOCK_Stream,非阻塞调用send函数的时候【注】,timeout参数必须填写为NULL。它最后调用的是ACE::send。将ACE_Time_Value填写为ACE_Time_Value::zero (0,0)是不行的。如果填写ACE_Time_Value::zero,会大大降低这个非阻塞调用的性能。
ssize_t
ACE::send (ACE_HANDLE handle,
const void *buf,
size_t n,
int flags,
const ACE_Time_Value *timeout)
{
if (timeout == 0)
return ACE_OS::send (handle, (const char *) buf, n, flags);
else
{
…………
}
}
timeout);
注意使用非阻塞的的IO要调用recv,send函数,而不要调用recv_n,send_n这些函数接口,这些函数接口如果timeout参数传递NULL,表示阻塞。
另外非阻塞IO还是要自己设置Socket的选项。
但是ACE_SOCK_Connector却采用另外一个封装方式,其是传入一个NULL表示阻塞,而传入ACE_Time_Value::zero (0,0)表示进行非阻塞链接操作。
* @param timeout Pointer to an @c ACE_Time_Value object with amount
* of time to wait to connect. If the pointer is 0
* then the call blocks until the connection attempt
* is complete, whether it succeeds or fails. If
* *timeout == {0, 0} then the connection is done
* using nonblocking mode. In this case, if the
* connection can't be made immediately, this method
* returns -1 and errno == EWOULDBLOCK.
int connect (ACE_SOCK_Stream &new_stream,
const ACE_Addr &remote_sap,
const ACE_Time_Value *timeout = 0,
const ACE_Addr &local_sap = ACE_Addr::sap_any,
int reuse_addr = 0,
int flags = 0,
int perms = 0,
int protocol = 0);
大家在处理这些IO时务必当心。