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(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
00613 );
00614
00624 virtual PBoolean Write(
00625 const VectorOfSlice & slices
00626 );
00628
00629 protected:
00630 PChannel(const PChannel &);
00631 PChannel & operator=(const PChannel &);
00632
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
00667 );
00668
00669
00670 PBoolean ReceiveCommandString(
00671 int nextChar,
00672 const PString & reply,
00673 PINDEX & pos,
00674 PINDEX start
00675 );
00676
00677
00678
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
00697 void Construct();
00698
00699
00700
00701 PBoolean abortCommandString;
00702
00703
00704
00705
00706 #ifdef _WIN32
00707 #include "msos/ptlib/channel.h"
00708 #else
00709 #include "unix/ptlib/channel.h"
00710 #endif
00711
00712 };
00713
00714
00715 #endif // PTLIB_CHANNEL_H
00716
00717
00718