PTLib  Version 2.10.10
 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: 24459 $
30  * $Author: shorne $
31  * $Date: 2010-06-06 08:59:59 -0500 (Sun, 06 Jun 2010) $
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 iostream {
108  PCLASSINFO(PChannel, PObject);
109 
110  public:
113 
114  PChannel();
115 
117  ~PChannel();
119 
131  virtual Comparison Compare(
132  const PObject & obj
133  ) const;
134 
148  virtual PINDEX HashFunction() const;
150 
160  virtual PBoolean IsOpen() const;
161 
167  virtual PString GetName() const;
168 
174  int GetHandle() const;
175 
185  virtual PChannel * GetBaseReadChannel() const;
186 
196  virtual PChannel * GetBaseWriteChannel() const;
198 
208  void SetReadTimeout(
209  const PTimeInterval & time
210  );
211 
219 
232  virtual PBoolean Read(
233  void * buf,
234  PINDEX len
235  );
236 
251  virtual PINDEX GetLastReadCount() const;
252 
260  virtual int ReadChar();
261 
271  void * buf,
272  PINDEX len
273  );
274 
283  PINDEX len
284  );
285 
297  virtual PBoolean ReadAsync(
298  void * buf,
299  PINDEX len
300  );
301 
306  virtual void OnReadComplete(
307  void * buf,
308  PINDEX len
309  );
311 
321  void SetWriteTimeout(
322  const PTimeInterval & time
323  );
324 
333 
345  virtual PBoolean Write(
346  const void * buf,
347  PINDEX len
348  );
349 
362  virtual PBoolean Write(
363  const void * buf,
364  PINDEX len,
365  const void * mark
366  );
367 
380  virtual PINDEX GetLastWriteCount() const;
381 
390  PBoolean WriteChar(int c);
391 
398  PBoolean WriteString(const PString & str);
399 
409  virtual PBoolean WriteAsync(
410  const void * buf,
411  PINDEX len
412  );
413 
419  virtual void OnWriteComplete(
420  const void * buf,
421  PINDEX len
422  );
424 
431  virtual PBoolean Close();
432 
437  };
438 
446  virtual PBoolean Shutdown(
447  ShutdownValue option
448  );
449 
457  virtual bool SetLocalEcho(
458  bool localEcho
459  );
460 
464  virtual bool FlowControl(const void * flowData);
465 
472  PINDEX newSize
473  );
474 
514  const PString & command
515  );
516 
521  void AbortCommandString();
523 
529  enum Errors {
558  };
559 
565  enum ErrorGroup {
570  };
571 
577  ErrorGroup group = NumErrorGroups
578  ) const;
579 
585  int GetErrorNumber(
586  ErrorGroup group = NumErrorGroups
587  ) const;
588 
594  virtual PString GetErrorText(
595  ErrorGroup group = NumErrorGroups
596  ) const;
597 
604  static PString GetErrorText(
605  Errors lastError,
606  int osError = 0
607  );
609 
616  static PBoolean ConvertOSError(
617  int libcReturnValue,
618  Errors & lastError,
619  int & osError
620  );
621 
626 #if P_HAS_RECVMSG
627  typedef iovec Slice;
628 #else
629  struct Slice {
630  void * iov_base;
631  size_t iov_len;
632  };
633 #endif
634 
635  typedef std::vector<Slice> VectorOfSlice;
636 
646  virtual PBoolean Read(
647  const VectorOfSlice & slices // slices to read to
648  );
649 
659  virtual PBoolean Write(
660  const VectorOfSlice & slices // slices to read to
661  );
663 
664  protected:
665  PChannel(const PChannel &);
666  PChannel & operator=(const PChannel &);
667  // Prevent usage by external classes
668 
669 
676  virtual PBoolean ConvertOSError(
677  int libcReturnValue,
679  );
680 
681  public:
686  Errors errorCode,
687  int osError,
689  );
690 
691  protected:
701  PTimeInterval & timeout // Timeout for read.
702  );
703 
704  // Receive a (partial) command string, determine if completed yet.
706  int nextChar,
707  const PString & reply,
708  PINDEX & pos,
709  PINDEX start
710  );
711 
712 
713  // Member variables
728 
729 
730  private:
731  // New functions for class
732  void Construct();
733  // Complete platform dependent construction.
734 
735  // Member variables
736  PBoolean abortCommandString;
737  // Flag to abort the transmission of a command in SendCommandString().
738 
739 
740 // Include platform dependent part of class
741 #ifdef _WIN32
742 #include "msos/ptlib/channel.h"
743 #else
744 #include "unix/ptlib/channel.h"
745 #endif
746 
747 };
748 
749 
750 #endif // PTLIB_CHANNEL_H
751 
752 
753 // End Of File ///////////////////////////////////////////////////////////////