changed socketstream to be derived from std::iostream

This commit is contained in:
Bob Polis 2020-04-25 17:56:11 +02:00
parent 5012f2e582
commit 3c26c0616e
2 changed files with 21 additions and 26 deletions

View File

@ -98,7 +98,7 @@ namespace sc {
int port;
};
class socketstream : public fdstream {
class socketstream : public std::iostream {
public:
socketstream();
socketstream(int domain, int type);
@ -130,15 +130,15 @@ namespace sc {
socket_address address_from_endpoint(const struct sockaddr* endpoint) const;
// getters & setters
int socket() const { return _socket; }
void socket(int sock);
int socket() const { return _iobuf.fd(); }
void socket(int sock) { _iobuf.fd(sock); }
int domain() const { return _domain; }
void domain(int dom) { _domain = dom; }
int type() const { return _type; }
void type(int typ) { _type = typ; }
protected:
int _socket {-1};
fdiobuf _iobuf {-1};
int _domain {0};
int _type {0};
struct sockaddr_storage _sa;

View File

@ -19,42 +19,42 @@
using namespace sc::io;
socketstream::socketstream(int domain, int type)
: fdstream(), _domain(domain), _type(type) {
this->socket(::socket(_domain, _type, 0));
throw_if_min1_msg(_socket, "could not create socket");
: std::iostream(&_iobuf), _domain(domain), _type(type) {
_iobuf.fd(::socket(_domain, _type, 0));
throw_if_min1_msg(_iobuf.fd(), "could not create socket");
std::memset(&_sa, 0, sizeof(struct sockaddr_storage));
}
socketstream::socketstream() : fdstream() {
socketstream::socketstream() : std::iostream(&_iobuf) {
std::memset(&_sa, 0, sizeof(struct sockaddr_storage));
}
socketstream::socketstream(socketstream&& other) : fdstream(other._socket) {
this->socket(other._socket);
socketstream::socketstream(socketstream&& other) : std::iostream(&_iobuf) {
_iobuf.fd(other._iobuf.fd());
_domain = other._domain;
_type = other._type;
std::memcpy(&_sa, &other._sa, sizeof(struct sockaddr_storage));
other._socket = -1;
other._iobuf.fd(-1);
std::memset(&other._sa, 0, sizeof(struct sockaddr_storage));
}
socketstream& socketstream::operator=(socketstream&& other) {
if (this != &other) {
this->socket(other._socket);
_iobuf.fd(other._iobuf.fd());
_domain = other._domain;
_type = other._type;
std::memcpy(&_sa, &other._sa, sizeof(struct sockaddr_storage));
other._socket = -1;
other._iobuf.fd(-1);
std::memset(&other._sa, 0, sizeof(struct sockaddr_storage));
}
return *this;
}
socketstream::~socketstream() {
if (_socket != -1) {
close(_socket);
if (_iobuf.fd() != -1) {
::close(_iobuf.fd());
}
}
@ -89,28 +89,28 @@ void socketstream::make_server(const socket_address& sa) {
throw_if_min1_msg(-1, "unsupported socket domain");
}
struct sockaddr* addr = reinterpret_cast<struct sockaddr*>(&_sa);
throw_if_min1(::bind(_socket, addr, addr_sz));
throw_if_min1(::bind(_iobuf.fd(), addr, addr_sz));
if (_type == SOCK_STREAM) {
throw_if_min1(::listen(_socket, SOMAXCONN));
throw_if_min1(::listen(_iobuf.fd(), SOMAXCONN));
}
}
socketstream socketstream::accept() const {
socketstream peer;
socklen_t len = sizeof(struct sockaddr_storage);
int active_socket = ::accept(_socket, reinterpret_cast<struct sockaddr*>(&peer._sa), &len);
int active_socket = ::accept(_iobuf.fd(), reinterpret_cast<struct sockaddr*>(&peer._sa), &len);
throw_if_min1(active_socket);
peer._socket = active_socket;
peer._iobuf.fd(active_socket);
peer._domain = _domain;
peer._type = _type;
peer.fd(active_socket);
peer._iobuf.fd(active_socket);
return peer;
}
void socketstream::connect(const socket_address& host) {
endpoint_from_address(host, &_sa);
struct sockaddr* addr = reinterpret_cast<struct sockaddr*>(&_sa);
throw_if_min1(::connect(_socket, addr, sizeof(_sa)));
throw_if_min1(::connect(_iobuf.fd(), addr, sizeof(_sa)));
}
std::string socketstream::address() const {
@ -187,8 +187,3 @@ socket_address socketstream::address_from_endpoint(const struct sockaddr* endpoi
}
return sa;
}
void socketstream::socket(int sock) {
_socket = sock;
fd(sock);
}