summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/erebos/network.h2
-rw-r--r--src/network.cpp14
-rw-r--r--src/network.h6
3 files changed, 16 insertions, 6 deletions
diff --git a/include/erebos/network.h b/include/erebos/network.h
index 07ae586..b47efee 100644
--- a/include/erebos/network.h
+++ b/include/erebos/network.h
@@ -24,7 +24,7 @@ public:
const Head<LocalState> & localHead() const;
const Bhv<LocalState> & localState() const;
- const Identity & identity() const;
+ Identity identity() const;
template<class S> S & svc();
class PeerList & peerList() const;
diff --git a/src/network.cpp b/src/network.cpp
index 8d318f7..dafab67 100644
--- a/src/network.cpp
+++ b/src/network.cpp
@@ -48,8 +48,9 @@ const Bhv<LocalState> & Server::localState() const
return p->localState;
}
-const Identity & Server::identity() const
+Identity Server::identity() const
{
+ shared_lock lock(p->selfMutex);
return p->self;
}
@@ -293,7 +294,7 @@ shared_ptr<Server::Priv> Server::Priv::getptr()
void Server::Priv::doListen()
{
vector<uint8_t> buf, decrypted, *current;
- unique_lock<mutex> lock(dataMutex);
+ unique_lock lock(dataMutex);
for (; !finish; lock.lock()) {
sockaddr_in paddr;
@@ -340,7 +341,9 @@ void Server::Priv::doListen()
ReplyBuilder reply;
- scoped_lock<mutex> hlock(dataMutex);
+ scoped_lock hlock(dataMutex);
+ shared_lock slock(selfMutex);
+
handlePacket(peer, *header, reply);
peer.updateIdentity(reply);
peer.updateChannel(reply);
@@ -359,13 +362,14 @@ void Server::Priv::doListen()
void Server::Priv::doAnnounce()
{
- unique_lock<mutex> lock(dataMutex);
+ unique_lock lock(dataMutex);
auto lastAnnounce = steady_clock::now() - announceInterval;
while (!finish) {
auto now = steady_clock::now();
if (lastAnnounce + announceInterval < now) {
+ shared_lock slock(selfMutex);
TransportHeader header({
{ TransportHeader::Type::AnnounceSelf, *self.ref() }
});
@@ -578,6 +582,8 @@ void Server::Priv::handlePacket(Server::Peer & peer, const TransportHeader & hea
void Server::Priv::handleLocalHeadChange(const Head<LocalState> & head)
{
scoped_lock lock(dataMutex);
+ scoped_lock slock(selfMutex);
+
if (auto id = head->identity()) {
if (*id != self) {
self = *id;
diff --git a/src/network.h b/src/network.h
index c620cf6..5a656cb 100644
--- a/src/network.h
+++ b/src/network.h
@@ -6,6 +6,7 @@
#include <condition_variable>
#include <mutex>
+#include <shared_mutex>
#include <thread>
#include <vector>
@@ -15,6 +16,8 @@ using std::condition_variable;
using std::monostate;
using std::mutex;
using std::optional;
+using std::shared_lock;
+using std::shared_mutex;
using std::shared_ptr;
using std::string;
using std::thread;
@@ -162,8 +165,9 @@ struct Server::Priv
condition_variable announceCondvar;
bool finish = false;
+ shared_mutex selfMutex;
Identity self;
- Bhv<LocalState> localState;
+ const Bhv<LocalState> localState;
thread threadListen;
thread threadAnnounce;