非阻塞网络函数封装不一致

 

16         非阻塞网络函数封装不一致

ACE的非阻塞网络函数参数设计有不合理的地方。ACE_SOCK_StreamACE_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要调用recvsend函数,而不要调用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时务必当心。

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值