#ifndef CHILON_POLL_SOCKET_CLIENT_HPP
#define CHILON_POLL_SOCKET_CLIENT_HPP
#include <chilon/poll/pollable.hpp>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <exception>
namespace chilon { namespace poll {
struct could_not_create_socket : std::exception {};
/**
* @brief socket_client represents a stream for a single socket based client
*/
template <class Client>
struct socket_client : public pollable {
void tcp_create() {
fd_ = ::socket(PF_INET, SOCK_STREAM, 0);
fcntl(fd_, F_SETFL, O_NONBLOCK);
if (-1 == fd_) throw could_not_create_socket();
}
void tcp_close() {
shutdown(fd_, SHUT_RDWR);
fd_ = -1;
}
bool closed() const { return -1 == fd_; }
int fd() const { return fd_; }
socket_client() {}
socket_client(int const socket) : pollable(socket) {}
};
} }
#endif