PTLib  Version 2.12.9
 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: 29415 $
30  * $Author: rjongbloed $
31  * $Date: 2013-04-03 08:41:13 +1100 (Wed, 03 Apr 2013) $
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(off_type, 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 P_IOSTREAM {
108  PCLASSINFO(PChannel, PObject);
109 
112  protected:
114  PChannel();
115 
116  public:
118  ~PChannel();
120 
132  virtual Comparison Compare(
133  const PObject & obj
134  ) const;
135 
149  virtual PINDEX HashFunction() const;
151 
161  virtual PBoolean IsOpen() const;
162 
168  virtual PString GetName() const { return channelName; }
169 
175  P_INT_PTR GetHandle() const;
176 
186  virtual PChannel * GetBaseReadChannel() const;
187 
197  virtual PChannel * GetBaseWriteChannel() const;
199 
205  enum Errors {
222  };
223 
229  enum ErrorGroup {
234  };
235 
241  ErrorGroup group = NumErrorGroups
242  ) const;
243 
249  int GetErrorNumber(
250  ErrorGroup group = NumErrorGroups
251  ) const;
252 
258  virtual PString GetErrorText(
259  ErrorGroup group = NumErrorGroups
260  ) const;
261 
268  static PString GetErrorText(
269  Errors lastError,
270  int osError = 0
271  );
272 
279  static PBoolean ConvertOSError(
280  P_INT_PTR libcReturnValue,
281  Errors & lastError,
282  int & osError
283  );
285 
295  void SetReadTimeout(
296  const PTimeInterval & time
297  );
298 
306 
319  virtual PBoolean Read(
320  void * buf,
321  PINDEX len
322  );
323 
338  virtual PINDEX GetLastReadCount() const;
339 
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 
473 #ifdef _MSC_VER
474  #pragma pack(push)
475  #if P_64BIT
476  #pragma pack(8)
477  #else
478  #pragma pack(4)
479  #endif
480 #endif
481 
483 #ifdef _WIN32
484  typedef OVERLAPPED AsyncContextBase;
485 #elif defined _AIO_H
486  typedef struct aiocb AsyncContextBase;
487 #else
488  class AsyncContextBase { off_t m_offset; };
489 #endif
491  {
492  public:
493  AsyncContext(
494  void * buf = NULL,
495  PINDEX len = 0,
496  const AsyncNotifier & notifier = AsyncNotifier()
497  );
498 
501  void SetOffset(off_t offset);
502 
503  void * m_buffer;
504  PINDEX m_length;
506 
509 
510  // Internal stuff
514  bool Initialise(PChannel * channel, CompletionFunction onComplete);
515  void OnIOComplete(PINDEX length, int errorNumber);
516  };
517 #ifdef _MSC_VER
518  #pragma pack(pop)
519 #endif
520 
534  virtual bool ReadAsync(
535  AsyncContext & context
536  );
537 
543  virtual void OnReadComplete(
544  AsyncContext & context
545  );
546 
560  virtual bool WriteAsync(
561  AsyncContext & context
562  );
563 
569  virtual void OnWriteComplete(
570  AsyncContext & context
571  );
573 
580  virtual PBoolean Close();
581 
586  };
587 
595  virtual PBoolean Shutdown(
596  ShutdownValue option
597  );
598 
606  virtual bool SetLocalEcho(
607  bool localEcho
608  );
609 
613  virtual bool FlowControl(const void * flowData);
614 
621  PINDEX newSize
622  );
623 
663  const PString & command
664  );
665 
670  void AbortCommandString();
672 
673  protected:
674  PChannel(const PChannel &);
675  PChannel & operator=(const PChannel &);
676  // Prevent usage by external classes
677 
678 
685  virtual PBoolean ConvertOSError(
686  P_INT_PTR libcReturnValue,
688  );
689 
690  public:
695  Errors errorCode,
696  int osError,
698  );
699 
700  protected:
710  PTimeInterval & timeout // Timeout for read.
711  );
712 
713  // Receive a (partial) command string, determine if completed yet.
715  int nextChar,
716  const PString & reply,
717  PINDEX & pos,
718  PINDEX start
719  );
720 
721 
722  // Member variables
739 
740  private:
741  // New functions for class
742  void Construct();
743  // Complete platform dependent construction.
744 
745  // Member variables
746  PBoolean abortCommandString;
747  // Flag to abort the transmission of a command in SendCommandString().
748 
749 
750 // Include platform dependent part of class
751 #ifdef _WIN32
752 #include "msos/ptlib/channel.h"
753 #else
754 #include "unix/ptlib/channel.h"
755 #endif
756 
757 };
758 
759 
762 class PNullChannel : public PChannel
763 {
765  public:
766  PNullChannel();
767 
768  PBoolean Read(void *, PINDEX);
769  PBoolean Write(const void *, PINDEX);
770 };
771 
772 
773 #endif // PTLIB_CHANNEL_H
774 
775 
776 // End Of File ///////////////////////////////////////////////////////////////