upnp/upnpstack/dlnawebserver/inc/upnptcpsession.h
changeset 0 f5a58ecadc66
equal deleted inserted replaced
-1:000000000000 0:f5a58ecadc66
       
     1 /** @file
       
     2 * Copyright (c) 2005-2006 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:  Class that is derived by CUpnpHttpSession. Implements TCP-level
       
    15 *                functionalities
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 #ifndef C_CUPNPTCPSESSION_H
       
    21 #define C_CUPNPTCPSESSION_H
       
    22 
       
    23 // INCLUDES
       
    24 #include <e32base.h>
       
    25 #include "upnphttpmessage.h"
       
    26 #include "upnplist.h" 
       
    27 #include "upnpnotifytimer.h"
       
    28 #include "upnpsocketshutdown.h"
       
    29 
       
    30 // FORWARD DECLARATIONS
       
    31 
       
    32 class CUpnpHttpBuffer;
       
    33 class CUpnpHttpServer;
       
    34 class CUpnpSocketShutdown;
       
    35 class RSocket;
       
    36 class CUpnpTcpSessionWriter;
       
    37 class CUpnpHttpFileTransferReader;
       
    38 class CUpnpHttpFileTransferWriter;
       
    39 class CUpnpTcpSessionReader;
       
    40 class CUpnpHttpFileAccess;
       
    41 
       
    42 // CONSTANTS
       
    43 static const TInt KErrorTimeout = 3000000;
       
    44 //when reading from a file
       
    45 static const TInt KServeFileSizeDefault = 10000;
       
    46 // Based on 7.2.9.4 (DLNA 1.5 rev 0.90)
       
    47 // We need wait until 30second for response
       
    48 static const TInt KSessionTimeout = 35000000; 
       
    49 
       
    50 static const TInt KConnectTimeout = 7000000;
       
    51 
       
    52 //default buffer size for reading from socket
       
    53 static const TInt KRcvBufSizeDefault = 10000;
       
    54 //default size for fiel content reading from a socket, must be a power of 2
       
    55 static const TInt KFileTransferReaderBufSizeDefault = 16384;
       
    56 //timeout value for awaiting 100 continue
       
    57 static const TInt K100ContinueTimeout = 5000000;
       
    58 
       
    59 // CLASS DECLARATION
       
    60 
       
    61 /**
       
    62 *  @brief CUpnpTcpSession is a class handling TCP functionality of the session.
       
    63 *   Class is responsible for managing connection and its timeout as well as
       
    64 *   for reading from and writing to socket. Those two last are directly 
       
    65 *   handled by CUpnpTcpSessionReader and CUpnpTcpSessionWriter but CUpnpTcpSession 
       
    66 *   supervises those processes.
       
    67 *  @lib DLNAWebServer.lib
       
    68 *  @since Series60 2.6
       
    69 */  
       
    70 NONSHARABLE_CLASS (CUpnpTcpSession) : public CBase, public MUpnpNotifyTimerObserver
       
    71     {
       
    72        friend class CUpnpSocketShutdown;        
       
    73 public: // Constructors and destructor
       
    74 
       
    75     /** 
       
    76     * Constructor for building CUpnpTcpSession. 
       
    77     * This constructor is used when connection is iniatiated from network.
       
    78     * Second-phase construction is made by TcpConstructL().
       
    79     * @param aSocket Pointer to new Socket.
       
    80     * @param aPriority Priority of this session
       
    81     * @return A new CUpnpTcpServer instance.
       
    82     **/
       
    83     CUpnpTcpSession( RSocket& aSocket, TThreadPriority aPriority );
       
    84 
       
    85     /** 
       
    86     * Virtual destructor.
       
    87     **/
       
    88     virtual ~CUpnpTcpSession();
       
    89 
       
    90    
       
    91 public: // Session functionality
       
    92     
       
    93     void ShutdownStop();
       
    94 
       
    95     /**
       
    96     * Starts to receive messages to this session
       
    97     * @since Series60 3.2
       
    98     * @return None.
       
    99     **/
       
   100     void InitialReceiveL();
       
   101     
       
   102     /**
       
   103     * Receive messages to this session
       
   104     * @since Series60 3.2
       
   105     * @return None.
       
   106     **/
       
   107     void ReceiveL();
       
   108 
       
   109     /**
       
   110     * Writes data thru this session's socket
       
   111     * @since Series60 2.6
       
   112     * @param aBuffer Data to write.
       
   113     * @return None.
       
   114     **/
       
   115     void WriteL( const TDesC8& aBuffer );
       
   116 
       
   117     /**
       
   118     * Checks is session is connected.
       
   119     * @since Series60 2.6
       
   120     * @return ETrue, if session is connected; EFalse othewise.
       
   121     **/
       
   122     TBool IsConnected();
       
   123 
       
   124     /**
       
   125     * Returns the address that session is connected to.
       
   126     * @since Series60 2.6
       
   127     * @return Address of remote endpoint of session's socket.
       
   128     **/
       
   129     TInetAddr& RemoteHost();
       
   130 
       
   131     /**
       
   132     * Starts the timeoutTimer for session. If timer expires, no reply has 
       
   133     * been sent to remote host.
       
   134     * @since Series60 2.6
       
   135     * @param aSessionErrorReplyNeeded ETrue, if in timeout there should be
       
   136     * sent HTTP error 408 Server timeout.
       
   137     **/
       
   138     void StartTimeoutTimer( TBool aSessionErrorReplyNeeded );
       
   139 
       
   140     /**
       
   141     * Starts the timeoutTimer for session. Session is deleted after 0.5ms by default.
       
   142     * @since Series60 2.6
       
   143     **/
       
   144     void StartDisconnectingTimer(TInt aTime = 500);
       
   145     
       
   146     /**
       
   147     * Starts disconnect session.
       
   148     * @since Series60 3.2
       
   149     **/
       
   150     
       
   151     void StartDisconnect( TBool aDisconnectNotification = EFalse );
       
   152     
       
   153     /**
       
   154     * Function that is used to send notification to client and disconnect 
       
   155     * the session.
       
   156     * @since Series60 2.6
       
   157     **/
       
   158     void ServerBusyL();
       
   159     
       
   160     /**
       
   161     * Returns the SessionId of this session.
       
   162     * @since Series60 3.1
       
   163     * @return SessionId.
       
   164     **/
       
   165     TInt Id();
       
   166 
       
   167 public: // Callback functions for reader and writer
       
   168 
       
   169     /**
       
   170     * Handles possible error that can occure in the session as well as
       
   171     * in reader and writer active objects.
       
   172     * @since Series60 3.0
       
   173     * @param aError error code
       
   174     **/
       
   175     void HandleErrorL( TInt aError  );
       
   176     
       
   177     /**
       
   178     * Callback function for reader object. When the reader gets some 
       
   179     * data, it forwards it to the session with this callback.
       
   180     * @since Series60 3.0
       
   181     * @param aBuffer buffer with received data.
       
   182     **/
       
   183     void ReceivingCompletedL( TDesC8& aBuffer );
       
   184     
       
   185     /**
       
   186     * Callback function for writer object. When the request writing is
       
   187     * completed then writer calls this function to notify session about it.
       
   188     * @since Series60 3.0
       
   189     **/
       
   190     void WritingCompletedL();
       
   191     
       
   192     /**
       
   193     * Callback function for writer object. When the request writing is
       
   194     * not completed due to an error, a new attempt to write is being made...
       
   195     * @since Series60 3.2
       
   196     **/
       
   197     void WaitRetryError(TInt aError, TBool aStartTimer = EFalse);    
       
   198      /** 
       
   199     * Second phase constructor for building CUpnpTcpSession. 
       
   200     * This constructor is used when connection is iniatiated from network.
       
   201     * @param aSocket Pointer to new Socket which is already connected 
       
   202     *  to remote host.
       
   203     * @return None
       
   204     **/
       
   205     void TcpConstructL( RSocket aSocket, TInt aReadBufferSize, TInt aFileReadBufferSize );
       
   206 
       
   207    
       
   208     /** 
       
   209     * Cancels TcpReader
       
   210     * @return None.
       
   211     **/
       
   212     void CancelTcpReader();
       
   213     
       
   214 
       
   215 public: // virtual callback functions
       
   216 
       
   217     /**
       
   218     * Callback function. Called when data has been received.
       
   219     * @since Series60 2.6
       
   220     * @param aBuffer Data that has been received thru session.
       
   221     **/
       
   222     virtual void BufferReceivedL( TDesC8& aBuffer ) = 0;
       
   223     
       
   224     /**
       
   225     * Callback function. Deletes given session.
       
   226     * @since Series60 2.6
       
   227     * @param aSession Session to delete.
       
   228     **/
       
   229     virtual void DeleteThisSessionL( CUpnpTcpSession* aSession ) = 0;
       
   230 
       
   231     /**
       
   232     * Checks if FileServe Exists.
       
   233     * @since Series60 2.6
       
   234     * @return ETrue, if fileServe exists; EFalse otherwise.
       
   235     **/
       
   236     virtual TBool FileServeExists() = 0;
       
   237 
       
   238     /**
       
   239     * Callback function. Notifies upper layers that session has been disconnected.
       
   240     * @since Series60 2.6
       
   241     * @param aAddr Address of remote host.
       
   242     **/
       
   243     virtual void NotifyDisconnectL( TInetAddr aAddr ) = 0;
       
   244     virtual void NotifyTimeoutL() = 0;
       
   245     
       
   246     //file transfer reader callbacks
       
   247     
       
   248     /**
       
   249     * Callback function. Notifies http session when upload finished
       
   250     * @since Series60 3.2    
       
   251     **/
       
   252     virtual void FileTransferReaderDoneL() = 0;
       
   253     
       
   254     /**
       
   255     * Callback function. Notifies http session when error in upload
       
   256     * @since Series60 3.2    
       
   257     **/
       
   258     virtual void FileTransferReaderErrorL(TInt aError) = 0;
       
   259     
       
   260     /**
       
   261     * Callback function. Notifies http session when download finished
       
   262     * @since Series60 3.2    
       
   263     **/
       
   264     virtual void FileTransferWriterDoneL() = 0;
       
   265     
       
   266     /**
       
   267     * Callback function. Checks for outstanding downloads
       
   268     * @since Series60 3.2    
       
   269     **/
       
   270     virtual void FileTransferWriterHandleMoreL() = 0;
       
   271     
       
   272     /**
       
   273     * Callback function. Notifies upper layers that SocketServer is busy.
       
   274     * @since Series60 2.6
       
   275     * @param aAddr Address of remote host.
       
   276     **/
       
   277     virtual void SocketServerBusyL( const TInetAddr& aAddr ) = 0;
       
   278 
       
   279     /**
       
   280     * Callback function. Deletes fileserve of this session.
       
   281     * @since Series60 2.6
       
   282     **/
       
   283     virtual void DeleteServeFileL() = 0;
       
   284     
       
   285     /**
       
   286     * Returns a pointer to FileAccess object of this session.
       
   287     * @since Series60 3.2
       
   288     * @return Pointer to FileAccess object.
       
   289     **/
       
   290     virtual CUpnpHttpFileAccess* FileAccess()=0;
       
   291 
       
   292     /**
       
   293     * Callback function. Returns the port number of parent HttpServer.
       
   294     * @since Series60 2.6
       
   295     * @return Portnumber of http server.
       
   296     **/
       
   297     virtual TInt ServerPort() = 0;
       
   298 
       
   299     /**
       
   300     * Callback function. Checks if connection uses Connection: Close 
       
   301     * header, and if so, tries to parse message and send it to upper layers.
       
   302     * @since Series60 2.6
       
   303     **/
       
   304     virtual void CheckConnectionCloseL() = 0;
       
   305 
       
   306     /**
       
   307     * Callback function. Informs upper layers that HTTP POST has been started 
       
   308     * by sending HTTP error code 801 message to upper layers.
       
   309     * @since Series60 2.6
       
   310     **/
       
   311     virtual void NotifyUpperLayersOnPostL() = 0;
       
   312 
       
   313     /**
       
   314     * Callback function. Informs upper layers that HTTP POST has been finished 
       
   315     * @since Series60 2.6
       
   316     **/
       
   317     virtual void PostingFileCompleteL() = 0;
       
   318     
       
   319 public: // From MNotifyTimeObserver
       
   320 
       
   321     /**
       
   322     * Callback function for timer expirations.
       
   323     * @since Series60 2.6
       
   324     * @param aTimer Timer that has expired.
       
   325     **/
       
   326     void TimerEventL( CUpnpNotifyTimer* aTimer );   
       
   327     
       
   328     /**
       
   329     * Cancel timer
       
   330     * @since Series60 3.2
       
   331     **/
       
   332     void TimerCancel();
       
   333  
       
   334     /**
       
   335     * Handles disconnect request
       
   336     * @since Series60 3.2
       
   337     **/
       
   338     void DisconnectRequestL();      
       
   339     
       
   340     /**
       
   341     * Starts closing the session, no more message to be sent to the client
       
   342     * @since Series60 3.2
       
   343     **/
       
   344     void StartClosingSession();
       
   345     
       
   346      /**
       
   347     * Marked if a persistent connection
       
   348     * @since Series60 3.2
       
   349     **/
       
   350     TBool IsPersistentConnection();
       
   351     
       
   352      /**
       
   353     * Returns pointer to CUpnpHttpFileTransferReader
       
   354     * @since Series60 3.2
       
   355     * @return CUpnpHttpFileTransferReader*
       
   356     **/
       
   357     CUpnpHttpFileTransferReader* FileTransferReader();
       
   358     
       
   359      /**
       
   360     * Returns pointer to CUpnpHttpFileTransferWriter
       
   361     * @since Series60 3.2
       
   362     * @return CUpnpHttpFileTransferWriter*
       
   363     **/
       
   364     CUpnpHttpFileTransferWriter* FileTransferWriter();
       
   365             
       
   366     /**
       
   367     * Checks if there are outstanding sending operations.
       
   368     * @since Series60 3.2
       
   369     * @return TBool
       
   370     **/
       
   371     TBool DownloadOngoing();
       
   372     
       
   373     /**
       
   374     * Checks if the session is being shutdown
       
   375     * @since Series60 3.2
       
   376     * @return TBool
       
   377     **/
       
   378     TBool ShuttingDown() ;
       
   379       /**
       
   380     * Returns current value of the timeout
       
   381     * @since Series60 3.2
       
   382     * @return TInt
       
   383     */
       
   384     
       
   385      /**
       
   386     * Closes the socket
       
   387     * @since Series60 3.2
       
   388     */
       
   389     void CloseSocketL();
       
   390 
       
   391     TInt TimeoutCurrentValue();  
       
   392 private: // private enumerations
       
   393 
       
   394     /**
       
   395     * State of session.
       
   396     *   - EnotConnected - Connection is not established yet.
       
   397     *   - EConnecting - TcpSession requested connection in asynchronous mode.
       
   398     *   - EConnected  - Connection is established, TcpReader is started and TcpWriter 
       
   399     *                 can write to the socket.
       
   400     *   - EDisconnecting - Session is about to be disconnected but socket is not closed
       
   401     *                 so TcpWriter can still write a response.
       
   402     *   - EServerBusy - Server is already occupied with processing requests so no other
       
   403     *                 session can be established.
       
   404     **/
       
   405     enum TUpnpTcpSessionState 
       
   406       {
       
   407       ENotConnected,
       
   408       EConnecting,
       
   409       EConnected,
       
   410       EDisconnecting,
       
   411       EShuttingDownWriteWait,
       
   412       EShuttingDownStop,
       
   413       EShuttingDown,
       
   414       EServerBusy
       
   415       };
       
   416       
       
   417 private: // New functions
       
   418   
       
   419     /**
       
   420     * Sets session statel.
       
   421     * @since Series60 2.6
       
   422     * @param aState new state of the session.
       
   423     **/
       
   424     void SetState( TUpnpTcpSessionState aState );
       
   425     
       
   426     /**
       
   427     * Checks if there are outstanding writing operations.
       
   428     * @since Series60 3.2
       
   429     * @return TBool
       
   430     **/
       
   431     TBool OutstandingWritings();
       
   432 
       
   433 protected:
       
   434     
       
   435      /**
       
   436     * Contructs file transfer reader
       
   437     * @since Series60 3.2
       
   438     **/
       
   439     void FileTransferReaderConstructL();
       
   440     
       
   441     virtual void DestroyIt(CUpnpTcpSession* aSession ) = 0;
       
   442 
       
   443 
       
   444     /**
       
   445     * Contructs file transfer writer
       
   446     * @since Series60 3.2
       
   447     **/
       
   448     void FileTransferWriterConstructL(TInt aWriteSize);    
       
   449     
       
   450 protected:  
       
   451     
       
   452     TInt iFileReadBufferSize;
       
   453     // Session's socket, owned
       
   454     RSocket iSocket;
       
   455 
       
   456     // Address of remote host, owned
       
   457     TInetAddr iAddr;
       
   458 
       
   459     // iTcpReader, active object to control reads from the socket.
       
   460     // Owned by CUpnpTcpSession object.
       
   461     CUpnpTcpSessionReader* iTcpReader;
       
   462     
       
   463     // iFileTransferReader, active object to control reads from the socket.
       
   464     // Owned by CUpnpTcpSession object.
       
   465     CUpnpHttpFileTransferReader* iFTReader;
       
   466     
       
   467     // iFileTransferWriter, active object to control writes from the socket.
       
   468     // Owned by CUpnpTcpSession object.
       
   469     CUpnpHttpFileTransferWriter* iFTWriter;
       
   470 
       
   471     // iTcpWriter, active object to control writes to the socket.
       
   472     // Owned by CUpnpTcpSession object.
       
   473     CUpnpTcpSessionWriter* iTcpWriter;
       
   474     
       
   475     // Timer that is used to disconnect session when timeout expires, owned
       
   476     CUpnpNotifyTimer* iTimeoutTimer;
       
   477     
       
   478     // Buffer that is used to receive messages. See CUpnpHttpBuffer.
       
   479     CUpnpHttpBuffer* iReceivedMessage;
       
   480 
       
   481     // Session id of this session, owned
       
   482     TInt iId;
       
   483 
       
   484     // State of session, owned
       
   485     TUpnpTcpSessionState iState;
       
   486 
       
   487     // ETrue, if session is meant to be kept alive; EFalse otherwise, owned
       
   488     TBool iSessionKeepAlive;
       
   489     
       
   490     // ETrue, if session needs 408 HTTP error on timeout, owned
       
   491     TBool iSessionNeedsErrorReply;
       
   492     
       
   493     // Used with session disconnects. If ETrue, session send notification 
       
   494     // of disconnect to upper layers, owned
       
   495     TBool iDisconnectNotification;
       
   496     
       
   497     //Flag set true if any pending requests are left in a pipeline awating to be proccessed
       
   498     TBool iPendingPostRequests;
       
   499 
       
   500     // value that is used to check if downloaded content should be saved 
       
   501     // into a file.
       
   502     TBool iSaveToFile;
       
   503 
       
   504     // The current value of timeout, changes dependent on a operation-connecting, sending,...
       
   505     TInt iTimeoutCurrentValue;
       
   506     
       
   507     // Timeout that defines the session length
       
   508     TInt iTimeoutSessionValue;
       
   509     
       
   510     // Flag indicating if POST message with header "Expect: 100-continue"
       
   511     // was received and server should wait for download of body after
       
   512     // sending permission to continue download.
       
   513     TBool iContinuedMessageDownload;
       
   514     
       
   515     // Initial priority, priority set for message that initialize this session
       
   516     TThreadPriority iInitialPriority;
       
   517     
       
   518     //flag for indicating a persistant connection
       
   519     TBool iIsPersistent;
       
   520     
       
   521     // Implementation of the socket shutdown algorithm
       
   522     CUpnpSocketShutdown *iSocketShutdown;
       
   523     
       
   524     // Retry counter during export action
       
   525     TInt iExportRetryCounter;
       
   526     
       
   527     TInt iSessionError;
       
   528     };
       
   529 
       
   530 #endif // C_CUPNPTCPSESSION_H
       
   531 
       
   532 // End Of File