1 /* 2 * COPYRIGHT: See COPYING in the top level directory 3 * PROJECT: ReactOS HTTP Daemon 4 * FILE: include/socket.h 5 */ 6 #ifndef __SOCKET_H 7 #define __SOCKET_H 8 #include <stdio.h> 9 #include <winsock2.h> 10 #include <thread.h> 11 #include <list.h> 12 #include <exception> 13 #include <assert.h> 14 15 #define MAX_PENDING_CONNECTS 4 // The backlog allowed for listen() 16 17 VOID InitWinsock(); 18 VOID DeinitWinsock(); 19 20 class CSocket; 21 class CClientSocket; 22 class CServerClientSocket; 23 class CServerClientThread; 24 class CServerSocket; 25 26 typedef CSocket* LPCSocket; 27 typedef CClientSocket* LPCClientSocket; 28 typedef CServerClientSocket* LPCServerClientSocket; 29 typedef CServerClientThread* LPCServerClientThread; 30 typedef CServerSocket* LPCServerSocket; 31 32 class ESocket { 33 public: 34 ESocket() { Description = NULL; } 35 ESocket(LPTSTR description) { Description = description; } 36 LPTSTR what() { return Description; } 37 protected: 38 LPTSTR Description; 39 }; 40 41 class ESocketWinsock : public ESocket { 42 public: 43 ESocketWinsock(LPTSTR description) { Description = description; } 44 }; 45 46 class ESocketDll : public ESocket { 47 public: 48 ESocketDll(LPTSTR description) { Description = description; } 49 }; 50 51 class ESocketOpen : public ESocket { 52 public: 53 ESocketOpen(LPTSTR description) { Description = description; } 54 }; 55 56 class ESocketClose : public ESocket { 57 public: 58 ESocketClose(LPTSTR description) { Description = description; } 59 }; 60 61 class ESocketSend : public ESocket { 62 public: 63 ESocketSend(LPTSTR description) { Description = description; } 64 }; 65 66 class ESocketReceive : public ESocket { 67 public: 68 ESocketReceive(LPTSTR description) { Description = description; } 69 }; 70 71 72 class CSocket { 73 public: 74 CSocket(); 75 virtual ~CSocket(); 76 virtual SOCKET GetSocket(); 77 virtual VOID SetSocket(SOCKET socket); 78 virtual SOCKADDR_IN GetSockAddrIn(); 79 virtual VOID SetSockAddrIn(SOCKADDR_IN sockaddrin); 80 virtual VOID SetEvents(LONG lEvents); 81 virtual LONG GetEvents(); 82 virtual VOID SetPort( UINT nPort) {}; 83 virtual VOID Open(); 84 virtual VOID Close(); 85 virtual INT Transmit( LPCSTR lpsBuffer, UINT nLength) { return 0; }; 86 virtual INT Receive(LPSTR lpsBuffer, UINT nLength) { return 0; }; 87 virtual INT SendText( LPCSTR lpsStr) { return 0; }; 88 protected: 89 SOCKET Socket; 90 SOCKADDR_IN SockAddrIn; 91 WSAEVENT Event; 92 UINT Port; 93 BOOL Active; 94 private: 95 LONG Events; 96 }; 97 98 class CServerClientSocket : public CSocket { 99 public: 100 CServerClientSocket() {}; 101 CServerClientSocket(LPCServerSocket lpServerSocket); 102 CServerSocket *GetServerSocket(); 103 virtual INT Transmit( LPCSTR lpsBuffer, UINT nLength); 104 virtual INT Receive(LPSTR lpsBuffer, UINT nLength); 105 virtual INT SendText( LPCSTR lpsText); 106 virtual VOID MessageLoop(); 107 virtual VOID OnRead() {}; 108 //virtual VOID OnWrite() {}; 109 virtual VOID OnClose() {}; 110 protected: 111 LPCServerSocket ServerSocket; 112 }; 113 114 class CServerClientThread : public CThread { 115 public: 116 CServerClientThread() {}; 117 CServerClientThread(CServerClientSocket *socket); 118 virtual ~CServerClientThread(); 119 protected: 120 CServerClientSocket *ClientSocket; 121 }; 122 123 class CServerSocket : public CSocket { 124 public: 125 CServerSocket(); 126 virtual ~CServerSocket(); 127 virtual VOID SetPort( UINT nPort); 128 virtual VOID Open(); 129 virtual VOID Close(); 130 virtual LPCServerClientSocket OnGetSocket(LPCServerSocket lpServerSocket); 131 virtual LPCServerClientThread OnGetThread(LPCServerClientSocket lpSocket); 132 virtual VOID OnAccept( LPCServerClientThread lpThread) {}; 133 virtual VOID MessageLoop(); 134 VOID InsertClient(LPCServerClientThread lpClient); 135 VOID RemoveClient(LPCServerClientThread lpClient); 136 protected: 137 CList<LPCServerClientThread> Connections; 138 }; 139 140 #endif /* __SOCKET_H */ 141