E-MailRelay

greadwrite.h
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2021 Graeme Walker <graeme_walker@users.sourceforge.net>
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///
49 class G::ReadWrite
50{
51 public:
52 using size_type = std::size_t ;
53 using ssize_type = ssize_t ;
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() const = 0 ;
71 ///< See read() and write().
72
73 virtual SOCKET fd() const noexcept = 0 ;
74 ///< Returns the file descriptor.
75
76 virtual ~ReadWrite() = default ;
77 ///< Destructor.
78} ;
79
80#endif
G::ReadWrite
An abstract interface for reading and writing from a non-blocking i/o channel.
Definition: greadwrite.h:50
G::ReadWrite::write
virtual ssize_type write(const char *buf, size_type len)=0
Sends data.
G::ReadWrite::eWouldBlock
virtual bool eWouldBlock() const =0
See read() and write().
G::ReadWrite::read
virtual ssize_type read(char *buffer, size_type buffer_length)=0
Reads data.
G::ReadWrite::fd
virtual SOCKET fd() const noexcept=0
Returns the file descriptor.
G
Low-level classes.
Definition: galign.h:28