plugins/consoles/rcons/server/win32/ClientSocket.cpp
changeset 0 7f656887cf89
equal deleted inserted replaced
-1:000000000000 0:7f656887cf89
       
     1 // ClientSocket.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 "ClientSocket.h"
       
    16 #include "DynamicBuffer.h"
       
    17 
       
    18 const int KInitialBufferLength = 1;
       
    19 const int KNotifyReadComplete = 9000;
       
    20 
       
    21 
       
    22 CClientSocket* CClientSocket::New(CWindow& aWindow, SOCKET aSocket)
       
    23 	{
       
    24 	std::auto_ptr<CClientSocket> self(new(EThrow) CClientSocket(aWindow, aSocket));
       
    25 	self->Construct();
       
    26 	return self.release();
       
    27 	}
       
    28 
       
    29 CClientSocket::CClientSocket(CWindow& aWindow, SOCKET aSocket)
       
    30 	: CSocket(aWindow, aSocket), iReadHandler(NULL), iReadBuf(NULL), iReadLength(0)
       
    31 	{
       
    32 	}
       
    33 
       
    34 void CClientSocket::Read(char* aBuf, int aLength, MSocketReadHandler& aHandler)
       
    35 	{
       
    36 	ASSERT(iReadHandler == NULL);
       
    37 	iReadBuf = aBuf;
       
    38 	iReadLength = aLength;
       
    39 	iReadHandler = &aHandler;
       
    40 	HandleRead(FALSE);
       
    41 	}
       
    42 
       
    43 void CClientSocket::Write(char* aBuf, int aLength)
       
    44 	{
       
    45 	iWriteBuf->Add(aBuf, aLength);
       
    46 	HandleWrite();
       
    47 	}
       
    48 
       
    49 CClientSocket::~CClientSocket()
       
    50 	{
       
    51 	delete iWriteBuf;
       
    52 	}
       
    53 
       
    54 void CClientSocket::Construct()
       
    55 	{
       
    56 	CSocket::Construct();
       
    57 
       
    58 	if (WSAAsyncSelect(iSocket, iWindow.Handle(), KSocketMessage, FD_READ | FD_WRITE | FD_CLOSE) == SOCKET_ERROR)
       
    59 		{
       
    60 		throw KExceptionSocketSelectFailed;
       
    61 		}
       
    62 
       
    63 	iWriteBuf = CDynamicWriteBuffer::New(KInitialBufferLength);
       
    64 	}
       
    65 
       
    66 void CClientSocket::HandleRead(bool aNotifySynchronously)
       
    67 	{
       
    68 	if (iReadLength > 0)
       
    69 		{
       
    70 		int ret = recv(iSocket, iReadBuf, iReadLength, 0);
       
    71 		if (ret == SOCKET_ERROR)
       
    72 			{
       
    73 			if (WSAGetLastError() != WSAEWOULDBLOCK)
       
    74 				{
       
    75 				HandleClosure();
       
    76 				}
       
    77 			}
       
    78 		else if (ret == 0)
       
    79 			{
       
    80 			HandleClosure();
       
    81 			}
       
    82 		else
       
    83 			{
       
    84 			iReadLength -= ret;
       
    85 			ASSERT(iReadLength >= 0);
       
    86 			if (iReadLength == 0)
       
    87 				{
       
    88 				if (aNotifySynchronously)
       
    89 					{
       
    90 					NotifyReadComplete();
       
    91 					}
       
    92 				else
       
    93 					{
       
    94 					PostMessage(iWindow.Handle(), KSocketMessage, iSocket, KNotifyReadComplete);
       
    95 					}
       
    96 				}
       
    97 			else
       
    98 				{
       
    99 				iReadBuf += ret;
       
   100 				}
       
   101 			}
       
   102 		}
       
   103 	}
       
   104 
       
   105 void CClientSocket::HandleWrite()
       
   106 	{
       
   107 	if (iWriteBuf->Length() > 0)
       
   108 		{
       
   109 		int ret = send(iSocket, iWriteBuf->Buffer(), iWriteBuf->Length(), 0);
       
   110 		if (ret == SOCKET_ERROR)
       
   111 			{
       
   112 			if (WSAGetLastError() != WSAEWOULDBLOCK)
       
   113 				{
       
   114 				HandleClosure();
       
   115 				}
       
   116 			}
       
   117 		else if (ret == 0)
       
   118 			{
       
   119 			HandleClosure();
       
   120 			}
       
   121 		else
       
   122 			{
       
   123 			iWriteBuf->Remove(ret);
       
   124 			}
       
   125 		}
       
   126 	}
       
   127 
       
   128 void CClientSocket::NotifyReadComplete()
       
   129 	{
       
   130 	ASSERT(iReadHandler);
       
   131 	MSocketReadHandler* readHandler = iReadHandler;
       
   132 	iReadBuf = NULL;
       
   133 	iReadLength = 0;
       
   134 	iReadHandler = NULL;
       
   135 	readHandler->SocketReadComplete();
       
   136 	}
       
   137 
       
   138 LRESULT CClientSocket::HandleSocketMessage(LPARAM aLParam)
       
   139 	{
       
   140 	switch (WSAGETSELECTEVENT(aLParam))
       
   141 		{
       
   142 		case FD_READ:
       
   143 			HandleRead(TRUE);
       
   144 			break;
       
   145 		case FD_WRITE:
       
   146 			HandleWrite();
       
   147 			break;
       
   148 		case KNotifyReadComplete:
       
   149 			NotifyReadComplete();
       
   150 			break;
       
   151 		default:
       
   152 			ASSERT(FALSE);
       
   153 		}
       
   154 	return 0;
       
   155 	}
       
   156