upnp/upnpstack/ssdpserver/inc/upnpudpserver.h
changeset 0 f5a58ecadc66
child 31 b51a6f4f15ba
equal deleted inserted replaced
-1:000000000000 0:f5a58ecadc66
       
     1 /** @file
       
     2  * Copyright (c) 2005-2008 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:  Declares main application class.
       
    15  *
       
    16  */
       
    17 
       
    18 #ifndef C_CUPNPUDPSERVER_H
       
    19 #define C_CUPNPUDPSERVER_H
       
    20 
       
    21 // INCLUDES
       
    22 #include <e32base.h>
       
    23 #include <es_sock.h>
       
    24 #include <es_enum.h>
       
    25 #include <in_sock.h>
       
    26 #include <commdbconnpref.h>
       
    27 #include "upnpssdpmessage.h"
       
    28 #include "upnpmessageobserver.h"
       
    29 #include "upnpconnectionmanagerproxy.h"
       
    30 
       
    31 // FORWARD DECLARATIONS
       
    32 class CUpnpHttpMessage;
       
    33 class CUpnpUdpSendRequest;
       
    34 
       
    35 // CONSTANTS
       
    36 static const TUint KDefaultStartPort = 2000;
       
    37 static const TUint KDefaultMulticastPort = 1900;
       
    38 static const TUint32 KDefaultMulticastAddress = INET_ADDR(239,255,255,250);
       
    39 static const TUint KAddressLength = 35;
       
    40 static const TInt KSendTimerLimit = 10;
       
    41 static const TUint KSendingDelay = 15000; //DLNA specyfies that: UPnP network devices must not send more than 10 ssdp:alive messages
       
    42 //on a single network interface in any given 200 ms period. So 15ms delay between each message
       
    43 //should be sufficient 15ms = 15000 microseconds.
       
    44 static const TUint32 KDefaultAutoIPAddress = INET_ADDR(169,254,255,255);
       
    45 
       
    46 // DATA TYPES
       
    47 typedef CUpnpHttpMessage CUdpMessage;
       
    48 
       
    49 // CLASS DECLARATION
       
    50 
       
    51 /**
       
    52  *  MUpnpUdpServerObserver is used as interface-class for message delivery
       
    53  *  from CUpnpUdpServer to CUpnpSsdpServer
       
    54  *
       
    55  *  @since Series60 2.0
       
    56  */
       
    57 class MUpnpUdpServerObserver
       
    58     {
       
    59 public:
       
    60     // New functions
       
    61 
       
    62     /**
       
    63      * Notifies udp message reception
       
    64      * @param aMessage UDP message received
       
    65      */
       
    66     virtual void UdpMessageReceivedL( CUdpMessage* aMessage ) = 0;
       
    67 
       
    68     };
       
    69 
       
    70 // CLASS DECLARATION
       
    71 
       
    72 /**
       
    73  *  CUpnpUdpServer is a base class for CUpnpSsdpServer and it contains all
       
    74  *  "real" methods, as CUpnpSsdpServer contains logical functions.
       
    75  *
       
    76  *  @since Series60 2.0
       
    77  */
       
    78 NONSHARABLE_CLASS(CUpnpUdpServer) : public CActive,
       
    79         public MUpnpMessageObserver
       
    80     {
       
    81 public:
       
    82     // Constructors and destructor
       
    83 
       
    84     /**
       
    85      * C++ default constructor.
       
    86      * @param aSocketServ Pointer to SocketServer used.
       
    87      * @param aObserver Pointer to class derived from MUpnpUdpServerObserver.
       
    88      * Used for callbacks.
       
    89      * @param aListeningPort Listening port.
       
    90      * @return A new CUpnpUdpServer instance.
       
    91      */
       
    92     CUpnpUdpServer( RSocketServ* aSocketServ,
       
    93         MUpnpUdpServerObserver* aObserver, TInt aListeningPort );
       
    94 
       
    95     /**
       
    96      * Destructor.
       
    97      */
       
    98     ~CUpnpUdpServer();
       
    99 
       
   100 public:
       
   101     // New functions
       
   102 
       
   103     /**
       
   104      * Returns ETrue if udp server stared, EFalse otherwise
       
   105      * @since Series60 3.2
       
   106      * @return stared flag
       
   107      */
       
   108     TBool IsStarted();
       
   109 
       
   110     /**
       
   111      * Active Object RunL.
       
   112      * @since Series60 2.0
       
   113      */
       
   114     void RunL();
       
   115 
       
   116     /**
       
   117      * Callback for RunL leave situations.
       
   118      * @since Series60 2.0
       
   119      * @param aError contains the error code that caused the RunL leave.
       
   120      * @return KErrNone.
       
   121      */
       
   122     TInt RunError( TInt aError );
       
   123 
       
   124     /**
       
   125      * Active Object DoCancel.
       
   126      * @since Series60 2.0
       
   127      */
       
   128     void DoCancel();
       
   129 
       
   130     /**
       
   131      * Starts to listen for address.
       
   132      * @since Series60 2.0
       
   133      */
       
   134     void OpenL();
       
   135 
       
   136     /**
       
   137      * Closes the server.
       
   138      * @since Series60 2.0
       
   139      */
       
   140     void Close();
       
   141 
       
   142     /**
       
   143      * Sets the server ready for receiving.
       
   144      * @since Series60 2.0
       
   145      */
       
   146     void ReceiveL();
       
   147 
       
   148     /**
       
   149      * Sends aBuffer to IP address anAddr.
       
   150      * @since Series60 2.0
       
   151      * @param aBuffer Buffer that is to be sent.
       
   152      * @param anAddr Address that aBuffer is to be sent.
       
   153      */
       
   154     void SendL( TDesC8& aBuffer, TInetAddr& anAddr );
       
   155 
       
   156     /**
       
   157      * Prepares server for receiving after sending.
       
   158      * @since Series60 2.0
       
   159      */
       
   160     void SendDoneL();
       
   161 
       
   162     /**
       
   163      * From MUpnpAddressObserver
       
   164      * This function will be invoke if IP Address of UPnP changes
       
   165      */
       
   166     IMPORT_C void AddressChangedL( TInetAddr& aAddr );
       
   167 
       
   168 protected:
       
   169 
       
   170     // DATA TYPES
       
   171     enum TSsdpValidity
       
   172         {
       
   173         // Message is valid SSDP message.
       
   174         EMessageOk,
       
   175         // Message is missing some essential headers.
       
   176         EHeadersMissing,
       
   177         // Name of method is not "M-SEARCH".
       
   178         EHeaderNotMSearch,
       
   179         // Value of MX header is not numerical.
       
   180         EErrorInMxConversion,
       
   181         // Value of MX header not valid.
       
   182         EInvalidMXValue,
       
   183         // Value of MAN header not valid.
       
   184         EInvalidManHeader,
       
   185         // Value of ST header not valid.
       
   186         EInvalidStHeader,
       
   187         // Value of HOST header not valid.
       
   188         EInvalidHostHeader,
       
   189         // Message is not valid SSDP response.
       
   190         EBadSsdpResponse,
       
   191         // Value of CACHE-CONTROL header not valid.
       
   192         EInvalidCacheControlHeader
       
   193         };
       
   194 
       
   195     /**
       
   196      * Checks that given SSDP message is valid.
       
   197      * @since Series60 2.0
       
   198      * @param aMsg Message to validate.
       
   199      * @return Error code. KErrNone, if valid message.
       
   200      */
       
   201     TSsdpValidity ValidateSsdpMessageL( CUpnpSsdpMessage* aMsg );
       
   202 
       
   203     /**
       
   204      * Function gets called when message's timer expires.
       
   205      * @since Series60 2.0
       
   206      * @param aMessage Pointer to message that has expired.
       
   207      */
       
   208     void MessageExpiredL( CUpnpHttpMessage* aMessage );
       
   209 
       
   210     /**
       
   211      * Function ompares  two line ignoring number of space between elements
       
   212      * @since Series60 2.0
       
   213      * @param aValue current value
       
   214      * @param aPattern pattern value
       
   215      * @return true if equal, else false
       
   216      */
       
   217     TBool CompareLinesL( const TDesC8& aValue, const TDesC8& aPattern );
       
   218 
       
   219     /**
       
   220      * Sets socket option which  adds address to multicast group
       
   221      */
       
   222     void JoinMulticastGroupL();
       
   223 
       
   224     /**
       
   225      * Sets socket configuration specific to multicast server
       
   226      */
       
   227     void ApplyMulticastSocketSettingsL();
       
   228 
       
   229     /**
       
   230      * Sets socket common configuration
       
   231      */
       
   232     void ApplyCommonSocketSettingsL();
       
   233 
       
   234     /**
       
   235      * Open socket and starts receiving datagrams
       
   236      */
       
   237     void OpenSocketAndStartReceiveL();
       
   238 
       
   239     /**
       
   240      * Refresh IP address and change auto ip status accordingly
       
   241      */
       
   242     void RefreshLocalAddressInfo();
       
   243 
       
   244 private:
       
   245 
       
   246     /**
       
   247      * Function continue procesing queued request or switch to receive mode
       
   248      * @since Series60 3.2
       
   249      */
       
   250     void RestartOrContinueProcessingL();
       
   251 
       
   252     /**
       
   253      * Function continue send procesing
       
   254      * @return ETrue if processing restarted
       
   255      * @since Series60 3.2
       
   256      */
       
   257     TBool RestartOrContinueSendProcessingL();
       
   258 
       
   259     /**
       
   260      * Function calculates MX value
       
   261      * @return MX value
       
   262      * @since Series60 3.2
       
   263      */
       
   264     TInt CalculateMxValue( const TDesC8& aMxString );
       
   265 
       
   266     /**
       
   267      * Function queues response for M-Serach message 
       
   268      * @param Message to be sent after timer elapse
       
   269      * @since Series60 MCL
       
   270      */
       
   271     void AddPendingTimerMessageL( CUpnpSsdpMessage* aMessage );
       
   272 
       
   273 protected:
       
   274 
       
   275     /**
       
   276      * Used from CUpnpSsdpServer to construct the CUpnpUdpServer correctly.
       
   277      * @since Series60 2.0
       
   278      */
       
   279     void UdpConstructL();
       
   280 
       
   281     // DATA TYPES
       
   282     enum TState
       
   283         {
       
   284         EClosed,
       
   285         EOpen,
       
   286         EReceiving,
       
   287         ESending,
       
   288         ESendingTimer, // new state introduced to handle delay timer between sending messages
       
   289         ESendDone
       
   290         };
       
   291 
       
   292 protected:
       
   293 
       
   294     // State of CUpnpUdpServer defined as TState
       
   295     TState iState;
       
   296 
       
   297     // Random seed that is used for generating random timeouts
       
   298     // for sending SSDP responses.
       
   299     TInt64 iRandomSeed;
       
   300 
       
   301     // Address of remote client returned by RSocket::RecvFrom() function
       
   302     TInetAddr iClientAddr;
       
   303 
       
   304     // Address of remote host, e.g. destination of message
       
   305     TInetAddr iSenderAddress;
       
   306 
       
   307     // Flags of UDP message
       
   308     TUint iMulticastFlags;
       
   309 
       
   310     // List of messages that are waiting to be sent.
       
   311     RPointerArray<CUpnpUdpSendRequest> iSendRequestList;
       
   312 
       
   313     // Pointer to observer, CUpnpSsdpServer
       
   314     MUpnpUdpServerObserver* iObserver;
       
   315 
       
   316     // Pointer to RSocketServer, that is used to bind
       
   317     // sockets
       
   318     RSocketServ* iSocketServ;
       
   319 
       
   320     // Socket that is used to receive (and send) SSDP messages.
       
   321     RSocket iSocket;
       
   322 
       
   323     // Port number of CUpnpUdpServer
       
   324     TUint iServerPort;
       
   325 
       
   326     // Buffer that is used for receiving messages.
       
   327     HBufC8* iMessage;
       
   328 
       
   329     // Pointer to receive buffer.
       
   330     TPtr8 iMessagePtr;
       
   331 
       
   332     // Buffer that is used for sending
       
   333     HBufC8* iSendMessage;
       
   334 
       
   335     // Array that holds list of messages which are waiting
       
   336     // for sending after their timers have expired.
       
   337     CArrayPtrSeg<CUpnpSsdpMessage>* iPendingTimerMessages;
       
   338 
       
   339     // IAP ID of the connection
       
   340     TInt iActiveIap;
       
   341 
       
   342     // Connection for opened sockets
       
   343     CUpnpConnectionManagerProxy* iConnectionManagerProxy;
       
   344 
       
   345     // Timer used to introduce delays to prevent from sending
       
   346     // SSDP messages more often than one in 20ms time period.
       
   347     RTimer iSendTimer;
       
   348 
       
   349     // indicate if server is started, socket was opened correctly
       
   350     TBool iIsStarted;
       
   351 
       
   352     //Local IP Address of Server
       
   353     TInetAddr iLocalInterfaceAddress;
       
   354     
       
   355     // If the interface goes to AutoIp address
       
   356     TBool iAutoIP;
       
   357     };
       
   358 
       
   359 #endif // C_CUPNPUDPSERVER_H
       
   360 // End of File