plugins/consoles/rcons/server/win32/Socket.cpp
changeset 0 7f656887cf89
equal deleted inserted replaced
-1:000000000000 0:7f656887cf89
       
     1 // Socket.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 "Socket.h"
       
    17 #include "DynamicBuffer.h"
       
    18 
       
    19 
       
    20 CSocket::CSocket(CWindow& aWindow)
       
    21 	: iWindow(aWindow), iSocket(INVALID_SOCKET), iClosureObserver(NULL)
       
    22 	{
       
    23 	}
       
    24 
       
    25 CSocket::CSocket(CWindow& aWindow, SOCKET aSocket)
       
    26 	: iWindow(aWindow), iSocket(aSocket), iClosureObserver(NULL)
       
    27 	{
       
    28 	}
       
    29 
       
    30 CSocket::~CSocket()
       
    31 	{
       
    32 	if (iSocket != INVALID_SOCKET)
       
    33 		{
       
    34 		closesocket(iSocket);
       
    35 		}
       
    36 
       
    37 	iWindow.RemoveMessageHandler(*this);
       
    38 	}
       
    39 
       
    40 void CSocket::SetClosureObserver(MSocketClosureObserver* aClosureObserver)
       
    41 	{
       
    42 	iClosureObserver = aClosureObserver;
       
    43 	}
       
    44 
       
    45 void CSocket::Construct()
       
    46 	{
       
    47 	if (iSocket == INVALID_SOCKET)
       
    48 		{
       
    49 		if ((iSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET)
       
    50 			{
       
    51 			throw KExceptionSocketConstructFailed;
       
    52 			}
       
    53 		BOOL b = 1;
       
    54 		setsockopt(iSocket, IPPROTO_TCP, TCP_NODELAY, (char*)&b, sizeof(BOOL));
       
    55 		}
       
    56 
       
    57 	iWindow.AddMessageHandler(*this);
       
    58 	}
       
    59 
       
    60 void CSocket::HandleClosure()
       
    61 	{
       
    62 	if (iClosureObserver)
       
    63 		{
       
    64 		iClosureObserver->HandleSocketClosure(*this);
       
    65 		}
       
    66 	}
       
    67 
       
    68 LRESULT CSocket::HandleWindowMessage(UINT aMessage, WPARAM aWParam, LPARAM aLParam)
       
    69 	{
       
    70 	if ((aMessage == KSocketMessage) && (aWParam == iSocket))
       
    71 		{
       
    72 		if (WSAGETSELECTEVENT(aLParam) == FD_CLOSE)
       
    73 			{
       
    74 			HandleClosure();
       
    75 			}
       
    76 		else
       
    77 			{
       
    78 			HandleSocketMessage(aLParam);
       
    79 			}
       
    80 		return 0;
       
    81 		}
       
    82 	return 1;
       
    83 	}
       
    84