From e6ed8f19771d23a2e3496eb8fd71187ce0acc615 Mon Sep 17 00:00:00 2001 From: dec05eba <0xdec05eba@gmail.com> Date: Wed, 6 Jun 2018 20:06:14 +0200 Subject: Add bootstrap class (server) --- include/sibs/BootstrapNode.hpp | 28 ++++++++++++++++++++++++++++ include/sibs/DirectConnection.hpp | 22 ++++------------------ include/sibs/IpAddress.hpp | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 18 deletions(-) create mode 100644 include/sibs/BootstrapNode.hpp create mode 100644 include/sibs/IpAddress.hpp (limited to 'include') diff --git a/include/sibs/BootstrapNode.hpp b/include/sibs/BootstrapNode.hpp new file mode 100644 index 0000000..eaf48eb --- /dev/null +++ b/include/sibs/BootstrapNode.hpp @@ -0,0 +1,28 @@ +#pragma once + +#include "DirectConnection.hpp" +#include "IpAddress.hpp" + +namespace sibs +{ + class BootstrapException : public std::runtime_error + { + public: + BootstrapException(const std::string &errMsg) : std::runtime_error(errMsg) {} + }; + + class BootstrapNode + { + DISABLE_COPY(BootstrapNode) + public: + // Throws BootstrapException on error + BootstrapNode(const Ipv4 &address); + ~BootstrapNode(); + private: + void acceptConnections(); + private: + DirectConnections connections; + int socket; + std::thread acceptConnectionsThread; + }; +} diff --git a/include/sibs/DirectConnection.hpp b/include/sibs/DirectConnection.hpp index 8e92137..c4431a5 100644 --- a/include/sibs/DirectConnection.hpp +++ b/include/sibs/DirectConnection.hpp @@ -7,19 +7,12 @@ #include #include #include +#include "IpAddress.hpp" #include "../types.hpp" #include "../utils.hpp" -struct addrinfo; - namespace sibs { - class InvalidAddressException : public std::runtime_error - { - public: - InvalidAddressException(const std::string &errMsg) : std::runtime_error(errMsg) {} - }; - class ConnectionException : public std::runtime_error { public: @@ -32,16 +25,6 @@ namespace sibs SendException(const std::string &errMsg) : std::runtime_error(errMsg) {} }; - class Ipv4 - { - DISABLE_COPY(Ipv4) - public: - // Throws InvalidAddressException on error - Ipv4(const char *ip, u16 port); - ~Ipv4(); - - struct addrinfo *address; - }; enum class PubSubConnectResult { @@ -61,6 +44,7 @@ namespace sibs class DirectConnections { DISABLE_COPY(DirectConnections) + friend class BootstrapNode; public: DirectConnections(u16 port = 27137); ~DirectConnections(); @@ -69,6 +53,8 @@ namespace sibs std::shared_ptr connect(const Ipv4 &address, PubSubReceiveDataCallback receiveDataCallbackFunc); // Throws SendException on error void send(const std::shared_ptr &peer, const void *data, const usize size); + protected: + int createSocket(const Ipv4 &addressToBind, bool rendezvous, bool reuseAddr); private: void receiveData(); bool receiveDataFromPeer(const int socket, char *output); diff --git a/include/sibs/IpAddress.hpp b/include/sibs/IpAddress.hpp new file mode 100644 index 0000000..a0fad75 --- /dev/null +++ b/include/sibs/IpAddress.hpp @@ -0,0 +1,35 @@ +#pragma once + +#include "../utils.hpp" +#include +#include +#ifndef WIN32 + #include + #include +#else + #include + #include +#endif + +namespace sibs +{ + class InvalidAddressException : public std::runtime_error + { + public: + InvalidAddressException(const std::string &errMsg) : std::runtime_error(errMsg) {} + }; + + class Ipv4 + { + DISABLE_COPY(Ipv4) + public: + // If @ip is nullptr, then bind to all available sockets (typical for servers) + // Throws InvalidAddressException on error. + Ipv4(const char *ip, unsigned short port); + + std::string getAddress() const; + unsigned short getPort() const; + + struct sockaddr_in address; + }; +} -- cgit v1.2.3