zeroconf/server/src/csockethandler.cpp
changeset 14 da856f45b798
equal deleted inserted replaced
12:78fbd574edf4 14:da856f45b798
       
     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 // csockethandler.cpp
       
    15 // 
       
    16 //
       
    17 /**
       
    18 @file
       
    19 @internalTechnology
       
    20 */
       
    21 
       
    22 //User include
       
    23 #include "csockethandler.h"
       
    24 
       
    25 #include <mdns/cdnsmessagecomposerparser.h>
       
    26 #include <mdns/cdnsmessage.h>
       
    27 __FLOG_STMT(_LIT8(KComponent,"MDNSServer");)
       
    28 /**
       
    29 Two-phase construction method
       
    30 @param 	aSocket Reference to  RSocket
       
    31 @param 	aObserver Reference to  MSocketHandlerObserver used to notify the client.
       
    32 @param 	aOperation TSocketOperation specfies the operation socket should perform.
       
    33 @return a pointer to the requested socket handler object
       
    34 */
       
    35 
       
    36 CSocketHandler* CSocketHandler::NewL(RSocket& aSocket, MSocketHandlerObserver& aObserver, TSocketOperation aOperation)
       
    37  	{
       
    38  	CSocketHandler* self = new (ELeave)CSocketHandler(aSocket, aObserver, aOperation);
       
    39  	CleanupStack::PushL(self);	
       
    40  	self->ConstructL();
       
    41  	CleanupStack::Pop();
       
    42  	return self;
       
    43  	}
       
    44  	
       
    45  
       
    46  /**
       
    47  Destructor
       
    48  */
       
    49 CSocketHandler::~CSocketHandler()
       
    50 	{
       
    51 	__FLOG(_L8("CSocketHandler::~CSocketHandler - Entry"));
       
    52 	Cancel();
       
    53 	delete iPacketBuffer;
       
    54 	__FLOG(_L8("CSocketHandler::~CSocketHandler - Exit"));
       
    55 	__FLOG_CLOSE;
       
    56 	}
       
    57 	
       
    58 
       
    59 /**
       
    60 This to activate the socket to perform either send or recieve based on the operation set.
       
    61 @param aParams Reference to TSocketHandlerParams containing operation,packet and address
       
    62 @return void
       
    63 */	
       
    64 void CSocketHandler::Activate ( const TSocketHandlerParams& aParams )
       
    65 	{
       
    66 	__FLOG(_L8("CSocketHandler::Activate - Entry"));
       
    67 	iOperation = static_cast < TSocketOperation > ( aParams.iArgs[0] );
       
    68 	switch ( iOperation )
       
    69 		{
       
    70 		case ESocketSend:
       
    71 			{
       
    72 			iPacketPtr.Zero();
       
    73 			HBufC8* bufChain = reinterpret_cast < HBufC8* > ( aParams.iArgs[1] );
       
    74 			iLength = 0;
       
    75 			iSocket.Send(*bufChain, KDefaultFlags, iStatus, iLength );
       
    76 			}
       
    77 		break;
       
    78 		
       
    79 		case ESocketSendTo:
       
    80 			{
       
    81 			iPacketPtr.Zero();
       
    82 			RBuf8* bufChain = reinterpret_cast < RBuf8* > ( aParams.iArgs[1] );
       
    83 			iSockAddr = *(reinterpret_cast < TSockAddr* > ( aParams.iArgs[2] ));
       
    84 			iLength = 0;
       
    85 			iSocket.SendTo ( *bufChain, iSockAddr, KDefaultFlags, iStatus, iLength );
       
    86 			}
       
    87 		break;
       
    88 
       
    89 		case ESocketRecieve:
       
    90 			{
       
    91 			iPacketPtr.Zero();
       
    92 			iSocket.Recv (iPacketPtr, KDefaultFlags, iStatus );
       
    93 			}
       
    94 		break;
       
    95 
       
    96 		case ESocketRecieveFrom:
       
    97 			{
       
    98 			iPacketPtr.Zero();
       
    99 			iSocket.RecvFrom ( iPacketPtr, iSockAddr, KDefaultFlags, iStatus );
       
   100 			}
       
   101 		break;
       
   102 
       
   103 		default:
       
   104 			User::Invariant ();
       
   105 		};
       
   106 	
       
   107 	SetActive();
       
   108 	__FLOG(_L8("CSocketHandler::Activate - Exit"));
       
   109 	}
       
   110 /**
       
   111 
       
   112 */
       
   113 void CSocketHandler::CompleteSelf ( TInt aError )
       
   114 	{
       
   115 	__FLOG(_L8("CSocketHandler::CompleteSelf - Entry"));
       
   116     TRequestStatus* pStat = &iStatus;
       
   117     User::RequestComplete ( pStat, aError );
       
   118 	SetActive ( );
       
   119 	__FLOG(_L8("CSocketHandler::CompleteSelf - Exit"));
       
   120     }
       
   121 /**
       
   122 Constructor
       
   123 */
       
   124 CSocketHandler::CSocketHandler(RSocket& aSocket, MSocketHandlerObserver& aObserver, TSocketOperation aOperation):CActive ( EPriorityNormal ),iSocket(aSocket),iObserver(aObserver),iOperation(aOperation),iPacketPtr(NULL,0)
       
   125 	{
       
   126 		
       
   127 	}
       
   128 /**
       
   129 Constructs a heap to hold the packet of almost 9k size
       
   130 */
       
   131 void CSocketHandler::ConstructL()
       
   132 	{
       
   133 	__FLOG_OPEN(KMDNSSubsystem, KComponent);
       
   134 	__FLOG(_L8("CSocketHandler::ConstructL - Entry"));
       
   135 	// Allocate a 9K buffer to contain the biggest mDNS packet allowable
       
   136 	iPacketBuffer = HBufC8::NewL(KMaxDnsPacketSize);
       
   137 	iPacketPtr.Set(iPacketBuffer->Des());	
       
   138 	CActiveScheduler::Add(this);
       
   139 	__FLOG(_L8("CSocketHandler::ConstructL - Exit"));
       
   140 	}
       
   141 
       
   142 /**
       
   143 cancels the current operation
       
   144 */
       
   145 void CSocketHandler::CancelCurrent()
       
   146 	{
       
   147 	__FLOG(_L8("CSocketHandler::CancelCurrent - Entry"));
       
   148 	switch ( iOperation )
       
   149 		{
       
   150 		case ESocketSend:	
       
   151 		case ESocketSendTo:
       
   152 			iSocket.CancelSend ();
       
   153 		break;
       
   154 
       
   155 		case ESocketRecieve:
       
   156 		case ESocketRecieveFrom:
       
   157 			iSocket.CancelRecv ();
       
   158 		break;
       
   159 
       
   160 		};
       
   161 	__FLOG(_L8("CSocketHandler::CancelCurrent - Exit"));
       
   162 	}
       
   163 /**
       
   164 Notifies the client with parameters based on the operation.
       
   165 a.	Returns the packet and addres for recieve operation.
       
   166 b.	returns only length of data send for send operation.
       
   167 */
       
   168 void CSocketHandler::NotifyCompletionL( )
       
   169 	{
       
   170 	__FLOG(_L8("CSocketHandler::NotifyCompletionL - Entry"));
       
   171 	switch ( iOperation )
       
   172 		{
       
   173 		case ESocketSend:	
       
   174 			{
       
   175 			iObserver.OnCompletionL(iPacketPtr,NULL,iLength());
       
   176 			break;
       
   177 			}
       
   178 	
       
   179 		case ESocketSendTo:
       
   180 			{
       
   181 			iObserver.OnCompletionL(iPacketPtr,iSockAddr,iLength());
       
   182 			break;
       
   183 			}
       
   184 					
       
   185 		case ESocketRecieve:
       
   186 		case ESocketRecieveFrom:		
       
   187 			{
       
   188 
       
   189 			iObserver.OnCompletionL(iPacketPtr,iSockAddr,iLength());
       
   190 			break;
       
   191 			}
       
   192 	
       
   193 		};
       
   194 	__FLOG(_L8("CSocketHandler::NotifyCompletionL - Exit"));
       
   195 	}
       
   196 /**
       
   197 Notifies the client after the send or recieve  	
       
   198 //Derived from CActive
       
   199 */
       
   200 void CSocketHandler::RunL()
       
   201 	{
       
   202 	__FLOG(_L8("CSocketHandler::RunL - Entry"));
       
   203 	TInt err = iStatus.Int ();
       
   204 	if ( err == KErrNone )
       
   205 		{
       
   206 		NotifyCompletionL();
       
   207 		}
       
   208 	else
       
   209 		{
       
   210 		iObserver.OnError ( err );
       
   211 		}
       
   212 	__FLOG(_L8("CSocketHandler::RunL - Exit"));
       
   213 	}
       
   214 	
       
   215 
       
   216 /**
       
   217 Derived from CActive
       
   218 Cancellation of an outstanding request.
       
   219 */	
       
   220 void CSocketHandler::DoCancel()
       
   221 	{
       
   222 	__FLOG(_L8("CSocketHandler::DoCancel - Entry"));
       
   223 	CancelCurrent ();
       
   224 	__FLOG(_L8("CSocketHandler::DoCancel - Exit"));
       
   225 	}
       
   226 
       
   227 
       
   228 	
       
   229 /**
       
   230 Handles any leave from the RunL
       
   231 @param aError Error with which Runl leaves
       
   232 @return error 
       
   233 */	
       
   234 TInt CSocketHandler::RunError(TInt /*aError*/)
       
   235 	{
       
   236 	__FLOG(_L8("CSocketHandler::RunError - Entry"));
       
   237 	//__ASSERT_DEBUG(aError,User::Invariant());
       
   238 	//Ignore any leave from RSocket
       
   239 	__FLOG(_L8("CSocketHandler::RunError - Exit"));
       
   240 	return KErrNone;
       
   241 	}