aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2018-10-16 05:23:12 +0200
committerdec05eba <dec05eba@protonmail.com>2020-08-18 22:56:48 +0200
commitcf5bcde77556847075e6cbdac9426892844ee643 (patch)
treea3bce8388280aa2453c669634427de776520b630
parentc47870421f189eb98fc66e912693d73fbd8477ee (diff)
Add more state functions
-rw-r--r--include/sibs/BootstrapConnection.hpp3
-rw-r--r--src/BootstrapConnection.cpp15
-rw-r--r--src/Socket.cpp6
3 files changed, 18 insertions, 6 deletions
diff --git a/include/sibs/BootstrapConnection.hpp b/include/sibs/BootstrapConnection.hpp
index 08af775..9e2dfff 100644
--- a/include/sibs/BootstrapConnection.hpp
+++ b/include/sibs/BootstrapConnection.hpp
@@ -35,13 +35,14 @@ namespace sibs
// Throws BootstrapConnectionException on error
BootstrapConnection(const Ipv4 &bootstrapAddress);
- // Throws PubsubKeyAlreadyListeningException if we are already listening on the key @pubsubKey
+ // If we are already listening on the key @pubsubKey then the callback function is overwritten
ListenHandle listen(const PubsubKey &pubsubKey, BoostrapConnectionListenCallbackFunc callbackFunc);
// Returns false if data is larger than 800kb.
// Note: @data is copied in this function.
// Note: You can't put data on a pubsubkey that you are not listening on. Call @listen first.
bool put(const PubsubKey &pubsubKey, const void *data, const usize size);
bool cancelListen(const ListenHandle &listener);
+ bool areWeListeningOnKey(const PubsubKey &pubsubKey);
std::vector<std::shared_ptr<DirectConnectionPeer>> getPeers();
private:
diff --git a/src/BootstrapConnection.cpp b/src/BootstrapConnection.cpp
index 0237a90..995fdcd 100644
--- a/src/BootstrapConnection.cpp
+++ b/src/BootstrapConnection.cpp
@@ -160,8 +160,13 @@ namespace sibs
{
{
std::lock_guard<std::recursive_mutex> lock(listenerCallbackFuncMutex);
- if(listenCallbackFuncs.find(pubsubKey) != listenCallbackFuncs.end())
- throw PubsubKeyAlreadyListeningException("");
+ auto it = listenCallbackFuncs.find(pubsubKey);
+ if(it != listenCallbackFuncs.end())
+ {
+ Log::warn("BootstrapConnection::listen called on existing listener, overwriting callback function");
+ it->second = callbackFunc;
+ return { pubsubKey };
+ }
listenCallbackFuncs[pubsubKey] = callbackFunc;
}
@@ -246,6 +251,12 @@ namespace sibs
return true;
}
+ bool BootstrapConnection::areWeListeningOnKey(const PubsubKey &pubsubKey)
+ {
+ std::lock_guard<std::recursive_mutex> lock(listenerCallbackFuncMutex);
+ return listenCallbackFuncs.find(pubsubKey) != listenCallbackFuncs.end();
+ }
+
std::vector<std::shared_ptr<DirectConnectionPeer>> BootstrapConnection::getPeers()
{
return connections.getPeers();
diff --git a/src/Socket.cpp b/src/Socket.cpp
index 9c8da69..a078cab 100644
--- a/src/Socket.cpp
+++ b/src/Socket.cpp
@@ -28,13 +28,13 @@ namespace sibs
{
eid = other.eid;
udtSocket = other.udtSocket;
- other.eid = 0;
- other.udtSocket = 0;
+ other.eid = -1;
+ other.udtSocket = -1;
}
Socket::~Socket()
{
- UDT::close(udtSocket);
UDT::epoll_remove_usock(eid, udtSocket);
+ UDT::close(udtSocket);
}
} \ No newline at end of file