plugins/consoles/rcons/server/win32/ServerSocket.cpp
changeset 0 7f656887cf89
equal deleted inserted replaced
-1:000000000000 0:7f656887cf89
       
     1 // ServerSocket.cpp
       
     2 // 
       
     3 // Copyright (c) 2010 Accenture. All rights reserved.
       
     4 // This component and the accompanying materials are made available
       
     5 // under the terms of the "Eclipse Public License v1.0"
       
     6 // which accompanies this distribution, and is available
       
     7 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 // 
       
     9 // Initial Contributors:
       
    10 // Accenture - Initial contribution
       
    11 //
       
    12 
       
    13 #include "Misc.h"
       
    14 #include "stdafx.h"
       
    15 #include "console_host.h"
       
    16 #include "ServerSocket.h"
       
    17 #include "ClientSocket.h"
       
    18 
       
    19 
       
    20 CServerSocket* CServerSocket::New(CWindow& aWindow)
       
    21 	{
       
    22 	std::auto_ptr<CServerSocket> self(new(EThrow) CServerSocket(aWindow));
       
    23 	self->Construct();
       
    24 	return self.release();
       
    25 	}
       
    26 
       
    27 CServerSocket::CServerSocket(CWindow& aWindow)
       
    28 	: CSocket(aWindow), iClientAcceptor(NULL)
       
    29 	{
       
    30 	}
       
    31 
       
    32 void CServerSocket::Listen(unsigned short aPort, unsigned int aBacklogSize, MSocketClientAcceptor& aClientAcceptor)
       
    33 	{
       
    34 	SOCKADDR_IN sockAddr;
       
    35 	memset(&sockAddr, 0, sizeof(sockAddr));
       
    36 	sockAddr.sin_family = AF_INET;
       
    37 	sockAddr.sin_addr.s_addr = htonl(INADDR_ANY);
       
    38 	sockAddr.sin_port = htons((u_short)aPort);
       
    39 
       
    40 	if (bind(iSocket, (SOCKADDR*)&sockAddr, sizeof(sockAddr)) == SOCKET_ERROR)
       
    41 		{
       
    42 		throw KExceptionSocketBindFailed;
       
    43 		}
       
    44 
       
    45 	if (listen(iSocket, aBacklogSize) == SOCKET_ERROR)
       
    46 		{
       
    47 		throw KExceptionSocketListenFailed;
       
    48 		}
       
    49 
       
    50 	iClientAcceptor = &aClientAcceptor;
       
    51 	}
       
    52 
       
    53 CServerSocket::~CServerSocket()
       
    54 	{
       
    55 	}
       
    56 
       
    57 void CServerSocket::Construct()
       
    58 	{
       
    59 	CSocket::Construct();
       
    60 
       
    61 	if (WSAAsyncSelect(iSocket, iWindow.Handle(), KSocketMessage, FD_ACCEPT | FD_CLOSE) == SOCKET_ERROR)
       
    62 		{
       
    63 		throw KExceptionSocketSelectFailed;
       
    64 		}
       
    65 	}
       
    66 
       
    67 void CServerSocket::HandleAccept()
       
    68 	{
       
    69 	ASSERT(iClientAcceptor);
       
    70 	SOCKET newSocket = WSAAccept(iSocket, NULL, NULL, NULL, 0);
       
    71 	if (newSocket == INVALID_SOCKET)
       
    72 		{
       
    73 		int err = WSAGetLastError();
       
    74 		throw KExceptionSocketAcceptFailed;
       
    75 		}
       
    76 	std::auto_ptr<CClientSocket> clientSocket(CClientSocket::New(iWindow, newSocket));
       
    77 	iClientAcceptor->HandleNewClient(*clientSocket.get());
       
    78 	clientSocket.release();
       
    79 	}
       
    80 
       
    81 LRESULT CServerSocket::HandleSocketMessage(LPARAM /*aLParam*/)
       
    82 	{
       
    83 	HandleAccept();
       
    84 	return 0;
       
    85 	}
       
    86