美文网首页
Google quiche客户端socket读写模型

Google quiche客户端socket读写模型

作者: JeffreyLau | 来源:发表于2023-10-13 19:09 被阅读0次

    1、前言

    • 本文通过学习google quic默认客户端的网络IO事件读取实现,来学习在google quic中数据的发送和接收的处理/分发机制
    • 同时通过本节学习默认IO读写实现和其他模块的关系,为后续深入分析google quic源码做铺垫

    2、QuicClientDefaultNetworkHelper定义

    • 在google quic中,默认实现QuicClientDefaultNetworkHelper模块用于往socket中写入数据,以及从socket中读取数据

    • 其中QuicClientDefaultNetworkHelper的派生关系如下:

      quic_client_socket_read_write_01.png
    • QuicClientDefaultNetworkHelper模块分别由QuicClientBase::NetWorkHelper、QuicSocketEventListener、ProcessPacketInterface接口派生,其中1)QuicClientBase::NetWorkHelper用于创建socket,往QuicEventLoop注册和注销IO读写事件;2)QuicSocketEventListener用于接收处理socket IO事件如可读和可写事件的处理;3)ProcessPacketInterface接口提供ProcessPacket()函数完成对QuicReceivedPacket报文传递到QuicClientDefaultNetworkHelper模块

    • 同时QuicClientDefaultNetworkHelper由三大成员变量QuicEventLoop、QuicPacketReader、QuicClientBase组成,1)成员event_loop_指针由其他模块传入用于关联socket io框架监听socket 读写事件;2)packet_reader成员用于读取Udp报文,并将udp包转成QuicReceivedPacket包,并最终通过ProcessPacketInterface接口将QuicReceivedPacket报文回调到QuicClientDefaultNetworkHelper模块,然后再将该报文交给成员client_进行处理

    3、QuicClientBase::NetworkHelper接口的定义及实现

    class QuicClientBase : public QuicSession::Visitor {
     public:
      // An interface to various network events that the QuicClient will need to
      // interact with.
      class NetworkHelper {
       public:
        virtual ~NetworkHelper();
    
        // Runs one iteration of the event loop.
        // 用于socket io 循环loop
        virtual void RunEventLoop() = 0;
    
        // Used during initialization: creates the UDP socket FD, sets socket
        // options, and binds the socket to our address.
        // 创建一个udp socket,并将socket fd 向事件循环注册,通过IO监听该socket
        virtual bool CreateUDPSocketAndBind(QuicSocketAddress server_address,
                                            QuicIpAddress bind_to_address,
                                            int bind_to_port) = 0;
    
        // Unregister and close all open UDP sockets.
        // 关闭一个socket,并将socket从io 事件循环中注销
        virtual void CleanUpAllUDPSockets() = 0;
    
        // If the client has at least one UDP socket, return address of the latest
        // created one. Otherwise, return an empty socket address.
        virtual QuicSocketAddress GetLatestClientAddress() const = 0;
    
        // Creates a packet writer to be used for the next connection.
        // 创建一个socket writer,用于往socket写入数据
        virtual QuicPacketWriter* CreateQuicPacketWriter() = 0;
      };
      ....
    }
    
    • RunEventLoop函数的视线如下
    void QuicClientDefaultNetworkHelper::RunEventLoop() {
      quiche::QuicheRunSystemEventLoopIteration();
      event_loop_->RunEventLoopOnce(QuicTime::Delta::FromMilliseconds(50));
    }
    
    • 该函数实现较为简单,就是直接调用```QuicEventLoop::RunEventLoopOnce()``,对IO时间循环就行迭代,并且在50ms后假设无其他时间触发,将退出IO迭代
    bool QuicClientDefaultNetworkHelper::CreateUDPSocketAndBind(
        QuicSocketAddress server_address, QuicIpAddress bind_to_address,
        int bind_to_port) {
      SocketFd fd = CreateUDPSocket(server_address, &overflow_supported_);
      if (fd == kInvalidSocketFd) {
        return false;
      }
      auto closer = absl::MakeCleanup([fd] { (void)socket_api::Close(fd); });
    
      ......
    
      if (event_loop_->RegisterSocket(
              fd, kSocketEventReadable | kSocketEventWritable, this)) {
        fd_address_map_[fd] = client_address;
        std::move(closer).Cancel();
        return true;
      }
      return false;
    }
    
    • CreateUDPSocketAndBind函数的核心作用是根据一个server_address创建一个udp端口,并将该fd往时间循环中注册读写事件
    • QuicClientBase::Initialize()函数中有使用该函数创建socket句柄
    void QuicClientDefaultNetworkHelper::CleanUpAllUDPSockets() {
      for (std::pair<int, QuicSocketAddress> fd_address : fd_address_map_) {
        CleanUpUDPSocketImpl(fd_address.first);
      }
      fd_address_map_.clear();
    }
    
    void QuicClientDefaultNetworkHelper::CleanUpUDPSocketImpl(SocketFd fd) {
      if (fd != kInvalidSocketFd) {
        bool success = event_loop_->UnregisterSocket(fd);
        QUICHE_DCHECK(success || fds_unregistered_externally_);
        absl::Status rc = socket_api::Close(fd);
        QUICHE_DCHECK(rc.ok()) << rc;
      }
    }
    
    • 以上为关闭socket fd的实现,同时注销事件循环
    QuicPacketWriter* QuicClientDefaultNetworkHelper::CreateQuicPacketWriter() {
      if (event_loop_->SupportsEdgeTriggered()) {
        return new QuicDefaultPacketWriter(GetLatestFD());
      } else {
        return new LevelTriggeredPacketWriter(GetLatestFD(), event_loop_);
      }
    }
    
    • 创建QuicPacketWriter指针,用于往socket fd中写入数据

    4、QuicSocketEventListener接口的定义及实现

    
    class QUICHE_NO_EXPORT QuicSocketEventListener {
     public:
      virtual ~QuicSocketEventListener() = default;
    
      virtual void OnSocketEvent(QuicEventLoop* event_loop, SocketFd fd,
                                 QuicSocketEventMask events) = 0;
    };
    
    • 在上节中有提到通过CreateUDPSocketAndBind()函数创建socket fd,并将fd向QuicEventLoop注册读写事件,当IO检测到该fd有可读或者可写事件的时候,就会触发对应的回调,而该回调就是OnSocketEvent(...)函数

    • 对应的实现如下:

    void QuicClientDefaultNetworkHelper::OnSocketEvent(
        QuicEventLoop* /*event_loop*/, QuicUdpSocketFd fd,
        QuicSocketEventMask events) {
      if (events & kSocketEventReadable) {
        QUIC_DVLOG(1) << "Read packets on kSocketEventReadable";
        int times_to_read = max_reads_per_event_loop_;
        bool more_to_read = true;
        QuicPacketCount packets_dropped = 0;
        while (client_->connected() && more_to_read && times_to_read > 0) {
          // 读取udp包,并将包转换成QuicReceivedPacket包,然后通过ProcessPacketInterface::ProcessPacket
          // 对报文进行处理
          more_to_read = packet_reader_->ReadAndDispatchPackets(
              fd, GetLatestClientAddress().port(), *client_->helper()->GetClock(),
              this, overflow_supported_ ? &packets_dropped : nullptr);
          --times_to_read;
        }
        ....
      }
      if (client_->connected() && (events & kSocketEventWritable)) {
        client_->writer()->SetWritable();
        client_->session()->connection()->OnCanWrite();
      }
    }
    
    • OnSocketEvent函数为IO事件监听函数,当socket可读的时候,通过QuicPacketReader模块读取Udp包,并将包转换成QuicReceivedPacket同时将QuicReceivedPacket通过ProcessPacketInterface::ProcessPacket()由传递到QuicClientDefaultNetworkHelper模块
    • 当socket有可写事件触发的时候,通过成员client_QuicPacketWriter置成可写,同时也对对应的QuicConnection模块调用**OnCanWrite()处理,后面进行详细分析

    5、ProcessPacketInterface接口的定义及实现

    // A class to process each incoming packet.
    class QUIC_NO_EXPORT ProcessPacketInterface {
     public:
      virtual ~ProcessPacketInterface() {}
      virtual void ProcessPacket(const QuicSocketAddress& self_address,
                                 const QuicSocketAddress& peer_address,
                                 const QuicReceivedPacket& packet) = 0;
    };
    
    void QuicClientDefaultNetworkHelper::ProcessPacket(
        const QuicSocketAddress& self_address,
        const QuicSocketAddress& peer_address, const QuicReceivedPacket& packet) {
      client_->session()->ProcessUdpPacket(self_address, peer_address, packet);
    }
    
    • QuicClientDefaultNetworkHelper::ProcessPacket()函数的视线可以看出,最终对QuicReceivedPacket报文的处理是在QuicSession中进行处理的
    • QuicSession最终包报文解析工作交给QuicConnection模块
    void QuicSession::ProcessUdpPacket(const QuicSocketAddress& self_address,
                                       const QuicSocketAddress& peer_address,
                                       const QuicReceivedPacket& packet) {
      QuicConnectionContextSwitcher cs(connection_->context());
      connection_->ProcessUdpPacket(self_address, peer_address, packet);
    }
    
    • 在google quic应用中无论是服务端还是客户端端,都是以QuicSession为一次会话单位,而每个QuicSession一定会有一个QuicConnection成员,可以称作为连接
    • 此处无论是服务端还是客户端对QuicReceivedPacket的解析工作最终都是在*QuicConnection模块中完成

    总结

    • 通过本文学习google quiche开源项目中客户端socket发送数据和接收数据的基本模型
    • 在上文的分析中最终引出了QuicSession、QuicConnection、以及QuicPacketWriter模块,这些模块是google quic中的核心模块
    • 通过本文的学习对后续深入研究google quic做一个简单铺垫工作

    相关文章

      网友评论

          本文标题:Google quiche客户端socket读写模型

          本文链接:https://www.haomeiwen.com/subject/vveabdtx.html