• muduo源码剖析之Socket类


    Socket

    封装了一个sockfd相关的设置

    比较简单,已经编写注释

    // Copyright 2010, Shuo Chen.  All rights reserved.
    // http://code.google.com/p/muduo/
    //
    // Use of this source code is governed by a BSD-style license
    // that can be found in the License file.
    
    // Author: Shuo Chen (chenshuo at chenshuo dot com)
    //
    // This is an internal header file, you should not include this.
    
    #ifndef MUDUO_NET_SOCKET_H
    #define MUDUO_NET_SOCKET_H
    
    #include "muduo/base/noncopyable.h"
    
    // struct tcp_info is in 
    struct tcp_info;
    
    namespace muduo
    {
    ///
    /// TCP networking.
    ///
    namespace net
    {
    
    class InetAddress;
    
    ///
    /// Wrapper of socket file descriptor.
    ///
    /// It closes the sockfd when desctructs.
    /// It's thread safe, all operations are delagated to OS.
    class Socket : noncopyable
    {
     public:
      explicit Socket(int sockfd)
        : sockfd_(sockfd)
      { }
    
      // Socket(Socket&&) // move constructor in C++11
      ~Socket();
      //返回socket fd
      int fd() const { return sockfd_; }
      // return true if success.
      //获取sockfd_成员变量的tcp相关属性
      bool getTcpInfo(struct tcp_info*) const;
      //将getTcpInfo()信息转换为string格式
      bool getTcpInfoString(char* buf, int len) const;
    
      /// abort if address in use
      //绑定地址
      void bindAddress(const InetAddress& localaddr);
      /// abort if address in use
      void listen();//监听
    
      /// On success, returns a non-negative integer that is
      /// a descriptor for the accepted socket, which has been
      /// set to non-blocking and close-on-exec. *peeraddr is assigned.
      /// On error, -1 is returned, and *peeraddr is untouched.
      //接收连接
      int accept(InetAddress* peeraddr);
      //调用shutdown()关闭写端
      void shutdownWrite();
    
      ///
      /// Enable/disable TCP_NODELAY (disable/enable Nagle's algorithm).
      ///
      //开启或者关闭控制 TCP(传输控制协议)的ack延迟确认机制。
      void setTcpNoDelay(bool on);
    
      ///
      /// Enable/disable SO_REUSEADDR
      ///
      //设置地址复用
      void setReuseAddr(bool on);
    
      ///
      /// Enable/disable SO_REUSEPORT
      ///
      //设置端口复用
      void setReusePort(bool on);
    
      ///
      /// Enable/disable SO_KEEPALIVE
      ///
      //开启 or 关闭KeepAlive(tcp探活,默认时长2h)
      void setKeepAlive(bool on);
    
     private:
      //socket fd
      const int sockfd_;
    };
    
    }  // namespace net
    }  // namespace muduo
    
    #endif  // MUDUO_NET_SOCKET_H
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    // Copyright 2010, Shuo Chen.  All rights reserved.
    // http://code.google.com/p/muduo/
    //
    // Use of this source code is governed by a BSD-style license
    // that can be found in the License file.
    
    // Author: Shuo Chen (chenshuo at chenshuo dot com)
    
    #include "muduo/net/Socket.h"
    
    #include "muduo/base/Logging.h"
    #include "muduo/net/InetAddress.h"
    #include "muduo/net/SocketsOps.h"
    
    #include 
    #include 
    #include   // snprintf
    
    using namespace muduo;
    using namespace muduo::net;
    
    Socket::~Socket()
    {
      sockets::close(sockfd_);
    }
    
    bool Socket::getTcpInfo(struct tcp_info* tcpi) const
    {
      socklen_t len = sizeof(*tcpi);
      memZero(tcpi, len);
      return ::getsockopt(sockfd_, SOL_TCP, TCP_INFO, tcpi, &len) == 0;
    }
    
    bool Socket::getTcpInfoString(char* buf, int len) const
    {
      struct tcp_info tcpi;
      bool ok = getTcpInfo(&tcpi);
      if (ok)
      {
        snprintf(buf, len, "unrecovered=%u "
                 "rto=%u ato=%u snd_mss=%u rcv_mss=%u "
                 "lost=%u retrans=%u rtt=%u rttvar=%u "
                 "sshthresh=%u cwnd=%u total_retrans=%u",
                 tcpi.tcpi_retransmits,  // Number of unrecovered [RTO] timeouts
                 tcpi.tcpi_rto,          // Retransmit timeout in usec
                 tcpi.tcpi_ato,          // Predicted tick of soft clock in usec
                 tcpi.tcpi_snd_mss,
                 tcpi.tcpi_rcv_mss,
                 tcpi.tcpi_lost,         // Lost packets
                 tcpi.tcpi_retrans,      // Retransmitted packets out
                 tcpi.tcpi_rtt,          // Smoothed round trip time in usec
                 tcpi.tcpi_rttvar,       // Medium deviation
                 tcpi.tcpi_snd_ssthresh,
                 tcpi.tcpi_snd_cwnd,
                 tcpi.tcpi_total_retrans);  // Total retransmits for entire connection
      }
      return ok;
    }
    
    void Socket::bindAddress(const InetAddress& addr)
    {
      sockets::bindOrDie(sockfd_, addr.getSockAddr());
    }
    
    void Socket::listen()
    {
      sockets::listenOrDie(sockfd_);
    }
    
    int Socket::accept(InetAddress* peeraddr)
    {
      struct sockaddr_in6 addr;
      memZero(&addr, sizeof addr);
      int connfd = sockets::accept(sockfd_, &addr);
      if (connfd >= 0)
      {
        peeraddr->setSockAddrInet6(addr);
      }
      return connfd;
    }
    
    void Socket::shutdownWrite()
    {
      sockets::shutdownWrite(sockfd_);
    }
    
    void Socket::setTcpNoDelay(bool on)
    {
      int optval = on ? 1 : 0;
      ::setsockopt(sockfd_, IPPROTO_TCP, TCP_NODELAY,
                   &optval, static_cast<socklen_t>(sizeof optval));
      // FIXME CHECK
    }
    
    void Socket::setReuseAddr(bool on)
    {
      int optval = on ? 1 : 0;
      ::setsockopt(sockfd_, SOL_SOCKET, SO_REUSEADDR,
                   &optval, static_cast<socklen_t>(sizeof optval));
      // FIXME CHECK
    }
    
    void Socket::setReusePort(bool on)
    {
    #ifdef SO_REUSEPORT
      int optval = on ? 1 : 0;
      int ret = ::setsockopt(sockfd_, SOL_SOCKET, SO_REUSEPORT,
                             &optval, static_cast<socklen_t>(sizeof optval));
      if (ret < 0 && on)
      {
        LOG_SYSERR << "SO_REUSEPORT failed.";
      }
    #else
      if (on)
      {
        LOG_ERROR << "SO_REUSEPORT is not supported.";
      }
    #endif
    }
    
    void Socket::setKeepAlive(bool on)
    {
      int optval = on ? 1 : 0;
      ::setsockopt(sockfd_, SOL_SOCKET, SO_KEEPALIVE,
                   &optval, static_cast<socklen_t>(sizeof optval));
      // FIXME CHECK
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
  • 相关阅读:
    网络丢包,网络延迟?这款神器帮你搞定所有!
    gtest语法(一)TEST和TEST_F宏
    小H靶场学习笔记:DC-1
    “唯品会VIP商品API:一键获取奢侈品详情,尊享品质生活!“
    高等数学啃书汇总重难点(五)定积分
    centos7 安装mariadb
    【Django】执行查询——查询JSONField
    汽车票订票系统
    基于javaweb谣言平台系统
    Win7C盘不能扩大怎么办?Win7C盘扩展卷不能点怎么解决
  • 原文地址:https://blog.csdn.net/weixin_50448879/article/details/134256192