posix_qui-master 服务器端QuicAccept()函数

本文深入探讨了QuicSocketAccept函数的工作原理,详细分析了如何从监听套接字中接受新的连接请求,包括状态检查、资源获取及新连接的创建过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

QuicSocketAccept函数 返回一个套接字

QuicSocket newSocket = QuicSocketAccept(fd);

跟踪这个函数

QuicSocket QuicSocketAccept(QuicSocket listenSock)
{
//判断传入的值
    auto socket = EntryBase::GetFdManager().Get(listenSock);
    if (!socket || socket->Category() != EntryCategory::Socket) {
        DebugPrint(dbg_api, "sock = %d, return = -1, errno = EBADF", listenSock);
        errno = EBADF;
        return -1;
    }
//重点是这个AcceptSocket函数 返回一个整数
    auto newSocket = ((QuicSocketEntry*)socket.get())->AcceptSocket();
    if (!newSocket) {
        DebugPrint(dbg_api, "sock = %d, return = -1, errno = %d", listenSock, errno);
        return -1;
    }

    DebugPrint(dbg_api, "sock = %d, newSocket = %d", listenSock, newSocket->Fd());
    return newSocket->Fd();
}

//跟踪一下这个AcceptSocket函数

QuicSocketEntryPtr QuicSocketEntry::AcceptSocket()
{
//还是先判断一下状态
    if (socketState_ != QuicSocketState_Binded) {
        errno = EINVAL;
        return QuicSocketEntryPtr();
    }

    std::unique_lock<std::mutex> lock(acceptSocketsMtx_);
    if (acceptSockets_.empty()) {//判断是否为空
        errno = EAGAIN;
        return QuicSocketEntryPtr();
    }

    auto ptr = acceptSockets_.front();//返回这个列表的第一个元素
    acceptSockets_.pop_front();
    return ptr;
}

auto ptr = acceptSockets_.front();//返回这个列表的第一个元素

//返回这个列表的第一个元素
      // element access
      /**
       *  Returns a read/write reference to the data at the first
       *  element of the %list.
       */
      reference
      front() _GLIBCXX_NOEXCEPT
      { return *begin(); }

      /**
       *  Returns a read-only (constant) reference to the data at the first
       *  element of the %list.
       */
      const_reference
      front() const _GLIBCXX_NOEXCEPT
      { return *begin(); }

acceptSockets_.pop_front();

 /**
       *  @brief  Removes first element.
       *
       *  This is a typical stack operation.  It shrinks the %list by
       *  one.  Due to the nature of a %list this operation can be done
       *  in constant time, and only invalidates iterators/references to
       *  the element being removed.
       *
       *  Note that no data is returned, and if the first element's data
       *  is needed, it should be retrieved before pop_front() is
       *  called.
       */
      void
      pop_front() _GLIBCXX_NOEXCEPT
      { this->_M_erase(begin()); }

      /**
       *  @brief  Add data to the end of the %list.
       *  @param  __x  Data to be added.
       *
       *  This is a typical stack operation.  The function creates an
       *  element at the end of the %list and assigns the given data to
       *  it.  Due to the nature of a %list this operation can be done
       *  in constant time, and does not invalidate iterators and
       *  references.
       */
      void
      push_back(const value_type& __x)
      { this->_M_insert(end(), __x); }
类似于出栈操作
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值