muduo源码学习(19)-socket封装
2017-08-23 15:42
295 查看
在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 类型变量,还有地址转换函数,比较简单。
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 类型变量,还有地址转换函数,比较简单。
相关文章推荐
- muduo源码学习(20)-Acceptor封装
- muduo源码学习(12)-日志类封装2
- Redis源码学习之【Tcp Socket封装】
- muduo 5 网络库学习之MutexLock类、MutexLockGuard类、Condition类、CountDownLatch类封装中的知识点
- muduo源码学习(9)-单例类
- muduo源码学习(14)-网络库类库概述
- muduo源码分析:reactor模型封装
- redis源码学习3 启动、配置及socket
- linux源码-TCP/IP协议栈学习预备(1) 数据结构之各socket之间的关系
- SQLite3源码学习(19) printf的实现
- muduo源码学习(6)-锁,条件变量
- TCP/IP源码学习(47)——socket与VFS的关联
- Halide学习笔记----Halide tutorial源码阅读19
- Cocos2d-x学习笔记(19)(TestCpp源码分析-3)
- muduo 6 网络库学习之BlockinngQueue<T>类、ThreadPool 类、Singleton类封装中的知识点
- muduo-源码分析2:注册监听socket和建立连接
- muduo源码分析:线程类Thread封装
- .NET Core 2.1 源码学习:看 SocketsHttpHandler 如何在异步方法中连接 Socket
- 090828项目进展:学习emule源码4,CAsyncSocketEx
- Rfid通信1-串口模式(附:对串口通讯进行了封装源码及学习资料)