replaced separate inbuf and outbuf with iobuf

This commit is contained in:
Bob Polis 2020-04-24 17:45:49 +02:00
parent e9fa798ee7
commit eeaa962aba
2 changed files with 26 additions and 32 deletions

View File

@ -21,17 +21,23 @@ namespace sc {
namespace io {
class fdoutbuf : public std::streambuf {
class fdiobuf : public std::streambuf {
public:
fdoutbuf(int fd) : _fd(fd) {}
fdoutbuf() : _fd(-1) {}
fdiobuf(int fd) : _fd(fd) {}
fdiobuf() : _fd(-1) {}
int fd() const { return _fd; }
void fd(int fd) { _fd = fd; }
protected:
int _fd;
char _ch {};
// input
int_type underflow() override;
std::streamsize xsgetn(char* s, std::streamsize n) override;
// output
int_type overflow(int_type c) override;
std::streamsize xsputn(const char* s, std::streamsize num) override;
};
@ -41,27 +47,11 @@ namespace sc {
fdostream(int fd) : std::ostream(&_outbuf), _outbuf(fd) {}
fdostream() : std::ostream(&_outbuf), _outbuf(-1) {}
int ofd() const { return _outbuf.fd(); }
void ofd(int fd) { _outbuf.fd(fd); }
int fd() const { return _outbuf.fd(); }
void fd(int fd) { _outbuf.fd(fd); }
protected:
fdoutbuf _outbuf;
};
class fdinbuf : public std::streambuf {
public:
fdinbuf(int fd) : _fd(fd) {}
fdinbuf() : _fd(-1) {}
int fd() const { return _fd; }
void fd(int fd) { _fd = fd; }
protected:
int _fd;
char _ch {};
int_type underflow() override;
std::streamsize xsgetn(char* s, std::streamsize n) override;
fdiobuf _outbuf;
};
class fdistream : public std::istream {
@ -69,17 +59,23 @@ namespace sc {
fdistream(int fd) : std::istream(&_inbuf), _inbuf(fd) {}
fdistream() : std::istream(&_inbuf), _inbuf(-1) {}
int ifd() const { return _inbuf.fd(); }
void ifd(int fd) { _inbuf.fd(fd); }
int fd() const { return _inbuf.fd(); }
void fd(int fd) { _inbuf.fd(fd); }
protected:
fdinbuf _inbuf;
fdiobuf _inbuf;
};
class fdstream : public fdostream, public fdistream {
class fdstream : public std::iostream {
public:
fdstream(int fd) : fdostream(fd), fdistream(fd) {}
fdstream() : fdostream(-1), fdistream(-1) {}
fdstream() : std::iostream(&_iobuf) {}
fdstream(int fd) : std::iostream(&_iobuf), _iobuf(fd) {}
int fd() const { return _iobuf.fd(); }
void fd(int fd) { _iobuf.fd(fd); }
protected:
fdiobuf _iobuf {-1};
};
struct socket_address {

View File

@ -103,8 +103,7 @@ socketstream socketstream::accept() const {
peer._socket = active_socket;
peer._domain = _domain;
peer._type = _type;
peer._inbuf.fd(active_socket);
peer._outbuf.fd(active_socket);
peer.fd(active_socket);
return peer;
}
@ -191,6 +190,5 @@ socket_address socketstream::address_from_endpoint(const struct sockaddr* endpoi
void socketstream::socket(int sock) {
_socket = sock;
ifd(sock);
ofd(sock);
fd(sock);
}