libscio/socketstream.cpp
2020-03-03 19:31:17 +01:00

87 lines
1.7 KiB
C++

//
// socketstream.cpp
// libscio
//
// Created by Bob Polis at 2020-03-02
// Copyright (c) 2020 SwiftCoder. All rights reserved.
//
#include "libscio.hpp"
#include <unistd.h>
using namespace sc::io;
socketstream::socketstream(int domain, int type)
: fdstream(), _domain(domain), _type(type) {
_socket = socket(_domain, _type, 0);
if (_socket == -1) {
throw std::runtime_error("socketstream: could not create socket");
}
_inbuf.fd(_socket);
_outbuf.fd(_socket);
}
socketstream::socketstream(socketstream&& other) {
_socket = other._socket;
_domain = other._domain;
_type = other._type;
other._socket = -1;
}
socketstream& socketstream::operator=(socketstream&& other) {
if (this != &other) {
_socket = other._socket;
_domain = other._domain;
_type = other._type;
other._socket = -1;
}
return *this;
}
socketstream::~socketstream() {
if (_socket != -1) {
close(_socket);
}
}
void socketstream::make_server(const socket_address& address) const {
// build sockaddr
// bind
// if stream: listen
}
socketstream socketstream::accept() const {
int passive_socket = ::accept(_socket, nullptr, 0);
if (passive_socket == -1) {
throw std::runtime_error("socketstream: could not accept");
}
socketstream peer;
peer._socket = passive_socket;
peer._domain = _domain;
peer._type = _type;
return peer;
}
void socketstream::connect(const socket_address& host) {
// build sockaddr
// connect
}
std::string socketstream::address() const {
return ""; // TODO
}
std::string socketstream::peer_address() const {
return ""; // TODO
}
struct sockaddr socketstream::endpoint_from_address(const socket_address& address) const {
}
socket_address socketstream::address_from_endpoint(const struct sockaddr& endpoint) const {
}