hti/HtiServicePlugins/HtiIpProxyServicePlugin/IPProxyEngine/Src/Csocketrouter.h
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 #ifndef CSOCKETROUTER_H
       
    21 #define CSOCKETROUTER_H
       
    22 
       
    23 //  INCLUDES
       
    24 #include <e32base.h>
       
    25 #include "Csocket.h"
       
    26 #include "MSocketObserver.h"
       
    27 #include "MProtocolObserverTCP.h"
       
    28 #include "MProtocolObserverUDP.h"
       
    29 #include "MUDPSenderObserver.h"
       
    30 #include "CWriteEvent.h"
       
    31 
       
    32 // DATA TYPES
       
    33 enum TSocketRouterState
       
    34     {
       
    35     EStateWaitingFrameStart = 0,
       
    36     EStateFrameStartFound
       
    37     };
       
    38 
       
    39 // FORWARD DECLARATIONS
       
    40 class MBPProtocol;
       
    41 class MSocketRouterObserver;
       
    42 class CProtocolTCP;
       
    43 class CProtocolUDP;
       
    44 class CUDPSender;
       
    45 
       
    46 // CLASS DECLARATION
       
    47 
       
    48 /**
       
    49 *  Routes data from peers to host and vice versa.
       
    50 */
       
    51 NONSHARABLE_CLASS( CSocketRouter ) : public CActive,
       
    52     public MSocketObserver,
       
    53     public MProtocolObserverTCP,
       
    54     public MProtocolObserverUDP,
       
    55     public MUDPSenderObserver
       
    56     {
       
    57     public:  // Constructors and destructor
       
    58 
       
    59         /**
       
    60         * Two-phased constructor.
       
    61         * @param aObserver Pointer to observer.
       
    62         */
       
    63         static CSocketRouter* NewL( MSocketRouterObserver* aObserver );
       
    64 
       
    65         /**
       
    66         * Two-phased constructor.
       
    67         * @param aObserver Pointer to observer.
       
    68         */
       
    69         static CSocketRouter* NewLC( MSocketRouterObserver* aObserver );
       
    70 
       
    71         /**
       
    72         * Destructor.
       
    73         */
       
    74         ~CSocketRouter();
       
    75 
       
    76     public: // New functions
       
    77 
       
    78         /**
       
    79         * Adds peer socket. The ownership transfers.
       
    80         * @param aSocket Opened RSocket object.
       
    81         */
       
    82         void AddPeerSocketL( RSocket* aSocket );
       
    83 
       
    84         /**
       
    85         * Adds UDP socket.
       
    86         * @param aPort Port which is "listened" for datagrams
       
    87         */
       
    88         void AddUDPSocketL( TUint aPort );
       
    89 
       
    90         /**
       
    91         * Removes peer socket from the array.
       
    92         * @param aIndex Index of array.
       
    93         */
       
    94         void RemovePeerSocket( TInt aIndex );
       
    95 
       
    96         /**
       
    97         * Removes peer socket from the array.
       
    98         * @param aSocket Socket to remove.
       
    99         */
       
   100         void RemovePeerSocket( RSocket* aSocket );
       
   101 
       
   102         /**
       
   103         * Finds peer socket from the array.
       
   104         * @param aPort A port to remove from the array
       
   105         * @return index of the peer socket, -1 if not found
       
   106         */
       
   107         TInt FindPeerSocket( TUint aPort );
       
   108 
       
   109         /**
       
   110         * Removes all peer sockets from the array.
       
   111         */
       
   112         void RemoveAllPeers();
       
   113 
       
   114         /**
       
   115         * Get socket array count
       
   116         * @return Number of CSocket instances in iPeerSocketArray
       
   117         */
       
   118         TInt SocketCount() const;
       
   119 
       
   120         /**
       
   121         * Sets the host socket. The ownership will NOT be transferred.
       
   122         */
       
   123         void SetHostSocketL( MSocket* aSocket );
       
   124 
       
   125         /**
       
   126         * Starts routing.
       
   127         */
       
   128         void StartRouting();
       
   129 
       
   130         /**
       
   131         * Stops routing.
       
   132         */
       
   133         void StopRouting();
       
   134 
       
   135         /**
       
   136         * @return ETrue if routing is activated and vice versa.
       
   137         */
       
   138         TBool IsRouting() const;
       
   139 
       
   140         /**
       
   141         * Resets message queue.
       
   142         */
       
   143         void ResetQueue();
       
   144 
       
   145         /**
       
   146         * Sends command via HTI to close the TCP connection
       
   147         * @param aPort Phone remote port (midlet) to close
       
   148         */
       
   149         void SendCloseTCPConnection( TUint aPort );
       
   150 
       
   151         /**
       
   152         * Writes the data to with the correct protocol
       
   153         * @param aProtocolDesc Which protocol was used to recieve this data
       
   154         * @param aPeerPort Peer port
       
   155         * @param aOriginalPort Port where the data was sent from
       
   156         * @param aData Data to be transmitted
       
   157         */
       
   158         void WriteCorrectFrameL( TProtocolDesc aProtocolDesc,
       
   159                                 TUint aPeerPort,
       
   160                                 TUint aOriginalPort,
       
   161                                 const TDesC8& aData );
       
   162 
       
   163     protected: // Functions from base classes
       
   164 
       
   165         /**
       
   166         * From CActive. Pending request has been completed.
       
   167         */
       
   168         void RunL();
       
   169 
       
   170         /**
       
   171         * From CActive. Pending request has been cancelled.
       
   172         */
       
   173         void DoCancel();
       
   174 
       
   175 
       
   176     protected:  // From MSocketObserver
       
   177 
       
   178         void DataReceivedL( const MSocket* aSocket, const TDesC8& aData );
       
   179         void ErrorL( const MSocket* aSocket, TInt aErrorCode );
       
   180         void ObserverLeaved( const MSocket* aSocket, TInt aLeaveCode );
       
   181         void DisconnectedL( const MSocket* aSocket );
       
   182 
       
   183     protected:  // From MProtocolObserverTCP
       
   184 
       
   185         void TCPFrameParsedL( TUint aPort, const TDesC8& aData );
       
   186         void OpenLocalTCPConnectionL( TUint aPort );
       
   187         void OpenListeningTCPConnectionL( TUint aPort );
       
   188         void CloseTCPConnectionL( TUint aPort );
       
   189         void CloseAllTCPConnections();
       
   190 
       
   191     protected:  // From MProtocolObserverUDP
       
   192 
       
   193         void UDPFrameParsedL( TUint aPort, const TDesC8& aData );
       
   194 
       
   195     protected:  // From MProtocolObserver
       
   196 
       
   197         void FrameStarted();
       
   198         void ProtocolErrorL( TInt aErrorCode, const TDesC8& aReceivedData );
       
   199 
       
   200     protected:  // From MUDPSenderObserver
       
   201 
       
   202         void UDPSenderErrorL( TInt aErrorCode );
       
   203         void UDPSenderLeavedL( TInt aLeaveCode );
       
   204 
       
   205     private:
       
   206 
       
   207         void IssueHandleQueue();
       
   208         void WriteQueueL();
       
   209 
       
   210     protected:
       
   211         /**
       
   212         * Default constructor.
       
   213         * @param aObserver Pointer to observer.
       
   214         */
       
   215         CSocketRouter( MSocketRouterObserver* aObserver );
       
   216 
       
   217         /**
       
   218         * 2nd phase constructor.
       
   219         */
       
   220         void ConstructL();
       
   221 
       
   222     private:    // Owned data
       
   223         RSocketServ iSocketServ;
       
   224         TSocketRouterState iState;
       
   225         HBufC8* iReceiveBuffer;
       
   226         CArrayPtr<CSocket>* iPeerSocketArray;
       
   227         CArrayPtr<MBPProtocol>* iProtocolArray;
       
   228         MSocketRouterObserver* iObserver;
       
   229         MSocket* iHostSocket;
       
   230         TBool iRouting;
       
   231         CArrayPtr<CWriteEvent>* iWriteEventArray;
       
   232         CUDPSender* iUDPSender;
       
   233 
       
   234     private:    // Not owned
       
   235         CProtocolTCP* iProtocolTCP;
       
   236         CProtocolUDP* iProtocolUDP;
       
   237     };
       
   238 
       
   239 #endif      // CSOCKETROUTER_H
       
   240 
       
   241 // End of File