PTLib  Version 2.18.8
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
channel.h
Go to the documentation of this file.
1 /*
2  * channel.h
3  *
4  * I/O channel ancestor class.
5  *
6  * Portable Windows Library
7  *
8  * Copyright (c) 1993-1998 Equivalence Pty. Ltd.
9  *
10  * The contents of this file are subject to the Mozilla Public License
11  * Version 1.0 (the "License"); you may not use this file except in
12  * compliance with the License. You may obtain a copy of the License at
13  * http://www.mozilla.org/MPL/
14  *
15  * Software distributed under the License is distributed on an "AS IS"
16  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
17  * the License for the specific language governing rights and limitations
18  * under the License.
19  *
20  * The Original Code is Portable Windows Library.
21  *
22  * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
23  *
24  * Portions are Copyright (C) 1993 Free Software Foundation, Inc.
25  * All Rights Reserved.
26  *
27  * Contributor(s): ______________________________________.
28  */
29 
30 #ifndef PTLIB_CHANNEL_H
31 #define PTLIB_CHANNEL_H
32 
33 #ifdef P_USE_PRAGMA
34 #pragma interface
35 #endif
36 
37 #include <ptlib/mutex.h>
38 
40 // I/O Channels
41 
42 class PChannel;
43 
44 /* Buffer class used in PChannel stream.
45 This class is necessary for implementing the standard C++ iostream interface
46 on <code>PChannel</code> classes and its descendents. It is an internal class and
47 should not ever be used by application writers.
48 */
49 class PChannelStreamBuffer : public streambuf {
50 
51  protected:
52  /* Construct the streambuf for standard streams on a channel. This is used
53  internally by the <code>PChannel</code> class.
54  */
56  PChannel * chan // Channel the buffer operates on.
57  );
58 
59  virtual int_type overflow(int_type = EOF);
60  virtual int_type underflow();
61  virtual int sync();
62  virtual pos_type seekoff(std::streamoff, ios_base::seekdir, ios_base::openmode = ios_base::in | ios_base::out);
63  virtual pos_type seekpos(pos_type, ios_base::openmode = ios_base::in | ios_base::out);
64 
66  PINDEX newSize
67  );
68 
69  private:
70  // Member variables
71  PChannel * channel;
72  PCharArray input, output;
73 
74  public:
77 
78  friend class PChannel;
79 };
80 
81 
103 class PChannel : public PObject, public std::iostream
104 {
105  PCLASSINFO(PChannel, PObject);
106 
109  protected:
111  PChannel();
112 
113  public:
115  ~PChannel();
117 
129  virtual Comparison Compare(
130  const PObject & obj
131  ) const;
132 
146  virtual PINDEX HashFunction() const;
148 
158  virtual PBoolean IsOpen() const;
159 
165  virtual PString GetName() const = 0;
166 
172  virtual P_INT_PTR GetHandle() const;
173 
179  FILE * FDOpen(const char * mode);
180 
190  virtual PChannel * GetBaseReadChannel() const;
191 
201  virtual PChannel * GetBaseWriteChannel() const;
202 
210  virtual bool CloseBaseReadChannel();
211 
219  virtual bool CloseBaseWriteChannel();
221 
227  enum Errors {
244  };
245 
251  enum ErrorGroup {
256  };
257 
263  ErrorGroup group = NumErrorGroups
264  ) const;
265 
271  int GetErrorNumber(
272  ErrorGroup group = NumErrorGroups
273  ) const;
274 
280  virtual PString GetErrorText(
281  ErrorGroup group = NumErrorGroups
282  ) const;
283 
290  static PString GetErrorText(
291  Errors lastError,
292  int osError = 0
293  );
295 
305  void SetReadTimeout(
306  const PTimeInterval & time
307  );
308 
316 
329  virtual PBoolean Read(
330  void * buf,
331  PINDEX len
332  );
333 
348  virtual PINDEX GetLastReadCount() const;
349  virtual PINDEX SetLastReadCount(PINDEX count);
350 
362  virtual int ReadChar();
363 
373  void * buf,
374  PINDEX len
375  );
376 
385  PINDEX len
386  );
388 
398  void SetWriteTimeout(
399  const PTimeInterval & time
400  );
401 
410 
422  virtual PBoolean Write(
423  const void * buf,
424  PINDEX len
425  );
426 
439  virtual PBoolean Write(
440  const void * buf,
441  PINDEX len,
442  const void * /*mark*/
443  ) { return Write(buf,len); }
444 
445 
458  virtual PINDEX GetLastWriteCount() const;
459  virtual PINDEX SetLastWriteCount(PINDEX count);
460 
469  PBoolean WriteChar(int c);
470 
478  const PString & str
479  );
481 
486  #define PDECLARE_AsyncNotifier(cls, fn) PDECLARE_NOTIFIER2(PChannel, cls, fn, PChannel::AsyncContext &)
487  #define PCREATE_AsyncNotifier(fn) PCREATE_NOTIFIER2(fn, PChannel::AsyncContext &)
488 
491 #ifdef _WIN32
492  typedef OVERLAPPED AsyncContextBase;
493 #elif defined _AIO_H
494  typedef struct aiocb AsyncContextBase;
495 #else
496  class AsyncContextBase { off_t m_offset; };
497 #endif
499  {
500  public:
501  AsyncContext(
502  void * buf = NULL,
503  PINDEX len = 0,
504  const AsyncNotifier & notifier = AsyncNotifier()
505  );
506 
509  void SetOffset(off_t offset);
510 
511  void * m_buffer;
512  PINDEX m_length;
514 
517 
518  // Internal stuff
521  P_ALIGN_FIELD(CompletionFunction,m_onComplete,16);
522  bool Initialise(PChannel * channel, CompletionFunction onComplete);
523  void OnIOComplete(PINDEX length, int errorNumber);
524 
526  };
527 
541  virtual bool ReadAsync(
542  AsyncContext & context
543  );
544 
550  virtual void OnReadComplete(
551  AsyncContext & context
552  );
553 
567  virtual bool WriteAsync(
568  AsyncContext & context
569  );
570 
576  virtual void OnWriteComplete(
577  AsyncContext & context
578  );
580 
587  virtual PBoolean Close();
588 
593  };
594 
602  virtual PBoolean Shutdown(
603  ShutdownValue option
604  );
605 
613  virtual bool SetLocalEcho(
614  bool localEcho
615  );
616 
620  virtual bool FlowControl(const void * flowData);
621 
628  PINDEX newSize
629  );
630 
670  const PString & command
671  );
672 
677  void AbortCommandString();
679 
680  protected:
681  PChannel(const PChannel &);
682  PChannel & operator=(const PChannel &);
683  // Prevent usage by external classes
684 
685 
692  virtual PBoolean ConvertOSError(
693  P_INT_PTR libcReturnValue,
695  );
696 
697  public:
702  Errors errorCode,
703  int osError,
705  );
706 
707  protected:
717  PTimeInterval & timeout // Timeout for read.
718  );
719 
720  // Receive a (partial) command string, determine if completed yet.
722  int nextChar,
723  const PString & reply,
724  PINDEX & pos,
725  PINDEX start
726  );
727 
728  bool CheckNotOpen();
729  virtual int os_errno() const;
730 
731  // Member variables
734 
736 
737  struct Status
738  {
739  PINDEX m_lastCount;
742 
744  : m_lastCount(0)
746  , m_lastErrorNumber(0)
747  { }
748  };
750 
751  private:
752  // New functions for class
753  void Construct();
754  // Complete platform dependent construction.
755 
756  // Member variables
757  bool m_abortCommandString;
758  // Flag to abort the transmission of a command in SendCommandString().
759 
760 
761 // Include platform dependent part of class
762 #ifdef _WIN32
763 #include "msos/ptlib/channel.h"
764 #else
765 #include "unix/ptlib/channel.h"
766 #endif
767 
768 };
769 
770 
773 class PNullChannel : public PChannel
774 {
776  public:
777  PNullChannel();
778 
779  virtual PString GetName() const { return "null"; }
780  virtual PBoolean Read(void *, PINDEX);
781  virtual PBoolean Write(const void *, PINDEX);
782 };
783 
784 
785 class P_fd_set
786 {
787 public:
788  P_fd_set();
789  P_fd_set(intptr_t fd);
791  {
792  free(set);
793  }
794 
795  P_fd_set & operator=(intptr_t fd);
796  P_fd_set & operator+=(intptr_t fd);
797  P_fd_set & operator-=(intptr_t fd);
798 
799  void Zero();
800 
801  PBoolean IsPresent(intptr_t fd) const;
802 
803  operator fd_set*() const
804  {
805  return set;
806  }
807 
808 protected:
809  void Construct();
810 
811  intptr_t max_fd;
812  fd_set * set;
813 
814 private:
815  P_fd_set(const P_fd_set &) : max_fd(), set() { }
816  void operator=(const P_fd_set &) { }
817 };
818 
819 
820 #endif // PTLIB_CHANNEL_H
821 
822 
823 // End Of File ///////////////////////////////////////////////////////////////
Operation fail due to insufficient privilege.
Definition: channel.h:232
virtual bool WriteAsync(AsyncContext &context)
Begin an asynchronous write from channel.
PString ReadString(PINDEX len)
Read len character into a string from the channel.
virtual PBoolean Shutdown(ShutdownValue option)
Close one or both of the data streams associated with a channel.
void AbortCommandString()
Abort a command string that is in progress.
Definition: channel.h:737
virtual PBoolean Write(const void *buf, PINDEX len)
Low level write to the channel.
PTimeInterval readTimeout
Timeout for read operations.
Definition: channel.h:732
P_fd_set & operator-=(intptr_t fd)
virtual int sync()
Definition: channel.h:255
Open fail due to file already existing.
Definition: channel.h:230
void Zero()
This class defines an arbitrary time interval to millisecond accuracy.
Definition: timeint.h:51
No error.
Definition: channel.h:228
Array of characters.
Definition: array.h:552
#define PCLASSINFO(cls, par)
Declare all the standard PTLib class information.
Definition: object.h:2164
Status()
Definition: channel.h:743
Operation failed due to a timeout.
Definition: channel.h:237
virtual int os_errno() const
Error during other operation, eg Open()
Definition: channel.h:254
virtual PChannel * GetBaseWriteChannel() const
Get the base channel of channel indirection using PIndirectChannel.
virtual PBoolean Read(void *buf, PINDEX len)
Low level read from the channel.
virtual PBoolean Close()
Close the channel, shutting down the link to the data source.
Errors m_errorCode
Error returned after operation completed.
Definition: channel.h:515
PTimeInterval writeTimeout
Timeout for write operations.
Definition: channel.h:733
PChannel & operator=(const PChannel &)
Definition: channel.h:591
PNotifierTemplate< PChannel::AsyncContext & > AsyncNotifier
Definition: channel.h:484
Operation fail due to bad parameters.
Definition: channel.h:234
PTimeInterval GetWriteTimeout() const
Get the timeout for write operations to complete.
virtual PINDEX HashFunction() const
Calculate a hash value for use in sets and dictionaries.
PBoolean WriteString(const PString &str)
Write a string to the channel.
Errors m_lastErrorCode
The platform independant error code.
Definition: channel.h:740
Definition: channel.h:498
Comparison
Result of the comparison operation performed by the Compare() function.
Definition: object.h:2251
FILE * FDOpen(const char *mode)
Re-open the device using the stdio library.
virtual PString GetName() const =0
Get the platform and I/O channel type name of the channel.
virtual void OnReadComplete(AsyncContext &context)
User callback function for when a ReadAsync() call has completed or timed out.
Definition: channel.h:49
Definition: channel.h:590
PChannel()
Create the channel.
virtual PBoolean Write(const void *, PINDEX)
Low level write to the channel.
void SetOffset(off_t offset)
Set the offset to do the read/write operation.
virtual PINDEX GetLastWriteCount() const
Get the number of bytes written by the last Write() call.
Open fail due to device or file not found.
Definition: channel.h:229
PBoolean SetBufferSize(PINDEX newSize)
PBoolean ReceiveCommandString(int nextChar, const PString &reply, PINDEX &pos, PINDEX start)
ShutdownValue
Definition: channel.h:589
virtual Comparison Compare(const PObject &obj) const
Get the relative rank of the two strings.
Definition: channel.h:243
intptr_t max_fd
Definition: channel.h:811
virtual PINDEX SetLastWriteCount(PINDEX count)
PBoolean ReadBlock(void *buf, PINDEX len)
Read len bytes into the buffer from the channel.
PThreadLocalStorage< Status > m_status[NumErrorGroups+1]
Definition: channel.h:749
Errors
Normalised error codes.
Definition: channel.h:227
atomic< P_INT_PTR > os_handle
The operating system file handle return by standard open() function.
Definition: channel.h:735
virtual bool CloseBaseWriteChannel()
Close the base channel of channel indirection using PIndirectChannel.
High level protocol failure.
Definition: channel.h:241
void Construct()
int m_errorNumber
OS error returned after operation completed.
Definition: channel.h:516
void SetReadTimeout(const PTimeInterval &time)
Set the timeout for read operations.
~PChannel()
Close down the channel.
Error during Read() operation.
Definition: channel.h:252
Definition: channel.h:592
PBoolean SetErrorValues(Errors errorCode, int osError, ErrorGroup group=LastGeneralError)
Set error values to those specified.
virtual PString GetName() const
Get the platform and I/O channel type name of the channel.
Definition: channel.h:779
void SetWriteTimeout(const PTimeInterval &time)
Set the timeout for write operations to complete.
PINDEX m_length
Maximum number of bytes to read into the buffer.
Definition: channel.h:512
PINDEX m_lastCount
Last read/write count.
Definition: channel.h:739
bool Initialise(PChannel *channel, CompletionFunction onComplete)
#define free(p)
Override of system call for memory check system.
Definition: object.h:1870
PChannel * m_channel
Definition: channel.h:519
virtual bool FlowControl(const void *flowData)
Flow Control information Pass data to the channel for flowControl determination.
Abstract class defining I/O channel semantics.
Definition: channel.h:103
fd_set * set
Definition: channel.h:812
Parameters for asynchronous I/O operation.
Definition: channel.h:496
PBoolean SendCommandString(const PString &command)
Send a command meta-string.
Errors GetErrorCode(ErrorGroup group=NumErrorGroups) const
Get normalised error code.
bool PBoolean
Definition: object.h:174
virtual pos_type seekpos(pos_type, ios_base::openmode=ios_base::in|ios_base::out)
int ReadCharWithTimeout(PTimeInterval &timeout)
Read a character with specified timeout.
intptr_t P_INT_PTR
Definition: object.h:2646
Definition: channel.h:785
Operations buffer was too small for data.
Definition: channel.h:239
The character string class.
Definition: pstring.h:108
virtual bool ReadAsync(AsyncContext &context)
Begin an asynchronous read from channel.
virtual int_type underflow()
virtual int_type overflow(int_type=EOF)
virtual PINDEX GetLastReadCount() const
Get the number of bytes read by the last Read() call.
PChannelStreamBuffer & operator=(const PChannelStreamBuffer &sbuf)
ErrorGroup
Error groups.
Definition: channel.h:251
Operation fail due to channel not being open yet.
Definition: channel.h:236
virtual PBoolean Read(void *, PINDEX)
Low level read from the channel.
Miscellaneous error.
Definition: channel.h:240
Operation fail due to insufficient memory.
Definition: channel.h:235
virtual PINDEX SetLastReadCount(PINDEX count)
P_fd_set & operator+=(intptr_t fd)
virtual PBoolean Write(const void *buf, PINDEX len, const void *)
Low level write to the channel with marker.
Definition: channel.h:439
virtual void OnWriteComplete(AsyncContext &context)
User callback function for when a WriteAsync() call has completed or timed out.
AsyncNotifier m_notifier
Notification function for when asynchronous operation complete.
Definition: channel.h:513
Error during Write() operation.
Definition: channel.h:253
virtual P_INT_PTR GetHandle() const
Get the integer operating system handle for the channel.
virtual PBoolean IsOpen() const
Determine if the channel is currently open.
int m_lastErrorNumber
The operating system error number (eg as returned by errno).
Definition: channel.h:741
Open fail due to device already open for exclusive use.
Definition: channel.h:233
PTimeInterval GetReadTimeout() const
Get the timeout for read operations.
Operation was interrupted.
Definition: channel.h:238
P_fd_set & operator=(intptr_t fd)
virtual PString GetErrorText(ErrorGroup group=NumErrorGroups) const
Get error message description.
Cannot use channel at this time.
Definition: channel.h:242
virtual int ReadChar()
Read a single character from the channel.
virtual bool CloseBaseReadChannel()
Close the base channel of channel indirection using PIndirectChannel.
P_ALIGN_FIELD(CompletionFunction, m_onComplete, 16)
virtual PChannel * GetBaseReadChannel() const
Get the base channel of channel indirection using PIndirectChannel.
Definition: thread.h:947
void(PChannel::* CompletionFunction)(AsyncContext &)
Definition: channel.h:520
#define PNEW_AND_DELETE_FUNCTIONS(align)
Definition: object.h:1908
PBoolean SetBufferSize(PINDEX newSize)
Set the iostream buffer size for reads and writes.
virtual bool SetLocalEcho(bool localEcho)
Set local echo mode.
void OnIOComplete(PINDEX length, int errorNumber)
bool CheckNotOpen()
~P_fd_set()
Definition: channel.h:790
int GetErrorNumber(ErrorGroup group=NumErrorGroups) const
Get OS errro code.
PBoolean IsPresent(intptr_t fd) const
AsyncContext(void *buf=NULL, PINDEX len=0, const AsyncNotifier &notifier=AsyncNotifier())
void * m_buffer
Pointer to a block of memory to receive the read bytes.
Definition: channel.h:511
A channel that does nothing.
Definition: channel.h:773
Write fail due to disk full.
Definition: channel.h:231
virtual pos_type seekoff(std::streamoff, ios_base::seekdir, ios_base::openmode=ios_base::in|ios_base::out)
virtual PBoolean ConvertOSError(P_INT_PTR libcReturnValue, ErrorGroup group=LastGeneralError)
Convert an operating system error into platform independent error.
Ultimate parent class for all objects in the class library.
Definition: object.h:2204
PChannelStreamBuffer(PChannel *chan)
PBoolean WriteChar(int c)
Write a single character to the channel.