aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/BootstrapNode.cpp5
-rw-r--r--src/DirectConnection.cpp30
-rw-r--r--src/IpAddress.cpp5
3 files changed, 36 insertions, 4 deletions
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();
+ }
}