genericopenlibs/cstdlib/INC/FDESC.H
changeset 0 e4d67989cc36
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     1 // Copyright (c) 1997-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 // Abstraction representing a UNIX file descriptor
       
    15 // CFileDescBase maintains the simple semantics of
       
    16 // Dup() and Close(), so the final Close() will
       
    17 // delete the object and hence call the destructor.
       
    18 // 
       
    19 //
       
    20 
       
    21 #ifndef _FDESC_H
       
    22 #define _FDESC_H
       
    23 
       
    24 #include <e32std.h>
       
    25 #include <e32cons.h>
       
    26 #include <f32file.h>
       
    27 #include <es_sock.h>
       
    28 #include <c32comm.h>
       
    29 
       
    30 
       
    31 #include <stddef.h>
       
    32 #include <redircli.h>
       
    33 #include <redircliinternal.h>
       
    34 
       
    35 // Queuing support for asynchronous requests
       
    36 //
       
    37 class CPosixRequest;
       
    38 class TPosixRequestQueue : public TSglQue<CPosixRequest>
       
    39 /**
       
    40 @internalComponent
       
    41 */
       
    42 	{
       
    43 public:
       
    44 	inline TPosixRequestQueue();
       
    45 	};
       
    46 
       
    47 //  The base class for all flavours of FileDescriptor
       
    48 //
       
    49 class CSerialTimer;
       
    50 class CPosixRequest;
       
    51 class CSocketDesc;
       
    52 NONSHARABLE_CLASS(CFileDescBase) : public CBase
       
    53 /**
       
    54 @internalComponent
       
    55 */
       
    56 	{
       
    57 public:
       
    58 
       
    59 	static CFileDescBase* Open(RSessionBase& aSession, const wchar_t* name, int mode, int perms, TInt& err);
       
    60 	static CFileDescBase* Socket(RSocketServ& aSs, int family, int style, int protocol, TInt& err);
       
    61 
       
    62 	virtual void Read(TDes8& aDesc, TRequestStatus& aStatus);
       
    63 	virtual TInt ReadCompletion(TDes8& aDesc, TInt aStatus);
       
    64 	virtual void ReadCancel();
       
    65 
       
    66 	virtual void Write(TDes8& aDesc, TRequestStatus& aStatus);
       
    67 	virtual TInt WriteCompletion(TDes8& aDesc, TInt aStatus);
       
    68 	virtual void WriteCancel();
       
    69 
       
    70 	virtual void Ioctl(int aCmd, void* aParam, TRequestStatus& aStatus);
       
    71 	virtual TInt IoctlCompletion(int aCmd, void* aParam, TInt aStatus);
       
    72 	virtual void IoctlCancel();
       
    73 
       
    74 	virtual void RecvFrom(TDes8& aDesc, TSockAddr& from, int flags, TRequestStatus& aStatus);
       
    75 	virtual TInt RecvFromCompletion(TInt& aLength, TInt aStatus);
       
    76 	virtual void RecvFromCancel();
       
    77 
       
    78 	virtual void SendTo(TDes8& aDesc, TSockAddr& to, int flags, TRequestStatus& aStatus);
       
    79 	virtual TInt SendToCompletion(TDes8& aDesc, TInt aStatus);
       
    80 	virtual void SendToCancel();
       
    81 
       
    82 	virtual void Sync(TRequestStatus& aStatus);
       
    83 	virtual void SyncCancel();
       
    84 
       
    85 	virtual void Accept(CSocketDesc*& aNewSocket, TRequestStatus& aStatus, RSocketServ& aSs);
       
    86 	virtual void AcceptCancel();
       
    87 
       
    88 	virtual void Connect(TSockAddr& anAddr,TRequestStatus& aStatus);
       
    89 	virtual void ConnectCancel();
       
    90 
       
    91 	virtual void Shutdown(TUint aHow,TRequestStatus& aStatus);
       
    92 	virtual void ShutdownCancel();
       
    93 
       
    94 	virtual TBool TimedRead() {return EFalse;}	//default implementation
       
    95 	TInt TimeoutValue() const {return iReadTimeout;}
       
    96 
       
    97 	virtual TInt LSeek(int& offset, int whence);
       
    98 	virtual TInt FStat(struct stat* st);
       
    99 	virtual TInt Bind(TSockAddr& anAddr);
       
   100 	virtual TInt Listen(TUint qSize);
       
   101 	virtual TInt SockName(int anEnd, TSockAddr& anAddr);
       
   102 	virtual TInt GetSockOpt(TUint anOptionName,TUint anOptionLevel,TDes8& anOption);
       
   103 	virtual TInt SetSockOpt(TUint anOptionName,TUint anOptionLevel,TDesC8& anOption);
       
   104 
       
   105 	static void Cleanup(TAny *aPtr);
       
   106 	void PushLC();
       
   107 
       
   108 	TInt Close();
       
   109 	virtual void UserClose() {return;}
       
   110 	inline CFileDescBase* Dup();
       
   111 	
       
   112 	enum IOQueues 
       
   113 		{ 
       
   114 		IOreadQ=0, IOwriteQ=1, IOioctlQ=2,IOioctlNQ=3,
       
   115 		IOmaxQ=IOioctlNQ 
       
   116 		};
       
   117 
       
   118 	void AddLast(CPosixRequest& aRequest, IOQueues aQueue);
       
   119 	void Remove(CPosixRequest& aRequest, IOQueues aQueue);
       
   120 
       
   121 	TBool ReadIsTimed;
       
   122 	TBool ReadWasCancelled;
       
   123 	CSerialTimer * TimedMessage;
       
   124 
       
   125 protected:
       
   126 	CFileDescBase();
       
   127 	virtual TInt FinalClose();
       
   128 	static void Complete(TRequestStatus& aStatus, TInt aResult);
       
   129 	TInt iReadTimeout;
       
   130 
       
   131 private:
       
   132 	TInt iDupCount;
       
   133 	TFixedArray<TPosixRequestQueue,IOmaxQ+1> iQueues;
       
   134 	};
       
   135 
       
   136 inline CFileDescBase* CFileDescBase::Dup () 
       
   137 	{ iDupCount += 1; return this; }
       
   138 
       
   139 
       
   140 NONSHARABLE_CLASS(CTtyDesc) : public CFileDescBase
       
   141 /**
       
   142 Abstraction of a teletype device, which will be used for
       
   143 the console.
       
   144 @internalComponent
       
   145 */
       
   146 	{
       
   147 public:
       
   148 	inline CTtyDesc(CConsoleBase *c);
       
   149 	inline CTtyDesc();
       
   150 	~CTtyDesc();
       
   151 
       
   152 	void Read(TDes8& aDesc, TRequestStatus &aStatus);
       
   153 	void ReadCancel();
       
   154 	TInt ReadCompletion(TDes8& aDesc, TInt aStatus);
       
   155 	void Write(TDes8& aDesc, TRequestStatus& aStatus);
       
   156 	void Ioctl(int aCmd, void* aParam, TRequestStatus& aStatus);
       
   157 	TInt IoctlCompletion(int aCmd, void* aParam, TInt aStatus);
       
   158 
       
   159 protected:
       
   160 	virtual TInt FinalClose();
       
   161 private:
       
   162 	void MapCodeAndEcho(TDes8& aDesc, TKeyCode aCode);
       
   163 	void CheckConsoleCreated();
       
   164 	void Write(TDes8& aDesc);
       
   165 	CConsoleBase *iConsole;
       
   166 	};
       
   167 
       
   168 inline CTtyDesc::CTtyDesc(CConsoleBase *c) : CFileDescBase(), iConsole(c) {}
       
   169 inline CTtyDesc::CTtyDesc() : CFileDescBase() {}
       
   170 
       
   171 NONSHARABLE_CLASS(CRedirDesc) : public CFileDescBase
       
   172 /**
       
   173 client-side connection to the redirection server
       
   174 @internalComponent
       
   175 */
       
   176 	{
       
   177 public:
       
   178 	CRedirDesc();
       
   179 	virtual TInt Connect();
       
   180 	virtual void Read(TDes8& aDesc, TRequestStatus& aStatus);
       
   181 	virtual void Write(TDes8& aDesc, TRequestStatus& aStatus);
       
   182 	virtual void Sync(TRequestStatus& aStatus);
       
   183 	TInt FinalClose();
       
   184 private:
       
   185 	RRedirSession2 iRedirSession;
       
   186 	TBool iAttemptedConnection;
       
   187 	};
       
   188 
       
   189 NONSHARABLE_CLASS(CFileDesc) : public CFileDescBase
       
   190 /**
       
   191 Abstractions for a plain file and a temporary file
       
   192 @internalComponent
       
   193 */
       
   194 	{
       
   195 public:
       
   196 	CFileDesc();
       
   197 	~CFileDesc();
       
   198 
       
   199 	TInt Open(RFs& aSession, const TDesC& aName, int mode, int perms);
       
   200 	TInt LSeek(int& offset, int whence);
       
   201 	void Read(TDes8& aDesc, TRequestStatus& aStatus);
       
   202 	void Write(TDes8& aDesc, TRequestStatus& aStatus);
       
   203 	TInt FStat(struct stat *st);
       
   204 	void Sync(TRequestStatus &aStatus);
       
   205 	TInt IoctlCompletion(int aCmd, void* aParam, TInt aStatus);
       
   206 
       
   207 	static void MapStat(struct stat& st, const TTime& aModTime, TUint& aAttr);
       
   208 
       
   209 protected:
       
   210 	virtual TInt FinalClose();
       
   211 private:
       
   212 	TInt FileRead(TUint8* aPtr,TInt aLength);
       
   213 	TInt FileWrite(TUint8* aPtr,TInt aLength);
       
   214 	TInt Flush();
       
   215 	TInt DoSync();
       
   216 	TInt DoRead(TDes8& aDesc);
       
   217 	TInt DoWrite(TDes8& aDesc);
       
   218 	TInt Pos();
       
   219 	TInt Ext();
       
   220 	TInt Alloc();
       
   221 private:
       
   222 	enum {EAlloc,EReading,EWriting};
       
   223 	enum {EBufferSize = 0x600,EReadAhead = 0x200};
       
   224 protected:
       
   225 	RFile	iFile;
       
   226 	TInt16	iDrive;	// for use with stat()
       
   227 private:
       
   228 	TUint8 iState;
       
   229 	TUint8* iBuffer;
       
   230 	TUint8* iPtr;
       
   231 	TUint8* iEnd;
       
   232 	TInt iSize;
       
   233 	TInt iPos;
       
   234 	TInt iExt;
       
   235 	};
       
   236 
       
   237 NONSHARABLE_CLASS(CTempFileDesc) : public CFileDesc
       
   238 /**
       
   239 @internalComponent
       
   240 */
       
   241 	{
       
   242 public:
       
   243 	TInt Open(RFs& aSession, const TDesC& aPath);
       
   244 protected:
       
   245 	virtual TInt FinalClose();
       
   246 private:
       
   247 	RFs iSession;
       
   248 	TFileName iName;
       
   249 	};
       
   250 
       
   251 NONSHARABLE_CLASS(CSocketDesc) : public CFileDescBase
       
   252 /**
       
   253 Abstraction for a socket
       
   254 @internalComponent
       
   255 */
       
   256 	{
       
   257 public:
       
   258 	CSocketDesc() : CFileDescBase(), iIoctlBuf(0,0) {}
       
   259 
       
   260 	TInt Socket(RSocketServ& aSs, int family, int style, int protocol);
       
   261 
       
   262 	TInt LSeek(int& offset, int whence);
       
   263 	void Read(TDes8& aDesc, TRequestStatus& aStatus);
       
   264 	void ReadCancel();
       
   265 	void Write(TDes8& aDesc, TRequestStatus& aStatus);
       
   266 	void WriteCancel();
       
   267 	TInt FStat(struct stat* st);
       
   268 	void Sync(TRequestStatus& aStatus);
       
   269 	void Ioctl(int aCmd, void* aParam, TRequestStatus& aStatus);
       
   270 	TInt IoctlCompletion(int aCmd, void* aParam, TInt aStatus);
       
   271 	void IoctlCancel();
       
   272 
       
   273 	void RecvFrom(TDes8& aDesc, TSockAddr& from, int flags, TRequestStatus& aStatus);
       
   274 	TInt RecvFromCompletion(TInt& aLength, TInt aStatus);
       
   275 	void RecvFromCancel();
       
   276 	void SendTo(TDes8& aDesc, TSockAddr& to, int flags, TRequestStatus& aStatus);
       
   277 	void SendToCancel();
       
   278 
       
   279 	void Shutdown(TUint aHow,TRequestStatus& aStatus);
       
   280 	void Accept(CSocketDesc*& aNewSocket, TRequestStatus& aStatus, RSocketServ& aSs);
       
   281 	void AcceptCancel();
       
   282 	void Connect(TSockAddr& anAddr,TRequestStatus& aStatus);
       
   283 	void ConnectCancel();
       
   284 
       
   285 	TInt Bind(TSockAddr& anAddr);
       
   286 	TInt Listen(TUint qSize);
       
   287 	TInt SockName(int anEnd, TSockAddr& anAddr);
       
   288 	TInt GetSockOpt(TUint anOptionName,TUint anOptionLevel,TDes8& anOption);
       
   289 	TInt SetSockOpt(TUint anOptionName,TUint anOptionLevel,TDesC8& anOption);
       
   290 
       
   291 protected:
       
   292 	TInt FinalClose();
       
   293 private:
       
   294 	inline TInt isStream() const;
       
   295 
       
   296 	RSocket iSocket;
       
   297 	TInt iStyle;
       
   298 	TSockXfrLength iLength;
       
   299 	TPtr8 iIoctlBuf;
       
   300 	};
       
   301 
       
   302 class TUSockAddr : public TSockAddr
       
   303 /**
       
   304 Utility class for converting struct sockaddr to and from EPOC32 TSockAddr
       
   305 @internalComponent
       
   306 */
       
   307 	{
       
   308 public:
       
   309 	TUSockAddr() : TSockAddr() {}
       
   310 
       
   311 	TUSockAddr(TAny* addr);			// constructor form of Prepare
       
   312 	TUSockAddr(TAny* addr, TUint len);	// constructor form of Set
       
   313 
       
   314 	void Prepare(TAny* addr);
       
   315 	void Set(TAny* addr, TUint len);
       
   316 	void Get(TAny* addr, unsigned long* len);
       
   317 	};
       
   318 
       
   319 NONSHARABLE_CLASS(CSerialTimer) : public CTimer
       
   320 /**
       
   321 the serial timer class
       
   322 @internalComponent
       
   323 */
       
   324   	{
       
   325 public:
       
   326 	CSerialTimer();
       
   327 	~CSerialTimer();
       
   328 
       
   329 public:
       
   330 	static CSerialTimer* NewLC(CFileDescBase*);
       
   331 	static CSerialTimer* NewL(CFileDescBase*);
       
   332 
       
   333 public:
       
   334 	void ConstructL(CFileDescBase*);
       
   335 	void IssueRequest(); 
       
   336 	void DoCancel();
       
   337 	void RunL();
       
   338 
       
   339 private:
       
   340 	CFileDescBase* iFile;
       
   341 	};
       
   342 
       
   343 
       
   344 class CNotifier;
       
   345 
       
   346 NONSHARABLE_CLASS(CSerialDesc) : public CFileDescBase
       
   347 /**
       
   348 @internalComponent
       
   349 */
       
   350 	{
       
   351 
       
   352 	friend class CNotifier;
       
   353 
       
   354 	public:
       
   355 	CSerialDesc() : CFileDescBase(), iReadThreshold(-1), iRequestedSignals(0),
       
   356 					iNotifyStatus(NULL), iNotifyParamPtr(NULL) {}
       
   357 
       
   358 	TInt Open(RCommServ& aSession, const wchar_t* name, int mode, int perms);
       
   359 	TInt FinalClose();
       
   360 
       
   361 	void Read(TDes8& aDesc, TRequestStatus& aStatus);
       
   362 	void ReadCancel();
       
   363 	
       
   364 	TInt& TimeoutValue() const;
       
   365 	void Write(TDes8& aDesc, TRequestStatus& aStatus);
       
   366 	
       
   367 	void Ioctl(int aCmd, void* aParam, TRequestStatus& aStatus);
       
   368 	TInt IoctlCompletion(int aCmd, void* aParam, TInt aStatus);
       
   369 	void IoctlCancel();
       
   370 
       
   371 	TBool TimedRead();
       
   372 	void UserClose();
       
   373 
       
   374 protected:
       
   375 
       
   376 private:
       
   377 
       
   378 	void NotifyDataAvailable(TRequestStatus& aStatus);
       
   379 	void NotifyOutputEmpty(TRequestStatus& aStatus);
       
   380 	void NotifyBreak(TRequestStatus& aStatus);
       
   381 	void NotifyWriteErrors(TRequestStatus& aStatus, TUint* aRequestParams, TUint aSignalsMask);
       
   382 	void NotifySignalChange(TRequestStatus& iStatus, TUint& aRequestParam, TUint aSignalsMask);
       
   383 	void NotifyDataAvailableCancel();
       
   384 	void NotifyOutputEmptyCancel();
       
   385 	void NotifyBreakCancel();
       
   386 	void NotifyWriteErrorsCancel();
       
   387 	void NotifySignalChangeCancel();
       
   388 	void Notify(TInt aVal);		//complete the notify request
       
   389 	TInt NotifiesSupported();	//return the notifies supported at the moment
       
   390 	TBool RequestedNotifiesSupported(TInt aRequested);	//see if the notifies requested are supported
       
   391 	void CancelNotifiers(const CNotifier* aCompletedNotifier);	//cancel them all apart from the passed one
       
   392 	TUint Signals();
       
   393 
       
   394 	TInt ReadCompletion (TDes8& aBuf, TInt aStatus);	
       
   395 
       
   396 	TInt iReadThreshold;
       
   397 	RComm iCommPort;
       
   398 
       
   399 	TInt iRequestedSignals;
       
   400 	TRequestStatus* iNotifyStatus;
       
   401 	TUint* iNotifyParamPtr;
       
   402 	TUint* iRequestDataPtr;
       
   403 
       
   404 	CNotifier* iDataAvailableNotifier;
       
   405 	CNotifier* iOutputEmptyNotifier;
       
   406 	CNotifier* iBreakNotifier;
       
   407 	CNotifier* iErrorsNotifier;
       
   408 	CNotifier* iSignalsNotifier;
       
   409 
       
   410 	};
       
   411 
       
   412 
       
   413 #endif // !_FDESC_H