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 类型变量,还有地址转换函数,比较简单。
阅读全文
0 0
- muduo源码学习(19)-socket封装
- muduo源码学习(5)-线程封装
- muduo源码学习(20)-Acceptor封装
- muduo库的Socket封装
- muduo源码学习(11)-日志类封装1
- muduo源码学习(12)-日志类封装2
- muduo源码学习(15)-IO复用的封装
- muduo源码学习(1)
- muduo源码分析:异常类封装
- muduo源码分析:线程类Thread封装
- muduo源码分析:reactor模型封装
- Redis源码学习之【Tcp Socket封装】
- Redis源码学习之【Tcp Socket封装】
- muduo源码学习(7)-队列
- muduo源码学习(8)-ThreadPool
- muduo源码学习(9)-单例类
- muduo源码学习(10)-ThreadLocal
- muduo源码学习(18)-EventLoopThread
- 简单的斐波那契数列
- 实验吧CTF题目之抓到你了(代号:1813)
- js 数组去重 数组求差集、交集
- 一步一步写Makefile(1):概念,变量,自动推导,Makefile相互引用
- Eclipse中使用jstl标签
- muduo源码学习(19)-socket封装
- Hybris 新建自己的项目工程
- 动态代理(一)---JDK动态代理
- 快速掌握多进程切地图缓存
- android 生命周期梳理
- HDU
- I2C总线双向电平转换实用电路
- win下安装memcached
- 解决8080端口被占用的问题