?? socket.h
字號:
/*************************************************************************** socket.h - description ------------------- begin : Fri Jul 20 2001 copyright : (C) 2001 by Mark email : alben@yeah.net ***************************************************************************//*************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * ***************************************************************************/#ifndef SOCKET_H#define SOCKET_H#include <sys/socket.h>#include <sys/types.h>#include <netinet/in.h>#include <sys/un.h>#include <arpa/inet.h>#include <netdb.h>#include <sys/time.h>#include <sys/types.h>#include <unistd.h>#include <fcntl.h>#include <errno.h>#include <tinycxx/exception.h>#include <tinycxx/stream.h>typedef unsigned short int tcport_t;class CInetAddress{protected: struct in_addr m_stInetAddr;public: CInetAddress(); CInetAddress(const struct in_addr& stInetAddr); CInetAddress(const char* sInetAddr); inline struct in_addr GetAddress() const { return m_stInetAddr; } bool IsInetAddress() const; CInetAddress& operator = (const struct in_addr& stInetAddr); CInetAddress& operator = (const char* sInetAddr);};class CUnixAddress{protected: enum { PATH_LEN_LIMIT = 99, }; char m_sPath[PATH_LEN_LIMIT+1]; public: CUnixAddress() { strcpy(m_sPath, ""); } CUnixAddress(const char* sPath) { strncpy(m_sPath, sPath, PATH_LEN_LIMIT); m_sPath[PATH_LEN_LIMIT] = 0; } CUnixAddress& operator = (const char* sPath) { strncpy(m_sPath, sPath, PATH_LEN_LIMIT); m_sPath[PATH_LEN_LIMIT] = 0; return *this; } const char* path() const { return m_sPath; } bool IsUnixAddress() const { return m_sPath[0]; }};class CSocket{protected: enum CState { S_INITIAL, S_AVAILABLE, S_BOUND, S_CONNECTED, S_CONNECTING }; protected: int m_iSocket; int m_iDomain; CState m_eState; bool m_bBlock; public: int SocketFd() { return m_iSocket; } void SetBlock(bool bBlock);protected: CSocket(); CSocket(const CSocket* pstSocket); CSocket(int iDomain, int iType, int iProtocol = 0); virtual ~CSocket() { End(); } void End(); void Bind(const CInetAddress& stInetAddr, tcport_t tPort); void Bind(const CUnixAddress& stUnixAddr); private: void Init(); inline void Socket(int iDomain, int iType, int iProtocol = 0) { if ((m_iSocket = socket(iDomain, iType, iProtocol)) < 0) throw CSocketException(strerror(errno), __FILE__, __LINE__); }};class CTcpStream;class CTcpSocket : public CSocket{protected: // return false will close new connected socket virtual bool OnAccept(const CInetAddress& stInetAddr, tcport_t tPort) { return true; } virtual bool OnAccept(const CUnixAddress& stUnixAddr) { return true; } virtual bool OnCheckOpenIntr() { return true; } virtual bool OnAcceptIntr() { return true; } public: CTcpSocket() {} CTcpSocket(const CSocket* pstSocket); //bind ... CTcpSocket(const CInetAddress& stInetAddr, tcport_t tPort); CTcpSocket(const CUnixAddress& stUnixAddr); virtual ~CTcpSocket() {} //open as server to listen ... void Open(int iBackLog = 5); //open as client to connect ... //if EINPROGRESS or EINTR error occurs return false //if other error occurs throw exception //else return true bool Open(const CInetAddress& stInetAddr, tcport_t tPort); bool Open(const CUnixAddress& stUnixAddr); //if open with return value false should call this function to check if already opened //if not connected return false //if error occurs throw exception //if connected return true bool CheckOpen(long lSec, long lUSec = 0); //accept incomming connections ... bool Accept(CTcpStream* pstTcpStream); private: void Listen(int iBackLog = 5);};class CTcpStream : public CTcpSocket, public CStream{ friend class CTcpSocket; public: CTcpStream(int iBufSize = 512); CTcpStream(const CSocket* pstSocket, int iBufSize = 512); CTcpStream(const CInetAddress& stInetAddr, tcport_t tPort, int iBufSize = 512); CTcpStream(const CUnixAddress& stUnixAddr, int iBufSize = 512); virtual ~CTcpStream() { flush(); } virtual int overflow(int ch); virtual int underflow(); protected: virtual bool OnWriteIntr() { return true; } virtual bool OnReadIntr() { return true; }};class CUdpSocket : public CSocket{protected: bool m_bConnected; CInetAddress m_stInetAddr; tcport_t m_tPort; //host byte order CUnixAddress m_stUnixAddr;public: CUdpSocket(int iDomain = PF_INET) : CSocket(iDomain, SOCK_DGRAM) { m_bConnected = false; m_tPort = 0; } CUdpSocket(const CInetAddress& stInetAddr, tcport_t tPort); CUdpSocket(const CUnixAddress& stUnixAddr); virtual ~CUdpSocket() { } void Connect(const CInetAddress& stInetAddr, tcport_t tPort); void Connect(const CUnixAddress& stUnixAddr); void Disconnect(); int Read(void* pMsg, int iLen); int Read(void* pMsg, int iLen, CInetAddress& stInetAddr, tcport_t& tPort); int Read(void* pMsg, int iLen, CUnixAddress& stUnixAddr); int Write(const void* pMsg, int iLen); int Write(const void* pMsg, int iLen, const CInetAddress& stInetAddr, tcport_t tPort); int Write(const void* pMsg, int iLen, const CUnixAddress& stUnixAddr);};class hton4{ friend ostream& operator << (ostream& os, const hton4& obj); private: unsigned long m_ulValue; public: hton4(unsigned long ulValue) { m_ulValue = htonl(ulValue); }};class hton2{ friend ostream& operator << (ostream& os, const hton2& obj); private: unsigned short m_ushValue; public: hton2(unsigned short ushValue) { m_ushValue = htons(ushValue); }};class ntoh4{ friend istream& operator >> (istream& is, const ntoh4& obj); private: unsigned long* m_pulValue; public: ntoh4(unsigned long& ulValue) { m_pulValue = &ulValue; } ntoh4(long& lValue) { m_pulValue = (unsigned long*)&lValue; } ntoh4(unsigned int& uiValue) { m_pulValue = (unsigned long*)&uiValue; } ntoh4(int& iValue) { m_pulValue = (unsigned long*)&iValue; }};class ntoh2{ friend istream& operator >> (istream& is, const ntoh2& obj); private: unsigned short* m_pushValue; public: ntoh2(unsigned short& ushValue) { m_pushValue = &ushValue; } ntoh2(short& shValue) { m_pushValue = (unsigned short*)&shValue; }};#endif
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -