PTLib  Version 2.14.3
 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  * $Revision: 32474 $
30  * $Author: rjongbloed $
31  * $Date: 2014-08-09 14:23:47 +1000 (Sat, 09 Aug 2014) $
32  */
33 
34 #ifndef PTLIB_CHANNEL_H
35 #define PTLIB_CHANNEL_H
36 
37 #ifdef P_USE_PRAGMA
38 #pragma interface
39 #endif
40 
41 #include <ptlib/mutex.h>
42 
44 // I/O Channels
45 
46 class PChannel;
47 
48 /* Buffer class used in PChannel stream.
49 This class is necessary for implementing the standard C++ iostream interface
50 on <code>PChannel</code> classes and its descendents. It is an internal class and
51 should not ever be used by application writers.
52 */
53 class PChannelStreamBuffer : public streambuf {
54 
55  protected:
56  /* Construct the streambuf for standard streams on a channel. This is used
57  internally by the <code>PChannel</code> class.
58  */
60  PChannel * chan // Channel the buffer operates on.
61  );
62 
63  virtual int_type overflow(int_type = EOF);
64  virtual int_type underflow();
65  virtual int sync();
66  virtual pos_type seekoff(std::streamoff, ios_base::seekdir, ios_base::openmode = ios_base::in | ios_base::out);
67  virtual pos_type seekpos(pos_type, ios_base::openmode = ios_base::in | ios_base::out);
68 
70  PINDEX newSize
71  );
72 
73  private:
74  // Member variables
75  PChannel * channel;
76  PCharArray input, output;
77 
78  public:
81 
82  friend class PChannel;
83 };
84 
85 
107 class PChannel : public PObject, public std::iostream
108 {
109  PCLASSINFO(PChannel, PObject);
110 
113  protected:
115  PChannel();
116 
117  public:
119  ~PChannel();
121 
133  virtual Comparison Compare(
134  const PObject & obj
135  ) const;
136 
150  virtual PINDEX HashFunction() const;
152 
162  virtual PBoolean IsOpen() const;
163 
169  virtual PString GetName() const { return channelName; }
170 
176  virtual P_INT_PTR GetHandle() const;
177 
183  FILE * FDOpen(const char * mode);
184 
194  virtual PChannel * GetBaseReadChannel() const;
195 
205  virtual PChannel * GetBaseWriteChannel() const;
207 
213  enum Errors {
230  };
231 
237  enum ErrorGroup {
242  };
243 
249  ErrorGroup group = NumErrorGroups
250  ) const;
251 
257  int GetErrorNumber(
258  ErrorGroup group = NumErrorGroups
259  ) const;
260 
266  virtual PString GetErrorText(
267  ErrorGroup group = NumErrorGroups
268  ) const;
269 
276  static PString GetErrorText(
277  Errors lastError,
278  int osError = 0
279  );
281 
291  void SetReadTimeout(
292  const PTimeInterval & time
293  );
294 
302 
315  virtual PBoolean Read(
316  void * buf,
317  PINDEX len
318  );
319 
334  PINDEX GetLastReadCount() const;
335 
347  virtual int ReadChar();
348 
358  void * buf,
359  PINDEX len
360  );
361 
370  PINDEX len
371  );
373 
383  void SetWriteTimeout(
384  const PTimeInterval & time
385  );
386 
395 
407  virtual PBoolean Write(
408  const void * buf,
409  PINDEX len
410  );
411 
424  virtual PBoolean Write(
425  const void * buf,
426  PINDEX len,
427  const void * /*mark*/
428  ) { return Write(buf,len); }
429 
430 
443  virtual PINDEX GetLastWriteCount() const;
444 
453  PBoolean WriteChar(int c);
454 
462  const PString & str
463  );
465 
470  #define PDECLARE_AsyncNotifier(cls, fn) PDECLARE_NOTIFIER2(PChannel, cls, fn, PChannel::AsyncContext &)
471  #define PCREATE_AsyncNotifier(fn) PCREATE_NOTIFIER2(fn, PChannel::AsyncContext &)
472 
475 #ifdef _WIN32
476  typedef OVERLAPPED AsyncContextBase;
477 #elif defined _AIO_H
478  typedef struct aiocb AsyncContextBase;
479 #else
480  class AsyncContextBase { off_t m_offset; };
481 #endif
483  {
484  public:
485  AsyncContext(
486  void * buf = NULL,
487  PINDEX len = 0,
488  const AsyncNotifier & notifier = AsyncNotifier()
489  );
490 
493  void SetOffset(off_t offset);
494 
495  void * m_buffer;
496  PINDEX m_length;
498 
501 
502  // Internal stuff
505  P_ALIGN_FIELD(CompletionFunction,m_onComplete,16);
506  bool Initialise(PChannel * channel, CompletionFunction onComplete);
507  void OnIOComplete(PINDEX length, int errorNumber);
508  };
509 
523  virtual bool ReadAsync(
524  AsyncContext & context
525  );
526 
532  virtual void OnReadComplete(
533  AsyncContext & context
534  );
535 
549  virtual bool WriteAsync(
550  AsyncContext & context
551  );
552 
558  virtual void OnWriteComplete(
559  AsyncContext & context
560  );
562 
569  virtual PBoolean Close();
570 
575  };
576 
584  virtual PBoolean Shutdown(
585  ShutdownValue option
586  );
587 
595  virtual bool SetLocalEcho(
596  bool localEcho
597  );
598 
602  virtual bool FlowControl(const void * flowData);
603 
610  PINDEX newSize
611  );
612 
652  const PString & command
653  );
654 
659  void AbortCommandString();
661 
662  protected:
663  PChannel(const PChannel &);
664  PChannel & operator=(const PChannel &);
665  // Prevent usage by external classes
666 
667 
674  virtual PBoolean ConvertOSError(
675  P_INT_PTR libcReturnValue,
677  );
678 
679  public:
684  Errors errorCode,
685  int osError,
687  );
688 
689  protected:
699  PTimeInterval & timeout // Timeout for read.
700  );
701 
702  // Receive a (partial) command string, determine if completed yet.
704  int nextChar,
705  const PString & reply,
706  PINDEX & pos,
707  PINDEX start
708  );
709 
710  virtual int os_errno() const;
711 
712  // Member variables
715 
730 
731  private:
732  // New functions for class
733  void Construct();
734  // Complete platform dependent construction.
735 
736  // Member variables
737  PBoolean abortCommandString;
738  // Flag to abort the transmission of a command in SendCommandString().
739 
740 
741 // Include platform dependent part of class
742 #ifdef _WIN32
743 #include "msos/ptlib/channel.h"
744 #else
745 #include "unix/ptlib/channel.h"
746 #endif
747 
748 };
749 
750 
753 class PNullChannel : public PChannel
754 {
756  public:
757  PNullChannel();
758 
759  PBoolean Read(void *, PINDEX);
760  PBoolean Write(const void *, PINDEX);
761 };
762 
763 
764 #endif // PTLIB_CHANNEL_H
765 
766 
767 // End Of File ///////////////////////////////////////////////////////////////