plugins/networking/winsockprt/src/wsp_socket.cpp
changeset 0 7f656887cf89
equal deleted inserted replaced
-1:000000000000 0:7f656887cf89
       
     1 // wsp_socket.cpp
       
     2 // 
       
     3 // Copyright (c) 2002 - 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 <in_sock.h>
       
    14 #include "wsp_socket.h"
       
    15 #include "wsp_session.h"
       
    16 #include "wsp_factory.h"
       
    17 #include "wsp_panic.h"
       
    18 #include "wsp_log.h"
       
    19 #undef SetPort // Defined the Microsoft header "winspool.h".
       
    20 #include <emulator.h>
       
    21 
       
    22 //
       
    23 // RWin32Socket.
       
    24 //
       
    25 
       
    26 TInt RWin32Socket::Open(RWin32Factory& aFactory, TType aType)
       
    27 	{
       
    28 	WSP_LOG(WspLog::Printf(_L("RWin32Socket::Open: aType: %d, this: 0x%x"), aType, this));
       
    29 	TPckgC<TType> typePckg(aType);
       
    30 	return CreateSubSession(aFactory, CWin32Factory::ENewSocket, typePckg);
       
    31 	}
       
    32 
       
    33 void RWin32Socket::Close()
       
    34 	{
       
    35 	WSP_LOG(WspLog::Printf(_L("RWin32Socket::Close: this: 0x%x"), this));
       
    36 	RWin32SubSession::Close();
       
    37 	}
       
    38 
       
    39 TInt RWin32Socket::Connect(const TInetAddr& aAddress, TRequestStatus& aStatus)
       
    40 	{
       
    41 	WSP_LOG(WspLog::Printf(_L("RWin32Socket::Connect: this: 0x%x"), this));
       
    42 	iWin32Message.Set(CWin32Socket::EConnect, aAddress, aStatus);
       
    43 	return MakeRequest(iWin32Message);
       
    44 	}
       
    45 
       
    46 TInt RWin32Socket::Send(const TDesC8& aSendBuffer, TRequestStatus& aStatus)
       
    47 	{
       
    48 	WSP_LOG(WspLog::Printf(_L("RWin32Socket::Send: this: 0x%x, bytes to send: %d"), this, aSendBuffer.Length()));
       
    49 	iWin32Message.Set(CWin32Socket::ESend, aSendBuffer, aStatus);
       
    50 	return MakeRequest(iWin32Message);
       
    51 	}
       
    52 
       
    53 TInt RWin32Socket::SendTo(const TDesC8& aSendBuffer, const TInetAddr& aAddress, TRequestStatus& aStatus)
       
    54 	{
       
    55 	WSP_LOG(WspLog::Printf(_L("RWin32Socket::SendTo: this: 0x%x, bytes to send: %d"), this, aSendBuffer.Length()));
       
    56 	TPtr8 addressPtr(const_cast<TUint8*>(aAddress.Ptr()), aAddress.Length(), aAddress.MaxLength()); // Slight hack - need to send two read buffers, but the framework doesn't support this, so send the address as a write buffer.
       
    57 	iWin32Message.Set(CWin32Socket::ESendTo, aSendBuffer, addressPtr, aStatus);
       
    58 	return MakeRequest(iWin32Message);
       
    59 	}
       
    60 
       
    61 TInt RWin32Socket::Receive(TDes8& aReceiveBuffer, TRequestStatus& aStatus)
       
    62 	{
       
    63 	WSP_LOG(WspLog::Printf(_L("RWin32Socket::Receive: this: 0x%x, bytes to receive: %d"), this, aReceiveBuffer.Length()));
       
    64 	iWin32Message.Set(CWin32Socket::EReceive, aReceiveBuffer, aStatus);
       
    65 	return MakeRequest(iWin32Message);
       
    66 	}
       
    67 
       
    68 TInt RWin32Socket::ReceiveFrom(TDes8& aReceiveBuffer, TInetAddr& aAddress, TRequestStatus& aStatus)
       
    69 	{
       
    70 	WSP_LOG(WspLog::Printf(_L("RWin32Socket::ReceiveFrom: this: 0x%x, bytes to receive: %d"), this, aReceiveBuffer.Length()));
       
    71 	TPtrC8 addressPtr((TUint8*)&aAddress, 0); // Slight hack - need to send two write buffers, but the framework doesn't support this, so send the address as a read buffer.
       
    72 	iWin32Message.Set(CWin32Socket::EReceiveFrom, addressPtr, aReceiveBuffer, aStatus);
       
    73 	return MakeRequest(iWin32Message);
       
    74 	}
       
    75 
       
    76 void RWin32Socket::CancelConnect()
       
    77 	{
       
    78 	TWin32Message cancelWin32Message;
       
    79 	cancelWin32Message.Set(CWin32Socket::ECancelConnect);
       
    80 	MakeRequest(cancelWin32Message); // Throw away return code - can't do anything useful with it.
       
    81 	}
       
    82 
       
    83 void RWin32Socket::CancelSend()
       
    84 	{
       
    85 	TWin32Message cancelWin32Message;
       
    86 	cancelWin32Message.Set(CWin32Socket::ECancelSend);
       
    87 	MakeRequest(cancelWin32Message); // Throw away return code - can't do anything useful with it.
       
    88 	}
       
    89 
       
    90 void RWin32Socket::CancelSendTo()
       
    91 	{
       
    92 	TWin32Message cancelWin32Message;
       
    93 	cancelWin32Message.Set(CWin32Socket::ECancelSendTo);
       
    94 	MakeRequest(cancelWin32Message); // Throw away return code - can't do anything useful with it.
       
    95 	}
       
    96 
       
    97 void RWin32Socket::CancelReceive()
       
    98 	{
       
    99 	TWin32Message cancelWin32Message;
       
   100 	cancelWin32Message.Set(CWin32Socket::ECancelReceive);
       
   101 	MakeRequest(cancelWin32Message); // Throw away return code - can't do anything useful with it.
       
   102 	}
       
   103 
       
   104 void RWin32Socket::CancelReceiveFrom()
       
   105 	{
       
   106 	TWin32Message cancelWin32Message;
       
   107 	cancelWin32Message.Set(CWin32Socket::ECancelReceiveFrom);
       
   108 	MakeRequest(cancelWin32Message); // Throw away return code - can't do anything useful with it.
       
   109 	}
       
   110 
       
   111 TInt RWin32Socket::GetSocketName(TInetAddr& aAddress) const
       
   112 	{
       
   113 	WSP_LOG(WspLog::Printf(_L("RWin32Socket::GetSocketName: this: 0x%x"), this));
       
   114 	TRequestStatus status;
       
   115 	iWin32Message.Set(CWin32Socket::EGetSocketName, aAddress, status);
       
   116 	TInt err = MakeRequest(iWin32Message);
       
   117 	if (err)
       
   118 		{
       
   119 		return err;
       
   120 		}
       
   121 	else
       
   122 		{
       
   123 		User::WaitForRequest(status);
       
   124 		return status.Int();
       
   125 		}
       
   126 	}
       
   127 
       
   128 TInt RWin32Socket::Bind(const TInetAddr& aAddress)
       
   129 	{
       
   130 	WSP_LOG(WspLog::Printf(_L("RWin32Socket::Bind: this: 0x%x"), this));
       
   131 	TRequestStatus status;
       
   132 	iWin32Message.Set(CWin32Socket::EBind, aAddress, status);
       
   133 	TInt err = MakeRequest(iWin32Message);
       
   134 	if (err)
       
   135 		{
       
   136 		return err;
       
   137 		}
       
   138 	else
       
   139 		{
       
   140 		User::WaitForRequest(status);
       
   141 		return status.Int();
       
   142 		}
       
   143 	}
       
   144 
       
   145 TInt RWin32Socket::GetPeerName(TInetAddr& aAddress) const
       
   146 	{
       
   147 	WSP_LOG(WspLog::Printf(_L("RWin32Socket::GetPeerName: this: 0x%x"), this));
       
   148 	TRequestStatus status;
       
   149 	iWin32Message.Set(CWin32Socket::EGetPeerName, aAddress, status);
       
   150 	TInt err = MakeRequest(iWin32Message);
       
   151 	if (err)
       
   152 		{
       
   153 		return err;
       
   154 		}
       
   155 	else
       
   156 		{
       
   157 		User::WaitForRequest(status);
       
   158 		return status.Int();
       
   159 		}
       
   160 	}
       
   161 
       
   162 TInt RWin32Socket::Listen(TUint aQueueSize)
       
   163 	{
       
   164 	WSP_LOG(WspLog::Printf(_L("RWin32Socket::Listen: this: 0x%x"), this));
       
   165 	TPckgC<TUint> queueSizePckg(aQueueSize);
       
   166 	TRequestStatus status;
       
   167 	iWin32Message.Set(CWin32Socket::EListen, queueSizePckg, status);
       
   168 	TInt err = MakeRequest(iWin32Message);
       
   169 	if (err)
       
   170 		{
       
   171 		return err;
       
   172 		}
       
   173 	else
       
   174 		{
       
   175 		User::WaitForRequest(status);
       
   176 		return status.Int();
       
   177 		}
       
   178 	}
       
   179 
       
   180 TInt RWin32Socket::Accept(RWin32Socket& aNewSocket, TRequestStatus& aStatus)
       
   181 	{
       
   182 	WSP_LOG(WspLog::Printf(_L("RWin32Socket::Accept: this: 0x%x"), this));
       
   183 	TPckgC<TInt> newSocketHandlePckg(aNewSocket.iHandle);
       
   184 	iWin32Message.Set(CWin32Socket::EAccept, newSocketHandlePckg, aStatus);
       
   185 	return MakeRequest(iWin32Message);
       
   186 	}
       
   187 
       
   188 void RWin32Socket::CancelAccept()
       
   189 	{
       
   190 	TWin32Message cancelWin32Message;
       
   191 	cancelWin32Message.Set(CWin32Socket::ECancelAccept);
       
   192 	MakeRequest(cancelWin32Message); // Throw away return code - can't do anything useful with it.
       
   193 	}
       
   194 
       
   195 class TWin32SocketOptionsC
       
   196 	{
       
   197 public:
       
   198 	TInt aLevel;
       
   199 	TInt aName;
       
   200 	const char* aValue;
       
   201 	TInt aLength;
       
   202 	};
       
   203 
       
   204 class TWin32SocketOptions
       
   205 	{
       
   206 public:
       
   207 	TInt aLevel;
       
   208 	TInt aName;
       
   209 	char* aValue;
       
   210 	TInt* aLength;
       
   211 	};
       
   212 
       
   213 TInt RWin32Socket::GetOption(TInt aLevel, TInt aName, char* aValue, TInt* aLength) const
       
   214 	{
       
   215 	WSP_LOG(WspLog::Printf(_L("RWin32Socket::GetOption: this: 0x%x"), this));
       
   216 	TWin32SocketOptions options = {aLevel, aName, aValue, aLength};
       
   217 	TPckg<TWin32SocketOptions> optionsPckg(options);
       
   218 	TRequestStatus status;
       
   219 	iWin32Message.Set(CWin32Socket::EGetOption, optionsPckg, status);
       
   220 	TInt err = MakeRequest(iWin32Message);
       
   221 	if (err)
       
   222 		{
       
   223 		return err;
       
   224 		}
       
   225 	else
       
   226 		{
       
   227 		User::WaitForRequest(status);
       
   228 		return status.Int();
       
   229 		}
       
   230 	}
       
   231 
       
   232 TInt RWin32Socket::SetOption(TInt aLevel, TInt aName, const char* aValue, TInt aLength)
       
   233 	{
       
   234 	WSP_LOG(WspLog::Printf(_L("RWin32Socket::SetOption: this: 0x%x"), this));
       
   235 	TWin32SocketOptionsC options = {aLevel, aName, aValue, aLength};
       
   236 	TPckgC<TWin32SocketOptionsC> optionsPckg(options);
       
   237 	TRequestStatus status;
       
   238 	iWin32Message.Set(CWin32Socket::ESetOption, optionsPckg, status);
       
   239 	TInt err = MakeRequest(iWin32Message);
       
   240 	if (err)
       
   241 		{
       
   242 		return err;
       
   243 		}
       
   244 	else
       
   245 		{
       
   246 		User::WaitForRequest(status);
       
   247 		return status.Int();
       
   248 		}
       
   249 	}
       
   250 
       
   251 //
       
   252 // CWin32Socket.
       
   253 //
       
   254 
       
   255 CWin32Socket* CWin32Socket::NewL(CWin32Scheduler& aScheduler, RWin32Socket::TType aType)
       
   256 	{
       
   257 	CWin32Socket* self = new(ELeave) CWin32Socket(aScheduler, aType);
       
   258 	CleanupStack::PushL(self);
       
   259 	self->ConstructL();
       
   260 	CleanupStack::Pop(self);
       
   261 	return self;
       
   262 	}
       
   263 
       
   264 CWin32Socket::CWin32Socket(CWin32Scheduler& aScheduler, RWin32Socket::TType aType)
       
   265 	: CWin32SubSession(aScheduler), iType(aType)
       
   266 	{
       
   267 	}
       
   268 
       
   269 void CWin32Socket::ConstructL()
       
   270 	{
       
   271 	CWin32SubSession::ConstructL();
       
   272 
       
   273 	switch (iType)
       
   274 		{
       
   275 		case RWin32Socket::EBlank:
       
   276 			{
       
   277 			// Nothing to do.
       
   278 			break;
       
   279 			}
       
   280 		case RWin32Socket::ETcp:
       
   281 			{
       
   282 			__ASSERT_DEBUG(iSocket == 0, Panic(EWinSockPrtCWin32SocketCreateSocketUnexpectedValidSocket));
       
   283 			Emulator::Lock();
       
   284 			iSocket = WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, NULL, WSA_FLAG_OVERLAPPED);
       
   285 			Emulator::Unlock();
       
   286 			if (iSocket == INVALID_SOCKET)
       
   287 				{
       
   288 				User::Leave(MapWinSockError(WSAGetLastError()));
       
   289 				}
       
   290 			break;
       
   291 			}
       
   292 		case RWin32Socket::EUdp:
       
   293 			{
       
   294 			__ASSERT_DEBUG(iSocket == 0, Panic(EWinSockPrtCWin32SocketCreateSocketUnexpectedValidSocket));
       
   295 			Emulator::Lock();
       
   296 			iSocket = WSASocket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, NULL, NULL, WSA_FLAG_OVERLAPPED);
       
   297 			Emulator::Unlock();
       
   298 			if (iSocket == INVALID_SOCKET)
       
   299 				{
       
   300 				User::Leave(MapWinSockError(WSAGetLastError()));
       
   301 				}
       
   302 			break;
       
   303 			}
       
   304 		default:
       
   305 			{
       
   306 			__ASSERT_DEBUG(EFalse, Panic(EWinSockPrtCWin32SocketConstructLInvalidSocketType));
       
   307 			}
       
   308 		}
       
   309 	}
       
   310 
       
   311 CWin32Socket::~CWin32Socket()
       
   312 	{
       
   313 	CloseSocket();
       
   314 	}
       
   315 
       
   316 void CWin32Socket::ServiceL(TWin32Message& aMessage)
       
   317 	{
       
   318 	switch (aMessage.OppCode())
       
   319 		{
       
   320 		case EConnect:
       
   321 			{
       
   322 			Connect(aMessage);
       
   323 			break;
       
   324 			}
       
   325 		case ECancelConnect:
       
   326 			{
       
   327 			if (iConnectMessage)
       
   328 				{
       
   329 				Complete(iConnectMessage, KErrCancel);
       
   330 				}
       
   331 			break;
       
   332 			}
       
   333 		case ESend:
       
   334 			{
       
   335 			Send(aMessage);
       
   336 			break;
       
   337 			}
       
   338 		case ECancelSend:
       
   339 			{
       
   340 			if (iSendMessage)
       
   341 				{
       
   342 				Complete(iSendMessage, KErrCancel);
       
   343 				}
       
   344 			break;
       
   345 			}
       
   346 		case ESendTo:
       
   347 			{
       
   348 			SendTo(aMessage);
       
   349 			break;
       
   350 			}
       
   351 		case ECancelSendTo:
       
   352 			{
       
   353 			if (iSendMessage)
       
   354 				{
       
   355 				Complete(iSendMessage, KErrCancel);
       
   356 				}
       
   357 			break;
       
   358 			}
       
   359 		case EReceive:
       
   360 			{
       
   361 			Receive(aMessage);
       
   362 			break;
       
   363 			}
       
   364 		case ECancelReceive:
       
   365 			{
       
   366 			if (iReceiveMessage)
       
   367 				{
       
   368 				Complete(iReceiveMessage, KErrCancel);
       
   369 				}
       
   370 			break;
       
   371 			}
       
   372 		case EReceiveFrom:
       
   373 			{
       
   374 			ReceiveFrom(aMessage);
       
   375 			break;
       
   376 			}
       
   377 		case ECancelReceiveFrom:
       
   378 			{
       
   379 			if (iReceiveMessage)
       
   380 				{
       
   381 				Complete(iReceiveMessage, KErrCancel);
       
   382 				}
       
   383 			break;
       
   384 			}
       
   385 		case EGetSocketName:
       
   386 			{
       
   387 			GetSocketName(aMessage);
       
   388 			break;
       
   389 			}
       
   390 		case EBind:
       
   391 			{
       
   392 			Bind(aMessage);
       
   393 			break;
       
   394 			}
       
   395 		case EGetPeerName:
       
   396 			{
       
   397 			GetPeerName(aMessage);
       
   398 			break;
       
   399 			}
       
   400 		case EListen:
       
   401 			{
       
   402 			Listen(aMessage);
       
   403 			break;
       
   404 			}
       
   405 		case EAccept:
       
   406 			{
       
   407 			Accept(aMessage);
       
   408 			break;
       
   409 			}
       
   410 		case ECancelAccept:
       
   411 			{
       
   412 			if (iAcceptMessage)
       
   413 				{
       
   414 				Complete(iAcceptMessage, KErrCancel);
       
   415 				}
       
   416 			break;
       
   417 			}
       
   418 		case EGetOption:
       
   419 			{
       
   420 			GetOption(aMessage);
       
   421 			break;
       
   422 			}
       
   423 		case ESetOption:
       
   424 			{
       
   425 			SetOption(aMessage);
       
   426 			break;
       
   427 			}
       
   428 		default:
       
   429 			{
       
   430 			__ASSERT_DEBUG(EFalse, Panic(EWinSockPrtInvalidSocketOppCode));
       
   431 			}
       
   432 		}
       
   433 	}
       
   434 
       
   435 void CWin32Socket::Connect(TWin32Message& aMessage)
       
   436 	{
       
   437 	__ASSERT_DEBUG(iConnectMessage == NULL, Panic(EWinSockPrtCWin32SocketMultipleConnectRequests));
       
   438 	iConnectMessage = &aMessage;
       
   439 	WSP_LOG(WspLog::Printf(_L("CWin32Socket::Connect: this: 0x%x"), this));
       
   440 
       
   441 	if (WSAEventSelect(iSocket, iEvent, FD_CONNECT) == SOCKET_ERROR)
       
   442 		{
       
   443 		TInt err = MapWinSockError(WSAGetLastError());
       
   444 		CloseSocket();
       
   445 		Complete(iConnectMessage, err);
       
   446 		return;
       
   447 		}
       
   448 
       
   449 	const TInetAddr& address = static_cast<const TInetAddr&>(iConnectMessage->ReadBuffer());
       
   450 	SOCKADDR_IN winSockAddress;
       
   451 	ConvertAddress(address, winSockAddress);
       
   452 
       
   453 	if (WSAConnect(iSocket, (LPSOCKADDR)&winSockAddress, sizeof(struct sockaddr_in), NULL, NULL, NULL, NULL) == SOCKET_ERROR)
       
   454 		{
       
   455 		TInt nError = WSAGetLastError();
       
   456 		if (nError != WSAEWOULDBLOCK)
       
   457 			{
       
   458 			CloseSocket();
       
   459 			Complete(iConnectMessage, MapWinSockError(nError));
       
   460 			}
       
   461 		}
       
   462 	else
       
   463 		{
       
   464 		Complete(iConnectMessage, KErrNone);
       
   465 		}
       
   466 	}
       
   467 
       
   468 void CWin32Socket::Send(TWin32Message& aMessage)
       
   469 	{
       
   470 	__ASSERT_DEBUG((iSendMessage == NULL) && (iBytesSent == 0), Panic(EWinSockPrtCWin32SocketMultipleSendRequests));
       
   471 	iSendMessage = &aMessage;
       
   472 	WSP_LOG(WspLog::Printf(_L("CWin32Socket::Send: this: 0x%x, bytes to send: %d"), this, iSendMessage->ReadBuffer().Length()));
       
   473 	DoSend();
       
   474 	}
       
   475 
       
   476 void CWin32Socket::SendTo(TWin32Message& aMessage)
       
   477 	{
       
   478 	__ASSERT_DEBUG((iSendMessage == NULL) && (iBytesSent == 0), Panic(EWinSockPrtCWin32SocketMultipleSendToRequests));
       
   479 	iSendMessage = &aMessage;
       
   480 	WSP_LOG(WspLog::Printf(_L("CWin32Socket::SendTo: this: 0x%x, bytes to send: %d"), this, iSendMessage->ReadBuffer().Length()));
       
   481 	TPtrC8 bufPtr(iSendMessage->ReadBuffer());
       
   482 	iSendBuffer.buf = reinterpret_cast<char*>(const_cast<TUint8*>(bufPtr.Ptr()));
       
   483 	iSendBuffer.len = bufPtr.Length();
       
   484 	iSendOverlapped.hEvent = (void*) this;
       
   485 	TInetAddr address;
       
   486 	address.Copy(iSendMessage->WriteBuffer());
       
   487 	SOCKADDR_IN winSockAddress;
       
   488 	ConvertAddress(address, winSockAddress);
       
   489 	DWORD numberOfBytesSent;
       
   490 	TInt ret = WSASendTo(iSocket, &iSendBuffer, 1, &numberOfBytesSent, 0, (LPSOCKADDR)&winSockAddress, sizeof(struct sockaddr_in), &iSendOverlapped, &SendToCompletion);
       
   491 	if (ret == SOCKET_ERROR)
       
   492 		{
       
   493 		TInt err = WSAGetLastError();
       
   494 		if (err != WSA_IO_PENDING)
       
   495 			{
       
   496 			WSP_LOG(WspLog::Printf(_L("\tsocket error: %d"), err));
       
   497 			Complete(iSendMessage, MapWinSockError(err));
       
   498 			}
       
   499 		}
       
   500 	}
       
   501 
       
   502 void CWin32Socket::Receive(TWin32Message& aMessage)
       
   503 	{
       
   504 	__ASSERT_DEBUG(iReceiveMessage == NULL, Panic(EWinSockPrtCWin32SocketMultipleReceiveRequests));
       
   505 	iReceiveMessage = &aMessage;
       
   506 	WSP_LOG(WspLog::Printf(_L("CWin32Socket::Receive: this: 0x%x, bytes to get: %d"), this, iReceiveMessage->WriteBuffer().MaxLength()));
       
   507 	iReceiveBuffer.buf = reinterpret_cast<char*>(const_cast<TUint8*>(iReceiveMessage->WriteBuffer().Ptr()));
       
   508 	iReceiveBuffer.len = iReceiveMessage->WriteBuffer().MaxLength();
       
   509 	iFlags = 0;
       
   510 	iReceiveOverlapped.hEvent = (void*) this;
       
   511 	DWORD numberOfBytesReceived;
       
   512 	TInt ret = WSARecv(iSocket, &iReceiveBuffer, 1, &numberOfBytesReceived, &iFlags, &iReceiveOverlapped, &ReceiveCompletion);
       
   513 	if (ret == SOCKET_ERROR)
       
   514 		{
       
   515 		TInt err = WSAGetLastError();
       
   516 		if (err != WSA_IO_PENDING)
       
   517 			{
       
   518 			WSP_LOG(WspLog::Printf(_L("\tsocket error: %d"), err));
       
   519 			Complete(iReceiveMessage, MapWinSockError(err));
       
   520 			}
       
   521 		}
       
   522 	}
       
   523 
       
   524 void CWin32Socket::ReceiveFrom(TWin32Message& aMessage)
       
   525 	{
       
   526 	__ASSERT_DEBUG(iReceiveMessage == NULL, Panic(EWinSockPrtCWin32SocketMultipleReceiveFromRequests));
       
   527 	iReceiveMessage = &aMessage;
       
   528 	WSP_LOG(WspLog::Printf(_L("CWin32Socket::ReceiveFrom: this: 0x%x, bytes to get: %d"), this, iReceiveMessage->WriteBuffer().MaxLength()));
       
   529 	iReceiveBuffer.buf = reinterpret_cast<char*>(const_cast<TUint8*>(iReceiveMessage->WriteBuffer().Ptr()));
       
   530 	iReceiveBuffer.len = iReceiveMessage->WriteBuffer().MaxLength();
       
   531 	iFlags = 0;
       
   532 	iReceiveOverlapped.hEvent = (void*) this;
       
   533 	iReceiveFromAddressLength = sizeof(struct sockaddr_in);
       
   534 	iReceiveFromClientAddress = (TInetAddr*)iReceiveMessage->ReadBuffer().Ptr();
       
   535 	DWORD numberOfBytesReceived;
       
   536 	TInt ret = WSARecvFrom(iSocket, &iReceiveBuffer, 1, &numberOfBytesReceived, &iFlags, (LPSOCKADDR)&iReceiveFromAddress, &iReceiveFromAddressLength, &iReceiveOverlapped, &ReceiveFromCompletion);
       
   537 	if (ret == SOCKET_ERROR)
       
   538 		{
       
   539 		TInt err = WSAGetLastError();
       
   540 		if (err != WSA_IO_PENDING)
       
   541 			{
       
   542 			WSP_LOG(WspLog::Printf(_L("\tsocket error: %d"), err));
       
   543 			Complete(iReceiveMessage, MapWinSockError(err));
       
   544 			}
       
   545 		}
       
   546 	}
       
   547 
       
   548 void CWin32Socket::GetSocketName(TWin32Message& aMessage) const
       
   549 	{
       
   550 	SOCKADDR_IN winSockAddress;
       
   551 	TInt length = sizeof(struct sockaddr_in);
       
   552 	if (getsockname(iSocket, (LPSOCKADDR)&winSockAddress, &length))
       
   553 		{
       
   554 		aMessage.Complete(MapWinSockError(WSAGetLastError()));
       
   555 		}
       
   556 	else
       
   557 		{
       
   558 		__ASSERT_DEBUG(length == sizeof(struct sockaddr_in), Panic(EWinSockPrtCWin32SocketGetSocketNameInvalidSocketAddress));
       
   559 		TInetAddr& address = static_cast<TInetAddr&>(aMessage.WriteBuffer());
       
   560 		ConvertAddress(winSockAddress, address);
       
   561 		aMessage.Complete(KErrNone);
       
   562 		}
       
   563 	}
       
   564 
       
   565 void CWin32Socket::Bind(TWin32Message& aMessage)
       
   566 	{
       
   567 	const TInetAddr& address = static_cast<const TInetAddr&>(aMessage.ReadBuffer());
       
   568 	SOCKADDR_IN winSockAddress;
       
   569 	ConvertAddress(address, winSockAddress);
       
   570 	if (bind(iSocket, (LPSOCKADDR)&winSockAddress, sizeof(struct sockaddr_in)))
       
   571 		{
       
   572 		aMessage.Complete(MapWinSockError(WSAGetLastError()));
       
   573 		}
       
   574 	else
       
   575 		{
       
   576 		aMessage.Complete(KErrNone);
       
   577 		}
       
   578 	}
       
   579 
       
   580 void CWin32Socket::GetPeerName(TWin32Message& aMessage) const
       
   581 	{
       
   582 	SOCKADDR_IN winSockAddress;
       
   583 	TInt length = sizeof(struct sockaddr_in);
       
   584 	if (getpeername(iSocket, (LPSOCKADDR)&winSockAddress, &length))
       
   585 		{
       
   586 		aMessage.Complete(MapWinSockError(WSAGetLastError()));
       
   587 		}
       
   588 	else
       
   589 		{
       
   590 		__ASSERT_DEBUG(length == sizeof(struct sockaddr_in), Panic(EWinSockPrtCWin32SocketGetPeerNameInvalidSocketAddress));
       
   591 		TInetAddr& address = static_cast<TInetAddr&>(aMessage.WriteBuffer());
       
   592 		ConvertAddress(winSockAddress, address);
       
   593 		aMessage.Complete(KErrNone);
       
   594 		}
       
   595 	}
       
   596 
       
   597 void CWin32Socket::Listen(TWin32Message& aMessage)
       
   598 	{
       
   599 	TPckgBuf<TUint> queueSizeBuf;
       
   600 	queueSizeBuf.Copy(aMessage.ReadBuffer());
       
   601 	TUint& queueSize = queueSizeBuf();
       
   602 	if (listen(iSocket, queueSize))
       
   603 		{
       
   604 		aMessage.Complete(MapWinSockError(WSAGetLastError()));
       
   605 		}
       
   606 	else
       
   607 		{
       
   608 		aMessage.Complete(KErrNone);
       
   609 		}
       
   610 	}
       
   611 
       
   612 void CWin32Socket::Accept(TWin32Message& aMessage)
       
   613 	{
       
   614 	__ASSERT_DEBUG(iAcceptMessage == NULL, Panic(EWinSockPrtCWin32SocketMultipleAcceptRequests));
       
   615 	__ASSERT_DEBUG(iBlankAcceptSocket == NULL, Panic(EWinSockPrtCWin32SocketAcceptBlankSocketNotNull));
       
   616 	iAcceptMessage = &aMessage;
       
   617 	WSP_LOG(WspLog::Printf(_L("CWin32Socket::Accept: this: 0x%x"), this));
       
   618 	if (WSAEventSelect(iSocket, iEvent, FD_ACCEPT) == SOCKET_ERROR)
       
   619 		{
       
   620 		Complete(iAcceptMessage, MapWinSockError(WSAGetLastError()));
       
   621 		}
       
   622 	TPckgBuf<TInt> newSocketHandleBuf;
       
   623 	newSocketHandleBuf.Copy(iAcceptMessage->ReadBuffer());
       
   624 	iBlankAcceptSocket = (CWin32Socket*)newSocketHandleBuf();
       
   625 	}
       
   626 
       
   627 void CWin32Socket::GetOption(TWin32Message& aMessage) const
       
   628 	{
       
   629 	__ASSERT_DEBUG(aMessage.WriteBuffer().Length() == sizeof(TWin32SocketOptions), EWinSockPrtCWin32SocketGetOptionInvalidParameters);
       
   630 	TWin32SocketOptions* options = reinterpret_cast<TWin32SocketOptions*>(const_cast<TUint8*>(aMessage.WriteBuffer().Ptr()));
       
   631 	TInt err = getsockopt(iSocket, options->aLevel, options->aName, options->aValue, options->aLength);
       
   632 	TWin32Message* messagePrt = &aMessage;
       
   633 	if (err)
       
   634 		{
       
   635 		Complete(messagePrt, MapWinSockError(WSAGetLastError()));
       
   636 		}
       
   637 	else
       
   638 		{
       
   639 		Complete(messagePrt, KErrNone);
       
   640 		}
       
   641 	}
       
   642 
       
   643 void CWin32Socket::SetOption(TWin32Message& aMessage)
       
   644 	{
       
   645 	__ASSERT_DEBUG(aMessage.ReadBuffer().Length() == sizeof(TWin32SocketOptionsC), EWinSockPrtCWin32SocketSetOptionInvalidParameters);
       
   646 	const TWin32SocketOptionsC* options = reinterpret_cast<const TWin32SocketOptionsC*>(aMessage.ReadBuffer().Ptr());
       
   647 	TInt err = setsockopt(iSocket, options->aLevel, options->aName, options->aValue, options->aLength);
       
   648 	TWin32Message* messagePrt = &aMessage;
       
   649 	if (err)
       
   650 		{
       
   651 		Complete(messagePrt, MapWinSockError(WSAGetLastError()));
       
   652 		}
       
   653 	else
       
   654 		{
       
   655 		Complete(messagePrt, KErrNone);
       
   656 		}
       
   657 	}
       
   658 
       
   659 void CWin32Socket::DoSend()
       
   660 	{
       
   661 	WSP_LOG(WspLog::Printf(_L("CWin32Socket::DoSend: this: 0x%x, bytes to send: %d"), this, iSendMessage->ReadBuffer().Length() - iBytesSent));
       
   662 	TPtrC8 bufPtr(iSendMessage->ReadBuffer());
       
   663 	iSendBuffer.buf = reinterpret_cast<char*>(const_cast<TUint8*>(bufPtr.Ptr())) + iBytesSent;
       
   664 	iSendBuffer.len = bufPtr.Length() - iBytesSent;
       
   665 	iSendOverlapped.hEvent = (void*) this;
       
   666 	DWORD numberOfBytesSent;
       
   667 	Emulator::Lock();
       
   668 	TInt ret = WSASend(iSocket, &iSendBuffer, 1, &numberOfBytesSent, 0, &iSendOverlapped, &SendCompletion);
       
   669 	Emulator::Unlock();
       
   670 	if (ret == SOCKET_ERROR)
       
   671 		{
       
   672 		Emulator::Lock();
       
   673 		TInt err = WSAGetLastError();
       
   674 		Emulator::Unlock();
       
   675 		if (err != WSA_IO_PENDING)
       
   676 			{
       
   677 			WSP_LOG(WspLog::Printf(_L("\tsocket error: %d"), err));
       
   678 			Complete(iSendMessage, MapWinSockError(err));
       
   679 			}
       
   680 		}
       
   681 	}
       
   682 
       
   683 void CWin32Socket::CloseSocket()
       
   684 	{
       
   685 	WSP_LOG(WspLog::Printf(_L("CWin32Socket::CloseSocket: this: 0x%x"), this));
       
   686 	if (iSocket != INVALID_SOCKET)
       
   687 		{
       
   688 		closesocket(iSocket);
       
   689 		iSocket = INVALID_SOCKET;
       
   690 		}
       
   691 	}
       
   692 
       
   693 void CWin32Socket::Run()
       
   694 	{
       
   695 	WSP_LOG(WspLog::Printf(_L("CWin32Socket::Run: this: 0x%x"), this));
       
   696 	WSANETWORKEVENTS eventStatus;
       
   697 	if (WSAEnumNetworkEvents(iSocket, iEvent, &eventStatus) == 0)
       
   698 		{
       
   699 		if (eventStatus.lNetworkEvents & FD_CONNECT)
       
   700 			{
       
   701 			HandleConnectionComplete(MapWinSockError(eventStatus.iErrorCode[FD_CONNECT_BIT]));
       
   702 			}
       
   703 		else if (eventStatus.lNetworkEvents & FD_ACCEPT)
       
   704 			{
       
   705 			HandleAcceptReady(MapWinSockError(eventStatus.iErrorCode[FD_ACCEPT_BIT]));
       
   706 			}
       
   707 		else
       
   708 			{
       
   709 			__ASSERT_DEBUG(EFalse, Panic(EWinSockPrtUnexpectedWSANetworkEvent));
       
   710 			}
       
   711 		}
       
   712 	else
       
   713 		{
       
   714 		__ASSERT_DEBUG(EFalse, Panic(EWinSockPrtUnexpectedWSAEnumNetworkEventsError));
       
   715 		}
       
   716 	}
       
   717 
       
   718 void CWin32Socket::HandleConnectionComplete(TInt aError)
       
   719 	{
       
   720 	ReleaseEventHandle(); // No longer needed because all further asynchronous I/O is done using overlapped buffer callbacks.
       
   721 	if (iConnectMessage)  // May have been cancelled.
       
   722 		{
       
   723 		Complete(iConnectMessage, aError);
       
   724 		}
       
   725 	}
       
   726 
       
   727 void CWin32Socket::HandleAcceptReady(TInt aError)
       
   728 	{
       
   729 	if (aError)
       
   730 		{
       
   731 		Complete(iAcceptMessage, aError);
       
   732 		}
       
   733 	else
       
   734 		{
       
   735 		if (iAcceptMessage)  // May have been cancelled.
       
   736 			{
       
   737 			iBlankAcceptSocket->iSocket = WSAAccept(iSocket, NULL, NULL, NULL, 0);
       
   738 			if (iBlankAcceptSocket->iSocket == INVALID_SOCKET)
       
   739 				{
       
   740 				Complete(iAcceptMessage, MapWinSockError(WSAGetLastError()));
       
   741 				}
       
   742 			else
       
   743 				{
       
   744 				Complete(iAcceptMessage, KErrNone);
       
   745 				}
       
   746 			}
       
   747 
       
   748 		iBlankAcceptSocket = NULL;
       
   749 		if (WSAEventSelect(iSocket, iEvent, 0) == SOCKET_ERROR)
       
   750 			{
       
   751 			__ASSERT_DEBUG(EFalse, Panic(EWinSockPrtCWin32SocketHandleAcceptReadyUnexpectedEventSelectError));
       
   752 			}
       
   753 		}
       
   754 	}
       
   755 
       
   756 void CALLBACK CWin32Socket::SendCompletion(DWORD dwError, DWORD cbTransferred, LPWSAOVERLAPPED lpOverlapped, DWORD /*dwFlags*/)
       
   757 	{
       
   758 	Emulator::Reenter(); // This is a win32 callback so we're outside of the emulator at this point
       
   759 	WSP_LOG(WspLog::Printf(_L("CWin32Socket::SendCompletion: error: %d, object: 0x%x"), dwError, lpOverlapped->hEvent));
       
   760 	switch (dwError)
       
   761 		{
       
   762 		case 0:
       
   763 			{
       
   764 			CWin32Socket* socket = reinterpret_cast<CWin32Socket*>(lpOverlapped->hEvent);
       
   765 			if (socket && socket->iSendMessage) // May have been cancelled.
       
   766 				{
       
   767 				socket->iBytesSent += cbTransferred;
       
   768 				if (socket->iBytesSent < socket->iSendMessage->ReadBuffer().Length())
       
   769 					{
       
   770 					socket->DoSend();
       
   771 					}
       
   772 				else
       
   773 					{
       
   774 					socket->Complete(socket->iSendMessage, KErrNone);
       
   775 					socket->iBytesSent = 0;
       
   776 					}
       
   777 				}
       
   778 			break;
       
   779 			}
       
   780 		case WSAEINVAL:
       
   781 			{
       
   782 			// Ignore - probably a completion on a closed socket due to cancellation.
       
   783 			break;
       
   784 			}
       
   785 		case WSAECONNRESET:
       
   786 			{
       
   787 			// Ignore - remote host has terminated the connection.
       
   788 			}
       
   789 		default:
       
   790 			{
       
   791 			__ASSERT_DEBUG(EFalse, Panic(EWinSockPrtUnexpectedWSASendCompletionError));
       
   792 			}
       
   793 		}
       
   794 	Emulator::Escape(); // And return to how we were
       
   795 	}
       
   796 
       
   797 void CALLBACK CWin32Socket::SendToCompletion(DWORD dwError, DWORD cbTransferred, LPWSAOVERLAPPED lpOverlapped, DWORD /*dwFlags*/)
       
   798 	{
       
   799 	Emulator::Reenter(); // This is a win32 callback so we're outside of the emulator at this point
       
   800 	WSP_LOG(WspLog::Printf(_L("CWin32Socket::SendToCompletion: error: %d, object: 0x%x"), dwError, lpOverlapped->hEvent));
       
   801 	switch (dwError)
       
   802 		{
       
   803 		case 0:
       
   804 			{
       
   805 			CWin32Socket* socket = reinterpret_cast<CWin32Socket*>(lpOverlapped->hEvent);
       
   806 			if (socket->iSendMessage) // May have been cancelled.
       
   807 				{
       
   808 				__ASSERT_DEBUG(cbTransferred == (TUint)socket->iSendMessage->ReadBuffer().Length(), Panic(EWinSockPrtCWin32SocketSendToCompletionAllDataNotSent));
       
   809 				socket->Complete(socket->iSendMessage, KErrNone);
       
   810 				}
       
   811 			break;
       
   812 			}
       
   813 		case WSAEINVAL:
       
   814 			{
       
   815 			// Ignore - probably a completion on a closed socket due to cancellation.
       
   816 			break;
       
   817 			}
       
   818 		default:
       
   819 			{
       
   820 			__ASSERT_DEBUG(EFalse, Panic(EWinSockPrtUnexpectedWSASendToCompletionError));
       
   821 			}
       
   822 		}
       
   823 	Emulator::Escape(); // And return to how we were
       
   824 	}
       
   825 
       
   826 void CALLBACK CWin32Socket::ReceiveCompletion(DWORD dwError, DWORD cbTransferred, LPWSAOVERLAPPED lpOverlapped, DWORD /*dwFlags*/)
       
   827 	{
       
   828 	if (dwError == WSAEINVAL)
       
   829 		{
       
   830 		// Ignore - probably a completion on a closed socket due to cancellation.
       
   831 		// Also, might not be a valid context from which to call Emulator::Reenter().
       
   832 		return;
       
   833 		}
       
   834 
       
   835 	Emulator::Reenter(); // This is a win32 callback so we're outside of the emulator at this point
       
   836 	WSP_LOG(WspLog::Printf(_L("CWin32Socket::ReceiveCompletion: error: %d, object: 0x%x"), dwError, lpOverlapped->hEvent));
       
   837 	switch (dwError)
       
   838 		{
       
   839 		case 0:
       
   840 			{
       
   841 			CWin32Socket* socket = reinterpret_cast<CWin32Socket*>(lpOverlapped->hEvent);
       
   842 			if (socket && socket->iReceiveMessage) // May have been cancelled.
       
   843 				{
       
   844 				socket->iReceiveMessage->WriteBuffer().SetLength(cbTransferred);
       
   845 				socket->Complete(socket->iReceiveMessage, KErrNone);
       
   846 				}
       
   847 			break;
       
   848 			}
       
   849 		case WSAECONNRESET:
       
   850 			{
       
   851 			// Remote host has reset the connection.
       
   852 			// Ignore - seen these after Opera gets asked to receive a cookie.
       
   853 			break;
       
   854 			}
       
   855 		default:
       
   856 			{
       
   857 			__ASSERT_DEBUG(EFalse, Panic(EWinSockPrtUnexpectedWSAReceiveCompletionError));
       
   858 			}
       
   859 		}
       
   860 	Emulator::Escape(); // And return to how we were
       
   861 	}
       
   862 
       
   863 void CALLBACK CWin32Socket::ReceiveFromCompletion(DWORD dwError, DWORD cbTransferred, LPWSAOVERLAPPED lpOverlapped, DWORD /*dwFlags*/)
       
   864 	{
       
   865 	Emulator::Reenter(); // This is a win32 callback so we're outside of the emulator at this point
       
   866 	WSP_LOG(WspLog::Printf(_L("CWin32Socket::ReceiveFromCompletion: error: %d, object: 0x%x"), dwError, lpOverlapped->hEvent));
       
   867 	switch (dwError)
       
   868 		{
       
   869 		case 0:
       
   870 			{
       
   871 			CWin32Socket* socket = reinterpret_cast<CWin32Socket*>(lpOverlapped->hEvent);
       
   872 			if (socket && socket->iReceiveMessage) // May have been cancelled.
       
   873 				{
       
   874 				socket->iReceiveMessage->WriteBuffer().SetLength(cbTransferred);
       
   875 				__ASSERT_DEBUG(socket->iReceiveFromAddressLength == sizeof(struct sockaddr_in), Panic(EWinSockPrtCWin32SocketReceiveFromCompletionInvalidAddressLength));
       
   876 				TInetAddr address;
       
   877 				socket->ConvertAddress(socket->iReceiveFromAddress, address);
       
   878 				socket->iReceiveFromClientAddress->Copy(address);
       
   879 				socket->iReceiveFromClientAddress = NULL;
       
   880 				socket->Complete(socket->iReceiveMessage, KErrNone);
       
   881 				}
       
   882 			break;
       
   883 			}
       
   884 		case WSAEINVAL:
       
   885 			{
       
   886 			// Ignore - probably a completion on a closed socket due to cancellation.
       
   887 			break;
       
   888 			}
       
   889 		case WSAECONNRESET:
       
   890 			{
       
   891 			// Remote host has reset the connection.
       
   892 			// Ignore - seen these after Opera gets asked to receive a cookie.
       
   893 			break;
       
   894 			}
       
   895 		default:
       
   896 			{
       
   897 			__ASSERT_DEBUG(EFalse, Panic(EWinSockPrtUnexpectedWSAReceiveCompletionError));
       
   898 			}
       
   899 		}
       
   900 	Emulator::Escape(); // And return to how we were
       
   901 	}
       
   902 
       
   903 void CWin32Socket::ConvertAddress(const TInetAddr& aESockAddress, SOCKADDR_IN& aWinSockAddress) const
       
   904 	{
       
   905 	aWinSockAddress.sin_family = AF_INET;
       
   906 	aWinSockAddress.sin_addr.s_addr = htonl(aESockAddress.Address());
       
   907 	aWinSockAddress.sin_port = htons(static_cast<TUint16>(aESockAddress.Port()));
       
   908 	}
       
   909 
       
   910 void CWin32Socket::ConvertAddress(const SOCKADDR_IN& aWinSockAddress, TInetAddr& aESockAddress) const
       
   911 	{
       
   912 	aESockAddress.SetAddress(htonl(aWinSockAddress.sin_addr.s_addr));
       
   913 	aESockAddress.SetPort(htons(aWinSockAddress.sin_port));
       
   914 	}
       
   915 
       
   916 
       
   917 
       
   918 //
       
   919 // CWin32SocketWrapper.
       
   920 //
       
   921 
       
   922 CWin32SocketWrapper* CWin32SocketWrapper::NewL(MWin32SocketObserver& aObserver, RWin32Socket& aWin32Socket)
       
   923 	{
       
   924 	CWin32SocketWrapper* self = new(ELeave) CWin32SocketWrapper(aObserver, aWin32Socket);
       
   925 	CleanupStack::PushL(self);
       
   926 	self->ConstructL();
       
   927 	CleanupStack::Pop(self);
       
   928 	return self;
       
   929 	}
       
   930 
       
   931 CWin32SocketWrapper::~CWin32SocketWrapper()
       
   932 	{
       
   933 	Cancel();
       
   934 	}
       
   935 
       
   936 CWin32SocketWrapper::CWin32SocketWrapper(MWin32SocketObserver& aObserver, RWin32Socket& aWin32Socket)
       
   937 	: CActive(CActive::EPriorityStandard), iObserver(aObserver), iWin32Socket(aWin32Socket)
       
   938 	{
       
   939 	}
       
   940 
       
   941 void CWin32SocketWrapper::ConstructL()
       
   942 	{
       
   943 	CActiveScheduler::Add(this);
       
   944 	}
       
   945 
       
   946 TInt CWin32SocketWrapper::Connect(const TInetAddr& aAddress)
       
   947 	{
       
   948 	__ASSERT_DEBUG(!IsActive(), Panic(EWinSockPrtSocketWrapperConnectWhilstActive));
       
   949 	iRequestType = MWin32SocketObserver::EConnect;
       
   950 	TInt err = iWin32Socket.Connect(aAddress, iStatus);
       
   951 	if (err == KErrNone)
       
   952 		{
       
   953 		SetActive();
       
   954 		}
       
   955 	return err;
       
   956 	}
       
   957 
       
   958 TInt CWin32SocketWrapper::Send(const TDesC8& aSendBuffer)
       
   959 	{
       
   960 	__ASSERT_DEBUG(!IsActive(), Panic(EWinSockPrtSocketWrapperSendWhilstActive));
       
   961 	iRequestType = MWin32SocketObserver::ESend;
       
   962 	TInt err = iWin32Socket.Send(aSendBuffer, iStatus);
       
   963 	if (err == KErrNone)
       
   964 		{
       
   965 		SetActive();
       
   966 		}
       
   967 	return err;
       
   968 	}
       
   969 
       
   970 TInt CWin32SocketWrapper::SendTo(const TDesC8& aSendBuffer, const TInetAddr& aAddress)
       
   971 	{
       
   972 	__ASSERT_DEBUG(!IsActive(), Panic(EWinSockPrtSocketWrapperSendToWhilstActive));
       
   973 	iRequestType = MWin32SocketObserver::ESendTo;
       
   974 	TInt err = iWin32Socket.SendTo(aSendBuffer, aAddress, iStatus);
       
   975 	if (err == KErrNone)
       
   976 		{
       
   977 		SetActive();
       
   978 		}
       
   979 	return err;
       
   980 	}
       
   981 
       
   982 TInt CWin32SocketWrapper::Receive(TDes8& aReceiveBuffer)
       
   983 	{
       
   984 	__ASSERT_DEBUG(!IsActive(), Panic(EWinSockPrtSocketWrapperReceiveWhilstActive));
       
   985 	iRequestType = MWin32SocketObserver::EReceive;
       
   986 	TInt err = iWin32Socket.Receive(aReceiveBuffer, iStatus);
       
   987 	if (err == KErrNone)
       
   988 		{
       
   989 		SetActive();
       
   990 		}
       
   991 	return err;
       
   992 	}
       
   993 
       
   994 TInt CWin32SocketWrapper::ReceiveFrom(TDes8& aReceiveBuffer, TInetAddr& aAddress)
       
   995 	{
       
   996 	__ASSERT_DEBUG(!IsActive(), Panic(EWinSockPrtSocketWrapperReceiveFromWhilstActive));
       
   997 	iRequestType = MWin32SocketObserver::EReceiveFrom;
       
   998 	TInt err = iWin32Socket.ReceiveFrom(aReceiveBuffer, aAddress, iStatus);
       
   999 	if (err == KErrNone)
       
  1000 		{
       
  1001 		SetActive();
       
  1002 		}
       
  1003 	return err;
       
  1004 	}
       
  1005 
       
  1006 TInt CWin32SocketWrapper::Accept(RWin32Socket& aNewSocket)
       
  1007 	{
       
  1008 	__ASSERT_DEBUG(!IsActive(), Panic(EWinSockPrtSocketWrapperAcceptWhilstActive));
       
  1009 	iRequestType = MWin32SocketObserver::EAccept;
       
  1010 	TInt err = iWin32Socket.Accept(aNewSocket, iStatus);
       
  1011 	if (err == KErrNone)
       
  1012 		{
       
  1013 		SetActive();
       
  1014 		}
       
  1015 	return err;
       
  1016 	}
       
  1017 
       
  1018 void CWin32SocketWrapper::DoCancel()
       
  1019 	{
       
  1020 	switch (iRequestType)
       
  1021 		{
       
  1022 		case MWin32SocketObserver::EConnect:
       
  1023 			{
       
  1024 			iWin32Socket.CancelConnect();
       
  1025 			break;
       
  1026 			}
       
  1027 		case MWin32SocketObserver::ESend:
       
  1028 			{
       
  1029 			iWin32Socket.CancelSend();
       
  1030 			break;
       
  1031 			}
       
  1032 		case MWin32SocketObserver::ESendTo:
       
  1033 			{
       
  1034 			iWin32Socket.CancelSendTo();
       
  1035 			break;
       
  1036 			}
       
  1037 		case MWin32SocketObserver::EReceive:
       
  1038 			{
       
  1039 			iWin32Socket.CancelReceive();
       
  1040 			break;
       
  1041 			}
       
  1042 		case MWin32SocketObserver::EReceiveFrom:
       
  1043 			{
       
  1044 			iWin32Socket.CancelReceiveFrom();
       
  1045 			break;
       
  1046 			}
       
  1047 		case MWin32SocketObserver::EAccept:
       
  1048 			{
       
  1049 			iWin32Socket.CancelAccept();
       
  1050 			break;
       
  1051 			}
       
  1052 		case MWin32SocketObserver::ENone:
       
  1053 		default:
       
  1054 			{
       
  1055 			__ASSERT_DEBUG(EFalse, Panic(EWinSockPrtSocketWrapperInvalidSocketOppCode));
       
  1056 			break;
       
  1057 			}
       
  1058 		}
       
  1059 	}
       
  1060 
       
  1061 void CWin32SocketWrapper::RunL()
       
  1062 	{
       
  1063 	MWin32SocketObserver::TRequestType requestType = iRequestType;
       
  1064 	iRequestType = MWin32SocketObserver::ENone;
       
  1065 	iObserver.HandleWin32SocketCompletion(requestType, iStatus.Int());
       
  1066 	}
       
  1067 
       
  1068 TInt CWin32SocketWrapper::RunError(TInt /*aError*/)
       
  1069 	{
       
  1070 	__ASSERT_DEBUG(EFalse, Panic(EWinSockPrtSocketWrapperUnexpectedRunError));
       
  1071 	return 0;
       
  1072 	}