Socket.h
上传用户:maryhy001
上传日期:2007-05-02
资源大小:2317k
文件大小:9k
- //File Name : Socket.h.
- //Standard Net Socket Class header file that writen by Devia.
- //This code is free all, you can distribute it.
- //Author : Devia Li(devia@sina.com)
- //DateTime : 2004-04-02
- #ifndef _SOCKET_H
- #define _SOCKET_H
- #include "Mutex.h"
- #include "SafeVector.h"
- #include "NetAddress.h"
- //CXBaseSocket --- Original socket class,
- // all socket inherited it.
- class NETLIBDLLEXPORT CXBaseSocket
- {
- public:
- CXBaseSocket();
- virtual ~CXBaseSocket();
- //create a new CXBaseSocket object.
- //!!!"WSAStartup" must be invoked here!!!
- virtual bool Create();
- //create the CXBaseSocket object from a socket descriptor.
- virtual bool CreateByHandle(const SOCKET s);
-
- //destroy the CXBaseSocket object.
- //!!!"WSACleanup" must be invoked here.!!!
- virtual void Destroy();
- //Get the socket's infos
- const SOCKET Get_Socket_Handle();
- const ushort Get_Socket_Type();
- //
- const CXNetAddress &Get_Socket_Address();
- void Set_Socket_Address(const uint wPort, const char *pIpAddr = NULL);
- void Set_Socket_Address(CXNetAddress &pNetAddr);
-
- //determine the socket handle whether is valid
- const bool Socket_IsValid(void);
- //determine the socket whether is in opened state.
- const bool Socket_IsOpened();
- //determine the socket whether or not is blocking mode.
- const bool Socket_IsBlockingMode();
-
- //operator overrides
- operator SOCKET();
-
- //get / set socket timeout value.
- int GetTimeouts(void);
- bool SetTimeouts(const int nMillSecTimeOut);
- //override functions
- //bWantOpen = TRUE ---> Open the socket
- // = FALSE ---> Close the socket
- virtual bool Open(const bool bWantOpen = true);
-
- //select operation...
- //nOpt : 0 --- read / 1 --- write
- int Select(int nOpt = 1, int nMillSecTimeOut = -1);
- //receive and send buffer data
- int ReceiveBuf(void *lpBuf, const int nSize);
- int SendBuf(const void *lpBuf, const int nSize);
- //get the local socket name
- string Get_LocalHost_Name();
- //get the local socket IP
- string Get_LocalHost_IP();
- //get the remote socket name
- string Get_RemoteHost_Name();
- //get the remote socket name
- string Get_RemoteHost_IP();
- //look up the host ip info from the host name.
- static string LookupHostName(const char *lpszHostName);
- protected:
- //get the socket event mask value
- long Get_SockEventMask();
- //set or reset the socket event.
- bool Set_ASyncStyles(HWND hWnd);
- //"listen、accept server socket functions
- virtual bool Listen(const int nQueueSize = 2);
- virtual SOCKET Accept();
- //"AsyncSelect" functions
- virtual bool AsyncSelect(HWND hWnd, long lSockEventMask);
-
- //!!! Socket IO controler(For example: Blocking and None Blocking control)
- // If "AsyncSelect" function has been issued on a socket,
- // then any attempt to use ioctlsocket to set the socket
- // back to blocking mode will fail with WSAEINVAL.
- // use can get the "m_bBlockingMode" member variable flag
- // determine the socket current whether is in Blocking mode.
- bool IOCtrl(long lCommand, ulong ulArgp, int &nResult);
-
- protected:
- //common socket properties
- SOCKET m_hSocket; //base socket handle
- CXNetAddress m_saSockAddr; //........... address object
- bool m_bIsActivity; //........... whether or not in the activity state
- bool m_bIsServer; //flag the socket whether is server socket.
- //server socket listening pending queue size
- int m_nQueueSize;
-
- //Windows socket event flag.
- long m_lEventMask;
- //flag the socket whether or not is blocking mode.
- bool m_bBlockingMode;
- private:
- int m_stSockType; //........... socket type
- bool m_bInitWinSockDll; //flag whether or not initialize socket library successfully.
- int m_nMillSecTimeOut; //socket timeout value.
- };
- //CXWinSocket --- windows socket based on the socket event
- // and windows messages.
- class NETLIBDLLEXPORT CXWinSocket : public CXBaseSocket
- {
- public:
- CXWinSocket();
- virtual ~CXWinSocket();
- //create a new CXWinSocket object.
- virtual bool Create();
- //create the CXWinSocket object from a socket descriptor.
- virtual bool CreateByHandle(const SOCKET s);
-
- //destroy the CXWinSocket object.
- virtual void Destroy();
- //"AsyncSelect" functions
- virtual bool AsyncSelect(HWND hWnd, long lSockEventMask);
-
- //get the socket message receiver window's handle.
- HWND Get_Socket_WndHandle();
- protected:
- //do the socket self event process.
- void DoEvent(int nEventMask);
- virtual void OnAccept(CXWinSocket *pSvrtSocket){ /*empty*/}
- virtual void OnClose(CXWinSocket *pPeerSocket){ /*empty*/}
- virtual void OnConnect(CXWinSocket *pClientSocket){ /*empty*/}
- virtual void OnRead(CXWinSocket *pSocket){ /*empty*/}
- virtual void OnWrite(CXWinSocket *pSocket){ /*empty*/}
- virtual void OnOutofBound(CXWinSocket *pSocket){ /*empty*/}
- private:
- //windows socket message procedure.
- static LRESULT CALLBACK SockMsgProc(
- HWND hwnd, // handle to window
- UINT uMsg, // message identifier
- WPARAM wParam, // first message parameter
- LPARAM lParam // second message parameter
- );
- private:
- HWND m_hWnd; //socket message receiver window's handle.
- };
- //########################################################
- class CXWinServerSocket;
- //CXWinServerSocket --- windows server client socket based on
- // the windows socket CXWinSocket.
- class NETLIBDLLEXPORT CXWinServerClientSocket : public CXWinSocket
- {
- public:
- CXWinServerClientSocket();
- virtual ~CXWinServerClientSocket();
- //create the server client socket object
- //from a socket handle and server socket object
- bool Create(const SOCKET s, CXWinServerSocket *pServerSocket);
- //destroy the CXWinSocket object.
- virtual void Destroy();
- protected:
- void OnClose(CXWinSocket *pPeerSocket);
- void OnRead(CXWinSocket *pSocket);
- void OnWrite(CXWinSocket *pSocket);
- void OnOutofBound(CXWinSocket *pSocket);
-
- private:
- CXWinServerSocket *m_pServerSocket;
- };
- //CXWinServerSocket --- windows server socket based on the
- // windows socket CXWinSocket.
- class NETLIBDLLEXPORT CXWinServerSocket : public CXWinSocket
- {
- public:
- CXWinServerSocket();
- virtual ~CXWinServerSocket();
- //create a new CXWinServerSocket object.
- virtual bool Create();
-
- //destroy the CXWinServerSocket object.
- virtual void Destroy();
- //override functions
- //bWantOpen = TRUE ---> Open the socket
- // = FALSE ---> Close the socket
- virtual bool Open(const bool bWantOpen = true);
-
- //get the client connections count.
- const uint Get_ClientCounts();
- //get someone client on special index position.
- CXWinSocket *Get_ClientFromPos(const uint index);
- //find someone client connection from it's descriptor.
- CXWinSocket *Find_ClientFromHandle(SOCKET s, int *pos);
- //do the client socket event.
- void DoClientEvent(CXWinSocket *pClientSocket, int nEventMask);
-
- protected:
- //client event do here.
- virtual void OnClientConnect(CXWinSocket *pClientSocket){ /*empty*/};
- virtual void OnClientClose(CXWinSocket *pClientSocket){ /*empty*/};
- virtual void OnClientRead(CXWinSocket *pClientSocket){ /*empty*/};
- virtual void OnClientWrite(CXWinSocket *pClientSocket){ /*empty*/};
- virtual void OnClientOutofBound(CXWinSocket *pClientSocket){ /*empty*/};
- //self event do here.
- virtual bool OnAcceptVerify(CXWinSocket *pClientSocket);
- virtual void OnAccept(CXWinSocket *pSvrtSocket);
- private:
- //add someone client connection.
- void Add_Client(CXWinSocket *pSocket);
- //remove special client connection.
- void Remove_Client(CXWinSocket *pSocket);
- //remove all client connections.
- void Remove_AllClients();
- private:
- safe_vector<CXWinSocket*> m_lstConnections; //client connection list.
- };
- //CXWinClientSocket --- windows server socket based on the
- // windows socket CXWinSocket.
- class NETLIBDLLEXPORT CXWinClientSocket : public CXWinSocket
- {
- public:
- CXWinClientSocket();
- virtual ~CXWinClientSocket();
- //create a new CXWinClientSocket object.
- virtual bool Create();
- //destroy the CXWinClientSocket object.
- virtual void Destroy();
-
- //override functions
- //bWantOpen = TRUE ---> Open the socket
- // = FALSE ---> Close the socket
- virtual bool Open(const bool bWantOpen = true);
- //overrides event functions
- protected:
- virtual void OnClose(CXWinSocket *pPeerSocket);
- virtual void OnConnect(CXWinSocket *pClientSocket);
- virtual void OnRead(CXWinSocket *pSocket);
- virtual void OnWrite(CXWinSocket *pSocket);
- virtual void OnOutofBound(CXWinSocket *pSocket);
- private:
- //connect to server socket function.
- bool ConnectServer();
- };
- //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- //CXClientSocket --- windows server socket based on the
- // windows socket CXWinSocket.
- class NETLIBDLLEXPORT CXClientSocket : public CXBaseSocket
- {
- public:
- CXClientSocket();
- virtual ~CXClientSocket();
- //create a new CXClientSocket object.
- virtual bool Create();
-
- //destroy the CXClientSocket object.
- virtual void Destroy();
-
- //override functions
- //bWantOpen = TRUE ---> Open the socket
- // = FALSE ---> Close the socket
- virtual bool Open(const bool bWantOpen = true);
- private:
- //connect to server socket function.
- bool ConnectServer();
- };
- #endif //_SOCKET_H