hti/HtiServicePlugins/HtiIpProxyServicePlugin/IPProxyEngine/Src/Csocketrouter.cpp
branchRCL_3
changeset 59 8ad140f3dd41
equal deleted inserted replaced
49:7fdc9a71d314 59:8ad140f3dd41
       
     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 aPort )
       
   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() == aPort 
       
   225                 || iPeerSocketArray->At( i )->LocalPort() == aPort)
       
   226             {
       
   227             return i;
       
   228             }
       
   229         }
       
   230     return -1;
       
   231     }
       
   232 
       
   233 // -----------------------------------------------------------------------------
       
   234 // CSocketRouter::RemoveAllPeers
       
   235 // -----------------------------------------------------------------------------
       
   236 //
       
   237 void CSocketRouter::RemoveAllPeers()
       
   238     {
       
   239     iPeerSocketArray->ResetAndDestroy();
       
   240     }
       
   241 
       
   242 // -----------------------------------------------------------------------------
       
   243 // CSocketRouter::SocketCount
       
   244 // -----------------------------------------------------------------------------
       
   245 //
       
   246 TInt CSocketRouter::SocketCount() const
       
   247     {
       
   248     return iPeerSocketArray->Count();
       
   249     }
       
   250 
       
   251 // -----------------------------------------------------------------------------
       
   252 // CSocketRouter::SetHostSocketL
       
   253 // -----------------------------------------------------------------------------
       
   254 //
       
   255 void CSocketRouter::SetHostSocketL( MSocket* aSocket )
       
   256     {
       
   257     DEBUG_PRINT( DEBUG_STRING(
       
   258         "CSocketRouter::SetHostSocketL()" ) );
       
   259 
       
   260     iHostSocket = aSocket;
       
   261     iHostSocket->SetObserver( this );
       
   262     }
       
   263 
       
   264 
       
   265 // -----------------------------------------------------------------------------
       
   266 // CSocketRouter::RunL
       
   267 // -----------------------------------------------------------------------------
       
   268 //
       
   269 void CSocketRouter::RunL()
       
   270     {
       
   271     WriteQueueL();
       
   272     }
       
   273 
       
   274 // -----------------------------------------------------------------------------
       
   275 // CSocketRouter::DoCancel
       
   276 // -----------------------------------------------------------------------------
       
   277 //
       
   278 void CSocketRouter::DoCancel()
       
   279     {
       
   280     }
       
   281 
       
   282 // -----------------------------------------------------------------------------
       
   283 // CSocketRouter::StartRouting
       
   284 // -----------------------------------------------------------------------------
       
   285 //
       
   286 void CSocketRouter::StartRouting()
       
   287     {
       
   288     DEBUG_PRINT( DEBUG_STRING( "CSocketRouter::StartRouting()" ) );
       
   289 
       
   290     iRouting = ETrue;
       
   291     iHostSocket->IssueRead();
       
   292     TInt peerSocketArrayCount = iPeerSocketArray->Count();
       
   293     for ( TInt i = 0;i < peerSocketArrayCount; i++ )
       
   294         {
       
   295         CSocket* socket = iPeerSocketArray->At( i );
       
   296         socket->IssueRead();
       
   297         }
       
   298     IssueHandleQueue();
       
   299     }
       
   300 
       
   301 // -----------------------------------------------------------------------------
       
   302 // CSocketRouter::IssueHandleQueue
       
   303 // -----------------------------------------------------------------------------
       
   304 //
       
   305 void CSocketRouter::IssueHandleQueue()
       
   306     {
       
   307     if ( !IsActive() )
       
   308         {
       
   309         TRequestStatus* status = &iStatus;
       
   310         User::RequestComplete( status, KErrNone );
       
   311 
       
   312         SetActive();
       
   313         }
       
   314     }
       
   315 
       
   316 
       
   317 // -----------------------------------------------------------------------------
       
   318 // CSocketRouter::StopRouting
       
   319 // -----------------------------------------------------------------------------
       
   320 //
       
   321 void CSocketRouter::StopRouting()
       
   322     {
       
   323     DEBUG_PRINT( DEBUG_STRING( "CSocketRouter::StopRouting()" ) );
       
   324 
       
   325     Cancel();   //If HandleQueue is issued
       
   326 
       
   327     iRouting = EFalse;
       
   328     if ( iHostSocket )
       
   329         {
       
   330         iHostSocket->Cancel();
       
   331         }
       
   332     TInt peerSocketArrayCount = iPeerSocketArray->Count();
       
   333     for ( TInt i = 0;i < peerSocketArrayCount; i++ )
       
   334         {
       
   335         CSocket* socket = iPeerSocketArray->At( i );
       
   336         socket->Cancel();
       
   337         }
       
   338     }
       
   339 
       
   340 
       
   341 // -----------------------------------------------------------------------------
       
   342 // CSocketRouter::IsRouting
       
   343 // -----------------------------------------------------------------------------
       
   344 //
       
   345 TBool CSocketRouter::IsRouting() const
       
   346     {
       
   347     return iRouting;
       
   348     }
       
   349 
       
   350 // -----------------------------------------------------------------------------
       
   351 // CSocketRouter::ResetQueue
       
   352 // -----------------------------------------------------------------------------
       
   353 //
       
   354 void CSocketRouter::ResetQueue()
       
   355     {
       
   356     iWriteEventArray->ResetAndDestroy();
       
   357     }
       
   358 
       
   359 // -----------------------------------------------------------------------------
       
   360 // CSocketRouter::SendCloseTCPConnection
       
   361 // -----------------------------------------------------------------------------
       
   362 //
       
   363 void CSocketRouter::SendCloseTCPConnection( TUint aPort )
       
   364     {
       
   365     DEBUG_PRINT( DEBUG_STRING( "CSocketRouter::SendCloseTCPConnection" ) );
       
   366 
       
   367     iProtocolTCP->SendCloseTCPConnection( *iHostSocket, aPort );
       
   368     }
       
   369 
       
   370 // -----------------------------------------------------------------------------
       
   371 // CSocketRouter::WriteCorrectFrame
       
   372 // -----------------------------------------------------------------------------
       
   373 //
       
   374 void CSocketRouter::WriteCorrectFrameL( TProtocolDesc aProtocolDesc,
       
   375                         TUint aPeerPort,
       
   376                         TUint aOriginalPort,
       
   377                         const TDesC8& aData )
       
   378     {
       
   379     DEBUG_PRINT( DEBUG_STRING(
       
   380             "CSocketRouter::WriteCorrectFrameL(), protocol = %d" ),
       
   381             aProtocolDesc.iProtocol );
       
   382 
       
   383     MBPProtocol* protocol;
       
   384 
       
   385     if ( aProtocolDesc.iProtocol == KProtocolInetUdp )
       
   386         {
       
   387         // UDP frame
       
   388         DEBUG_PRINT( DEBUG_STRING(
       
   389             "CSocketRouter::WriteCorrectFrameL(), UDP" ) );
       
   390         protocol = iProtocolUDP;
       
   391         }
       
   392     else
       
   393         {
       
   394         // TCP frame
       
   395         DEBUG_PRINT( DEBUG_STRING(
       
   396             "CSocketRouter::WriteCorrectFrameL(), TCP" ) );
       
   397         protocol = iProtocolTCP;
       
   398         }
       
   399 
       
   400     protocol->WriteFrameL(
       
   401         *iHostSocket, aPeerPort,
       
   402         aOriginalPort, aData );
       
   403     }
       
   404 
       
   405 // -----------------------------------------------------------------------------
       
   406 // CSocketRouter::DataReceivedL
       
   407 // -----------------------------------------------------------------------------
       
   408 //
       
   409 void CSocketRouter::DataReceivedL( const MSocket* aSocket, const TDesC8& aData )
       
   410     {
       
   411     DEBUG_PRINT( DEBUG_STRING( "CSocketRouter::DataReceivedL()" ) );
       
   412 
       
   413     __ASSERT_ALWAYS( aSocket, Panic( IPProxyEngineNullSocket ) );
       
   414 
       
   415     if ( iRouting )
       
   416         {
       
   417         //Route all data received from the host socket to protocol data handler.
       
   418         if ( aSocket == iHostSocket )
       
   419             {
       
   420             // join the previous buffer if there's something left
       
   421             TInt neededLength = iReceiveBuffer->Length() + aData.Length();
       
   422             HBufC8* joinedBuffers = HBufC8::NewLC( neededLength );
       
   423             TPtr8 joinedBuffersPtr( joinedBuffers->Des() );
       
   424             joinedBuffersPtr.Append( *iReceiveBuffer );
       
   425             joinedBuffersPtr.Append( aData );
       
   426 
       
   427             // Check if there's a protocol that can handle this message
       
   428             TInt length = 0; // length of the full msg
       
   429             TInt startPos = 0; // where the msg prefix starts
       
   430             TBool msgHandled = EFalse;
       
   431             TInt protCount = iProtocolArray->Count();
       
   432 
       
   433             for ( TInt i = 0; i < protCount && !msgHandled; i++ )
       
   434                 {
       
   435                 msgHandled = iProtocolArray->At( i )->HandleReceivedDataL(
       
   436                                                         joinedBuffersPtr,
       
   437                                                         startPos,
       
   438                                                         length  );
       
   439                 }
       
   440             if ( msgHandled )
       
   441                 {
       
   442                 if ( length > 0 )
       
   443                     {
       
   444                     // Length is set only when whole msg is processed
       
   445                     // and correct.
       
   446                     // Store the rest of the msg for the next round
       
   447                     delete iReceiveBuffer;
       
   448                     iReceiveBuffer = NULL;
       
   449                     iReceiveBuffer =
       
   450                             joinedBuffers->Mid( startPos + length ).AllocL();
       
   451                     CleanupStack::PopAndDestroy( joinedBuffers );
       
   452 
       
   453                     if ( iReceiveBuffer->Length() > 0 )
       
   454                         {
       
   455                         // Start again to process the rest
       
   456                         iState = EStateWaitingFrameStart;
       
   457                         DataReceivedL( aSocket, KNullDesC8 );
       
   458                         }
       
   459                     }
       
   460                 else
       
   461                     {
       
   462                     delete iReceiveBuffer;
       
   463                     iReceiveBuffer = joinedBuffers;
       
   464                     CleanupStack::Pop( joinedBuffers );
       
   465                     }
       
   466                 }
       
   467             else
       
   468                 {
       
   469                 // Could not find a valid prefix for this protocol
       
   470                 // store data for the next round
       
   471                 HBufC8* newBuffer = HBufC8::NewLC( KMaxPrefixLength -1 );
       
   472                 TPtr8 newBufferPtr( newBuffer->Des() );
       
   473                 if ( joinedBuffersPtr.Length() > 3 )
       
   474                     {
       
   475                     newBufferPtr =
       
   476                         joinedBuffersPtr.Right( KMaxPrefixLength - 1 );
       
   477                     }
       
   478                 else
       
   479                     {
       
   480                     iState = EStateWaitingFrameStart;
       
   481                     newBufferPtr = joinedBuffersPtr;
       
   482                     }
       
   483                 delete iReceiveBuffer;
       
   484                 iReceiveBuffer = newBuffer;
       
   485                 CleanupStack::Pop( newBuffer );
       
   486                 CleanupStack::PopAndDestroy( joinedBuffers );
       
   487                 return;
       
   488                 }
       
   489             }
       
   490         else    //Route all data received from any other socket to frame writer.
       
   491             {
       
   492             WriteQueueL();
       
   493             TProtocolDesc info;
       
   494             aSocket->SocketInfo( info );
       
   495             WriteCorrectFrameL(
       
   496                 info, aSocket->RemotePort(),
       
   497                 aSocket->LocalPort(), aData );
       
   498 
       
   499             }
       
   500         }
       
   501     else
       
   502         {
       
   503         //Add to queue
       
   504         TProtocolDesc desc;
       
   505         aSocket->SocketInfo( desc );
       
   506         iWriteEventArray->AppendL( CWriteEvent::NewLC( desc,
       
   507             aData, aSocket->RemotePort(), aSocket->LocalPort() ) );
       
   508 
       
   509         DEBUG_PRINT( DEBUG_STRING(
       
   510             "Add to queue: peerPort: %d. origPort: %d" ),
       
   511             aSocket->RemotePort(),
       
   512             aSocket->LocalPort() );
       
   513 
       
   514         CleanupStack::Pop(); // CWriteEvent
       
   515         }
       
   516     }
       
   517 
       
   518 // -----------------------------------------------------------------------------
       
   519 // CSocketRouter::WriteQueueL
       
   520 // -----------------------------------------------------------------------------
       
   521 //
       
   522 void CSocketRouter::WriteQueueL()
       
   523     {
       
   524     DEBUG_PRINT( DEBUG_STRING( "CSocketRouter::WriteQueueL()" ) );
       
   525 
       
   526     TInt writeEventArrayCount = iWriteEventArray->Count();
       
   527     for ( TInt i = 0; i < writeEventArrayCount; i++ )
       
   528         {
       
   529         const CWriteEvent* writeEvent = iWriteEventArray->At( 0 );
       
   530         WriteCorrectFrameL( writeEvent->ProtocolDesc(), writeEvent->PeerPort(),
       
   531             writeEvent->OriginalPort(), writeEvent->Data() );
       
   532         DEBUG_PRINT( DEBUG_STRING(
       
   533             "WriteQueueL(): peerPort: %d. origPort: %d" ),
       
   534             writeEvent->PeerPort(),
       
   535             writeEvent->OriginalPort() );
       
   536         iWriteEventArray->Delete( 0 );
       
   537         delete writeEvent;
       
   538         }
       
   539     }
       
   540 
       
   541 // -----------------------------------------------------------------------------
       
   542 // CSocketRouter::ErrorL
       
   543 // -----------------------------------------------------------------------------
       
   544 //
       
   545 void CSocketRouter::ErrorL( const MSocket* aSocket, TInt aErrorCode )
       
   546     {
       
   547     DEBUG_PRINT( DEBUG_STRING( "CSocketRouter::ErrorL()" ) );
       
   548 
       
   549     iObserver->SocketRouterErrorL( aSocket, aErrorCode );
       
   550     }
       
   551 
       
   552 // -----------------------------------------------------------------------------
       
   553 // CSocketRouter::ObserverLeaved
       
   554 // -----------------------------------------------------------------------------
       
   555 //
       
   556 void CSocketRouter::ObserverLeaved( const MSocket* aSocket, TInt aLeaveCode )
       
   557     {
       
   558     iObserver->ObserverLeaved( aSocket, aLeaveCode );
       
   559     }
       
   560 
       
   561 // -----------------------------------------------------------------------------
       
   562 // CSocketRouter::DisconnectedL
       
   563 // -----------------------------------------------------------------------------
       
   564 //
       
   565 void CSocketRouter::DisconnectedL( const MSocket* aSocket )
       
   566     {
       
   567     DEBUG_PRINT( DEBUG_STRING( "CSocketRouter::DisconnectedL()" ) );
       
   568 
       
   569     if ( aSocket == iHostSocket )
       
   570         {
       
   571         DEBUG_PRINT( DEBUG_STRING( "aSocket == iHostSocket" ) );
       
   572         iObserver->HostDisconnectedL( aSocket );
       
   573         iHostSocket = NULL;
       
   574         }
       
   575     else
       
   576         {
       
   577         TInt peerSocketArrayCount = iPeerSocketArray->Count();
       
   578         for ( TInt i = 0;i < peerSocketArrayCount; i++ )
       
   579             {
       
   580             CSocket* socket = iPeerSocketArray->At( i );
       
   581             if ( socket == aSocket )
       
   582                 {
       
   583                 SendCloseTCPConnection( socket->RemotePort() );
       
   584                 iObserver->PeerDisconnectedL( socket );
       
   585                 RemovePeerSocket( i );
       
   586                 break;
       
   587                 }
       
   588             }
       
   589         }
       
   590     }
       
   591 
       
   592 // -----------------------------------------------------------------------------
       
   593 // CSocketRouter::FrameStarted
       
   594 // -----------------------------------------------------------------------------
       
   595 //
       
   596 void CSocketRouter::FrameStarted()
       
   597     {
       
   598     iState = EStateFrameStartFound;
       
   599     }
       
   600 
       
   601 // -----------------------------------------------------------------------------
       
   602 // CSocketRouter::TCPFrameParsedL
       
   603 // -----------------------------------------------------------------------------
       
   604 //
       
   605 void CSocketRouter::TCPFrameParsedL( TUint aPort, const TDesC8& aData )
       
   606     {
       
   607     DEBUG_PRINT( DEBUG_STRING( "CSocketRouter::TCPFrameParsedL()" ) );
       
   608     DEBUG_PRINT( DEBUG_STRING( "    aPort=%d" ), aPort );
       
   609     DEBUG_PRINT( DEBUG_STRING( "    aData.Length=%d" ), aData.Length() );
       
   610 
       
   611     for ( TInt i = 0; i < iPeerSocketArray->Count(); i++ )
       
   612         {
       
   613         CSocket* socket = iPeerSocketArray->At( i );
       
   614         if ( socket->RemotePort() == aPort && !socket->IsUDP() )
       
   615             {
       
   616             socket->WriteL( aData );
       
   617             break; //for
       
   618             }
       
   619         }
       
   620     }
       
   621 
       
   622 // -----------------------------------------------------------------------------
       
   623 // CSocketRouter::OpenLocalTCPConnectionL
       
   624 // -----------------------------------------------------------------------------
       
   625 //
       
   626 void CSocketRouter::OpenLocalTCPConnectionL( TUint aPort )
       
   627     {
       
   628     DEBUG_PRINT( DEBUG_STRING( "OpenLocalTCPConnectionL, port = %d" ), aPort );
       
   629     if ( iObserver )
       
   630         {
       
   631         iObserver->OpenLocalTCPConnectionL( aPort );
       
   632         }
       
   633     }
       
   634 
       
   635 // -----------------------------------------------------------------------------
       
   636 // CSocketRouter::OpenListeningTCPConnectionL
       
   637 // -----------------------------------------------------------------------------
       
   638 //
       
   639 void CSocketRouter::OpenListeningTCPConnectionL( TUint aPort )
       
   640     {
       
   641     DEBUG_PRINT( DEBUG_STRING( "OpenListeningTCPConnectionL, port = %d" ), aPort );
       
   642     if ( iObserver )
       
   643         {
       
   644         iObserver->OpenListeningTCPConnectionL( aPort );
       
   645         }
       
   646     }
       
   647 
       
   648 // -----------------------------------------------------------------------------
       
   649 // CSocketRouter::CloseTCPConnection
       
   650 // -----------------------------------------------------------------------------
       
   651 //
       
   652 void CSocketRouter::CloseTCPConnectionL( TUint aPort )
       
   653     {
       
   654     DEBUG_PRINT( DEBUG_STRING( "CloseTCPConnection, port = %d" ), aPort );
       
   655     
       
   656     // Close the socket if it exists
       
   657     CArrayPtr<CSocket>* socketArrayForDestroy = new (ELeave) CArrayPtrFlat<CSocket> ( 10 );
       
   658     CleanupStack::PushL(socketArrayForDestroy);
       
   659     TInt index = FindPeerSocket( aPort );
       
   660     while ( index > -1 )
       
   661         {
       
   662         socketArrayForDestroy->AppendL(iPeerSocketArray->At( index ));
       
   663         iPeerSocketArray->Delete(index);
       
   664         index = FindPeerSocket( aPort );
       
   665         }
       
   666     socketArrayForDestroy->ResetAndDestroy();
       
   667     CleanupStack::PopAndDestroy(socketArrayForDestroy);
       
   668 
       
   669     if ( iObserver )
       
   670         {
       
   671         iObserver->CloseTCPConnection( aPort );
       
   672         }
       
   673     }
       
   674 
       
   675 // -----------------------------------------------------------------------------
       
   676 // CSocketRouter::CloseAllTCPConnections
       
   677 // -----------------------------------------------------------------------------
       
   678 //
       
   679 void CSocketRouter::CloseAllTCPConnections()
       
   680     {
       
   681     DEBUG_PRINT( DEBUG_STRING( "CloseAllTCPConnections" ) );
       
   682     if ( iObserver )
       
   683         {
       
   684         iObserver->CloseAllTCPConnections();
       
   685         }
       
   686     }
       
   687 
       
   688 // -----------------------------------------------------------------------------
       
   689 // CSocketRouter::UDPFrameParsedL
       
   690 // -----------------------------------------------------------------------------
       
   691 //
       
   692 void CSocketRouter::UDPFrameParsedL( TUint aPort, const TDesC8& aData )
       
   693     {
       
   694     DEBUG_PRINT( DEBUG_STRING( "CSocketRouter::UDPFrameParsedL()" ) );
       
   695     DEBUG_PRINT( DEBUG_STRING( "    aPort=%d" ), aPort );
       
   696     DEBUG_PRINT( DEBUG_STRING( "    aData.Length=%d" ), aData.Length() );
       
   697     iUDPSender->IssueWriteL( aPort, aData );
       
   698     }
       
   699 
       
   700 // -----------------------------------------------------------------------------
       
   701 // CSocketRouter::ProtocolErrorL
       
   702 // -----------------------------------------------------------------------------
       
   703 //
       
   704 void CSocketRouter::ProtocolErrorL(
       
   705     TInt aErrorCode, const TDesC8& /* aReceivedData */ )
       
   706     {
       
   707     DEBUG_PRINT( DEBUG_STRING( "CSocketRouter::ProtocolErrorL()" ) );
       
   708     DEBUG_PRINT( DEBUG_STRING( "    aErrorCode=%d" ), aErrorCode );
       
   709 
       
   710     if ( iObserver )
       
   711         {
       
   712         iObserver->SocketRouterErrorL( iHostSocket, aErrorCode );
       
   713         }
       
   714     }
       
   715 
       
   716 // -----------------------------------------------------------------------------
       
   717 // CSocketRouter::UDPSenderErrorL
       
   718 // -----------------------------------------------------------------------------
       
   719 //
       
   720 void CSocketRouter::UDPSenderErrorL( TInt aErrorCode )
       
   721     {
       
   722     DEBUG_PRINT( DEBUG_STRING(
       
   723         "CSocketRouter::UDPSenderErrorL(), error code %d" ), aErrorCode );
       
   724     iObserver->SocketRouterErrorL( NULL, aErrorCode );
       
   725     }
       
   726 
       
   727 // -----------------------------------------------------------------------------
       
   728 // CSocketRouter::UDPSenderLeavedL
       
   729 // -----------------------------------------------------------------------------
       
   730 //
       
   731 void CSocketRouter::UDPSenderLeavedL( TInt aLeaveCode )
       
   732     {
       
   733     DEBUG_PRINT( DEBUG_STRING(
       
   734         "CSocketRouter::UDPSenderLeavedL(), leavecode %d" ), aLeaveCode );
       
   735     iObserver->ObserverLeaved( NULL, aLeaveCode );
       
   736     }
       
   737 
       
   738 //  End of File