remotecontrol/remotecontrolfw/server/inc/session.h
changeset 51 20ac952a623c
equal deleted inserted replaced
48:22de2e391156 51:20ac952a623c
       
     1 // Copyright (c) 2004-2010 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 /**
       
    17  @file
       
    18  @internalComponent
       
    19 */
       
    20 
       
    21 #ifndef REMCONSESSION_H
       
    22 #define REMCONSESSION_H
       
    23 
       
    24 #include <e32base.h>
       
    25 #include <remconserverpanic.h>
       
    26 #include <remconaddress.h>
       
    27 #include <remcon/clientinfo.h>
       
    28 #include <remcon/clienttype.h>
       
    29 #include <remcon/messagetype.h>
       
    30 #include <remcon/playertype.h>
       
    31 #include <remcon/clientid.h>
       
    32 
       
    33 class CRemConServer;
       
    34 class CBearerManager;
       
    35 class CRemConMessage;
       
    36 class CMessageQueue;
       
    37 class CRemConInterfaceDetailsArray;
       
    38 class CRemConInterfaceDetails;
       
    39 
       
    40 /**
       
    41 Rem Con session.
       
    42 */
       
    43 NONSHARABLE_CLASS(CRemConSession) : public CSession2
       
    44 	{
       
    45 public:
       
    46 	~CRemConSession();
       
    47 
       
    48 public: // called by the server
       
    49 	/** 
       
    50 	@return The number of remotes the current 'send' has gone to successfully.
       
    51 	*/
       
    52 	inline TUint& NumRemotes();
       
    53 
       
    54 	/** 
       
    55 	@return The number of remotes the current 'send' is supposed to be going 
       
    56 	to. NB This is decremented as the sends actually occur.
       
    57 	*/
       
    58 	inline TInt& NumRemotesToTry();
       
    59 
       
    60 	/** 
       
    61 	@return The error the current 'send' will be completed with.
       
    62 	*/
       
    63 	inline TInt& SendError();
       
    64 
       
    65 	/** 
       
    66 	@return Information about the client using this session.
       
    67 	*/
       
    68 	inline TClientInfo& ClientInfo();
       
    69 
       
    70 	/** 
       
    71 	@return Information about the client using this session.
       
    72 	*/
       
    73 	inline const TClientInfo& ClientInfo() const;
       
    74 
       
    75 	/**
       
    76 	@return This session's unique identifier.
       
    77 	*/
       
    78 	inline TUint Id() const;
       
    79 
       
    80 	/**
       
    81 	Used by the server to complete a client send request. 
       
    82 	@param aError Error.					
       
    83 	*/
       
    84 	void CompleteSend();
       
    85 	
       
    86 	/**
       
    87 	Used by the server to complete a client send notify request.
       
    88 	*/
       
    89 	void CompleteSendNotify();
       
    90 	
       
    91 	/** 
       
    92 	Checks if this message is supported by the session
       
    93 	@return ETrue if it is, EFalse otherwise
       
    94 	*/
       
    95 	virtual TBool SupportedMessage(const CRemConMessage& aMsg) const = 0;
       
    96 
       
    97 	/**
       
    98 	Writes aMsg to the client's Receive message (NB RMessage2::Write may fail) 
       
    99 	and completes the client's Receive message with any error.
       
   100 	@return Error.
       
   101 	*/
       
   102 	TInt WriteMessageToClient(const CRemConMessage& aMsg);
       
   103 
       
   104 	/**
       
   105 	Accessor for the currently outstanding client Send message. This is used 
       
   106 	to check the client's capabilities to send over a particular bearer.
       
   107 	@return Handle to client's send message.
       
   108 	*/
       
   109 	inline const RMessage2& CurrentSendMessage() const;
       
   110 
       
   111 	/**
       
   112 	Accessor for the currently outstanding client Receive message.
       
   113 	@return Handle to client's receive message.
       
   114 	*/
       
   115 	inline const RMessage2& CurrentReceiveMessage() const;
       
   116 	
       
   117 	TInt SupportedInterfaces(RArray<TUid>& aUids);
       
   118 	TInt AppendSupportedInterfaces(RArray<TUid>& aUids);
       
   119 	TInt SupportedBulkInterfaces(RArray<TUid>& aUids);
       
   120 	TInt AppendSupportedBulkInterfaces(RArray<TUid>& aUids);
       
   121 	TInt SupportedOperations(TUid aInterfaceUid, RArray<TUint>& aOperations);
       
   122 
       
   123 	/**
       
   124 	Panics the client's current Send message with the given code.
       
   125 	*/
       
   126 	void PanicSend(TRemConClientPanic aCode);
       
   127 	
       
   128 	/**
       
   129 	Checks whether this session is fully initialised and available for use
       
   130 	by bearers.
       
   131 	@return ETrue if session can be used by bearers, EFalse otherwise
       
   132 	*/
       
   133 	inline TBool ClientAvailable() const;
       
   134 
       
   135 	/**
       
   136 	Indicates that a connection has come up or down. If the session has a 
       
   137 	'connections notification' outstanding, it should be completed. Note that, 
       
   138 	when a connection comes up or down, ConnectionsChanged is called before 
       
   139 	CompleteConnect or DisconnectComplete. These functions are called 
       
   140 	synchronously by the connection-handling code.
       
   141 	*/
       
   142 	void ConnectionsChanged();
       
   143 	
       
   144 protected:
       
   145     /**
       
   146     Constructor.
       
   147     @param aServer The server.
       
   148     @param aBearerManager The bearer manager.
       
   149     @param aId The unique ID of the new session.
       
   150     */
       
   151     CRemConSession(CRemConServer& aServer, 
       
   152         CBearerManager& aBearerManager,
       
   153         TUint aId);
       
   154     
       
   155     /**
       
   156     2nd-phase construction.
       
   157     @param aMessage The message received from the client.
       
   158     */
       
   159     void BaseConstructL(const TClientInfo& aClientInfo);
       
   160 
       
   161 	
       
   162 protected: 		
       
   163 
       
   164 	/**
       
   165 	Utility to complete the given message with the given error code.
       
   166 	@param aMessage Message to complete.
       
   167 	@param aError Error to complete with.
       
   168 	*/
       
   169 	void CompleteClient(const RMessage2& aMessage, TInt aError);
       
   170 
       
   171 	CRemConInterfaceDetailsArray* ExtractInterestedAPIsL(const RMessage2& aMessage);
       
   172 	
       
   173 	/**
       
   174 	Gets the information needed to send from the RMessage.
       
   175 	
       
   176 	@param aMessage The message containing the info.
       
   177 	@param aInterfaceUid The uid of the interface to send on
       
   178 	@param aOperationId The operation id to send
       
   179 	@param aMessageSubType The sub type of hte message
       
   180 	@param aSendDes The operation data to send.  Ownership is passed
       
   181 					to the caller and aSendDes is placed on the cleanup
       
   182 					stack when this function completes successfully.
       
   183 	@leave If the data was not successfully read. Note: if the message is invalid
       
   184 	       this function will not leave, but rather panic the message and return
       
   185 	       EFalse. This function will only leave if there's a system error, such
       
   186 	       as out of memory.
       
   187 	@return ETrue if the message information was successfully retrieved,
       
   188 			or EFalse if the message is invalid, in which case it got panicked.
       
   189 			If this function returns EFalse, aSendDes is not left on the cleanup stack.
       
   190 	 */
       
   191 	TBool DoGetSendInfoLC(const RMessage2& aMessage, 
       
   192 			TUid& aInterfaceUid,
       
   193 			TUint& aOperationId,
       
   194 			TRemConMessageSubType& aMessageSubType,
       
   195 			RBuf8& aSendDes);
       
   196 			
       
   197 	
       
   198 protected: // from CSession2
       
   199 	/**
       
   200 	Called when a message is received from the client.
       
   201 	
       
   202 	Also used from the session, where a session has stored
       
   203 	a message for later	processing.
       
   204 	
       
   205 	@param aMessage Message received from the client.
       
   206 	*/
       
   207 	void ServiceL(const RMessage2& aMessage);
       
   208 
       
   209 private: // utility- IPC command handlers
       
   210 	virtual void SetPlayerType(const RMessage2& aMessage)=0;
       
   211 	virtual void SendUnreliable(const RMessage2& aMessage) = 0;
       
   212 	virtual void SendCancel(const RMessage2& aMessage);
       
   213 	virtual void Receive(const RMessage2& aMessage);
       
   214 	virtual void ReceiveCancel(const RMessage2& aMessage);
       
   215 	virtual void GetConnectionCount(const RMessage2& aMessage);
       
   216 	virtual void GetConnections(const RMessage2& aMessage);
       
   217 	virtual void NotifyConnectionsChange(const RMessage2& aMessage);
       
   218 	virtual void NotifyConnectionsChangeCancel(const RMessage2& aMessage);
       
   219 	virtual void RegisterInterestedAPIs(const RMessage2& aMessage) = 0;
       
   220 	virtual void GoConnectionOriented(const RMessage2& aMessage);
       
   221 	virtual void GoConnectionless(const RMessage2& aMessage);
       
   222 	virtual void ConnectBearer(const RMessage2& aMessage);
       
   223 	virtual void ConnectBearerCancel(const RMessage2& aMessage);
       
   224 	virtual void DisconnectBearer(const RMessage2& aMessage);
       
   225 	virtual void DisconnectBearerCancel(const RMessage2& aMessage);
       
   226 	virtual void SendNotify(const RMessage2& aMessage);
       
   227 	void Send(const RMessage2& aMessage);
       
   228 
       
   229 private: // utility
       
   230 	virtual CRemConMessage* DoPrepareSendMessageL(const RMessage2& aMessage)=0;
       
   231 	virtual void DoSendCancel()=0;
       
   232 	virtual void DoReceive()=0;
       
   233 	void WriteMessageToClientL(const CRemConMessage& aMsg);
       
   234 	virtual void SendToServer(CRemConMessage& aMsg)=0;
       
   235 	static TInt SendNextCb(TAny* aThis);
       
   236 	void DoSendNext();
       
   237 
       
   238 protected:
       
   239 	void DoSendL(const RMessage2& aMessage);
       
   240 	void GetPlayerTypeAndNameL(const RMessage2& aMessage, TPlayerTypeInformation& aPlayerType, RBuf8& aPlayerName);
       
   241 	CRemConInterfaceDetails* FindInterfaceByUid(TUid aUid) const;
       
   242 	
       
   243 protected: // unowned
       
   244 	CRemConServer& iServer;
       
   245 	CBearerManager& iBearerManager;
       
   246 
       
   247 protected: // message handles for asynchronous IPC requests
       
   248 	RMessage2 iSendMsg;
       
   249 	RMessage2 iReceiveMsg;
       
   250 	RMessage2 iNotifyConnectionsChangeMsg;
       
   251 
       
   252 	CMessageQueue* iSendQueue;
       
   253 	
       
   254 protected: // owned
       
   255 	// The client's process ID, secure ID and caps.
       
   256 	TClientInfo iClientInfo;
       
   257 
       
   258 	// Unique session identifier.
       
   259 	const TUint iId;
       
   260 
       
   261 	// Used to control the completion of the client's send request until we 
       
   262 	// know the correct values of iNumSuccessfulRemotes and iSendError to use.
       
   263 	// Generally 0. 
       
   264 	// 1 when sending a connection-oriented command or a response.
       
   265 	// N when sending a connectionless command (where N is the number of 
       
   266 	// remotes the TSP addressed it to). 
       
   267 	// While processing outgoing commands to multiple remotes, 
       
   268 	// iNumRemotesToTry is decremented each time we finish trying to address a 
       
   269 	// remote. This may be at the connection stage or the actual send stage.
       
   270 	// On return to 0, the client is completed with either the result of the send
       
   271 	// or KErrCancel.
       
   272 	TInt iNumRemotesToTry;
       
   273 	// For completion of the current send request. NB A send may be completed 
       
   274 	// only after numerous asynchronous stages.
       
   275 	TUint iNumRemotes;
       
   276 	TInt iSendError;
       
   277 	
       
   278 	CRemConInterfaceDetailsArray* iInterestedAPIs;
       
   279 	
       
   280 	CAsyncCallBack* iSendNextCallBack;
       
   281 	
       
   282 	enum TRemConSessionSending
       
   283 		{
       
   284 		ENotSending,
       
   285 		ESendingReliable,
       
   286 		ESendingUnreliable
       
   287 		};
       
   288 	
       
   289 	TRemConSessionSending iSending;
       
   290 
       
   291 	TBool iInGetConnectionsProcedure;
       
   292 	};
       
   293 
       
   294 // Inlines
       
   295 
       
   296 TInt& CRemConSession::NumRemotesToTry()
       
   297 	{
       
   298 	return iNumRemotesToTry;
       
   299 	}
       
   300 
       
   301 TUint& CRemConSession::NumRemotes()
       
   302 	{
       
   303 	return iNumRemotes;
       
   304 	}
       
   305 
       
   306 TInt& CRemConSession::SendError()
       
   307 	{
       
   308 	return iSendError;
       
   309 	}
       
   310 
       
   311 TClientInfo& CRemConSession::ClientInfo()
       
   312 	{
       
   313 	return iClientInfo;
       
   314 	}
       
   315 
       
   316 const TClientInfo& CRemConSession::ClientInfo() const
       
   317 	{
       
   318 	return iClientInfo;
       
   319 	}
       
   320 
       
   321 TUint CRemConSession::Id() const
       
   322 	{
       
   323 	return iId;
       
   324 	}
       
   325 
       
   326 const RMessage2& CRemConSession::CurrentSendMessage() const
       
   327 	{
       
   328 	return iSendMsg;
       
   329 	}
       
   330 
       
   331 const RMessage2& CRemConSession::CurrentReceiveMessage() const
       
   332 	{
       
   333 	return iReceiveMsg;
       
   334 	}
       
   335 
       
   336 TBool CRemConSession::ClientAvailable() const
       
   337 	{
       
   338 	// Client is available as soon as it has registered the APIs
       
   339 	// it is interested in.
       
   340 	return iInterestedAPIs ? ETrue : EFalse;
       
   341 	}
       
   342 
       
   343 #endif // REMCONSESSION_H
       
   344