📄 socket.h
字号:
#ifndef UTILS_BASE_SOCKET_H_#define UTILS_BASE_SOCKET_H_#include <errno.h>#ifdef POSIX#include <sys/types.h>#include <sys/socket.h>#include <arpa/inet.h>#include <netinet/in.h>#define SOCKET_EACCES EACCES#endif#ifdef WIN32#include "win32.h"#endif#include "basictypes.h"#include "socketaddress.h"// Rather than converting errors into a private namespace,// Reuse the POSIX socket api errors. Note this depends on// Win32 compatibility.#ifdef WIN32#define EWOULDBLOCK WSAEWOULDBLOCK#define EINPROGRESS WSAEINPROGRESS#define EALREADY WSAEALREADY#define ENOTSOCK WSAENOTSOCK#define EDESTADDRREQ WSAEDESTADDRREQ#define EMSGSIZE WSAEMSGSIZE#define EPROTOTYPE WSAEPROTOTYPE#define ENOPROTOOPT WSAENOPROTOOPT#define EPROTONOSUPPORT WSAEPROTONOSUPPORT#define ESOCKTNOSUPPORT WSAESOCKTNOSUPPORT#define EOPNOTSUPP WSAEOPNOTSUPP#define EPFNOSUPPORT WSAEPFNOSUPPORT#define EAFNOSUPPORT WSAEAFNOSUPPORT#define EADDRINUSE WSAEADDRINUSE#define EADDRNOTAVAIL WSAEADDRNOTAVAIL#define ENETDOWN WSAENETDOWN#define ENETUNREACH WSAENETUNREACH#define ENETRESET WSAENETRESET#define ECONNABORTED WSAECONNABORTED#define ECONNRESET WSAECONNRESET#define ENOBUFS WSAENOBUFS#define EISCONN WSAEISCONN#define ENOTCONN WSAENOTCONN#define ESHUTDOWN WSAESHUTDOWN#define ETOOMANYREFS WSAETOOMANYREFS#define ETIMEDOUT WSAETIMEDOUT#define ECONNREFUSED WSAECONNREFUSED#define ELOOP WSAELOOP#undef ENAMETOOLONG // remove errno.h's definition#define ENAMETOOLONG WSAENAMETOOLONG#define EHOSTDOWN WSAEHOSTDOWN#define EHOSTUNREACH WSAEHOSTUNREACH#undef ENOTEMPTY // remove errno.h's definition#define ENOTEMPTY WSAENOTEMPTY#define EPROCLIM WSAEPROCLIM#define EUSERS WSAEUSERS#define EDQUOT WSAEDQUOT#define ESTALE WSAESTALE#define EREMOTE WSAEREMOTE#undef EACCES#define SOCKET_EACCES WSAEACCES#endif // WIN32#ifdef POSIX#define INVALID_SOCKET (-1)#define SOCKET_ERROR (-1)#define closesocket(s) close(s)#endif // POSIXnamespace utils_base {inline bool IsBlockingError(int e) { return (e == EWOULDBLOCK) || (e == EAGAIN) || (e == EINPROGRESS);}// General interface for the socket implementations of various networks. The// methods match those of normal UNIX sockets very closely.class Socket {public: virtual ~Socket() {} // Returns the address to which the socket is bound. If the socket is not // bound, then the any-address is returned. virtual SocketAddress GetLocalAddress() const = 0; // Returns the address to which the socket is connected. If the socket is // not connected, then the any-address is returned. virtual SocketAddress GetRemoteAddress() const = 0; virtual int Bind(const SocketAddress& addr) = 0; virtual int Connect(const SocketAddress& addr) = 0; virtual int Send(const void *pv, size_t cb) = 0; virtual int SendTo(const void *pv, size_t cb, const SocketAddress& addr) = 0; virtual int Recv(void *pv, size_t cb) = 0; virtual int RecvFrom(void *pv, size_t cb, SocketAddress *paddr) = 0; virtual int Listen(int backlog) = 0; virtual Socket *Accept(SocketAddress *paddr) = 0; virtual int Close() = 0; virtual int GetError() const = 0; virtual void SetError(int error) = 0; inline bool IsBlocking() const { return IsBlockingError(GetError()); } enum ConnState { CS_CLOSED, CS_CONNECTING, CS_CONNECTED }; virtual ConnState GetState() const = 0; // Fills in the given uint16 with the current estimate of the MTU along the // path to the address to which this socket is connected. virtual int EstimateMTU(uint16* mtu) = 0; enum Option { OPT_DONTFRAGMENT }; virtual int SetOption(Option opt, int value) = 0;protected: Socket() {}private: DISALLOW_EVIL_CONSTRUCTORS(Socket);};} // namespace utils_base#endif // UTILS_BASE_SOCKET_H_
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -