Skip to content
Snippets Groups Projects
Verified Commit a38639b2 authored by Benny Baumann's avatar Benny Baumann
Browse files

Basic work on async socket interface

parent 1f70cee2
No related branches found
No related tags found
No related merge requests found
#pragma once
#include <unistd.h>
namespace rmrf::net {
class null_fd
{
public:
constexpr explicit null_fd() {}
constexpr explicit null_fd(int) {}
constexpr operator int() const { return -1; }
};
constexpr null_fd nullfd{};
class auto_fd
{
private:
int _fd;
public:
inline auto_fd(null_fd nfd = nullfd) noexcept : _fd{nfd} {}
explicit inline auto_fd(int fd) noexcept : _fd{fd} {}
inline auto_fd(auto_fd &&fd) noexcept : _fd{fd.release()} {}
inline auto_fd &operator=(auto_fd &&fd) noexcept {
reset(fd.release());
return *this;
}
auto_fd(const auto_fd &) = delete;
auto_fd &operator=(const auto_fd &) = delete;
inline ~auto_fd() noexcept {
reset();
}
inline int get() const noexcept { return _fd; }
int release() noexcept
{
int r(_fd);
_fd = -1;
return r;
}
// Close an open file descriptor. Reset the descriptor to -1.
inline void close() noexcept {
if (_fd >= 0)
{
::close(_fd);
// If fdclose() failed then no reason to expect it to succeed the next time.
_fd = -1;
}
}
inline void reset(int fd = -1) noexcept {
if (_fd >= 0)
close(); // Don't check for an error as not much we can do here.
_fd = fd;
}
};
inline bool operator==(const auto_fd &x, const auto_fd &y) {
return x.get() == y.get();
}
inline bool operator!=(const auto_fd &x, const auto_fd &y) {
return !(x == y);
}
inline bool operator==(const auto_fd &x, null_fd) {
return x.get() == -1;
}
inline bool operator!=(const auto_fd &x, null_fd y) {
return !(x == y);
}
}
#pragma once
#include <functional>
#include <memory>
#include <net/async_fd.hpp>
namespace rmrf::net::asio {
class async_server_socket : public std::enable_shared_from_this<async_server_socket> {
public:
typedef std::shared_ptr<async_server_socket> self_ptr_type;
typedef std::function<void(self_ptr_type&, const auto_fd &)> accept_handler_type;
typedef std::function<void(self_ptr_type&)> error_handler_type;
private:
auto_fd socket;
accept_handler_type on_accept;
error_handler_type on_error;
public:
async_server_socket(auto_fd&& fd) : socket(std::forward(fd)) {
// Add this socket to libev ...
}
~async_server_socket() {
// Remove this socket from libev ...
}
public:
accept_handler_type get_accept_handler() const {
return on_accept;
}
void set_accept_handler(const accept_handler_type& value) {
on_accept = value;
}
};
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment