dvrengine/CommonRecordingEngine/inc/CCRSock.h
changeset 41 d88d70d98bbc
parent 34 814ba97beeb9
child 46 3bc36dbd63c2
equal deleted inserted replaced
34:814ba97beeb9 41:d88d70d98bbc
     1 /*
       
     2 * Copyright (c) 2007 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 the License "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:    Class for wrapping RSocket and CActive*
       
    15 */
       
    16 
       
    17 
       
    18 
       
    19 
       
    20 #ifndef CCRSOCK_H
       
    21 #define CCRSOCK_H
       
    22 
       
    23 // INCLUDES
       
    24 #include <in_sock.h>
       
    25 #include "videoserviceutilsLogger.h"
       
    26 
       
    27 // CONSTANTS
       
    28 const TInt KMaxDataSize( 8192 );
       
    29 
       
    30 // MACROS
       
    31 // None
       
    32 
       
    33 // DATA TYPES
       
    34 // None
       
    35 
       
    36 // FORWARD DECLARATIONS
       
    37 class CCRSock;
       
    38 class MCRSockObserver; 
       
    39 
       
    40 // CLASS DECLARATION
       
    41 
       
    42 /**
       
    43 *  Class for receiving data via socket.
       
    44 *
       
    45 *  This is solely owned by CCRSock and considered a helper class.
       
    46 *  Because there may be send and receive operations outstanding
       
    47 *  at the same time, one RSocket needs 2  CActive instances.
       
    48 *  @lib CommonRecordingEngine.lib
       
    49 *  @since Series 60 3.0
       
    50 */
       
    51 class CCRSockReader : public CActive
       
    52     {
       
    53 
       
    54 public: // Constructors and destructor
       
    55 
       
    56     /**
       
    57     * Two-phased constructor.
       
    58     * @param aSock is reference to socket object 
       
    59     *        that this class instance serves.
       
    60     * @since Series 60 3.0
       
    61     * @param aConnection a connection.
       
    62     * @param aSockServer a socket server.
       
    63     * @return CCRSockReader pointer to CCRSockReader class.
       
    64     */
       
    65     static CCRSockReader* NewL( CCRSock& aSock, 
       
    66                                 RConnection& aConnection,
       
    67                                 RSocketServ& aSockServer );
       
    68     /**
       
    69     * Destructor.
       
    70     */
       
    71     virtual ~CCRSockReader();
       
    72     
       
    73 public: // New Methods
       
    74 
       
    75     /**
       
    76     * Method for starting reading.
       
    77     * @since Series 60 3.0
       
    78     * @param none
       
    79     * @return always succeeds
       
    80     */
       
    81     void IssueRead( void );
       
    82     
       
    83 private: // Constructors and destructors
       
    84     
       
    85     /**
       
    86     * Default constructor
       
    87     */
       
    88     CCRSockReader( CCRSock& aSock,
       
    89                    RConnection& aConnection,
       
    90                    RSocketServ& aSockServer );
       
    91     /** 
       
    92     * 2nd phase constructor 
       
    93     */
       
    94     void ConstructL();
       
    95 
       
    96 private: // Methods from base classes
       
    97 
       
    98     /** 
       
    99     * From CActive.
       
   100     * This is the work-horse of this class.
       
   101     * @since Series 60 3.0
       
   102     * @param none.
       
   103     * @return none.
       
   104     */
       
   105     void RunL();
       
   106     
       
   107     /** 
       
   108     * From CActive.
       
   109     * This cancels work in progress.
       
   110     * @since Series 60 3.0
       
   111     * @param none.
       
   112     * @return none.
       
   113     */     
       
   114     void DoCancel();
       
   115     
       
   116     /** 
       
   117     * From CActive.
       
   118     * This handles errors.
       
   119     * @since Series 60 3.0
       
   120     * @param none.
       
   121     * @return none an status of method.
       
   122     */     
       
   123     TInt RunError( TInt aError );
       
   124     
       
   125 private: // Data
       
   126     
       
   127     /**
       
   128     * Socket.
       
   129     */
       
   130     CCRSock& iSock;
       
   131 
       
   132     /** 
       
   133     * connection to use.
       
   134     */
       
   135     RConnection& iConnection;
       
   136     
       
   137     /**
       
   138     * Socket server.
       
   139     */
       
   140     RSocketServ& iSockServer;
       
   141 
       
   142 #if defined ( LIVE_TV_FILE_TRACE ) || defined ( LIVE_TV_RDEBUG_TRACE ) 
       
   143     TUint recvCount; 
       
   144     TUint recvBytes; 
       
   145 #endif // LIVE_TV_FILE_TRACE || LIVE_TV_RDEBUG_TRACE
       
   146     };
       
   147 
       
   148 /**
       
   149 * Encapsulates a socket in easy-to-use way
       
   150 */
       
   151 class CCRSock : public CActive
       
   152     {
       
   153 
       
   154 public: // Data types
       
   155 
       
   156     /**
       
   157      * Enum for communicating what is going on with this socket 
       
   158      */
       
   159     enum TCRSockStatus
       
   160         {
       
   161         EInitNeeded = 1000,
       
   162         EIdle,       /**< Nothing in progress */
       
   163         EResolving,   /**< Finding out addr to connect to */
       
   164         EConnecting,  /**< Addr found but no connection yet */
       
   165         ESending,     /**< Request to send has been issued but not yet completed */
       
   166         EListening,   /**< Request to wait for incoming connection is not yet compl. */     
       
   167         EFailed       /**< Something went wrong */
       
   168         };
       
   169 
       
   170 public: // Constructors and destructor
       
   171 
       
   172     /**
       
   173     * Two-phased constructor.
       
   174     * @param aObserver is reference to object that 
       
   175     *        gets notified when something happens
       
   176     * @param aSockId is a number that this class instance
       
   177     *        will keep, do nothing with, and us the same 
       
   178     *        number when reporting statuses back to observer.
       
   179     *        motivation here is that same observer may have
       
   180     *        several instances of this class all reporting
       
   181     *        via same method. 
       
   182     * @param aProtoTCP ETrue for TCP socket, EFalse for UDP
       
   183     * @param aIssueRead ETrue to automatically receive data from
       
   184     *        socket and return to observer through DataReceived.
       
   185     *        EFalse to let user handle receiving data.
       
   186     * @return CCRSock pointer to CCRSock class
       
   187     */
       
   188     static CCRSock* NewL( MCRSockObserver& aObserver,
       
   189                           TInt aSockId,
       
   190                           RConnection& aConnection,
       
   191                           RSocketServ& aSockServer,
       
   192                           TBool aProtoTCP,
       
   193                           TBool aIssueRead );
       
   194 
       
   195     /**
       
   196     * Destructor.
       
   197     */
       
   198     virtual ~CCRSock();
       
   199 
       
   200 public: // New methods
       
   201 
       
   202     /**
       
   203     * method for causing the socket to connect to remote addr.
       
   204     * @since Series 60 3.0
       
   205     * @param aAddr is the addr to connect to 
       
   206     * @param aPort is ip port number to connect to
       
   207     * @param aLocalPort specifies the local port to bind to. If 0 random
       
   208     *        port is selected
       
   209     * @return KErrNone is returned if connection is all right
       
   210     */
       
   211     TInt ConnectSock( const TDesC& aAddr,
       
   212                       TUint aPort,
       
   213                       TInt aLocalPort = 0 );
       
   214 
       
   215     /**
       
   216     * method for causing the socket to connect to remote addr without
       
   217     * doing DNS lookup. 
       
   218     * @since Series 60 3.0
       
   219     * @param aAddr is the addr to connect to 
       
   220     * @param aLocalPort specifies the local port to bind to. If 0 random
       
   221     *        port is selected       
       
   222     * @return KErrNone is returned if connection is all right
       
   223     */
       
   224     TInt ConnectSock( const TSockAddr& aAddr, 
       
   225                       TInt aLocalPort = 0);
       
   226                       
       
   227     /**
       
   228     * method for causing the socket to start listening at part.
       
   229     * @since Series 60 3.0
       
   230     * @param aPort is the port to listen to 
       
   231     * @return KErrNone is returned if connection is all right
       
   232     */
       
   233     TInt ListenPort( TUint aPort );
       
   234     
       
   235     /**
       
   236     * method for joining a multicast group
       
   237     * @since Series 60 3.0
       
   238     * @param aGruopAddr IPv6 address of the group to join
       
   239     * @return KErrNone on success
       
   240     */
       
   241     TInt JoinGroup( const TInetAddr& aGroupAddr );
       
   242     
       
   243     /**
       
   244     * method for sending data over the sock
       
   245     * @since Series 60 3.0
       
   246     * @param aData is the data that is sent over sock
       
   247     * @return none, succeeds always, if something goes wrong, it does it in async way
       
   248     */      
       
   249     void SendData( const TDesC8& aDataThatIsSentOverSocket );
       
   250         
       
   251     /**
       
   252     * Method for asking the status: what is going on 
       
   253     * @since Series 60 3.0
       
   254     * @param none.
       
   255     * @return socket status.
       
   256     */
       
   257     CCRSock::TCRSockStatus SockStatus( void ) const;
       
   258 
       
   259     /** 
       
   260     * Helper class may frobnicate our private parts:
       
   261     */
       
   262     friend class CCRSockReader;
       
   263     
       
   264     /** 
       
   265     * Method for getting the addr this socket is connected to in the other end
       
   266     * @since Series 60 3.0
       
   267     * @param none.
       
   268     * @return the addr
       
   269     */
       
   270     TInetAddr ConnectedAddr( void );
       
   271 
       
   272     /** 
       
   273     * Method for getting the addr this socket is connected to in the local end
       
   274     * @since Series 60 3.0
       
   275     * @param none.
       
   276     * @return the addr
       
   277     */
       
   278     TInetAddr LocalAddr( void );
       
   279     
       
   280     /** 
       
   281     * Method for setting the "where to send addr" and this is applicable for UDP socks
       
   282     * @param aAddr is the new addr.
       
   283     * @return none
       
   284     */
       
   285     void SetToAddr( const TInetAddr &aAddr );
       
   286 
       
   287     /**
       
   288     * Gets reference underlying Symbian socket implementation. To be used with caution.
       
   289     * @since Series 60 3.0
       
   290     * @param none.
       
   291     * @return reference to underlying ES_SOCK socket
       
   292     */
       
   293     RSocket& Socket();
       
   294 
       
   295 private: // Constructors and destructors
       
   296 
       
   297     /**
       
   298     * default constructor
       
   299     */
       
   300     CCRSock( MCRSockObserver& aObserver,
       
   301              TInt aSockId,
       
   302              RConnection& aConnection,
       
   303              RSocketServ& aSockServer,
       
   304              TBool aProtoTCP,
       
   305              TBool aIssueRead );
       
   306 
       
   307     /** 
       
   308     * 2nd phase constructor 
       
   309     */
       
   310     void ConstructL();
       
   311        
       
   312 private: // Methods from base classes
       
   313 
       
   314     /** 
       
   315     * From CActive.
       
   316     * This is the work-horse of this class.
       
   317     * @since Series 60 3.0
       
   318     * @param none.
       
   319     * @return none.
       
   320     */
       
   321     void RunL();
       
   322     
       
   323     /** 
       
   324     * From CActive.
       
   325     * This cancels work in progress.
       
   326     * @since Series 60 3.0
       
   327     * @param none.
       
   328     * @return none.
       
   329     */     
       
   330     void DoCancel();
       
   331     
       
   332     /** 
       
   333     * From CActive.
       
   334     * This handles errors.
       
   335     * @since Series 60 3.0
       
   336     * @param none.
       
   337     * @return none an status of method.
       
   338     */     
       
   339     TInt RunError( TInt aError );
       
   340 
       
   341 private: // New methods
       
   342 
       
   343     /**
       
   344     * Handles send buffer.
       
   345     */
       
   346     void CopySendData( const TDesC8& aData );
       
   347 
       
   348     /**
       
   349     * Performs cleanup.
       
   350     */
       
   351     void CleanUp();
       
   352     
       
   353 private: // Data types
       
   354     
       
   355     /**
       
   356     * This is used to read/write.
       
   357     */
       
   358     RSocket iSocket;
       
   359     
       
   360     /**
       
   361     * status for above socket.
       
   362     */
       
   363     TBool iIsiSocketOpen;
       
   364     
       
   365     /**
       
   366     * This is used to listen.
       
   367     * Not used when we use this class for outgoing connection.
       
   368     */
       
   369     RSocket iListenSocket;  
       
   370     
       
   371     /**
       
   372     * Status for above socket.
       
   373     */
       
   374     TBool iIsiListenSocketOpen;
       
   375     
       
   376     /**
       
   377     * This is used to find out addr by name.
       
   378     */
       
   379     RHostResolver iResolver;       
       
   380     /**
       
   381     * This is needed to get hold of RSocket.
       
   382     */
       
   383     RSocketServ& iSockServer;
       
   384     
       
   385     /**
       
   386     * This is where we connect to.
       
   387     */
       
   388     TNameEntry iHostAddress;
       
   389     
       
   390     /**
       
   391     * this is our internal status.
       
   392     */
       
   393     TCRSockStatus iSockStatus;
       
   394     
       
   395     /**
       
   396     * Our observer.
       
   397     */
       
   398     MCRSockObserver& iObserver;
       
   399     
       
   400     /**
       
   401     * Our own internal id.
       
   402     */
       
   403     const TInt iSockId;
       
   404     
       
   405     /**
       
   406     * This tells whether we're about to connect via udp or tcp.
       
   407     */
       
   408     TBool iProtoTCP;
       
   409     
       
   410     /**
       
   411     * This tells whether receiving data from socket is handled by CCRSockReader or used.
       
   412     */
       
   413     TBool iIssueRead;
       
   414     
       
   415     /**
       
   416     * This tells the port we're about to connect.
       
   417     */
       
   418     TUint iPort;
       
   419     
       
   420     /**
       
   421     * This tells the port we're binding locally.
       
   422     */
       
   423     TUint iLocalPort;
       
   424     
       
   425     /**
       
   426     * This is where we keep the data received.
       
   427     */
       
   428     HBufC8* iReceivedDataBuf;
       
   429     
       
   430     /**
       
   431     * Pointer to received data buffer.
       
   432     */
       
   433     TPtr8 iReceivedData;
       
   434     
       
   435     /**
       
   436     * This is where we keep the data being sent.
       
   437     */
       
   438     HBufC8* iSentDataBuf;
       
   439 
       
   440     /**
       
   441     * Pointer to send data buffer.
       
   442     */
       
   443     TPtr8 iSentData;  
       
   444     
       
   445     /**
       
   446     * This tells how much data we got.
       
   447     */
       
   448     TSockXfrLength iReceivedDataLen;
       
   449     
       
   450     /**
       
   451     * This tells how much data we sent.
       
   452     */
       
   453     TSockXfrLength iSentDataLen;        
       
   454     
       
   455     /** 
       
   456     * This tells where the packet was received from.
       
   457     */
       
   458     TInetAddr iFromAddr;
       
   459     
       
   460     /**
       
   461     * This tells if we've been listening or receiving in the past
       
   462     */
       
   463     TBool iWasListening;
       
   464     
       
   465     /**
       
   466     * This is instance of a helper class doing the reading part.
       
   467     */
       
   468     CCRSockReader *iReader;
       
   469     
       
   470     /**
       
   471     * This tells where to send UDP packets.
       
   472     */
       
   473     TInetAddr iToAddr;
       
   474     
       
   475     /**
       
   476     * Connection to use.
       
   477     */
       
   478     RConnection& iConnection;
       
   479         
       
   480 #if defined ( LIVE_TV_FILE_TRACE ) || defined ( LIVE_TV_RDEBUG_TRACE ) 
       
   481     TUint sendCount; 
       
   482     TUint sendBytes; 
       
   483 #endif // LIVE_TV_FILE_TRACE || LIVE_TV_RDEBUG_TRACE
       
   484 
       
   485     };
       
   486 
       
   487 /**
       
   488 * Class for live tv socket "client" e.g. the user of class CCRSock.
       
   489 */
       
   490 class MCRSockObserver
       
   491     {
       
   492 
       
   493 public:
       
   494 
       
   495     /**
       
   496     * This method is called after some data has been received from socket.
       
   497     * @since Series 60 3.0
       
   498     * @param aData is descriptor containing the data received. 
       
   499     *        ownership of data is not passed via this call. 
       
   500     * @return none.
       
   501     */
       
   502     virtual void DataReceived( TInt aSockId,
       
   503                                const TDesC8& aData ) = 0;
       
   504 
       
   505     /**
       
   506     * This method is called after status of socket changes.
       
   507     * @since Series 60 3.0
       
   508     * @param aSockId a socket id.
       
   509     * @param aStatus is sock status.
       
   510     * @param aError a error code.
       
   511     * @return none
       
   512     */
       
   513     virtual void SockStatusChange( TInt aSockId,
       
   514                                    CCRSock::TCRSockStatus aStatus,
       
   515                                    TInt aError ) = 0;
       
   516     };
       
   517 
       
   518 #endif // CCRSOCK_H
       
   519 
       
   520 // End of file
       
   521