ReactOS  0.4.15-dev-4570-g4f8bbd1
CServerSocket Class Reference

#include <socket.h>

Inheritance diagram for CServerSocket:
Collaboration diagram for CServerSocket:

Public Member Functions

 CServerSocket ()
 
virtual ~CServerSocket ()
 
virtual VOID SetPort (UINT nPort)
 
virtual VOID Open ()
 
virtual VOID Close ()
 
virtual LPCServerClientSocket OnGetSocket (LPCServerSocket lpServerSocket)
 
virtual LPCServerClientThread OnGetThread (LPCServerClientSocket lpSocket)
 
virtual VOID OnAccept (LPCServerClientThread lpThread)
 
virtual VOID MessageLoop ()
 
VOID InsertClient (LPCServerClientThread lpClient)
 
VOID RemoveClient (LPCServerClientThread lpClient)
 
- Public Member Functions inherited from CSocket
 CSocket ()
 
virtual ~CSocket ()
 
virtual SOCKET GetSocket ()
 
virtual VOID SetSocket (SOCKET socket)
 
virtual SOCKADDR_IN GetSockAddrIn ()
 
virtual VOID SetSockAddrIn (SOCKADDR_IN sockaddrin)
 
virtual VOID SetEvents (LONG lEvents)
 
virtual LONG GetEvents ()
 
virtual INT Transmit (LPCSTR lpsBuffer, UINT nLength)
 
virtual INT Receive (LPSTR lpsBuffer, UINT nLength)
 
virtual INT SendText (LPCSTR lpsStr)
 

Protected Attributes

CList< LPCServerClientThreadConnections
 
- Protected Attributes inherited from CSocket
SOCKET Socket
 
SOCKADDR_IN SockAddrIn
 
WSAEVENT Event
 
UINT Port
 
BOOL Active
 

Detailed Description

Definition at line 123 of file socket.h.

Constructor & Destructor Documentation

◆ CServerSocket()

CServerSocket::CServerSocket ( )

Definition at line 171 of file socket.cpp.

172 {
173 }

◆ ~CServerSocket()

CServerSocket::~CServerSocket ( )
virtual

Definition at line 176 of file socket.cpp.

177 {
178  if (Active)
179  Close();
180 }
virtual VOID Close()
Definition: socket.cpp:212
_In_ ULONG _In_ BOOLEAN Active
Definition: potypes.h:560

Member Function Documentation

◆ Close()

VOID CServerSocket::Close ( void  )
virtual

Reimplemented from CSocket.

Definition at line 212 of file socket.cpp.

213 {
214  assert(Active);
215 
216  if (Event != WSA_INVALID_EVENT) {
217  // Tell winsock not to notify us about any events
219  throw ESocketClose(TS("Unable to select socket events."));
220 
221  if (!WSACloseEvent(Event))
222  throw ESocketClose(TS("Unable to close socket event."));
224  }
225 
226  CIterator<LPCServerClientThread> *i = Connections.CreateIterator();
227 
228  // Terminate and free all client threads
229  for (i->First(); !i->IsDone(); i->Next()) {
230  //i->CurrentItem()->Terminate();
231  delete i->CurrentItem();
232  }
233  delete i;
234  Connections.RemoveAll();
235 
238 
239  Active = FALSE;
240 }
#define SOCKET_ERROR
Definition: winsock.h:333
#define TS(x)
Definition: error.h:11
#define assert(x)
Definition: debug.h:53
#define closesocket
Definition: ncftp.h:477
#define WSA_INVALID_EVENT
Definition: winsock2.h:595
#define FALSE
Definition: types.h:117
SOCKET Socket
Definition: socket.h:87
INT WSAAPI WSAEventSelect(IN SOCKET s, IN WSAEVENT hEventObject, IN LONG lNetworkEvents)
Definition: select.c:182
BOOL WSAAPI WSACloseEvent(IN WSAEVENT hEvent)
Definition: event.c:23
#define INVALID_SOCKET
Definition: winsock.h:332
CList< LPCServerClientThread > Connections
Definition: socket.h:137
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
_In_ ULONG _In_ BOOLEAN Active
Definition: potypes.h:560

Referenced by ~CServerSocket().

◆ InsertClient()

VOID CServerSocket::InsertClient ( LPCServerClientThread  lpClient)

