changed socketstream to be derived from std::iostream
This commit is contained in:
parent
5012f2e582
commit
3c26c0616e
@ -98,7 +98,7 @@ namespace sc {
|
|||||||
int port;
|
int port;
|
||||||
};
|
};
|
||||||
|
|
||||||
class socketstream : public fdstream {
|
class socketstream : public std::iostream {
|
||||||
public:
|
public:
|
||||||
socketstream();
|
socketstream();
|
||||||
socketstream(int domain, int type);
|
socketstream(int domain, int type);
|
||||||
@ -130,15 +130,15 @@ namespace sc {
|
|||||||
socket_address address_from_endpoint(const struct sockaddr* endpoint) const;
|
socket_address address_from_endpoint(const struct sockaddr* endpoint) const;
|
||||||
|
|
||||||
// getters & setters
|
// getters & setters
|
||||||
int socket() const { return _socket; }
|
int socket() const { return _iobuf.fd(); }
|
||||||
void socket(int sock);
|
void socket(int sock) { _iobuf.fd(sock); }
|
||||||
int domain() const { return _domain; }
|
int domain() const { return _domain; }
|
||||||
void domain(int dom) { _domain = dom; }
|
void domain(int dom) { _domain = dom; }
|
||||||
int type() const { return _type; }
|
int type() const { return _type; }
|
||||||
void type(int typ) { _type = typ; }
|
void type(int typ) { _type = typ; }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
int _socket {-1};
|
fdiobuf _iobuf {-1};
|
||||||
int _domain {0};
|
int _domain {0};
|
||||||
int _type {0};
|
int _type {0};
|
||||||
struct sockaddr_storage _sa;
|
struct sockaddr_storage _sa;
|
||||||
|
@ -19,42 +19,42 @@
|
|||||||
using namespace sc::io;
|
using namespace sc::io;
|
||||||
|
|
||||||
socketstream::socketstream(int domain, int type)
|
socketstream::socketstream(int domain, int type)
|
||||||
: fdstream(), _domain(domain), _type(type) {
|
: std::iostream(&_iobuf), _domain(domain), _type(type) {
|
||||||
this->socket(::socket(_domain, _type, 0));
|
_iobuf.fd(::socket(_domain, _type, 0));
|
||||||
throw_if_min1_msg(_socket, "could not create socket");
|
throw_if_min1_msg(_iobuf.fd(), "could not create socket");
|
||||||
std::memset(&_sa, 0, sizeof(struct sockaddr_storage));
|
std::memset(&_sa, 0, sizeof(struct sockaddr_storage));
|
||||||
}
|
}
|
||||||
|
|
||||||
socketstream::socketstream() : fdstream() {
|
socketstream::socketstream() : std::iostream(&_iobuf) {
|
||||||
std::memset(&_sa, 0, sizeof(struct sockaddr_storage));
|
std::memset(&_sa, 0, sizeof(struct sockaddr_storage));
|
||||||
}
|
}
|
||||||
|
|
||||||
socketstream::socketstream(socketstream&& other) : fdstream(other._socket) {
|
socketstream::socketstream(socketstream&& other) : std::iostream(&_iobuf) {
|
||||||
this->socket(other._socket);
|
_iobuf.fd(other._iobuf.fd());
|
||||||
_domain = other._domain;
|
_domain = other._domain;
|
||||||
_type = other._type;
|
_type = other._type;
|
||||||
std::memcpy(&_sa, &other._sa, sizeof(struct sockaddr_storage));
|
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));
|
std::memset(&other._sa, 0, sizeof(struct sockaddr_storage));
|
||||||
}
|
}
|
||||||
|
|
||||||
socketstream& socketstream::operator=(socketstream&& other) {
|
socketstream& socketstream::operator=(socketstream&& other) {
|
||||||
if (this != &other) {
|
if (this != &other) {
|
||||||
this->socket(other._socket);
|
_iobuf.fd(other._iobuf.fd());
|
||||||
_domain = other._domain;
|
_domain = other._domain;
|
||||||
_type = other._type;
|
_type = other._type;
|
||||||
std::memcpy(&_sa, &other._sa, sizeof(struct sockaddr_storage));
|
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));
|
std::memset(&other._sa, 0, sizeof(struct sockaddr_storage));
|
||||||
}
|
}
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
socketstream::~socketstream() {
|
socketstream::~socketstream() {
|
||||||
if (_socket != -1) {
|
if (_iobuf.fd() != -1) {
|
||||||
close(_socket);
|
::close(_iobuf.fd());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -89,28 +89,28 @@ void socketstream::make_server(const socket_address& sa) {
|
|||||||
throw_if_min1_msg(-1, "unsupported socket domain");
|
throw_if_min1_msg(-1, "unsupported socket domain");
|
||||||
}
|
}
|
||||||
struct sockaddr* addr = reinterpret_cast<struct sockaddr*>(&_sa);
|
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) {
|
if (_type == SOCK_STREAM) {
|
||||||
throw_if_min1(::listen(_socket, SOMAXCONN));
|
throw_if_min1(::listen(_iobuf.fd(), SOMAXCONN));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
socketstream socketstream::accept() const {
|
socketstream socketstream::accept() const {
|
||||||
socketstream peer;
|
socketstream peer;
|
||||||
socklen_t len = sizeof(struct sockaddr_storage);
|
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);
|
throw_if_min1(active_socket);
|
||||||
peer._socket = active_socket;
|
peer._iobuf.fd(active_socket);
|
||||||
peer._domain = _domain;
|
peer._domain = _domain;
|
||||||
peer._type = _type;
|
peer._type = _type;
|
||||||
peer.fd(active_socket);
|
peer._iobuf.fd(active_socket);
|
||||||
return peer;
|
return peer;
|
||||||
}
|
}
|
||||||
|
|
||||||
void socketstream::connect(const socket_address& host) {
|
void socketstream::connect(const socket_address& host) {
|
||||||
endpoint_from_address(host, &_sa);
|
endpoint_from_address(host, &_sa);
|
||||||
struct sockaddr* addr = reinterpret_cast<struct sockaddr*>(&_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 {
|
std::string socketstream::address() const {
|
||||||
@ -187,8 +187,3 @@ socket_address socketstream::address_from_endpoint(const struct sockaddr* endpoi
|
|||||||
}
|
}
|
||||||
return sa;
|
return sa;
|
||||||
}
|
}
|
||||||
|
|
||||||
void socketstream::socket(int sock) {
|
|
||||||
_socket = sock;
|
|
||||||
fd(sock);
|
|
||||||
}
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user