mtptransports/mtpptpiptransport/ptpipplugin/inc/cptpipsockhandlerbase.h
changeset 0 d0791faffa3f
equal deleted inserted replaced
-1:000000000000 0:d0791faffa3f
       
     1 // Copyright (c) 2008-2009 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  @internalComponent
       
    18 */
       
    19 
       
    20 #ifndef CSOCKETHANDLER_H_
       
    21 #define CSOCKETHANDLER_H_
       
    22 
       
    23 #include "es_sock.h"
       
    24 #include "in_sock.h"
       
    25 #include "e32base.h"
       
    26 #include "cptpipconnection.h"
       
    27 
       
    28 #include "mtpdebug.h"
       
    29 #include "ptpipprotocolconstants.h"
       
    30 
       
    31 
       
    32 //The maximum number of bytes buffered locally before sending over socket
       
    33 // If the data chunk is larger than this, it is sent directly, if its smaller its buffered.
       
    34 const TInt KBufferSize = 64;
       
    35 
       
    36 /**
       
    37 This class encapsulates the functions for sending and receving data over
       
    38 the @see RSocket over TCP/IP
       
    39 
       
    40 It has the common functionality for handling and communicating with the socket, 
       
    41 and for communicating with the ptpip connection class. 
       
    42 
       
    43 The command and Event handler classes derived from this class.
       
    44  
       
    45 */
       
    46 class CPTPIPSocketHandlerBase : public CActive
       
    47 	{
       
    48 public :
       
    49 
       
    50 	enum TSocketState
       
    51 		{
       
    52 		
       
    53 		EIdle                     = 0x00000000,
       
    54 		EInitSendInProgress       = 0x80000005,
       
    55 		
       
    56         // The receive data stream is active.
       
    57         EReceivingState           = 0x40000000,
       
    58 		EReceiveInProgress        = 0x40000001,
       
    59 		EReceiveFastInProgress    = 0x40000002,
       
    60 		EReceiveComplete          = 0x40000003,
       
    61 
       
    62         // The send data stream is active.
       
    63         ESendingState             = 0x80000000,
       
    64 		ESendInitialising         = 0x80000001,
       
    65 		ESendInProgress           = 0x80000002,
       
    66 		ESendCompleting           = 0x80000003,
       
    67 		ESendComplete             = 0x80000004,
       
    68 		
       
    69 		// Send stream is active and is sending the data in packets during R to I data phase
       
    70 		ESendDataState            = 0x60000000,
       
    71 		ESendDataInitialising     = 0x60000001,
       
    72 		ESendingDataHeader        = 0x60000002,
       
    73 		ESendingDataPacket        = 0x60000003,
       
    74 		ESendDataPacketCompleting = 0x60000004,
       
    75 		ESendDataComplete         = 0x60000005,
       
    76 		ESendDataCancelled        = 0x60000006,
       
    77 
       
    78 		EStateDirection           = 0xF0000000
       
    79 		};
       
    80 	
       
    81 	// Data Sending functions
       
    82 	void SendDataL(const MMTPType& aData, TUint32 aTransactionId);
       
    83     virtual void SendDataCompleteL(TInt aError, const MMTPType& aSource);
       
    84 	void CancelSendL(TInt aReason);
       
    85 
       
    86 	// Data Receiving functions
       
    87 	void ReceiveDataL(MMTPType& aData);
       
    88     virtual void ReceiveDataCompleteL(TInt aError, MMTPType& aSink);
       
    89 	void CancelReceiveL(TInt aReason);
       
    90     
       
    91     // Other helper functions
       
    92 	virtual ~CPTPIPSocketHandlerBase();
       
    93 	CPTPIPConnection& Connection();
       
    94 	RSocket& Socket();
       
    95 	void SetSocket(RSocket& aSocket);
       
    96 	void SetState(TSocketState aState);
       
    97 	void SetSocketOptions();
       
    98 	void SetCancel()
       
    99 		{
       
   100 		iCancelReceived = ETrue;
       
   101 		}
       
   102 	
       
   103 	// From CActive
       
   104 	void DoCancel();
       
   105 
       
   106 public:
       
   107 
       
   108 	/*Flag to inidicate whether to populate the chunk given by MTP framework, 
       
   109 	 from the last saved location or beginning if its a new chunk
       
   110 	 * */
       
   111 	TBool iUseOffset ;			
       
   112 		
       
   113 protected:
       
   114 
       
   115 	CPTPIPSocketHandlerBase(CPTPIPConnection& aConnection, TPriority priority );
       
   116 #ifdef __FLOG_ACTIVE    
       
   117     virtual void ConstructL(const TDesC8& aComponentName);
       
   118 #else
       
   119     virtual void ConstructL();
       
   120 #endif  
       
   121 	virtual TInt ParsePTPIPHeaderL() = 0;
       
   122 	virtual TBool HandleInitAck() = 0;
       
   123 	
       
   124 	
       
   125 private:
       
   126 
       
   127 	// From CActive
       
   128 	void RunL();
       
   129 	TInt RunError(TInt aError);
       
   130 
       
   131 	// other helper functions.
       
   132 	void ProcessSendDataL();
       
   133 	void ProcessFirstReceivedChunkL();
       
   134 	void ResumeReceiveDataStreamL();
       
   135 	void InitiateFirstChunkReceiveL();
       
   136 	void ResetSendDataStream();
       
   137 	void ResetReceiveDataStream();
       
   138 	void CompleteSelf(TInt aCompletionCode);
       
   139 	TInt32 DataStreamDirection() const;
       
   140 	void SendDataPacketL();
       
   141 	void SendHeaderPacketL();
       
   142 	void CreateAndSendDataPacketsL();	
       
   143 
       
   144 protected :
       
   145 	/**
       
   146 	This is the socket used for transferring data , 
       
   147 	it is controlled by the socketHandler
       
   148 	*/
       
   149 	RSocket 			iSocket;
       
   150 	
       
   151     /**
       
   152     The active data stream chunk status.
       
   153     */
       
   154     TInt                iChunkStatus;
       
   155 
       
   156     /**
       
   157     The pointer to the current chunk in the send data stream.
       
   158     */
       
   159     TPtrC8              iSendChunkData;
       
   160     
       
   161     /**
       
   162     The send data stream data pointer.
       
   163     */
       
   164     TPtrC8              iSendData;
       
   165     
       
   166     /**
       
   167     The pointer to the current chunk in the receive data stream
       
   168     */
       
   169     TPtr8               iReceiveChunkData;
       
   170     
       
   171        
       
   172     
       
   173     /**
       
   174     The amount of data expected in one ptpip packet, this is filled from the size from ptp header
       
   175     */
       
   176     TUint32				iPTPPacketLength;
       
   177 	
       
   178     /**
       
   179     Keeps track of whether socket is sending, receiving, idle etc.
       
   180     */
       
   181     TSocketState		iState;
       
   182     
       
   183     /**
       
   184     FLOGGER debug trace member variable.
       
   185     */
       
   186 	__FLOG_DECLARATION_MEMBER_MUTABLE;
       
   187 
       
   188 private :
       
   189     /**
       
   190     The packet data buffer used to copy and append the chunk locally
       
   191     if the individual chunks are too small to send as one tcp packet. 
       
   192     */
       
   193     RBuf8               iPacketBuffer;
       
   194     
       
   195     /**
       
   196     The maximum size of the packet buffer
       
   197     */
       
   198     TUint               iPacketSizeMax;
       
   199     
       
   200     /**
       
   201     The receive data stream data pointer.
       
   202     */
       
   203     TPtr8               iReceiveData;
       
   204 
       
   205     /**
       
   206     A flag to tell if the receive stream is the first chunk.
       
   207     */			
       
   208     TBool 				iIsFirstChunk;
       
   209     
       
   210     /**
       
   211     The amount of data received so far, keeps incrementing with each tcp packet.
       
   212     */
       
   213     TUint32 			iPTPPacketLengthReceived;
       
   214      
       
   215     /** 
       
   216     The receive data stream commit flag.
       
   217     */
       
   218     TBool               iReceiveDataCommit;
       
   219 	
       
   220     /**
       
   221     The send data stream data pointer.
       
   222     */
       
   223     TPtrC8              iSendHeaderData;
       
   224     
       
   225 	/**
       
   226     The header for the ptp ip data packet.
       
   227     */
       
   228     CPTPIPDataContainer* iPTPIPDataHeader;
       
   229     
       
   230     /**
       
   231     The flag to keep track whether cancel has been received. 
       
   232     */
       
   233     TBool               iCancelReceived;
       
   234     
       
   235     
       
   236 private : // Not owned
       
   237 	CPTPIPConnection& iConnection;
       
   238 
       
   239 	/**
       
   240 	This is the buffer used to receive data from the socket. This will be a pointer 
       
   241 	to the PTPIP data type. 
       
   242 	*/
       
   243 	MMTPType* iReceiveDataSink;
       
   244 	
       
   245 	/**
       
   246 	This is the buffer used to send data to the socket. This will be a pointer 
       
   247 	to the PTPIP data type. 
       
   248 	*/
       
   249 	const MMTPType* iSendDataSource;	
       
   250 	
       
   251 	/**
       
   252     The pointer to the current chunk in the receive data stream
       
   253     */
       
   254     TPtr8               iCurrentChunkData; 
       
   255     
       
   256     /*Type of the current PTP/IP Packet */
       
   257     TUint32 iType;
       
   258 	
       
   259 	};
       
   260 
       
   261 #endif /*CSOCKETHANDLER_H_*/