hti/HtiServicePlugins/HtiIpProxyServicePlugin/IPProxyEngine/Src/Csocketrouter.cpp
changeset 0 a03f92240627
child 4 73ff0d268e1d
equal deleted inserted replaced
-1:000000000000 0:a03f92240627
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "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 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Routes data from peers to host and vice versa
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include "CSocketRouter.h"
       
    22 #include "CommRouterDefinitions.h"
       
    23 #include "MSocketRouterObserver.h"
       
    24 #include "MBPProtocol.h"
       
    25 #include "CProtocolTCP.h"
       
    26 #include "CProtocolUDP.h"
       
    27 #include "CLocalTCPConnection.h"
       
    28 #include "CUDPSender.h"
       
    29 #include "IPProxyEngine.pan"
       
    30 
       
    31 #include <es_sock.h>
       
    32 
       
    33 
       
    34 #define DEBUG_FILENAME "IPProxyEngine.log"
       
    35 #include "DebugPrint.h"
       
    36 
       
    37 // LOCAL CONSTANTS
       
    38 const TInt KSocketRouterSlots = 60;
       
    39 
       
    40 
       
    41 // ============================ MEMBER FUNCTIONS ===============================
       
    42 
       
    43 // -----------------------------------------------------------------------------
       
    44 // CSocketRouter::CSocketRouter
       
    45 // -----------------------------------------------------------------------------
       
    46 //
       
    47 CSocketRouter::CSocketRouter( MSocketRouterObserver* aObserver )
       
    48     : CActive( EPriorityIdle ), iObserver( aObserver )
       
    49     {
       
    50     __ASSERT_DEBUG( iObserver, User::Invariant() );
       
    51     }
       
    52 
       
    53 // -----------------------------------------------------------------------------
       
    54 // CSocketRouter::ConstructL
       
    55 // -----------------------------------------------------------------------------
       
    56 //
       
    57 void CSocketRouter::ConstructL()
       
    58     {
       
    59     User::LeaveIfError( iSocketServ.Connect( KSocketRouterSlots ) );
       
    60 
       
    61     iReceiveBuffer = HBufC8::NewL( 0 );
       
    62 
       
    63     iWriteEventArray = new (ELeave) CArrayPtrSeg<CWriteEvent> ( 10 );
       
    64     iPeerSocketArray = new (ELeave) CArrayPtrFlat<CSocket> ( 10 );
       
    65     iProtocolArray = new (ELeave) CArrayPtrFlat<MBPProtocol> ( 2 );
       
    66 
       
    67     // Add the protocols
       
    68     iProtocolTCP = CProtocolTCP::NewL( this );
       
    69     // takes the ownership
       
    70     iProtocolArray->AppendL( iProtocolTCP );
       
    71 
       
    72     iProtocolUDP = CProtocolUDP::NewL( this );
       
    73     // takes the ownership
       
    74     iProtocolArray->AppendL( iProtocolUDP );
       
    75 
       
    76     iUDPSender = CUDPSender::NewL( this );
       
    77 
       
    78     CActiveScheduler::Add( this );
       
    79     }
       
    80 
       
    81 // -----------------------------------------------------------------------------
       
    82 // CSocketRouter::NewL
       
    83 // -----------------------------------------------------------------------------
       
    84 //
       
    85 CSocketRouter* CSocketRouter::NewL( MSocketRouterObserver* aObserver )
       
    86     {
       
    87     CSocketRouter* self = CSocketRouter::NewLC( aObserver );
       
    88     CleanupStack::Pop();
       
    89 
       
    90     return self;
       
    91     }
       
    92 
       
    93 // -----------------------------------------------------------------------------
       
    94 // CSocketRouter::NewLC
       
    95 // -----------------------------------------------------------------------------
       
    96 //
       
    97 CSocketRouter* CSocketRouter::NewLC( MSocketRouterObserver* aObserver )
       
    98     {
       
    99     CSocketRouter* self = new( ELeave ) CSocketRouter( aObserver );
       
   100     CleanupStack::PushL( self );
       
   101 
       
   102     self->ConstructL();
       
   103     return self;
       
   104     }
       
   105 
       
   106 
       
   107 // Destructor
       
   108 CSocketRouter::~CSocketRouter()
       
   109     {
       
   110     Cancel();
       
   111 
       
   112     delete iUDPSender;
       
   113     delete iReceiveBuffer;
       
   114 
       
   115     if ( iProtocolArray )
       
   116         {
       
   117         iProtocolArray->ResetAndDestroy();
       
   118         delete iProtocolArray;
       
   119         }
       
   120     if ( iPeerSocketArray )
       
   121         {
       
   122         iPeerSocketArray->ResetAndDestroy();
       
   123         delete iPeerSocketArray;
       
   124         }
       
   125     if ( iWriteEventArray )
       
   126         {
       
   127         iWriteEventArray->ResetAndDestroy();
       
   128         delete iWriteEventArray;
       
   129         }
       
   130 
       
   131     iSocketServ.Close();
       
   132     }
       
   133 
       
   134 // -----------------------------------------------------------------------------
       
   135 // CSocketRouter::AddPeerSocketL
       
   136 // -----------------------------------------------------------------------------
       
   137 //
       
   138 void CSocketRouter::AddPeerSocketL( RSocket* aSocket )
       
   139     {
       
   140     CSocket* newSocket = CSocket::NewLC( aSocket );
       
   141     newSocket->SetObserver( this );
       
   142     iPeerSocketArray->AppendL( newSocket );
       
   143     newSocket->SetSocketOwnershipMode( ETrue );
       
   144 
       
   145     DEBUG_PRINT( DEBUG_STRING(
       
   146         "CSocketRouter::AddPeerSocketL(), localport=%d, remote port=%d" ),
       
   147         newSocket->LocalPort(),
       
   148         newSocket->RemotePort() );
       
   149 
       
   150     CleanupStack::Pop( newSocket );
       
   151     }
       
   152 
       
   153 
       
   154 // -----------------------------------------------------------------------------
       
   155 // CSocketRouter::AddUDPSocketL
       
   156 // -----------------------------------------------------------------------------
       
   157 //
       
   158 void CSocketRouter::AddUDPSocketL( TUint aPort )
       
   159     {
       
   160     RSocket* newSocket = new (ELeave) RSocket();
       
   161     CleanupStack::PushL( newSocket );
       
   162 
       
   163     // Open the socket
       
   164     DEBUG_PRINT( DEBUG_STRING( "CSocketRouter::AddUDPSocketL, Trying to open UDP port %d" ), aPort );
       
   165 
       
   166     User::LeaveIfError( newSocket->Open( iSocketServ, KAfInet, KSockDatagram,
       
   167         KProtocolInetUdp  ) );
       
   168 
       
   169     DEBUG_PRINT( DEBUG_STRING( "CSocketRouter::AddUDPSocketL, UDP port opened." ), aPort );
       
   170 
       
   171     // Bind the  socket to the correct port.
       
   172     TInetAddr anyAddrOnPort( KInetAddrAny, aPort );
       
   173     newSocket->Bind( anyAddrOnPort );
       
   174 
       
   175     CSocket* cSocket = CSocket::NewLC( newSocket, aPort );
       
   176     cSocket->SetObserver( this );
       
   177     iPeerSocketArray->AppendL( cSocket );
       
   178     cSocket->SetSocketOwnershipMode( ETrue );
       
   179     CleanupStack::Pop( cSocket );
       
   180 
       
   181     CleanupStack::Pop( newSocket );
       
   182     }
       
   183 
       
   184 // -----------------------------------------------------------------------------
       
   185 // CSocketRouter::RemovePeerSocket
       
   186 // -----------------------------------------------------------------------------
       
   187 //
       
   188 void CSocketRouter::RemovePeerSocket( TInt aIndex )
       
   189     {
       
   190     DEBUG_PRINT( DEBUG_STRING( "CSocketRouter::RemovePeerSocket()" ) );
       
   191     CSocket* socket = iPeerSocketArray->At( aIndex );
       
   192     iPeerSocketArray->Delete( aIndex );
       
   193     delete socket;
       
   194     }
       
   195 
       
   196 // -----------------------------------------------------------------------------
       
   197 // CSocketRouter::RemovePeerSocket
       
   198 // -----------------------------------------------------------------------------
       
   199 //
       
   200 void CSocketRouter::RemovePeerSocket( RSocket* aSocket )
       
   201     {
       
   202     DEBUG_PRINT( DEBUG_STRING( "CSocketRouter::RemovePeerSocket()" ) );
       
   203     TInt count = iPeerSocketArray->Count();
       
   204     for ( TInt i = 0; i < count; i++ )
       
   205         {
       
   206         if ( iPeerSocketArray->At( i )->GetRSocket() == aSocket )
       
   207             {
       
   208             iPeerSocketArray->Delete( i );
       
   209             break;
       
   210             }
       
   211         }
       
   212     }
       
   213 
       
   214 // -----------------------------------------------------------------------------
       
   215 // CSocketRouter::FindPeerSocket
       
   216 // -----------------------------------------------------------------------------
       
   217 //
       
   218 TInt CSocketRouter::FindPeerSocket( TUint aRemotePort )
       
   219     {
       
   220     DEBUG_PRINT( DEBUG_STRING( "CSocketRouter::FindPeerSocket()" ) );
       
   221     TInt count = iPeerSocketArray->Count();
       
   222     for ( TInt i = 0; i < count; i++ )
       
   223         {
       
   224         if ( iPeerSocketArray->At( i )->RemotePort() == aRemotePort )
       
   225             {
       
   226             return i;
       
   227             }
       
   228         }
       
   229     return -1;
       
   230     }
       
   231 
       
   232 // -----------------------------------------------------------------------------
       
   233 // CSocketRouter::RemoveAllPeers
       
   234 // -----------------------------------------------------------------------------
       
   235 //
       
   236 void CSocketRouter::RemoveAllPeers()
       
   237     {
       
   238     iPeerSocketArray->ResetAndDestroy();
       
   239     }
       
   240 
       
   241 // -----------------------------------------------------------------------------
       
   242 // CSocketRouter::SocketCount
       
   243 // -----------------------------------------------------------------------------
       
   244 //
       
   245 TInt CSocketRouter::SocketCount() const
       
   246     {
       
   247     return iPeerSocketArray->Count();
       
   248     }
       
   249 
       
   250 // -----------------------------------------------------------------------------
       
   251 // CSocketRouter::SetHostSocketL
       
   252 // -----------------------------------------------------------------------------
       
   253 //
       
   254 void CSocketRouter::SetHostSocketL( MSocket* aSocket )
       
   255     {
       
   256     DEBUG_PRINT( DEBUG_STRING(
       
   257         "CSocketRouter::SetHostSocketL()" ) );
       
   258 
       
   259     iHostSocket = aSocket;
       
   260     iHostSocket->SetObserver( this );
       
   261     }
       
   262 
       
   263 
       
   264 // -----------------------------------------------------------------------------
       
   265 // CSocketRouter::RunL
       
   266 // -----------------------------------------------------------------------------
       
   267 //
       
   268 void CSocketRouter::RunL()
       
   269     {
       
   270     WriteQueueL();
       
   271     }
       
   272 
       
   273 // -----------------------------------------------------------------------------
       
   274 // CSocketRouter::DoCancel
       
   275 // -----------------------------------------------------------------------------
       
   276 //
       
   277 void CSocketRouter::DoCancel()
       
   278     {
       
   279     }
       
   280 
       
   281 // -----------------------------------------------------------------------------
       
   282 // CSocketRouter::StartRouting
       
   283 // -----------------------------------------------------------------------------
       
   284 //
       
   285 void CSocketRouter::StartRouting()
       
   286     {
       
   287     DEBUG_PRINT( DEBUG_STRING( "CSocketRouter::StartRouting()" ) );
       
   288 
       
   289     iRouting = ETrue;
       
   290     iHostSocket->IssueRead();
       
   291     TInt peerSocketArrayCount = iPeerSocketArray->Count();
       
   292     for ( TInt i = 0;i < peerSocketArrayCount; i++ )
       
   293         {
       
   294         CSocket* socket = iPeerSocketArray->At( i );
       
   295         socket->IssueRead();
       
   296         }
       
   297     IssueHandleQueue();
       
   298     }
       
   299 
       
   300 // -----------------------------------------------------------------------------
       
   301 // CSocketRouter::IssueHandleQueue
       
   302 // -----------------------------------------------------------------------------
       
   303 //
       
   304 void CSocketRouter::IssueHandleQueue()
       
   305     {
       
   306     if ( !IsActive() )
       
   307         {
       
   308         TRequestStatus* status = &iStatus;
       
   309         User::RequestComplete( status, KErrNone );
       
   310 
       
   311         SetActive();
       
   312         }
       
   313     }
       
   314 
       
   315 
       
   316 // -----------------------------------------------------------------------------
       
   317 // CSocketRouter::StopRouting
       
   318 // -----------------------------------------------------------------------------
       
   319 //
       
   320 void CSocketRouter::StopRouting()
       
   321     {
       
   322     DEBUG_PRINT( DEBUG_STRING( "CSocketRouter::StopRouting()" ) );
       
   323 
       
   324     Cancel();   //If HandleQueue is issued
       
   325 
       
   326     iRouting = EFalse;
       
   327     if ( iHostSocket )
       
   328         {
       
   329         iHostSocket->Cancel();
       
   330         }
       
   331     TInt peerSocketArrayCount = iPeerSocketArray->Count();
       
   332     for ( TInt i = 0;i < peerSocketArrayCount; i++ )
       
   333         {
       
   334         CSocket* socket = iPeerSocketArray->At( i );
       
   335         socket->Cancel();
       
   336         }
       
   337     }
       
   338 
       
   339 
       
   340 // -----------------------------------------------------------------------------
       
   341 // CSocketRouter::IsRouting
       
   342 // -----------------------------------------------------------------------------
       
   343 //
       
   344 TBool CSocketRouter::IsRouting() const
       
   345     {
       
   346     return iRouting;
       
   347     }
       
   348 
       
   349 // -----------------------------------------------------------------------------
       
   350 // CSocketRouter::ResetQueue
       
   351 // -----------------------------------------------------------------------------
       
   352 //
       
   353 void CSocketRouter::ResetQueue()
       
   354     {
       
   355     iWriteEventArray->ResetAndDestroy();
       
   356     }
       
   357 
       
   358 // -----------------------------------------------------------------------------
       
   359 // CSocketRouter::SendCloseTCPConnection
       
   360 // -----------------------------------------------------------------------------
       
   361 //
       
   362 void CSocketRouter::SendCloseTCPConnection( TUint aPort )
       
   363     {
       
   364     DEBUG_PRINT( DEBUG_STRING( "CSocketRouter::SendCloseTCPConnection" ) );
       
   365 
       
   366     iProtocolTCP->SendCloseTCPConnection( *iHostSocket, aPort );
       
   367     }
       
   368 
       
   369 // -----------------------------------------------------------------------------
       
   370 // CSocketRouter::WriteCorrectFrame
       
   371 // -----------------------------------------------------------------------------
       
   372 //
       
   373 void CSocketRouter::WriteCorrectFrameL( TProtocolDesc aProtocolDesc,
       
   374                         TUint aPeerPort,
       
   375                         TUint aOriginalPort,
       
   376                         const TDesC8& aData )
       
   377     {
       
   378     DEBUG_PRINT( DEBUG_STRING(
       
   379             "CSocketRouter::WriteCorrectFrameL(), protocol = %d" ),
       
   380             aProtocolDesc.iProtocol );
       
   381 
       
   382     MBPProtocol* protocol;
       
   383 
       
   384     if ( aProtocolDesc.iProtocol == KProtocolInetUdp )
       
   385         {
       
   386         // UDP frame
       
   387         DEBUG_PRINT( DEBUG_STRING(
       
   388             "CSocketRouter::WriteCorrectFrameL(), UDP" ) );
       
   389         protocol = iProtocolUDP;
       
   390         }
       
   391     else
       
   392         {
       
   393         // TCP frame
       
   394         DEBUG_PRINT( DEBUG_STRING(
       
   395             "CSocketRouter::WriteCorrectFrameL(), TCP" ) );
       
   396         protocol = iProtocolTCP;
       
   397         }
       
   398 
       
   399     protocol->WriteFrameL(
       
   400         *iHostSocket, aPeerPort,
       
   401         aOriginalPort, aData );
       
   402     }
       
   403 
       
   404 // -----------------------------------------------------------------------------
       
   405 // CSocketRouter::DataReceivedL
       
   406 // -----------------------------------------------------------------------------
       
   407 //
       
   408 void CSocketRouter::DataReceivedL( const MSocket* aSocket, const TDesC8& aData )
       
   409     {
       
   410     DEBUG_PRINT( DEBUG_STRING( "CSocketRouter::DataReceivedL()" ) );
       
   411 
       
   412     __ASSERT_ALWAYS( aSocket, Panic( IPProxyEngineNullSocket ) );
       
   413 
       
   414     if ( iRouting )
       
   415         {
       
   416         //Route all data received from the host socket to protocol data handler.
       
   417         if ( aSocket == iHostSocket )
       
   418             {
       
   419             // join the previous buffer if there's something left
       
   420             TInt neededLength = iReceiveBuffer->Length() + aData.Length();
       
   421             HBufC8* joinedBuffers = HBufC8::NewLC( neededLength );
       
   422             TPtr8 joinedBuffersPtr( joinedBuffers->Des() );
       
   423             joinedBuffersPtr.Append( *iReceiveBuffer );
       
   424             joinedBuffersPtr.Append( aData );
       
   425 
       
   426             // Check if there's a protocol that can handle this message
       
   427             TInt length = 0; // length of the full msg
       
   428             TInt startPos = 0; // where the msg prefix starts
       
   429             TBool msgHandled = EFalse;
       
   430             TInt protCount = iProtocolArray->Count();
       
   431 
       
   432             for ( TInt i = 0; i < protCount && !msgHandled; i++ )
       
   433                 {
       
   434                 msgHandled = iProtocolArray->At( i )->HandleReceivedDataL(
       
   435                                                         joinedBuffersPtr,
       
   436                                                         startPos,
       
   437                                                         length  );
       
   438                 }
       
   439             if ( msgHandled )
       
   440                 {
       
   441                 if ( length > 0 )
       
   442                     {
       
   443                     // Length is set only when whole msg is processed
       
   444                     // and correct.
       
   445                     // Store the rest of the msg for the next round
       
   446                     delete iReceiveBuffer;
       
   447                     iReceiveBuffer = NULL;
       
   448                     iReceiveBuffer =
       
   449                             joinedBuffers->Mid( startPos + length ).AllocL();
       
   450                     CleanupStack::PopAndDestroy( joinedBuffers );
       
   451 
       
   452                     if ( iReceiveBuffer->Length() > 0 )
       
   453                         {
       
   454                         // Start again to process the rest
       
   455                         iState = EStateWaitingFrameStart;
       
   456                         DataReceivedL( aSocket, KNullDesC8 );
       
   457                         }
       
   458                     }
       
   459                 else
       
   460                     {
       
   461                     delete iReceiveBuffer;
       
   462                     iReceiveBuffer = joinedBuffers;
       
   463                     CleanupStack::Pop( joinedBuffers );
       
   464                     }
       
   465                 }
       
   466             else
       
   467                 {
       
   468                 // Could not find a valid prefix for this protocol
       
   469                 // store data for the next round
       
   470                 HBufC8* newBuffer = HBufC8::NewLC( KMaxPrefixLength -1 );
       
   471                 TPtr8 newBufferPtr( newBuffer->Des() );
       
   472                 if ( joinedBuffersPtr.Length() > 3 )
       
   473                     {
       
   474                     newBufferPtr =
       
   475                         joinedBuffersPtr.Right( KMaxPrefixLength - 1 );
       
   476                     }
       
   477                 else
       
   478                     {
       
   479                     iState = EStateWaitingFrameStart;
       
   480                     newBufferPtr = joinedBuffersPtr;
       
   481                     }
       
   482                 delete iReceiveBuffer;
       
   483                 iReceiveBuffer = newBuffer;
       
   484                 CleanupStack::Pop( newBuffer );
       
   485                 CleanupStack::PopAndDestroy( joinedBuffers );
       
   486                 return;
       
   487                 }
       
   488             }
       
   489         else    //Route all data received from any other socket to frame writer.
       
   490             {
       
   491             WriteQueueL();
       
   492             TProtocolDesc info;
       
   493             aSocket->SocketInfo( info );
       
   494             WriteCorrectFrameL(
       
   495                 info, aSocket->RemotePort(),
       
   496                 aSocket->LocalPort(), aData );
       
   497 
       
   498             }
       
   499         }
       
   500     else
       
   501         {
       
   502         //Add to queue
       
   503         TProtocolDesc desc;
       
   504         aSocket->SocketInfo( desc );
       
   505         iWriteEventArray->AppendL( CWriteEvent::NewLC( desc,
       
   506             aData, aSocket->RemotePort(), aSocket->LocalPort() ) );
       
   507 
       
   508         DEBUG_PRINT( DEBUG_STRING(
       
   509             "Add to queue: peerPort: %d. origPort: %d" ),
       
   510             aSocket->RemotePort(),
       
   511             aSocket->LocalPort() );
       
   512 
       
   513         CleanupStack::Pop(); // CWriteEvent
       
   514         }
       
   515     }
       
   516 
       
   517 // -----------------------------------------------------------------------------
       
   518 // CSocketRouter::WriteQueueL
       
   519 // -----------------------------------------------------------------------------
       
   520 //
       
   521 void CSocketRouter::WriteQueueL()
       
   522     {
       
   523     DEBUG_PRINT( DEBUG_STRING( "CSocketRouter::WriteQueueL()" ) );
       
   524 
       
   525     TInt writeEventArrayCount = iWriteEventArray->Count();
       
   526     for ( TInt i = 0; i < writeEventArrayCount; i++ )
       
   527         {
       
   528         const CWriteEvent* writeEvent = iWriteEventArray->At( 0 );
       
   529         WriteCorrectFrameL( writeEvent->ProtocolDesc(), writeEvent->PeerPort(),
       
   530             writeEvent->OriginalPort(), writeEvent->Data() );
       
   531         DEBUG_PRINT( DEBUG_STRING(
       
   532             "WriteQueueL(): peerPort: %d. origPort: %d" ),
       
   533             writeEvent->PeerPort(),
       
   534             writeEvent->OriginalPort() );
       
   535         iWriteEventArray->Delete( 0 );
       
   536         delete writeEvent;
       
   537         }
       
   538     }
       
   539 
       
   540 // -----------------------------------------------------------------------------
       
   541 // CSocketRouter::ErrorL
       
   542 // -----------------------------------------------------------------------------
       
   543 //
       
   544 void CSocketRouter::ErrorL( const MSocket* aSocket, TInt aErrorCode )
       
   545     {
       
   546     DEBUG_PRINT( DEBUG_STRING( "CSocketRouter::ErrorL()" ) );
       
   547 
       
   548     iObserver->SocketRouterErrorL( aSocket, aErrorCode );
       
   549     }
       
   550 
       
   551 // -----------------------------------------------------------------------------
       
   552 // CSocketRouter::ObserverLeaved
       
   553 // -----------------------------------------------------------------------------
       
   554 //
       
   555 void CSocketRouter::ObserverLeaved( const MSocket* aSocket, TInt aLeaveCode )
       
   556     {
       
   557     iObserver->ObserverLeaved( aSocket, aLeaveCode );
       
   558     }
       
   559 
       
   560 // -----------------------------------------------------------------------------
       
   561 // CSocketRouter::DisconnectedL
       
   562 // -----------------------------------------------------------------------------
       
   563 //
       
   564 void CSocketRouter::DisconnectedL( const MSocket* aSocket )
       
   565     {
       
   566     DEBUG_PRINT( DEBUG_STRING( "CSocketRouter::DisconnectedL()" ) );
       
   567 
       
   568     if ( aSocket == iHostSocket )
       
   569         {
       
   570         DEBUG_PRINT( DEBUG_STRING( "aSocket == iHostSocket" ) );
       
   571         iObserver->HostDisconnectedL( aSocket );
       
   572         iHostSocket = NULL;
       
   573         }
       
   574     else
       
   575         {
       
   576         TInt peerSocketArrayCount = iPeerSocketArray->Count();
       
   577         for ( TInt i = 0;i < peerSocketArrayCount; i++ )
       
   578             {
       
   579             CSocket* socket = iPeerSocketArray->At( i );
       
   580             if ( socket == aSocket )
       
   581                 {
       
   582                 SendCloseTCPConnection( socket->RemotePort() );
       
   583                 iObserver->PeerDisconnectedL( socket );
       
   584                 RemovePeerSocket( i );
       
   585                 break;
       
   586                 }
       
   587             }
       
   588         }
       
   589     }
       
   590 
       
   591 // -----------------------------------------------------------------------------
       
   592 // CSocketRouter::FrameStarted
       
   593 // -----------------------------------------------------------------------------
       
   594 //
       
   595 void CSocketRouter::FrameStarted()
       
   596     {
       
   597     iState = EStateFrameStartFound;
       
   598     }
       
   599 
       
   600 // -----------------------------------------------------------------------------
       
   601 // CSocketRouter::TCPFrameParsedL
       
   602 // -----------------------------------------------------------------------------
       
   603 //
       
   604 void CSocketRouter::TCPFrameParsedL( TUint aPort, const TDesC8& aData )
       
   605     {
       
   606     DEBUG_PRINT( DEBUG_STRING( "CSocketRouter::TCPFrameParsedL()" ) );
       
   607     DEBUG_PRINT( DEBUG_STRING( "    aPort=%d" ), aPort );
       
   608     DEBUG_PRINT( DEBUG_STRING( "    aData.Length=%d" ), aData.Length() );
       
   609 
       
   610     for ( TInt i = 0; i < iPeerSocketArray->Count(); i++ )
       
   611         {
       
   612         CSocket* socket = iPeerSocketArray->At( i );
       
   613         if ( socket->RemotePort() == aPort && !socket->IsUDP() )
       
   614             {
       
   615             socket->WriteL( aData );
       
   616             break; //for
       
   617             }
       
   618         }
       
   619     }
       
   620 
       
   621 // -----------------------------------------------------------------------------
       
   622 // CSocketRouter::OpenLocalTCPConnectionL
       
   623 // -----------------------------------------------------------------------------
       
   624 //
       
   625 void CSocketRouter::OpenLocalTCPConnectionL( TUint aPort )
       
   626     {
       
   627     DEBUG_PRINT( DEBUG_STRING( "OpenLocalTCPConnectionL, port = %d" ), aPort );
       
   628     if ( iObserver )
       
   629         {
       
   630         iObserver->OpenLocalTCPConnectionL( aPort );
       
   631         }
       
   632     }
       
   633 
       
   634 // -----------------------------------------------------------------------------
       
   635 // CSocketRouter::OpenListeningTCPConnectionL
       
   636 // -----------------------------------------------------------------------------
       
   637 //
       
   638 void CSocketRouter::OpenListeningTCPConnectionL( TUint aPort )
       
   639     {
       
   640     DEBUG_PRINT( DEBUG_STRING( "OpenListeningTCPConnectionL, port = %d" ), aPort );
       
   641     if ( iObserver )
       
   642         {
       
   643         iObserver->OpenListeningTCPConnectionL( aPort );
       
   644         }
       
   645     }
       
   646 
       
   647 // -----------------------------------------------------------------------------
       
   648 // CSocketRouter::CloseTCPConnection
       
   649 // -----------------------------------------------------------------------------
       
   650 //
       
   651 void CSocketRouter::CloseTCPConnection( TUint aPort )
       
   652     {
       
   653     DEBUG_PRINT( DEBUG_STRING( "CloseTCPConnection, port = %d" ), aPort );
       
   654     // Close the socket if it exists
       
   655     TInt index = FindPeerSocket( aPort );
       
   656     if ( index > -1 )
       
   657         {
       
   658         RemovePeerSocket( index );
       
   659         }
       
   660 
       
   661     if ( iObserver && index > -1 )
       
   662         {
       
   663         iObserver->CloseTCPConnection( aPort );
       
   664         }
       
   665     }
       
   666 
       
   667 // -----------------------------------------------------------------------------
       
   668 // CSocketRouter::CloseAllTCPConnections
       
   669 // -----------------------------------------------------------------------------
       
   670 //
       
   671 void CSocketRouter::CloseAllTCPConnections()
       
   672     {
       
   673     DEBUG_PRINT( DEBUG_STRING( "CloseAllTCPConnections" ) );
       
   674     if ( iObserver )
       
   675         {
       
   676         iObserver->CloseAllTCPConnections();
       
   677         }
       
   678     }
       
   679 
       
   680 // -----------------------------------------------------------------------------
       
   681 // CSocketRouter::UDPFrameParsedL
       
   682 // -----------------------------------------------------------------------------
       
   683 //
       
   684 void CSocketRouter::UDPFrameParsedL( TUint aPort, const TDesC8& aData )
       
   685     {
       
   686     DEBUG_PRINT( DEBUG_STRING( "CSocketRouter::UDPFrameParsedL()" ) );
       
   687     DEBUG_PRINT( DEBUG_STRING( "    aPort=%d" ), aPort );
       
   688     DEBUG_PRINT( DEBUG_STRING( "    aData.Length=%d" ), aData.Length() );
       
   689     iUDPSender->IssueWriteL( aPort, aData );
       
   690     }
       
   691 
       
   692 // -----------------------------------------------------------------------------
       
   693 // CSocketRouter::ProtocolErrorL
       
   694 // -----------------------------------------------------------------------------
       
   695 //
       
   696 void CSocketRouter::ProtocolErrorL(
       
   697     TInt aErrorCode, const TDesC8& /* aReceivedData */ )
       
   698     {
       
   699     DEBUG_PRINT( DEBUG_STRING( "CSocketRouter::ProtocolErrorL()" ) );
       
   700     DEBUG_PRINT( DEBUG_STRING( "    aErrorCode=%d" ), aErrorCode );
       
   701 
       
   702     if ( iObserver )
       
   703         {
       
   704         iObserver->SocketRouterErrorL( iHostSocket, aErrorCode );
       
   705         }
       
   706     }
       
   707 
       
   708 // -----------------------------------------------------------------------------
       
   709 // CSocketRouter::UDPSenderErrorL
       
   710 // -----------------------------------------------------------------------------
       
   711 //
       
   712 void CSocketRouter::UDPSenderErrorL( TInt aErrorCode )
       
   713     {
       
   714     DEBUG_PRINT( DEBUG_STRING(
       
   715         "CSocketRouter::UDPSenderErrorL(), error code %d" ), aErrorCode );
       
   716     iObserver->SocketRouterErrorL( NULL, aErrorCode );
       
   717     }
       
   718 
       
   719 // -----------------------------------------------------------------------------
       
   720 // CSocketRouter::UDPSenderLeavedL
       
   721 // -----------------------------------------------------------------------------
       
   722 //
       
   723 void CSocketRouter::UDPSenderLeavedL( TInt aLeaveCode )
       
   724     {
       
   725     DEBUG_PRINT( DEBUG_STRING(
       
   726         "CSocketRouter::UDPSenderLeavedL(), leavecode %d" ), aLeaveCode );
       
   727     iObserver->ObserverLeaved( NULL, aLeaveCode );
       
   728     }
       
   729 
       
   730 //  End of File