replaced separate inbuf and outbuf with iobuf
This commit is contained in:
parent
e9fa798ee7
commit
eeaa962aba
52
libscio.hpp
52
libscio.hpp
@ -21,17 +21,23 @@ namespace sc {
|
|||||||
|
|
||||||
namespace io {
|
namespace io {
|
||||||
|
|
||||||
class fdoutbuf : public std::streambuf {
|
class fdiobuf : public std::streambuf {
|
||||||
public:
|
public:
|
||||||
fdoutbuf(int fd) : _fd(fd) {}
|
fdiobuf(int fd) : _fd(fd) {}
|
||||||
fdoutbuf() : _fd(-1) {}
|
fdiobuf() : _fd(-1) {}
|
||||||
|
|
||||||
int fd() const { return _fd; }
|
int fd() const { return _fd; }
|
||||||
void fd(int fd) { _fd = fd; }
|
void fd(int fd) { _fd = fd; }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
int _fd;
|
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;
|
int_type overflow(int_type c) override;
|
||||||
std::streamsize xsputn(const char* s, std::streamsize num) 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(int fd) : std::ostream(&_outbuf), _outbuf(fd) {}
|
||||||
fdostream() : std::ostream(&_outbuf), _outbuf(-1) {}
|
fdostream() : std::ostream(&_outbuf), _outbuf(-1) {}
|
||||||
|
|
||||||
int ofd() const { return _outbuf.fd(); }
|
int fd() const { return _outbuf.fd(); }
|
||||||
void ofd(int fd) { _outbuf.fd(fd); }
|
void fd(int fd) { _outbuf.fd(fd); }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
fdoutbuf _outbuf;
|
fdiobuf _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;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class fdistream : public std::istream {
|
class fdistream : public std::istream {
|
||||||
@ -69,17 +59,23 @@ namespace sc {
|
|||||||
fdistream(int fd) : std::istream(&_inbuf), _inbuf(fd) {}
|
fdistream(int fd) : std::istream(&_inbuf), _inbuf(fd) {}
|
||||||
fdistream() : std::istream(&_inbuf), _inbuf(-1) {}
|
fdistream() : std::istream(&_inbuf), _inbuf(-1) {}
|
||||||
|
|
||||||
int ifd() const { return _inbuf.fd(); }
|
int fd() const { return _inbuf.fd(); }
|
||||||
void ifd(int fd) { _inbuf.fd(fd); }
|
void fd(int fd) { _inbuf.fd(fd); }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
fdinbuf _inbuf;
|
fdiobuf _inbuf;
|
||||||
};
|
};
|
||||||
|
|
||||||
class fdstream : public fdostream, public fdistream {
|
class fdstream : public std::iostream {
|
||||||
public:
|
public:
|
||||||
fdstream(int fd) : fdostream(fd), fdistream(fd) {}
|
fdstream() : std::iostream(&_iobuf) {}
|
||||||
fdstream() : fdostream(-1), fdistream(-1) {}
|
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 {
|
struct socket_address {
|
||||||
|
@ -103,8 +103,7 @@ socketstream socketstream::accept() const {
|
|||||||
peer._socket = active_socket;
|
peer._socket = active_socket;
|
||||||
peer._domain = _domain;
|
peer._domain = _domain;
|
||||||
peer._type = _type;
|
peer._type = _type;
|
||||||
peer._inbuf.fd(active_socket);
|
peer.fd(active_socket);
|
||||||
peer._outbuf.fd(active_socket);
|
|
||||||
return peer;
|
return peer;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -191,6 +190,5 @@ socket_address socketstream::address_from_endpoint(const struct sockaddr* endpoi
|
|||||||
|
|
||||||
void socketstream::socket(int sock) {
|
void socketstream::socket(int sock) {
|
||||||
_socket = sock;
|
_socket = sock;
|
||||||
ifd(sock);
|
fd(sock);
|
||||||
ofd(sock);
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user