Definition at line 294 of file socket.cpp.

295 {
296  Connections.Insert(lpClient);
297 }
CList< LPCServerClientThread > Connections
Definition: socket.h:137

Referenced by MessageLoop().

◆ MessageLoop()

VOID CServerSocket::MessageLoop ( )
virtual

Definition at line 251 of file socket.cpp.

252 {
253  UINT nStatus;
254  INT nAddrLen;
255  SOCKET ClientSocket;
257  WSANETWORKEVENTS NetworkEvents;
258  LPCServerClientSocket lpClient;
259  LPCServerClientThread lpThread;
260 
261  nStatus = WSAWaitForMultipleEvents(1, &Event, FALSE, 0, FALSE);
262  if ((nStatus == 0) && (WSAEnumNetworkEvents(Socket, Event, &NetworkEvents) != SOCKET_ERROR)) {
263  if ((NetworkEvents.lNetworkEvents & FD_ACCEPT) != 0) {
264  lpClient = OnGetSocket(this);
265  nAddrLen = sizeof(SockAddrIn);
266  ClientSocket = accept(Socket, (SOCKADDR *) &SockAddrIn, &nAddrLen);
267  if (ClientSocket != INVALID_SOCKET) {
268  // Set socket handle
269  lpClient->SetSocket(ClientSocket);
270  // Set socket address
271  lpClient->SetSockAddrIn(SockAddrIn);
272  // Set winsock events
273  lpClient->SetEvents(FD_READ | FD_CLOSE);
274  // Create client connection thread
275  lpThread = OnGetThread(lpClient);
276  // Add client thread to connection list
277  InsertClient(lpThread);
278  // Call OnAccept event handler
279  OnAccept(lpThread);
280  } else {
281  delete lpClient;
282  lpClient = NULL;
283  throw ESocketOpen(TS("No more sockets available."));
284  }
285  }
286  /*if ((NetworkEvents.lNetworkEvents & FD_CONNECT) != 0) {
287  }
288  if ((NetworkEvents.lNetworkEvents & FD_CLOSE) != 0) {
289  }*/
290  }
291 }
#define SOCKET_ERROR
Definition: winsock.h:333
virtual LPCServerClientThread OnGetThread(LPCServerClientSocket lpSocket)
Definition: socket.cpp:312
#define FD_ACCEPT
Definition: winsock.h:408
virtual VOID SetSockAddrIn(SOCKADDR_IN sockaddrin)
Definition: socket.cpp:60
virtual VOID SetEvents(LONG lEvents)
Definition: socket.cpp:66
SOCKADDR_IN SockAddrIn
Definition: socket.h:90
#define TS(x)
Definition: error.h:11
#define FD_READ
Definition: winsock.h:405
DWORD WSAAPI WSAWaitForMultipleEvents(IN DWORD cEvents, IN CONST WSAEVENT FAR *lphEvents, IN BOOL fWaitAll, IN DWORD dwTimeout, IN BOOL fAlertable)
Definition: event.c:75
int32_t INT
Definition: typedefs.h:58
virtual VOID OnAccept(LPCServerClientThread lpThread)
Definition: socket.h:132
#define FALSE
Definition: types.h:117
SOCKET Socket
Definition: socket.h:87
virtual VOID SetSocket(SOCKET socket)
Definition: socket.cpp:47
#define FD_CLOSE
Definition: winsock.h:410
#define INVALID_SOCKET
Definition: winsock.h:332
INT WSAAPI WSAEnumNetworkEvents(IN SOCKET s, IN WSAEVENT hEventObject, OUT LPWSANETWORKEVENTS lpNetworkEvents)
Definition: event.c:94
SOCKET WSAAPI accept(IN SOCKET s, OUT LPSOCKADDR addr, OUT INT FAR *addrlen)
Definition: socklife.c:23
virtual LPCServerClientSocket OnGetSocket(LPCServerSocket lpServerSocket)
Definition: socket.cpp:306
VOID InsertClient(LPCServerClientThread lpClient)
Definition: socket.cpp:294
unsigned int UINT
Definition: ndis.h:50
#define NULL
Definition: types.h:112
UINT_PTR SOCKET
Definition: winsock.h:47

Referenced by CHttpDaemonThread::Execute().

◆ OnAccept()

