aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2018-10-14 03:36:47 +0200
committerdec05eba <dec05eba@protonmail.com>2020-08-18 22:56:48 +0200
commitfdbcee4063db89a8ba49cc5c2367376884f4b9d4 (patch)
treef1255d7066bf223601e81d8f18f0c2d541eddaee
parent10b5eab3e2ba705b73ce3d8b7d8c623ba09bfc82 (diff)
Another test
-rw-r--r--include/sibs/DirectConnection.hpp2
-rw-r--r--include/sibs/IpAddress.hpp2
-rw-r--r--src/BootstrapNode.cpp5
-rw-r--r--src/DirectConnection.cpp30
-rw-r--r--src/IpAddress.cpp5
5 files changed, 39 insertions, 5 deletions
diff --git a/include/sibs/DirectConnection.hpp b/include/sibs/DirectConnection.hpp
index 62d15ad..4b5c67f 100644
--- a/include/sibs/DirectConnection.hpp
+++ b/include/sibs/DirectConnection.hpp
@@ -44,7 +44,7 @@ namespace sibs
DISABLE_COPY(DirectConnections)
friend class BootstrapNode;
public:
- DirectConnections(u16 port = 27137);
+ DirectConnections(u16 port = 0);
~DirectConnections();
// Throws ConnectionException on error
diff --git a/include/sibs/IpAddress.hpp b/include/sibs/IpAddress.hpp
index 7b757f4..c3b43c4 100644
--- a/include/sibs/IpAddress.hpp
+++ b/include/sibs/IpAddress.hpp
@@ -30,6 +30,8 @@ namespace sibs
std::string getAddress() const;
unsigned short getPort() const;
+
+ bool operator == (const Ipv4 &other) const;
struct sockaddr_in address;
};
diff --git a/src/BootstrapNode.cpp b/src/BootstrapNode.cpp
index 9edf008..e62e48d 100644
--- a/src/BootstrapNode.cpp
+++ b/src/BootstrapNode.cpp
@@ -14,6 +14,7 @@
namespace sibs
{
BootstrapNode::BootstrapNode(const Ipv4 &address) :
+ connections(27130),
socket(connections.createSocket(address, false, true))
{
if(UDT::listen(socket, 10) == UDT::ERROR)
@@ -66,7 +67,7 @@ namespace sibs
connections.peersMutex.unlock();
}
}
-
+
void BootstrapNode::peerSubscribe(std::shared_ptr<DirectConnectionPeer> newPeer, const void *data, const usize size)
{
Log::debug("BootstrapNode: Received peer subscribe from (ip: %s, port: %d)", newPeer->address.getAddress().c_str(), newPeer->address.getPort());
@@ -77,7 +78,7 @@ namespace sibs
auto &peers = subscribedPeers[pubsubKey];
for(auto &peer : peers)
{
- if(peer->address.address.sin_addr.s_addr == newPeer->address.address.sin_addr.s_addr)
+ if(peer->address == newPeer->address)
return;
}
diff --git a/src/DirectConnection.cpp b/src/DirectConnection.cpp
index 4a2c829..ec597de 100644
--- a/src/DirectConnection.cpp
+++ b/src/DirectConnection.cpp
@@ -2,6 +2,7 @@
#include "../include/Log.hpp"
#include <cstdio>
#include <cstring>
+#include <random>
#ifndef WIN32
#include <arpa/inet.h>
@@ -14,11 +15,20 @@
namespace sibs
{
+ static std::random_device rd;
+ static std::mt19937 gen(rd());
+
+ static u32 generateRandomNumber(u32 start, u32 end)
+ {
+ std::uniform_int_distribution<> dis(start, end);
+ return dis(gen);
+ }
+
// Max received data size allowed when receiving regular data, receive data as file to receive more data
const int MAX_RECEIVED_DATA_SIZE = 1024 * 1024 * 1; // 1Mb
DirectConnections::DirectConnections(u16 _port) :
- port(_port),
+ port(_port == 0 ? (u16)generateRandomNumber(2000, 32000) : _port),
alive(true)
{
UDT::startup();
@@ -80,7 +90,23 @@ namespace sibs
void DirectConnections::connect(const Ipv4 &address, PubSubConnectCallback connectCallbackFunc, PubSubReceiveDataCallback receiveDataCallbackFunc)
{
- connect(address, true, true, connectCallbackFunc, receiveDataCallbackFunc, true);
+ Ipv4 myIp(nullptr, port);
+
+ UDTSOCKET client = UDT::socket(AF_INET, SOCK_STREAM, 0);
+ bool rendezvous = true;
+ UDT::setsockopt(client, 0, UDT_RENDEZVOUS, &rendezvous, sizeof(bool));
+
+ if (UDT::ERROR == UDT::bind(client, (sockaddr*)&myIp.address, sizeof(myIp.address))) {
+ printf("bind error: %s\n", UDT::getlasterror_desc());
+ exit(2);
+ }
+
+ if (UDT::ERROR == UDT::connect(client, (sockaddr*)&address.address, sizeof(address.address))) {
+ printf("connect error: %s\n", UDT::getlasterror_desc());
+ exit(3);
+ }
+ printf("connected to peer!\n");
+ //connect(address, true, true, connectCallbackFunc, receiveDataCallbackFunc, true);
}
void DirectConnections::connect(const Ipv4 &address, bool rendezvous, bool reuseAddr, PubSubConnectCallback connectCallbackFunc, PubSubReceiveDataCallback receiveDataCallbackFunc, bool bind)
diff --git a/src/IpAddress.cpp b/src/IpAddress.cpp
index 38f758d..c90c924 100644
--- a/src/IpAddress.cpp
+++ b/src/IpAddress.cpp
@@ -53,4 +53,9 @@ namespace sibs
{
return ntohs(address.sin_port);
}
+
+ bool Ipv4::operator == (const Ipv4 &other) const
+ {
+ return address.sin_addr.s_addr == other.address.sin_addr.s_addr && getPort() == other.getPort();
+ }
}