dodo  0.0.1
A C++ library to create containerized Linux services
socketreadbuffer.cpp
1 /*
2  * This file is part of the dodo library (https://github.com/jmspit/dodo).
3  * Copyright (c) 2019 Jan-Marten Spit.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, version 3.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * 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 /**
19  * @file socketreadbuffer.hpp
20  * Implements the dodo::network::SocketReadBuffer class.
21  */
22 
24 
25 #include <stdio.h>
26 
27 namespace dodo {
28 
29  namespace network {
30 
31  SocketReadBuffer::SocketReadBuffer( BaseSocket* socket, size_t bufsize ) {
32  socket_ = socket;
33  if ( !socket_->getBlocking() ) throw_Exception( "SocketReadBuffer requires a blocking socket" );
34  idx_ = 0;
35  received_ = 0;
36  bufsize_ = bufsize;
37  buffer_ = new char[bufsize_];
38  }
39 
41  delete buffer_;
42  }
43 
46  if ( idx_ >= received_ ) {
47  underflows_++;
49  if ( error == common::SystemError::ecOK ) idx_ = 0;
50  }
51  return error;
52  }
53 
54  char SocketReadBuffer::get() const {
55  return buffer_[idx_];
56  }
57 
59  idx_++;
60  return underflow();
61  }
62 
63  FileReadBuffer::FileReadBuffer( std::string filename, size_t bufsize ) : VirtualReadBuffer() {
64  bufsize_ = bufsize;
65  read_ = 0;
66  idx_ = 0;
67  file_ = fopen( filename.c_str(), "r");
68  if ( !file_ ) throw_SystemException( common::Puts() << "file '" << filename << "' cannot be opened (", errno );
69  buffer_ = new char [bufsize_];
71  if ( error != common::SystemError::ecOK ) throw_SystemException( common::Puts() << "file '" << filename << "' cannot be opened", errno );
72  }
73 
74  FileReadBuffer::~FileReadBuffer() {
75  fclose( file_ );
76  delete[] buffer_;
77  }
78 
81  if ( idx_ >= read_ ) {
82  underflows_++;
83  read_ = fread( buffer_, 1, bufsize_, file_ );
84  idx_ = 0;
85  if ( read_ == 0 ) error = common::SystemError::ecEAGAIN;
86  }
87  return error;
88  }
89 
90 
91  char FileReadBuffer::get() const {
92  return buffer_[idx_];
93  }
94 
96  idx_++;
97  return underflow();
98  }
99 
100  }
101 
102 }
dodo::network::SocketReadBuffer::underflow
virtual common::SystemError underflow()
If the buffer_ is fully read.
Definition: socketreadbuffer.cpp:44
dodo::network::SocketReadBuffer::idx_
ssize_t idx_
The current index of get(char &c) in buffer_.
Definition: socketreadbuffer.hpp:135
dodo::network::FileReadBuffer::underflow
virtual common::SystemError underflow()
If the buffer_ underflows, the buffer_ must be reset refilledwith new data from the source.
Definition: socketreadbuffer.cpp:79
socketreadbuffer.hpp
dodo::network::FileReadBuffer::get
virtual char get() const
Get the current char from VirtualReadBuffer.
Definition: socketreadbuffer.cpp:91
dodo::network::VirtualReadBuffer
Interface to read individual bytes whilst the implementation can read from an actual source (such as ...
Definition: socketreadbuffer.hpp:44
dodo::network::VirtualReadBuffer::buffer_
char * buffer_
The buffer.
Definition: socketreadbuffer.hpp:81
dodo::network::FileReadBuffer::FileReadBuffer
FileReadBuffer(std::string filename, size_t bufsize=8192)
Construct a FileReadBuffer.
Definition: socketreadbuffer.cpp:63
dodo::common::SystemError::ecEAGAIN
@ ecEAGAIN
11 Try again
Definition: systemerror.hpp:71
dodo::network::SocketReadBuffer::~SocketReadBuffer
virtual ~SocketReadBuffer()
Destruct the SocketReadBuffer, de-allocate the internal buffer.
Definition: socketreadbuffer.cpp:40
dodo::network::VirtualReadBuffer::underflows_
size_t underflows_
The number underflow() invocations.
Definition: socketreadbuffer.hpp:91
dodo
A C++ platform interface to lean Linux services tailored for containerized deployment.
Definition: application.hpp:29
dodo::network::FileReadBuffer::read_
size_t read_
The number of chars read in buffer_.
Definition: socketreadbuffer.hpp:177
dodo::network::SocketReadBuffer::get
virtual char get() const
Get the current char from VirtualReadBuffer.
Definition: socketreadbuffer.cpp:54
dodo::network::SocketReadBuffer::socket_
BaseSocket * socket_
The associated network::BaseSocket*.
Definition: socketreadbuffer.hpp:130
dodo::network::BaseSocket::getBlocking
virtual bool getBlocking() const
Rerurn true if the socket is operating in blocking mode.
Definition: basesocket.cpp:125
throw_SystemException
#define throw_SystemException(what, errno)
Throws an Exception with errno, passes FILE and LINE to constructor.
Definition: exception.hpp:188
dodo::network::BaseSocket::receive
virtual common::SystemError receive(void *buf, ssize_t request, ssize_t &received)=0
Receive bytes from the socket.
throw_Exception
#define throw_Exception(what)
Throws an Exception, passes FILE and LINE to constructor.
Definition: exception.hpp:174
dodo::network::FileReadBuffer::next
virtual common::SystemError next()
Move to the next char from the VirtualReadBuffer.
Definition: socketreadbuffer.cpp:95
dodo::common::SystemError::ecOK
@ ecOK
0 Not an error, success
Definition: systemerror.hpp:60
dodo::network::SocketReadBuffer::received_
ssize_t received_
The number of chars received in the last underflow().
Definition: socketreadbuffer.hpp:140
dodo::network::FileReadBuffer::idx_
size_t idx_
The index into the buffer.
Definition: socketreadbuffer.hpp:172
dodo::common::Puts
Helper class to write strings in stream format, eg.
Definition: puts.hpp:46
dodo::network::SocketReadBuffer::SocketReadBuffer
SocketReadBuffer(BaseSocket *socket, size_t bufsize=8192)
Construct a SocketReadBuffer.
Definition: socketreadbuffer.cpp:31
dodo::common::SystemError
Linux system error primitive to provide a consistent interface to Linux error codes.
Definition: systemerror.hpp:53
dodo::network::VirtualReadBuffer::bufsize_
ssize_t bufsize_
The size of buffer_.
Definition: socketreadbuffer.hpp:86
dodo::network::FileReadBuffer::file_
FILE * file_
The file handle.
Definition: socketreadbuffer.hpp:169
dodo::network::SocketReadBuffer::next
virtual common::SystemError next()
Move to the next char from the VirtualReadBuffer.
Definition: socketreadbuffer.cpp:58
dodo::network::BaseSocket
Interface to and common implementation of concrete sockets (Socket, TLSSocket).
Definition: basesocket.hpp:36