virtual VOID CServerSocket::OnAccept ( LPCServerClientThread  lpThread)
inlinevirtual

Reimplemented in CHttpDaemon.

Definition at line 132 of file socket.h.

132 {};

Referenced by MessageLoop().

◆ OnGetSocket()

LPCServerClientSocket CServerSocket::OnGetSocket ( LPCServerSocket  lpServerSocket)
virtual

Reimplemented in CHttpDaemon.

Definition at line 306 of file socket.cpp.

307 {
308  return NULL;
309 }
#define NULL
Definition: types.h:112

Referenced by MessageLoop().

◆ OnGetThread()

LPCServerClientThread CServerSocket::OnGetThread ( LPCServerClientSocket  lpSocket)
virtual

Reimplemented in CHttpDaemon.

Definition at line 312 of file socket.cpp.

313 {
314  return NULL;
315 }
#define NULL
Definition: types.h:112

Referenced by MessageLoop().

◆ Open()

VOID CServerSocket::Open ( )
virtual

Reimplemented from CSocket.

Definition at line 183 of file socket.cpp.

184 {
185  assert(!Active);
186 
187  // Convert to network ordering
189 
190  if (Socket == INVALID_SOCKET) {
191  // Create socket
193  if (Socket == INVALID_SOCKET)
194  throw ESocketOpen(TS("Unable to allocate a socket."));
195  }
196 
197  // Associate an address with server socket
198  if (bind(Socket, (struct sockaddr FAR *) &SockAddrIn, sizeof(SockAddrIn)) == SOCKET_ERROR)
199  throw ESocketOpen(TS("Unable to associate address with socket."));
200 
201  // Listen for incoming connections
203  throw ESocketOpen(TS("Unable to listen on socket."));
204 
205  // Associate network events with socket
207 
208  Active = TRUE;
209 }
#define SOCKET_ERROR
Definition: winsock.h:333
SOCKET WSAAPI socket(IN INT af, IN INT type, IN INT protocol)
Definition: socklife.c:143
u_short sin_port
Definition: winsock.h:511
#define FD_ACCEPT
Definition: winsock.h:408
#define FD_CONNECT
Definition: winsock.h:409
#define TRUE
Definition: types.h:120
INT WSAAPI bind(IN SOCKET s, IN CONST struct sockaddr *name, IN INT namelen)
Definition: socklife.c:36
virtual VOID SetEvents(LONG lEvents)
Definition: socket.cpp:66
SOCKADDR_IN SockAddrIn
Definition: socket.h:90
#define TS(x)
Definition: error.h:11
#define assert(x)
Definition: debug.h:53
SOCKET Socket
Definition: socket.h:87
#define FD_CLOSE
Definition: winsock.h:410
#define INVALID_SOCKET
Definition: winsock.h:332
#define MAX_PENDING_CONNECTS
Definition: socket.h:15
#define htons(x)
Definition: module.h:213
INT WSAAPI listen(IN SOCKET s, IN INT backlog)
Definition: sockctrl.c:123
UINT Port
Definition: socket.h:92
#define AF_INET
Definition: tcpip.h:117
_In_ ULONG _In_ BOOLEAN Active
Definition: potypes.h:560
#define FAR
Definition: zlib.h:34
#define SOCK_STREAM
Definition: tcpip.h:118

◆ RemoveClient()

VOID CServerSocket::RemoveClient ( LPCServerClientThread  lpClient)

Definition at line 300 of file socket.cpp.

301 {
302  Connections.Remove(lpClient);
303 }
CList< LPCServerClientThread > Connections
Definition: socket.h:137

Referenced by CServerClientThread::~CServerClientThread().

◆ SetPort()

VOID CServerSocket::SetPort ( UINT  nPort)
virtual

Reimplemented from CSocket.

Definition at line 243 of file socket.cpp.

244 {
245  assert(!Active);
246 
247  Port = nPort;
248 }
#define assert(x)
Definition: debug.h:53
UINT Port
Definition: socket.h:92
_In_ ULONG _In_ BOOLEAN Active
Definition: potypes.h:560

Member Data Documentation

◆ Connections

CList<LPCServerClientThread> CServerSocket::Connections
protected

Definition at line 137 of file socket.h.

Referenced by Close(), InsertClient(), and RemoveClient().


The documentation for this class was generated from the following files: