muduo源码学习(19)-socket封装

来源:互联网 发布:供销e家 源码 编辑:程序博客网 时间:2024/06/06 12:40

在net/Endian.h中,提供了字节序转换的全局函数:

inline uint64_t hostToNetwork64(uint64_t host64){  return htobe64(host64);}inline uint32_t hostToNetwork32(uint32_t host32){  return htobe32(host32);}inline uint16_t hostToNetwork16(uint16_t host16){  return htobe16(host16);}inline uint64_t networkToHost64(uint64_t net64){  return be64toh(net64);}inline uint32_t networkToHost32(uint32_t net32){  return be32toh(net32);}inline uint16_t networkToHost16(uint16_t net16){  return be16toh(net16);}

net/SocketsOps.h中提供了一些基本的读写操作

int createNonblockingOrDie();int  connect(int sockfd, const struct sockaddr_in& addr);void bindOrDie(int sockfd, const struct sockaddr_in& addr);void listenOrDie(int sockfd);int  accept(int sockfd, struct sockaddr_in* addr);ssize_t read(int sockfd, void *buf, size_t count);ssize_t readv(int sockfd, const struct iovec *iov, int iovcnt);ssize_t write(int sockfd, const void *buf, size_t count);void close(int sockfd);void shutdownWrite(int sockfd);void toIpPort(char* buf, size_t size,              const struct sockaddr_in& addr);void toIp(char* buf, size_t size,          const struct sockaddr_in& addr);void fromIpPort(const char* ip, uint16_t port,                  struct sockaddr_in* addr);int getSocketError(int sockfd);struct sockaddr_in getLocalAddr(int sockfd);struct sockaddr_in getPeerAddr(int sockfd);bool isSelfConnect(int sockfd);

函数的实现比较简单,在muduo中,socket描述符都是非阻塞的,acept后也会设置为非阻塞。

muduo采用了RAII技法封装了socket描述符,在net/Socket.h中:

class Socket : boost::noncopyable{ public:  explicit Socket(int sockfd)    : sockfd_(sockfd)  { }  // Socket(Socket&&) // move constructor in C++11  ~Socket();  int fd() const { return sockfd_; }  /// 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.   //peeraddr  int accept(InetAddress* peeraddr);  void shutdownWrite();  ///  /// Enable/disable TCP_NODELAY (disable/enable Nagle's algorithm).  ///  void setTcpNoDelay(bool on);  ///  /// Enable/disable SO_REUSEADDR  ///  void setReuseAddr(bool on);  ///  /// Enable/disable SO_KEEPALIVE  ///  void setKeepAlive(bool on); private: //  const int sockfd_;};
无非就是对socket描述符的操作,还有socket选项的设置。在析构函数中,关闭了描述符:

Socket::~Socket(){  sockets::close(sockfd_);}
InetAdddress是对地质的封装,在net/InetAddress.h中:

class InetAddress : public muduo::copyable{ public:  /// Constructs an endpoint with given port number.  /// Mostly used in TcpServer listening.  explicit InetAddress(uint16_t port);  /// Constructs an endpoint with given ip and port.  /// @c ip should be "1.2.3.4"  InetAddress(const StringPiece& ip, uint16_t port);  /// Constructs an endpoint with given struct @c sockaddr_in  /// Mostly used when accepting new connections  InetAddress(const struct sockaddr_in& addr)    : addr_(addr)  { }  string toIp() const;  string toIpPort() const;  string toHostPort() const __attribute__ ((deprecated))  { return toIpPort(); }  // default copy/assignment are Okay  const struct sockaddr_in& getSockAddrInet() const { return addr_; }  void setSockAddrInet(const struct sockaddr_in& addr) { addr_ = addr; }  uint32_t ipNetEndian() const { return addr_.sin_addr.s_addr; }  uint16_t portNetEndian() const { return addr_.sin_port; } private:  struct sockaddr_in addr_;};
就是一个sockaddr_in 类型变量,还有地址转换函数,比较简单。


原创粉丝点击