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: 19008 $
00030  * $Author: rjongbloed $
00031  * $Date: 2007-11-29 09:17:41 +0000 (Thu, 29 Nov 2007) $
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 overflow(int=EOF);
00064     virtual int underflow();
00065     virtual int sync();
00066 #ifdef __USE_STL__
00067     virtual pos_type seekoff(off_type, ios_base::seekdir, ios_base::openmode = ios_base::in | ios_base::out);
00068     virtual pos_type seekpos(pos_type, ios_base::openmode = ios_base::in | ios_base::out);
00069 #else
00070     virtual streampos seekoff(streamoff, ios::seek_dir, int);
00071 #endif
00072 
00073     PBoolean SetBufferSize(
00074       PINDEX newSize
00075     );
00076 
00077   private:
00078     // Member variables
00079     PChannel * channel;
00080     PCharArray input, output;
00081 
00082   public:
00083     PChannelStreamBuffer(const PChannelStreamBuffer & sbuf);
00084     PChannelStreamBuffer & operator=(const PChannelStreamBuffer & sbuf);
00085 
00086   friend class PChannel;
00087 };
00088 
00089 
00111 class PChannel : public PObject, public iostream {
00112   PCLASSINFO(PChannel, PObject);
00113 
00114   public:
00117 
00118     PChannel();
00119 
00121     ~PChannel();
00123 
00135     virtual Comparison Compare(
00136       const PObject & obj   
00137     ) const;
00138 
00152     virtual PINDEX HashFunction() const;
00154 
00164     virtual PBoolean IsOpen() const;
00165 
00171     virtual PString GetName() const;
00172 
00178     int GetHandle() const;
00179 
00189     virtual PChannel * GetBaseReadChannel() const;
00190 
00200     virtual PChannel * GetBaseWriteChannel() const;
00202 
00212     void SetReadTimeout(
00213       const PTimeInterval & time  
00214     );
00215 
00222     PTimeInterval GetReadTimeout() const;
00223 
00236     virtual PBoolean Read(
00237       void * buf,   
00238       PINDEX len    
00239     );
00240 
00255     virtual PINDEX GetLastReadCount() const;
00256 
00264     virtual int ReadChar();
00265 
00274     PBoolean ReadBlock(
00275       void * buf,   
00276       PINDEX len    
00277     );
00278 
00286     PString ReadString(PINDEX len);
00287 
00299     virtual PBoolean ReadAsync(
00300       void * buf,   
00301       PINDEX len    
00302     );
00303 
00308     virtual void OnReadComplete(
00309       void * buf, 
00310       PINDEX len  
00311     );
00313 
00323     void SetWriteTimeout(
00324       const PTimeInterval & time 
00325     );
00326 
00334     PTimeInterval GetWriteTimeout() const;
00335 
00347     virtual PBoolean Write(
00348       const void * buf, 
00349       PINDEX len        
00350     );
00351 
00364     virtual PINDEX GetLastWriteCount() const;
00365 
00374     PBoolean WriteChar(int c);
00375 
00382     PBoolean WriteString(const PString & str);
00383 
00393     virtual PBoolean WriteAsync(
00394       const void * buf, 
00395       PINDEX len        
00396     );
00397 
00403     virtual void OnWriteComplete(
00404       const void * buf, 
00405       PINDEX len        
00406     );
00408 
00415     virtual PBoolean Close();
00416 
00417     enum ShutdownValue {
00418       ShutdownRead         = 0,
00419       ShutdownWrite        = 1,
00420       ShutdownReadAndWrite = 2
00421     };
00422 
00430     virtual PBoolean Shutdown(
00431       ShutdownValue option
00432     );
00433 
00439     PBoolean SetBufferSize(
00440       PINDEX newSize    
00441     );
00442 
00482     PBoolean SendCommandString(
00483       const PString & command  
00484     );
00485 
00490     void AbortCommandString();
00492 
00498     enum Errors {
00499       NoError,
00501       NotFound,       
00503       FileExists,     
00505       DiskFull,       
00507       AccessDenied,   
00509       DeviceInUse,    
00511       BadParameter,   
00513       NoMemory,       
00515       NotOpen,        
00517       Timeout,        
00519       Interrupted,    
00521       BufferTooSmall, 
00523       Miscellaneous,
00525       ProtocolFailure,
00526       NumNormalisedErrors
00527     };
00528 
00534     enum ErrorGroup {
00535       LastReadError,      
00536       LastWriteError,     
00537       LastGeneralError,   
00538       NumErrorGroups
00539     };
00540 
00545     Errors GetErrorCode(
00546       ErrorGroup group = NumErrorGroups   
00547     ) const;
00548 
00554     int GetErrorNumber(
00555       ErrorGroup group = NumErrorGroups   
00556     ) const;
00557 
00563     virtual PString GetErrorText(
00564       ErrorGroup group = NumErrorGroups   
00565     ) const;
00566 
00573     static PString GetErrorText(
00574       Errors lastError,   
00575       int osError = 0     
00576     );
00578 
00585     static PBoolean ConvertOSError(
00586       int libcReturnValue,
00587       Errors & lastError,
00588       int & osError
00589     );
00590 
00595 #if P_HAS_RECVMSG
00596     typedef iovec Slice;
00597 #else
00598     struct Slice {
00599       void * iov_base;
00600       size_t iov_len;
00601     };
00602 #endif
00603 
00604     typedef std::vector<Slice> VectorOfSlice;
00605 
00615     virtual PBoolean Read(
00616       const VectorOfSlice & slices    // slices to read to
00617     );
00618 
00628     virtual PBoolean Write(
00629       const VectorOfSlice & slices    // slices to read to
00630     );
00632 
00633   protected:
00634     PChannel(const PChannel &);
00635     PChannel & operator=(const PChannel &);
00636     // Prevent usage by external classes
00637 
00638 
00645     virtual PBoolean ConvertOSError(
00646       int libcReturnValue,
00647       ErrorGroup group = LastGeneralError 
00648     );
00649 
00650   public:
00654     PBoolean SetErrorValues(
00655       Errors errorCode,   
00656       int osError,        
00657       ErrorGroup group = LastGeneralError 
00658     );
00659 
00660   protected:
00669     int ReadCharWithTimeout(
00670       PTimeInterval & timeout  // Timeout for read.
00671     );
00672 
00673     // Receive a (partial) command string, determine if completed yet.
00674     PBoolean ReceiveCommandString(
00675       int nextChar,
00676       const PString & reply,
00677       PINDEX & pos,
00678       PINDEX start
00679     );
00680 
00681 
00682     // Member variables
00684     int os_handle;
00686     Errors lastErrorCode[NumErrorGroups+1];
00688     int lastErrorNumber[NumErrorGroups+1];
00690     PINDEX lastReadCount;
00692     PINDEX lastWriteCount;
00694     PTimeInterval readTimeout;
00696     PTimeInterval writeTimeout;
00697 
00698 
00699   private:
00700     // New functions for class
00701     void Construct();
00702       // Complete platform dependent construction.
00703 
00704     // Member variables
00705     PBoolean abortCommandString;
00706       // Flag to abort the transmission of a command in SendCommandString().
00707 
00708 
00709 // Include platform dependent part of class
00710 #ifdef _WIN32
00711 #include "msos/ptlib/channel.h"
00712 #else
00713 #include "unix/ptlib/channel.h"
00714 #endif
00715 
00716 };
00717 
00718 #endif
00719 
00720 // End Of File ///////////////////////////////////////////////////////////////

Generated on Mon Dec 10 11:18:56 2007 for PTLib by  doxygen 1.5.1