VideoTools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
greadwrite.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2017 Graeme Walker
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 // ===
17 ///
18 /// \file greadwrite.h
19 ///
20 
21 #ifndef G_READ_WRITE__H
22 #define G_READ_WRITE__H
23 
24 #include "gdef.h"
25 
26 namespace G
27 {
28  class ReadWrite ;
29 }
30 
31 /// \class G::ReadWrite
32 /// An abstract interface for reading and writing from a non-blocking
33 /// i/o channel.
34 ///
35 /// Eg:
36 /// \code
37 /// ssize_t rc = s.read( buffer , buffer.size() ) ;
38 /// if( rc == 0 ) throw Disconnected() ;
39 /// else if( rc < 0 && !s.eWouldBlock() ) throw ReadError() ;
40 /// else if( rc > 0 ) got_some( buffer , rc ) ;
41 /// else /*nothing-to-do*/ ;
42 ///
43 /// ssize_t rc = s.write( buffer , buffer.size() )
44 /// if( rc < 0 && !s.eWouldBlock() ) throw Disconnected() ;
45 /// else if( rc < 0 || rc < buffer.size() ) sent_some( rc < 0 ? 0 : rc ) ;
46 /// else sent_all() ;
47 /// \endcode
48 ///
50 {
51 public:
52  typedef size_t size_type ;
53  typedef ssize_t ssize_type ;
54 
55  virtual ssize_type read( char * buffer , size_type buffer_length ) = 0 ;
56  ///< Reads data. Returns 0 if the connection has been lost.
57  ///< Returns -1 on error or if there is nothing to read (in
58  ///< which case eWouldBlock() returns true).
59 
60  virtual ssize_type write( const char * buf , size_type len ) = 0 ;
61  ///< Sends data. Returns the amount of data sent.
62  ///<
63  ///< If this method returns -1 then use eWouldBlock() to
64  ///< determine whether there was a flow control problem;
65  ///< if it returns -1 and eWouldBlock() returns false then
66  ///< the connection is lost. If it returns less than the
67  ///< requested length then eWouldBlock() should not be
68  ///< used.
69 
70  virtual bool eWouldBlock() = 0 ;
71  ///< See read() and write().
72 
73  virtual SOCKET fd() const = 0 ;
74  ///< Returns the file descriptor.
75 
76  virtual ~ReadWrite() ;
77  ///< Destructor.
78 } ;
79 
80 #endif
An abstract interface for reading and writing from a non-blocking i/o channel.
Definition: greadwrite.h:49
virtual ssize_type write(const char *buf, size_type len)=0
Sends data.
virtual ~ReadWrite()
Destructor.
Definition: greadwrite.cpp:24
virtual bool eWouldBlock()=0
See read() and write().
virtual ssize_type read(char *buffer, size_type buffer_length)=0
Reads data.
virtual SOCKET fd() const =0
Returns the file descriptor.