ace nonblock

使用reactor, ACE_Sock_Stream进行收发数据。 nonblock模式下: [timeout参数均为NULL]

recv时, 可能返回-1. errno可能是EWOULDBLOCK(要求下次继续读), 或者其他系统错。


无需使用recv_n/send_n

ACE_Sock_Stream::recv  ---->ACE::recv():
ssize_t ACE::recv (ACE_HANDLE handle,   void *buf, size_t len, int flags, const ACE_Time_Value *timeout)
{
  if (timeout == 0)
    <span style="color:#FF0000;">return ACE_OS::recv (handle, (char *) buf, len, flags);</span>
  else
    {
      int val = 0;
      if (ACE::enter_recv_timedwait (handle, timeout, val) ==-1)
        return -1;
      else
        {
          ssize_t bytes_transferred =
            ACE_OS::recv (handle, (char *) buf, len, flags);
          ACE::restore_non_blocking_mode (handle, val);
          return bytes_transferred;
        }
    }
}

ACE::recv--->ACE_OS::recv():
ACE_INLINE ssize_t  ACE_OS::recv (ACE_HANDLE handle, char *buf, size_t len, int flags)
{
  ACE_OS_TRACE ("ACE_OS::recv");

  // On UNIX, a non-blocking socket with no data to receive, this
  // system call will return EWOULDBLOCK or EAGAIN, depending on the
  // platform.  UNIX 98 allows either errno, and they may be the same
  // numeric value.  So to make life easier for upper ACE layers as
  // well as application programmers, always change EAGAIN to
  // EWOULDBLOCK.  Rather than hack the ACE_OSCALL_RETURN macro, it's
  // handled explicitly here.  If the ACE_OSCALL macro ever changes,
  // this function needs to be reviewed.  On Win32, the regular macros
  // can be used, as this is not an issue.
#if defined (ACE_LACKS_RECV)
  ACE_UNUSED_ARG (handle);
  ACE_UNUSED_ARG (buf);
  ACE_UNUSED_ARG (len);
  ACE_UNUSED_ARG (flags);
  ACE_NOTSUP_RETURN (-1);
#elif defined (ACE_WIN32)
  ACE_SOCKCALL_RETURN (::recv ((ACE_SOCKET) handle, buf,
                               static_cast<int> (len), flags), ssize_t, -1);
#else
  <span style="color:#FF0000;">ssize_t ace_result_;
  ace_result_ = ::recv ((ACE_SOCKET) handle, buf, len, flags);

# if !(defined (EAGAIN) && defined (EWOULDBLOCK) && EAGAIN == EWOULDBLOCK)
  // Optimize this code out if we can detect that EAGAIN ==
  // EWOULDBLOCK at compile time.  If we cannot detect equality at
  // compile-time (e.g. if EAGAIN or EWOULDBLOCK are not preprocessor
  // macros) perform the check at run-time.  The goal is to avoid two
  // TSS accesses in the _REENTRANT case when EAGAIN == EWOULDBLOCK.
  if (ace_result_ == -1
#  if !defined (EAGAIN) || !defined (EWOULDBLOCK)
      && EAGAIN != EWOULDBLOCK
#  endif  /* !EAGAIN || !EWOULDBLOCK */
      && errno == EAGAIN)
    {
      errno = EWOULDBLOCK;
    }
# endif /* EAGAIN != EWOULDBLOCK*/

  return ace_result_;</span>
#endif /* ACE_LACKS_RECV */
}

同样, ACE_Sock_Stream::send--->ACE_OS::write---->::write
`set_nonblock` 通常用于设置套接字为非阻塞模式,这意味着套接字在执行某些操作时不会阻塞等待。在 C 语言中,可以使用 `fcntl()` 函数来设置套接字为非阻塞模式。 下面是一个示例代码,展示如何使用 `fcntl()` 函数将套接字设置为非阻塞模式: ```c #include <stdio.h> #include <stdlib.h> #include <fcntl.h> int set_nonblock(int sockfd) { // 获取套接字当前的标志位 int flags = fcntl(sockfd, F_GETFL, 0); if (flags == -1) { perror("fcntl get"); return -1; } // 设置套接字为非阻塞模式 if (fcntl(sockfd, F_SETFL, flags | O_NONBLOCK) == -1) { perror("fcntl set"); return -1; } return 0; } int main() { // 创建套接字 int sockfd = socket(AF_INET, SOCK_DGRAM, 0); if (sockfd == -1) { perror("socket"); exit(1); } // 设置套接字为非阻塞模式 if (set_nonblock(sockfd) == -1) { exit(1); } // 其他操作... // 关闭套接字 close(sockfd); return 0; } ``` 在上述示例中,我们定义了一个名为 `set_nonblock` 的函数,该函数接收一个套接字文件描述符作为参数。函数内部使用 `fcntl()` 函数获取当前套接字的标志位,然后通过 `fcntl()` 函数将 `O_NONBLOCK` 标志添加到当前标志位中,从而设置套接字为非阻塞模式。 在主函数中,我们首先创建了一个 UDP 套接字 `sockfd`,然后调用 `set_nonblock()` 函数将套接字设置为非阻塞模式。最后,我们可以继续执行其他操作。 请注意,上述示例代码仅演示了如何设置套接字为非阻塞模式,并不完整。你可能需要根据实际需求进行修改和扩展。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值