Msrp/MsrpServer/src/CMSRPConnector.cpp
branchMSRP_FrameWork
changeset 25 505ad3f0ce5c
child 58 cdb720e67852
equal deleted inserted replaced
22:f1578314b8da 25:505ad3f0ce5c
       
     1 /*
       
     2 * Copyright (c) 2009-2010 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 * Initial Contributors:
       
     9 * Nokia Corporation - initial contribution.
       
    10 * Contributors:
       
    11 *
       
    12 * Description:
       
    13 * MSRP Implementation
       
    14 *
       
    15 */
       
    16 
       
    17 // INTERNAL INCLUDES
       
    18 #include "CMSRPConnector.h"
       
    19 #include "MSRPCommon.h"
       
    20 
       
    21 // -----------------------------------------------------------------------------
       
    22 // CMSRPConnListener::NewL
       
    23 // Static constructor
       
    24 // -----------------------------------------------------------------------------
       
    25 //
       
    26 /*static*/ MMSRPConnector* CMSRPConnector::NewL(MMSRPConnectionManager& aConnMngr, MMSRPConnectorObserver& aConnection)
       
    27     {
       
    28     MSRPLOG( "CMSRPConnector::NewL enter" )
       
    29     CMSRPConnector* self = new (ELeave) CMSRPConnector( aConnMngr, aConnection );
       
    30     CleanupStack::PushL(self);
       
    31     self->ConstructL();
       
    32     CleanupStack::Pop(self);
       
    33     MSRPLOG( "CMSRPConnector::NewL exit" )
       
    34     return self;
       
    35     }
       
    36 
       
    37 // -----------------------------------------------------------------------------
       
    38 // CMSRPConnector::CMSRPConnector
       
    39 // Constructor
       
    40 // -----------------------------------------------------------------------------
       
    41 //
       
    42 CMSRPConnector::CMSRPConnector(MMSRPConnectionManager& aConnMngr, MMSRPConnectorObserver& aConnection)        
       
    43     : CActive(CActive::EPriorityStandard), iConnMngr(aConnMngr), iConnection( aConnection )
       
    44     {  
       
    45     CActiveScheduler::Add(this);
       
    46     }
       
    47 
       
    48 // -----------------------------------------------------------------------------
       
    49 // CMSRPConnector::~CMSRPConnector
       
    50 // Destructor
       
    51 // -----------------------------------------------------------------------------
       
    52 //
       
    53 CMSRPConnector::~CMSRPConnector()
       
    54     {
       
    55     MSRPLOG( "CMSRPConnector::~CMSRPConnector enter" )
       
    56     delete iTimer;
       
    57     Cancel();    
       
    58     if(iSocket)
       
    59         {
       
    60         iSocket->Close();
       
    61         delete iSocket;
       
    62         }
       
    63     MSRPLOG( "CMSRPConnector::~CMSRPConnector exit" )
       
    64     }
       
    65 
       
    66 // -----------------------------------------------------------------------------
       
    67 // CMSRPConnector::ConstructL
       
    68 // 2nd phase constructor
       
    69 // -----------------------------------------------------------------------------
       
    70 //
       
    71 void CMSRPConnector::ConstructL()
       
    72     {
       
    73     //iState = ENotConnected;
       
    74     iTimer = CMSRPTimeOutTimer::NewL( *this );
       
    75     //create socket
       
    76 
       
    77     iSocket = new (ELeave) RSocket();   
       
    78     User::LeaveIfError( iSocket->Open(
       
    79             iConnMngr.SocketServer(), KAfInet, KSockStream, KProtocolInetTcp, iConnMngr.SocketServerConn()));
       
    80     User::LeaveIfError( iSocket->SetOpt( KSoReuseAddr, KSolInetIp, ETrue ) );
       
    81     iSocket->SetLocalPort(KMsrpPort);
       
    82     }
       
    83 
       
    84 // -----------------------------------------------------------------------------
       
    85 // CMSRPConnector::DoCancel
       
    86 // Cancels outstanding request.
       
    87 // -----------------------------------------------------------------------------
       
    88 //
       
    89 void CMSRPConnector::DoCancel()
       
    90     {
       
    91     MSRPLOG( "CMSRPConnector::DoCancel enter" )
       
    92     iSocket->CancelConnect();
       
    93     MSRPLOG( "CMSRPConnector::DoCancel exit" )
       
    94     }
       
    95 
       
    96 // -----------------------------------------------------------------------------
       
    97 // CMSRPConnector::Connect
       
    98 // -----------------------------------------------------------------------------
       
    99 //
       
   100 void CMSRPConnector::Connect(TInetAddr& aRemoteAddr)
       
   101     {
       
   102     MSRPLOG( "CMSRPConnector::Connect enter" )
       
   103     iSocket->Connect( aRemoteAddr, iStatus );
       
   104     //iState = EConnecting;
       
   105     SetActive();
       
   106     iTimer->After( KTimeOutInSeconds * KSecondinMicroseconds );
       
   107     MSRPLOG( "CMSRPConnector::Connect exit" )
       
   108     }
       
   109 
       
   110 
       
   111 // -----------------------------------------------------------------------------
       
   112 // CMSRPConnector::TimerExpired
       
   113 // -----------------------------------------------------------------------------
       
   114 //
       
   115 void CMSRPConnector::TimerExpiredL()   
       
   116     {
       
   117     MSRPLOG( "CMSRPConnector::TimerExpired enter" )
       
   118     iConnection.ConnectionEstablishedL(MMSRPConnection::EConnectTimedOut, iSocket, KErrTimedOut);
       
   119     MSRPLOG( "CMSRPConnector::TimerExpired exit" )
       
   120     }
       
   121 
       
   122 // -----------------------------------------------------------------------------
       
   123 // CMSRPConnector::RunL
       
   124 // -----------------------------------------------------------------------------
       
   125 //
       
   126 void CMSRPConnector::RunL()
       
   127     {
       
   128     MSRPLOG( "CMSRPConnector::RunL enter" )
       
   129     iTimer->Cancel();
       
   130     
       
   131     if(iStatus.Int() != KErrNone)
       
   132         {
       
   133         MSRPLOG2( "CMSRPConnector::RunL Connection Error %d", iStatus.Int() );
       
   134         iConnection.ConnectionEstablishedL(MMSRPConnection::EError, iSocket, iStatus.Int());
       
   135         return;
       
   136         }
       
   137     
       
   138     iConnection.ConnectionEstablishedL(MMSRPConnection::EConnected, iSocket, KErrNone);
       
   139     MSRPLOG( "CMSRPConnector::RunL exit" )
       
   140     }
       
   141 
       
   142 // -----------------------------------------------------------------------------
       
   143 // CMSRPConnector::RunError
       
   144 // -----------------------------------------------------------------------------
       
   145 //
       
   146 TInt CMSRPConnector::RunError(TInt aError)
       
   147     {
       
   148     //handle error delete connector on error
       
   149     //make sure ConnectorStateL does not leave on EError
       
   150     MSRPLOG( "CMSRPConnector::RunError enter" )
       
   151     TRAPD(err, iConnection.ConnectionEstablishedL(MMSRPConnection::EError, iSocket, aError));
       
   152     MSRPLOG( "CMSRPConnector::RunError exit" )
       
   153     return err;
       
   154     }
       
   155     
       
   156 // -----------------------------------------------------------------------------
       
   157 // CMSRPConnector::ConnectComplete
       
   158 // -----------------------------------------------------------------------------
       
   159 //
       
   160 void CMSRPConnector::ConnectComplete()
       
   161     {
       
   162     MSRPLOG( "CMSRPConnector::ConnectComplete enter" )
       
   163     iSocket = NULL;
       
   164     MSRPLOG( "CMSRPConnector::ConnectComplete exit" )
       
   165     }
       
   166 
       
   167 
       
   168 // End of File