channel.h

Go to the documentation of this file.
00001 /*
00002  * channel.h
00003  *
00004  * I/O channel ancestor class.
00005  *
00006  * Portable Windows Library
00007  *
00008  * Copyright (c) 1993-1998 Equivalence Pty. Ltd.
00009  *
00010  * The contents of this file are subject to the Mozilla Public License
00011  * Version 1.0 (the "License"); you may not use this file except in
00012  * compliance with the License. You may obtain a copy of the License at
00013  * http://www.mozilla.org/MPL/
00014  *
00015  * Software distributed under the License is distributed on an "AS IS"
00016  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
00017  * the License for the specific language governing rights and limitations
00018  * under the License.
00019  *
00020  * The Original Code is Portable Windows Library.
00021  *
00022  * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
00023  *
00024  * Portions are Copyright (C) 1993 Free Software Foundation, Inc.
00025  * All Rights Reserved.
00026  *
00027  * Contributor(s): ______________________________________.
00028  *
00029  * $Revision: 20428 $
00030  * $Author: rjongbloed $
00031  * $Date: 2008-06-10 11:59:48 +0000 (Tue, 10 Jun 2008) $
00032  */
00033 
00034 #ifndef _PCHANNEL
00035 #define _PCHANNEL
00036 
00037 #ifdef P_USE_PRAGMA
00038 #pragma interface
00039 #endif
00040 
00041 #include <ptlib/mutex.h>
00042 
00044 // I/O Channels
00045 
00046 class PChannel;
00047 
00048 /* Buffer class used in PChannel stream.
00049 This class is necessary for implementing the standard C++ iostream interface
00050 on #PChannel# classes and its descendents. It is an internal class and
00051 should not ever be used by application writers.
00052 */
00053 class PChannelStreamBuffer : public streambuf {
00054 
00055   protected:
00056     /* Construct the streambuf for standard streams on a channel. This is used
00057        internally by the #PChannel# class.
00058      */
00059     PChannelStreamBuffer(
00060       PChannel * chan   // Channel the buffer operates on.
00061     );
00062 
00063     virtual int_type overflow(int_type = EOF);
00064     virtual int_type underflow();
00065     virtual int sync();
00066     virtual pos_type seekoff(off_type, ios_base::seekdir, ios_base::openmode = ios_base::in | ios_base::out);
00067     virtual pos_type seekpos(pos_type, ios_base::openmode = ios_base::in | ios_base::out);
00068 
00069     PBoolean SetBufferSize(
00070       PINDEX newSize
00071     );
00072 
00073   private:
00074     // Member variables
00075     PChannel * channel;
00076     PCharArray input, output;
00077 
00078   public:
00079     PChannelStreamBuffer(const PChannelStreamBuffer & sbuf);
00080     PChannelStreamBuffer & operator=(const PChannelStreamBuffer & sbuf);
00081 
00082   friend class PChannel;
00083 };
00084 
00085 
00107 class PChannel : public PObject, public iostream {
00108   PCLASSINFO(PChannel, PObject);
00109 
00110   public:
00113 
00114     PChannel();
00115 
00117     ~PChannel();
00119 
00131     virtual Comparison Compare(
00132       const PObject & obj   
00133     ) const;
00134 
00148     virtual PINDEX HashFunction() const;
00150 
00160     virtual PBoolean IsOpen() const;
00161 
00167     virtual PString GetName() const;
00168 
00174     int GetHandle() const;
00175 
00185     virtual PChannel * GetBaseReadChannel() const;
00186 
00196     virtual PChannel * GetBaseWriteChannel() const;
00198 
00208     void SetReadTimeout(
00209       const PTimeInterval & time  
00210     );
00211 
00218     PTimeInterval GetReadTimeout() const;
00219 
00232     virtual PBoolean Read(
00233       void * buf,   
00234       PINDEX len    
00235     );
00236 
00251     virtual PINDEX GetLastReadCount() const;
00252 
00260     virtual int ReadChar();
00261 
00270     PBoolean ReadBlock(
00271       void * buf,   
00272       PINDEX len    
00273     );
00274 
00282     PString ReadString(PINDEX len);
00283 
00295     virtual PBoolean ReadAsync(
00296       void * buf,   
00297       PINDEX len    
00298     );
00299 
00304     virtual void OnReadComplete(
00305       void * buf, 
00306       PINDEX len  
00307     );
00309 
00319     void SetWriteTimeout(
00320       const PTimeInterval & time 
00321     );
00322 
00330     PTimeInterval GetWriteTimeout() const;
00331 
00343     virtual PBoolean Write(
00344       const void * buf, 
00345       PINDEX len        
00346     );
00347 
00360     virtual PINDEX GetLastWriteCount() const;
00361 
00370     PBoolean WriteChar(int c);
00371 
00378     PBoolean WriteString(const PString & str);
00379 
00389     virtual PBoolean WriteAsync(
00390       const void * buf, 
00391       PINDEX len        
00392     );
00393 
00399     virtual void OnWriteComplete(
00400       const void * buf, 
00401       PINDEX len        
00402     );
00404 
00411     virtual PBoolean Close();
00412 
00413     enum ShutdownValue {
00414       ShutdownRead         = 0,
00415       ShutdownWrite        = 1,
00416       ShutdownReadAndWrite = 2
00417     };
00418 
00426     virtual PBoolean Shutdown(
00427       ShutdownValue option
00428     );
00429 
00435     PBoolean SetBufferSize(
00436       PINDEX newSize    
00437     );
00438 
00478     PBoolean SendCommandString(
00479       const PString & command  
00480     );
00481 
00486     void AbortCommandString();
00488 
00494     enum Errors {
00495       NoError,
00497       NotFound,       
00499       FileExists,     
00501       DiskFull,       
00503       AccessDenied,   
00505       DeviceInUse,    
00507       BadParameter,   
00509       NoMemory,       
00511       NotOpen,        
00513       Timeout,        
00515       Interrupted,    
00517       BufferTooSmall, 
00519       Miscellaneous,
00521       ProtocolFailure,
00522       NumNormalisedErrors
00523     };
00524 
00530     enum ErrorGroup {
00531       LastReadError,      
00532       LastWriteError,     
00533       LastGeneralError,   
00534       NumErrorGroups
00535     };
00536 
00541     Errors GetErrorCode(
00542       ErrorGroup group = NumErrorGroups   
00543     ) const;
00544 
00550     int GetErrorNumber(
00551       ErrorGroup group = NumErrorGroups   
00552     ) const;
00553 
00559     virtual PString GetErrorText(
00560       ErrorGroup group = NumErrorGroups   
00561     ) const;
00562 
00569     static PString GetErrorText(
00570       Errors lastError,   
00571       int osError = 0     
00572     );
00574 
00581     static PBoolean ConvertOSError(
00582       int libcReturnValue,
00583       Errors & lastError,
00584       int & osError
00585     );
00586 
00591 #if P_HAS_RECVMSG
00592     typedef iovec Slice;
00593 #else
00594     struct Slice {
00595       void * iov_base;
00596       size_t iov_len;
00597     };
00598 #endif
00599 
00600     typedef std::vector<Slice> VectorOfSlice;
00601 
00611     virtual PBoolean Read(
00612       const VectorOfSlice & slices    // slices to read to
00613     );
00614 
00624     virtual PBoolean Write(
00625       const VectorOfSlice & slices    // slices to read to
00626     );
00628 
00629   protected:
00630     PChannel(const PChannel &);
00631     PChannel & operator=(const PChannel &);
00632     // Prevent usage by external classes
00633 
00634 
00641     virtual PBoolean ConvertOSError(
00642       int libcReturnValue,
00643       ErrorGroup group = LastGeneralError 
00644     );
00645 
00646   public:
00650     PBoolean SetErrorValues(
00651       Errors errorCode,   
00652       int osError,        
00653       ErrorGroup group = LastGeneralError 
00654     );
00655 
00656   protected:
00665     int ReadCharWithTimeout(
00666       PTimeInterval & timeout  // Timeout for read.
00667     );
00668 
00669     // Receive a (partial) command string, determine if completed yet.
00670     PBoolean ReceiveCommandString(
00671       int nextChar,
00672       const PString & reply,
00673       PINDEX & pos,
00674       PINDEX start
00675     );
00676 
00677 
00678     // Member variables
00680     int os_handle;
00682     Errors lastErrorCode[NumErrorGroups+1];
00684     int lastErrorNumber[NumErrorGroups+1];
00686     PINDEX lastReadCount;
00688     PINDEX lastWriteCount;
00690     PTimeInterval readTimeout;
00692     PTimeInterval writeTimeout;
00693 
00694 
00695   private:
00696     // New functions for class
00697     void Construct();
00698       // Complete platform dependent construction.
00699 
00700     // Member variables
00701     PBoolean abortCommandString;
00702       // Flag to abort the transmission of a command in SendCommandString().
00703 
00704 
00705 // Include platform dependent part of class
00706 #ifdef _WIN32
00707 #include "msos/ptlib/channel.h"
00708 #else
00709 #include "unix/ptlib/channel.h"
00710 #endif
00711 
00712 };
00713 
00714 #endif
00715 
00716 // End Of File ///////////////////////////////////////////////////////////////

Generated on Mon Feb 23 01:57:53 2009 for PTLib by  doxygen 1.5.1