diff options
Diffstat (limited to 'include/sibs')
-rw-r--r-- | include/sibs/BootstrapNode.hpp | 28 | ||||
-rw-r--r-- | include/sibs/DirectConnection.hpp | 22 | ||||
-rw-r--r-- | include/sibs/IpAddress.hpp | 35 |
3 files changed, 67 insertions, 18 deletions
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 <memory> #include <thread> #include <mutex> +#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<DirectConnectionPeer> connect(const Ipv4 &address, PubSubReceiveDataCallback receiveDataCallbackFunc); // Throws SendException on error void send(const std::shared_ptr<DirectConnectionPeer> &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 <stdexcept> +#include <string> +#ifndef WIN32 + #include <arpa/inet.h> + #include <netdb.h> +#else + #include <winsock2.h> + #include <ws2tcpip.h> +#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; + }; +} |