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:
ESocket()34 	ESocket() { Description = NULL; }
ESocket(LPTSTR description)35 	ESocket(LPTSTR description)  { Description = description; }
what()36 	LPTSTR what()  { return Description; }
37 protected:
38 	LPTSTR Description;
39 };
40 
41 class ESocketWinsock : public ESocket {
42 public:
ESocketWinsock(LPTSTR description)43 	ESocketWinsock(LPTSTR description) { Description = description; }
44 };
45 
46 class ESocketDll : public ESocket {
47 public:
ESocketDll(LPTSTR description)48 	ESocketDll(LPTSTR description) { Description = description; }
49 };
50 
51 class ESocketOpen : public ESocket {
52 public:
ESocketOpen(LPTSTR description)53 	ESocketOpen(LPTSTR description) { Description = description; }
54 };
55 
56 class ESocketClose : public ESocket {
57 public:
ESocketClose(LPTSTR description)58 	ESocketClose(LPTSTR description) { Description = description; }
59 };
60 
61 class ESocketSend : public ESocket {
62 public:
ESocketSend(LPTSTR description)63 	ESocketSend(LPTSTR description) { Description = description; }
64 };
65 
66 class ESocketReceive : public ESocket {
67 public:
ESocketReceive(LPTSTR description)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();
SetPort(UINT nPort)82     virtual VOID SetPort( UINT nPort) {};
83 	virtual VOID Open();
84 	virtual VOID Close();
Transmit(LPCSTR lpsBuffer,UINT nLength)85 	virtual INT Transmit( LPCSTR lpsBuffer,  UINT nLength) { return 0; };
Receive(LPSTR lpsBuffer,UINT nLength)86 	virtual INT Receive(LPSTR lpsBuffer,  UINT nLength) { return 0; };
SendText(LPCSTR lpsStr)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:
CServerClientSocket()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();
OnRead()107 	virtual VOID OnRead() {};
108 	//virtual VOID OnWrite() {};
OnClose()109 	virtual VOID OnClose() {};
110 protected:
111 	LPCServerSocket ServerSocket;
112 };
113 
114 class CServerClientThread : public CThread {
115 public:
CServerClientThread()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);
OnAccept(LPCServerClientThread lpThread)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