diff --git a/libscio.hpp b/libscio.hpp index fb71311..70480ad 100644 --- a/libscio.hpp +++ b/libscio.hpp @@ -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 { diff --git a/socketstream.cpp b/socketstream.cpp index 9a03c64..2996ce5 100644 --- a/socketstream.cpp +++ b/socketstream.cpp @@ -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); }