⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 socketcc.h

📁 a open source project in linux for peer to peer on seep
💻 H
📖 第 1 页 / 共 4 页
字号:
/*                              string representing an IPv4 host name, an     *//*                              IPv4 address in an in_addr struct or an IPv6  *//*                              address in an in6_addr struct.                *//* operator==                 : Check equality of an IPAddress class.  Can be *//*                              compared against another IPAddress class, a   *//*                              string host name, or addresses stored in an   *//*                              in_addr or in6_addr structs.                  *//* SetHostName()              : Sets an IPAddress by providing a host name    *//*                              and a boolean variable to indicate whether we *//*                              want an IPv6 or IPv4 address.                 *//* SetAddress()               : Sets an IPAddress by providing a pointer to   *//*                              an address structure of the form in_addr or   *//*                              in6_addr.  This pointer is cast to (char *).  *//*                              A boolean value is used to indicate if this   *//*                              points to an IPv6 or IPv4 address.            *//* GetAddressFamily()         : Returns a constant integer of the address     *//*                              family represented by the IPAddress.          *//* GetHostName()              : Returns a constant string pointer to the      *//*                              hostname of the represented IP Address.       *//* Get_in_addr()              : Returns a (void *) to the address represented *//*                              by IPAddress. This must be cast to (in_addr *)*//*                              or to (in6_addr *) for use.                   *//* GetAddressString()         : Returns a constant string pointer to the      *//*                              string representation of the IP Address       *//*                              suitable for visual presentation.             *//* ConvertToAddressFamily()   : Converts the represented IP Address to the    *//*                              specified type.  An IPv4 Address can be mapped*//*                              to an IPv6 Address.  An IPv6 Address can only *//*                              be converted if it is an IPv6 Mapped IPv4     *//*                              Address.                                      *//* GetUnmappedAddressString() : Returns a constant string pointer to the      *//*                              string representation of the IP Address       *//*                              suitable for visual representation.  If the   *//*                              address is an IPv6 mapped IPv4 Address, the   *//*                              IPv4 string is returned.                      *//******************************************************************************/class IPAddress{    public:                            IPAddress();                            ~IPAddress() {}        IPAddress           & operator=(const IPAddress &cOrigAddr);        IPAddress           & operator=(const char *pcNewHostName);        IPAddress           & operator=(const in_addr sIP4Addr);        IPAddress           & operator=(const in6_addr sIP6Addr);        bool                operator==(const IPAddress &cOtherAddr) const;        bool                operator==(const char *pcNewHostName) const;        bool                operator==(const in_addr sIP4Addr) const;        bool                operator==(const in6_addr sIP6Addr) const;        operator            const char*() { return GetHostName(); }        void                SetHostName(const char *pcNewHostName, bool bIPv6);        void                SetAddress(const char *pcNewAddress, bool bIPv6);        const int           GetAddressFamily()   { return iAddressType;       }        const char *        GetHostName()        { privResolveHostName(); return strHostName.c_str(); }        const void *        Get_in_addr()        { return (void *) pcAddress; }        const int           Get_in_addr_length() { return iAddressLength;     }        const char *        GetAddressString()   { privResolveStrAddress(); return pcStrAddress; }        void                ConvertToAddressFamily(int iNewAddressFamily);        const char *        GetUnmappedAddressString();    private:        int                 iAddressType;        int                 iAddressLength;        std::string         strHostName;        char                pcAddress[16];        char                pcStrAddress[INET6_ADDRSTRLEN];        bool                bHostNameUnresolved, bStrAddressUnresolved;        static MutualExclusion  mutexKernelLock;        void                privSetAddress(const char *pcNewAddress,                                           const int iNewAddressLength);        void                privResolveHostName();        void                privResolveStrAddress();};/******************************************************************************//* class SocketBase.                                                          *//*                                                                            *//* This class is used to provide basic IP socket functionality within a C++   *//* class environment.  The socket descriptor is stored as a member variable   *//* within the socket class.  The member methods are simple wrappers to the    *//* stadard socket library function calls except they use IPAddress instances  *//* and port numbers rather than sockaddr structures.  Also, the methods throw *//* exceptions when errors occur allowing for affirmative programming within a *//* try block.  The provided methods are as follows:                           *//*                                                                            *//* Constructor   : Create either an IPv4 or IPv6 socket.  The default socket  *//*                 type is SOCK_STREAM or TCP.  A second constructor allows a *//*                 class instance to be created to represent a socket where   *//*                 a socket descriptor already exists.                        *//*                 A second constructor allows creation of a SocketBase class *//*                 given a socket descriptor.  This is primarily used to      *//*                 implement a cleaner interface for accepting connections on *//*                 a TCPServerSocket, but can also be used when migrating     *//*                 existing programs as it allows keeping existing code that  *//*                 creates the actual socket descriptor and then creating a   *//*                 socket class from that.  The overall effect is that these  *//*                 programs can be converted partially, one bit at a time.    *//* Destructor    : Destroy the created socket.                                *//* Connect()     : Connect the socket to the specified IP Address and port    *//*                 number.                                                    *//* Bind()        : Bind the local socket to the provided details, there are   *//*                 methods.  If the IP Address is specified then bind to that *//*                 IP Address, otherwise bind to the wildcard IP Address.     *//*                 The default port number of 0 specifies for the operating   *//*                 system to choose the port number.                          *//* Listen()      : The socket to begin listening for connections, specifying  *//*                 the maximum number of pending connections.                 *//* Accept()      : Accept a pending connection, returns an instance of a      *//*                 socket class of the specified templated type.  This socket *//*                 class must have a constructor that can be constructed from *//*                 a socket descriptor.                                       *//* Recv()        : Receive data into a buffer from the socket.                *//* RecvFrom()    : Receive data into a buffer from the socket, the IP Address *//*                 and port number of the sender are also returned.           *//* Send()        : The socket transmits the data to the remote socket.        *//* SendTo()      : The socket transmits the data payload to the specified IP  *//*                 address and port number.                                   *//* GetSockName() : Get details of the IP address and port number bound to the *//*                 local socket.                                              *//* GetPeerName() : Get details of the IP address and port number bound to the *//*                 remote socket.                                             *//* GetSockOpt()  : Get current option settings on the socket.  Needs the code *//*                 level and option name (same parameters as for getsockopt() *//*                 call).  Returns current setting of option and size of data.*//* SetSockOpt()  : Set current option settings on the socket.  Needs the code *//*                 level and option name (same parameters as for setsockopt() *//*                 call) as well as new value for the option.                 *//* FCntl()       : Call any fcntl command on the socket (same parameters as   *//*                 for fnctl()                                                *//*                                                                            *//* NOTE: SocketSet is made a friend of SocketBase so that it can access the   *//*       protected member variables of SocketBase when maintaining its fd_set.*//******************************************************************************/class SocketBase{    friend      class SocketSet;    public:                        SocketBase(bool bSocketIsIPv6 = false, int iSocketType = SOCK_STREAM);                        SocketBase(int iNewSockDesc);        virtual         ~SocketBase();        void            Connect(IPAddress &cServAddr, int iServPortNo);        void            Bind(IPAddress &cLocalAddr, int iLocalPort = 0);        void            Bind(int iLocalPort = 0);        void            Listen(int iBackLog);        void            Accept(SocketBase *pcConnection, IPAddress &cRemoteAddr, int &iRemotePortNo);        template<class ReturnType>        ReturnType *    Accept(IPAddress &cRemoteAddr, int &iRemotePortNo)        {            return new ReturnType(protAccept(cRemoteAddr, iRemotePortNo));        }        int             Recv(void *pBuffer, int iBufLength, unsigned int uiFlags);        int             RecvFrom(void *pBuffer, int iBufLength, unsigned int uiFlags,                                 IPAddress &cSourceIP, int &iSourcePortNumber);        int             Send(const void *pPayload, int iPayloadLength, unsigned int uiFlags);        int             SendTo(const void *pPayload, int iPayloadLength, unsigned int uiFlags,                               IPAddress &cDestinationIP, int iDestinationPortNumber);        void            GetSockName(IPAddress &cLocalAddr, int &iLocalPort);        void            GetPeerName(IPAddress &cRemoteAddr, int &iRemotePort);        void            GetSockOpt(int iCodeLevel, int iOptionName, void *pOptionData, int &iDataLength);        void            SetSockOpt(int iCodeLevel, int iOptionName, const void *pOptionData, int iDataLength);        int             FCntl(int iCommand, long lArgument);    protected:        MutualExclusion mutexSocket;        pid_t           pidMutExOwner;        int             iSockDesc;        bool            bIPv6Socket;        static bool     bFirstInstance;        static void     IOSignalHandler(int iSig) { return; }        int             protAccept(IPAddress &cRemoteAddr, int &iRemotePortNo);        void            WaitMutex() { mutexSocket.Lock(); pidMutExOwner = getpid(); }        void            ClearMutex() { mutexSocket.Unlock(); }};

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -