plugins/consoles/rcons/server/win32/Socket.cpp
author Tom Sutcliffe <thomas.sutcliffe@accenture.com>
Tue, 07 Dec 2010 17:29:09 +0000
changeset 90 ceac7084e2e5
parent 0 7f656887cf89
permissions -rw-r--r--
Implemented RObjectIx-based memoryaccess APIs. Upshot is that objinfo now works again on platforms that define FSHELL_NO_DOBJECTIX_SUPPORT.

// Socket.cpp
// 
// Copyright (c) 2010 Accenture. All rights reserved.
// This component and the accompanying materials are made available
// under the terms of the "Eclipse Public License v1.0"
// which accompanies this distribution, and is available
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
// 
// Initial Contributors:
// Accenture - Initial contribution
//

#include "Misc.h"
#include "stdafx.h"
#include "console_host.h"
#include "Socket.h"
#include "DynamicBuffer.h"


CSocket::CSocket(CWindow& aWindow)
	: iWindow(aWindow), iSocket(INVALID_SOCKET), iClosureObserver(NULL)
	{
	}

CSocket::CSocket(CWindow& aWindow, SOCKET aSocket)
	: iWindow(aWindow), iSocket(aSocket), iClosureObserver(NULL)
	{
	}

CSocket::~CSocket()
	{
	if (iSocket != INVALID_SOCKET)
		{
		closesocket(iSocket);
		}

	iWindow.RemoveMessageHandler(*this);
	}

void CSocket::SetClosureObserver(MSocketClosureObserver* aClosureObserver)
	{
	iClosureObserver = aClosureObserver;
	}

void CSocket::Construct()
	{
	if (iSocket == INVALID_SOCKET)
		{
		if ((iSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET)
			{
			throw KExceptionSocketConstructFailed;
			}
		BOOL b = 1;
		setsockopt(iSocket, IPPROTO_TCP, TCP_NODELAY, (char*)&b, sizeof(BOOL));
		}

	iWindow.AddMessageHandler(*this);
	}

void CSocket::HandleClosure()
	{
	if (iClosureObserver)
		{
		iClosureObserver->HandleSocketClosure(*this);
		}
	}

LRESULT CSocket::HandleWindowMessage(UINT aMessage, WPARAM aWParam, LPARAM aLParam)
	{
	if ((aMessage == KSocketMessage) && (aWParam == iSocket))
		{
		if (WSAGETSELECTEVENT(aLParam) == FD_CLOSE)
			{
			HandleClosure();
			}
		else
			{
			HandleSocketMessage(aLParam);
			}
		return 0;
		}
	return 1;
	}