servicediscoveryandcontrol/pnp/test/upnp/SocketHandler/src/rsockethandler.cpp
changeset 0 f5a58ecadc66
equal deleted inserted replaced
-1:000000000000 0:f5a58ecadc66
       
     1 // Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include <e32base.h>
       
    17 #include "rsockethandler.h"
       
    18 #include "csockethandler.h"
       
    19 
       
    20 /**
       
    21 Constructor.
       
    22 */
       
    23 EXPORT_C RSocketHandler::RSocketHandler ( MSocketHandlerObserver& aObserver )
       
    24 : iObserver ( aObserver )
       
    25 	{
       
    26 	}
       
    27 
       
    28 /**
       
    29 Attaches to the open socket.
       
    30 
       
    31 @param aSocket. The open RInternalSocket.
       
    32 */
       
    33 EXPORT_C void RSocketHandler::Attach ( RInternalSocket& aSocket )
       
    34 	{
       
    35 	CancelAll ();
       
    36 	iSocket = aSocket;
       
    37 	}
       
    38 
       
    39 /**
       
    40 Connects to a remote host.
       
    41 
       
    42 @param aAddr. A remote destination address.
       
    43 */
       
    44 EXPORT_C void RSocketHandler::Connect ( const TSockAddr& aAddr )
       
    45 	{
       
    46 	TSocketHandlerParams params ( EConnect, &aAddr );
       
    47 	ActivateSocketHandler ( params );
       
    48 	}
       
    49 
       
    50 /**
       
    51 Facilitates a client/server connection from a remote socket.
       
    52 
       
    53 On successful completion a new connected socket is returned to the socket handler observer 
       
    54 and it may then be used to transfer data.
       
    55 @param: void.
       
    56 @return: void 
       
    57 */
       
    58 EXPORT_C void RSocketHandler::Accept ()
       
    59 	{
       
    60 	TSocketHandlerParams params( EAccept );	
       
    61 	ActivateSocketHandler ( params );
       
    62 	}
       
    63 
       
    64 /**
       
    65 Sends data to remote host on a connected socket.
       
    66 
       
    67 @param aData. The data to be sent.
       
    68 */
       
    69 EXPORT_C void RSocketHandler::Send (  RMBufChain& aData )
       
    70 	{
       
    71 	TSocketHandlerParams params ( EMBufSend, &aData );
       
    72 	ActivateSocketHandler ( params );	
       
    73 	}
       
    74 
       
    75 /**
       
    76 Sends data to remote host on a unconnected socket.
       
    77 
       
    78 @param aData. The data to be sent.
       
    79 @param aAddr. A remote destination address
       
    80 */
       
    81 EXPORT_C void RSocketHandler::SendTo ( const RMBufChain& aData, TSockAddr& aAddr )
       
    82 	{
       
    83 	TSocketHandlerParams params ( EMBufSendTo, &aData, &aAddr );
       
    84 	ActivateSocketHandler ( params );	
       
    85 	}
       
    86 
       
    87 /**
       
    88 Receives data from a remote host.
       
    89 */
       
    90 EXPORT_C void RSocketHandler::Recv ()
       
    91 	{
       
    92 	TSocketHandlerParams params ( ERecv );
       
    93 	ActivateSocketHandler ( params );
       
    94 	
       
    95 	}
       
    96 
       
    97 /**
       
    98 Receives data from a remote host through a unconnected socket and returns a source address.
       
    99 */
       
   100 EXPORT_C void RSocketHandler::RecvFrom ()
       
   101 	{
       
   102 	TSocketHandlerParams params ( ERecvFrom );
       
   103 	ActivateSocketHandler ( params );
       
   104 
       
   105 	}
       
   106 
       
   107 /**
       
   108 Sends data to remote host on a connected socket.
       
   109 
       
   110 @param aData. The data to be sent.
       
   111 
       
   112 EXPORT_C void RSocketHandler::Send ( const TDesC8& aData )
       
   113 	{
       
   114 	TSocketHandlerParams params ( EDescSend, &aData );
       
   115 	ActivateSocketHandler ( params );
       
   116 
       
   117 	}
       
   118 
       
   119 Sends data to remote host on a unconnected socket.
       
   120 
       
   121 @param aData. The data to be sent.
       
   122 @param aAddr. A remote destination address
       
   123 
       
   124 EXPORT_C void RSocketHandler::SendTo ( const TDesC8& aData, TSockAddr& aAddr )
       
   125 	{
       
   126 	TSocketHandlerParams params ( EDescSendTo, &aData, &aAddr );
       
   127 	ActivateSocketHandler ( params );
       
   128 	}
       
   129 
       
   130 
       
   131 Applies an asynchronous I/O control operation on a socket.
       
   132 
       
   133 @param aCommand. Ioctl command.
       
   134 
       
   135 EXPORT_C void RSocketHandler::Ioctl ( TUint aCommand, TDes8* aData  = 0 , TUint aLevel  = KLevelUnspecified  )
       
   136 	{
       
   137 	TSocketHandlerParams params ( EIoctl, aCommand, aData, aLevel );	
       
   138 	ActivateSocketHandler ( params );					
       
   139 	}
       
   140 
       
   141 
       
   142 Cancels an outstanding socket operation.
       
   143 
       
   144 @param aOperation. Operation to be cancelled.
       
   145 
       
   146 EXPORT_C void RSocketHandler::Cancel ( TOperation aOperation )
       
   147 	{
       
   148 	CSocketHandler* handler = ActiveSocketHandler ( aOperation );
       
   149 	if ( handler )
       
   150 		handler->Cancel ();
       
   151 	}
       
   152 */
       
   153 
       
   154 /**
       
   155 Cancels all outstanding operations.
       
   156 */
       
   157 EXPORT_C void RSocketHandler::CancelAll ()
       
   158 	{
       
   159 	// The descrtrutor of CSocketHandler will take care of cancel
       
   160 	iSocketHandlers.ResetAndDestroy ();	
       
   161 	}
       
   162 
       
   163 /**
       
   164 Finds the first free socket handler to perform the socket operation and if none
       
   165 available it creates a new socket handler.
       
   166 
       
   167 @param aOperation. Socket operation to be performed.
       
   168 @return A free socket handler to perform the socket operation.
       
   169 */
       
   170 CSocketHandler* RSocketHandler::FreeSocketHandler ( TOperation aOperation )
       
   171 	{
       
   172 	CSocketHandler* freeHandler = ActiveSocketHandler ( aOperation );
       
   173 	if ( freeHandler == NULL )
       
   174 		{
       
   175 		// No active socket handlers for this operation			
       
   176 		// Find the first free one
       
   177 		for ( TInt i = 0; i < iSocketHandlers.Count(); ++i )
       
   178 			{
       
   179 			if ( ( iSocketHandlers[i]->IsRunning ( aOperation ) ) )
       
   180 				{
       
   181 				return iSocketHandlers[i];	
       
   182 				}
       
   183 			}
       
   184 		// None available. Create one, append to the array and return
       
   185 		CSocketHandler* handler = new CSocketHandler ( iSocket, iObserver );
       
   186 		if ( handler != NULL )
       
   187 			{
       
   188 			iSocketHandlers.Append ( handler );
       
   189 			return handler;
       
   190 			}	
       
   191 		}
       
   192 	else if ( freeHandler->IsRunning ( aOperation ) )
       
   193 		{
       
   194 		__ASSERT_DEBUG ( 1, User::Invariant () );
       
   195 		}
       
   196 	
       
   197 	return freeHandler;
       
   198 	}
       
   199 
       
   200 /**
       
   201 Finds an active socket handler which matches the socket operation.
       
   202 
       
   203 @param aOperation. Socket operation to be performed.
       
   204 @return A free socket handler to perform the socket operation.
       
   205 */
       
   206 CSocketHandler* RSocketHandler::ActiveSocketHandler ( TOperation aOperation )
       
   207 	{
       
   208 	// Find the active socket handler.
       
   209 	for ( TInt i = 0; i < iSocketHandlers.Count(); ++i )
       
   210 		{
       
   211 		if ( (iSocketHandlers[i]->IsRunning ( aOperation )) )
       
   212 			{
       
   213 			return iSocketHandlers[i];	
       
   214 			}
       
   215 		}
       
   216 	return NULL;	
       
   217 	}
       
   218 
       
   219 /**
       
   220 Finds or creates a socket handler which matches the socket operation and
       
   221 submits the request to socket handler.
       
   222 
       
   223 @param aParams. Socket operation parameters.
       
   224 */
       
   225 void RSocketHandler::ActivateSocketHandler ( TSocketHandlerParams& aParams )
       
   226 	{
       
   227 	TOperation op = static_cast < TOperation > ( aParams.iArgs[0] );
       
   228 	CSocketHandler* handler = FreeSocketHandler ( op );
       
   229 	if ( handler == NULL )
       
   230 		{
       
   231 		// Possibly failed due to nomemory.
       
   232 		iObserver.Error ( op, KErrNoMemory ); 
       
   233 		return;			
       
   234 		}
       
   235 	handler->Activate ( aParams );
       
   236 	}