00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #ifndef PTLIB_CHANNEL_H
00035 #define PTLIB_CHANNEL_H
00036
00037 #ifdef P_USE_PRAGMA
00038 #pragma interface
00039 #endif
00040
00041 #include <ptlib/mutex.h>
00042
00044
00045
00046 class PChannel;
00047
00048
00049
00050
00051
00052
00053 class PChannelStreamBuffer : public streambuf {
00054
00055 protected:
00056
00057
00058
00059 PChannelStreamBuffer(
00060 PChannel * chan
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
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(
00283 PINDEX len
00284 );
00285
00297 virtual PBoolean ReadAsync(
00298 void * buf,
00299 PINDEX len
00300 );
00301
00306 virtual void OnReadComplete(
00307 void * buf,
00308 PINDEX len
00309 );
00311
00321 void SetWriteTimeout(
00322 const PTimeInterval & time
00323 );
00324
00332 PTimeInterval GetWriteTimeout() const;
00333
00345 virtual PBoolean Write(
00346 const void * buf,
00347 PINDEX len
00348 );
00349
00362 virtual PINDEX GetLastWriteCount() const;
00363
00372 PBoolean WriteChar(int c);
00373
00380 PBoolean WriteString(const PString & str);
00381
00391 virtual PBoolean WriteAsync(
00392 const void * buf,
00393 PINDEX len
00394 );
00395
00401 virtual void OnWriteComplete(
00402 const void * buf,
00403 PINDEX len
00404 );
00406
00413 virtual PBoolean Close();
00414
00415 enum ShutdownValue {
00416 ShutdownRead = 0,
00417 ShutdownWrite = 1,
00418 ShutdownReadAndWrite = 2
00419 };
00420
00428 virtual PBoolean Shutdown(
00429 ShutdownValue option
00430 );
00431
00439 virtual bool SetLocalEcho(
00440 bool localEcho
00441 );
00442
00448 PBoolean SetBufferSize(
00449 PINDEX newSize
00450 );
00451
00490 PBoolean SendCommandString(
00491 const PString & command
00492 );
00493
00498 void AbortCommandString();
00500
00506 enum Errors {
00507 NoError,
00509 NotFound,
00511 FileExists,
00513 DiskFull,
00515 AccessDenied,
00517 DeviceInUse,
00519 BadParameter,
00521 NoMemory,
00523 NotOpen,
00525 Timeout,
00527 Interrupted,
00529 BufferTooSmall,
00531 Miscellaneous,
00533 ProtocolFailure,
00534 NumNormalisedErrors
00535 };
00536
00542 enum ErrorGroup {
00543 LastReadError,
00544 LastWriteError,
00545 LastGeneralError,
00546 NumErrorGroups
00547 };
00548
00553 Errors GetErrorCode(
00554 ErrorGroup group = NumErrorGroups
00555 ) const;
00556
00562 int GetErrorNumber(
00563 ErrorGroup group = NumErrorGroups
00564 ) const;
00565
00571 virtual PString GetErrorText(
00572 ErrorGroup group = NumErrorGroups
00573 ) const;
00574
00581 static PString GetErrorText(
00582 Errors lastError,
00583 int osError = 0
00584 );
00586
00593 static PBoolean ConvertOSError(
00594 int libcReturnValue,
00595 Errors & lastError,
00596 int & osError
00597 );
00598
00603 #if P_HAS_RECVMSG
00604 typedef iovec Slice;
00605 #else
00606 struct Slice {
00607 void * iov_base;
00608 size_t iov_len;
00609 };
00610 #endif
00611
00612 typedef std::vector<Slice> VectorOfSlice;
00613
00623 virtual PBoolean Read(
00624 const VectorOfSlice & slices
00625 );
00626
00636 virtual PBoolean Write(
00637 const VectorOfSlice & slices
00638 );
00640
00641 protected:
00642 PChannel(const PChannel &);
00643 PChannel & operator=(const PChannel &);
00644
00645
00646
00653 virtual PBoolean ConvertOSError(
00654 int libcReturnValue,
00655 ErrorGroup group = LastGeneralError
00656 );
00657
00658 public:
00662 PBoolean SetErrorValues(
00663 Errors errorCode,
00664 int osError,
00665 ErrorGroup group = LastGeneralError
00666 );
00667
00668 protected:
00677 int ReadCharWithTimeout(
00678 PTimeInterval & timeout
00679 );
00680
00681
00682 PBoolean ReceiveCommandString(
00683 int nextChar,
00684 const PString & reply,
00685 PINDEX & pos,
00686 PINDEX start
00687 );
00688
00689
00690
00692 int os_handle;
00694 Errors lastErrorCode[NumErrorGroups+1];
00696 int lastErrorNumber[NumErrorGroups+1];
00698 PINDEX lastReadCount;
00700 PINDEX lastWriteCount;
00702 PTimeInterval readTimeout;
00704 PTimeInterval writeTimeout;
00705
00706
00707 private:
00708
00709 void Construct();
00710
00711
00712
00713 PBoolean abortCommandString;
00714
00715
00716
00717
00718 #ifdef _WIN32
00719 #include "msos/ptlib/channel.h"
00720 #else
00721 #include "unix/ptlib/channel.h"
00722 #endif
00723
00724 };
00725
00726
00727 #endif // PTLIB_CHANNEL_H
00728
00729
00730