Skip to content
This repository has been archived by the owner on Jan 6, 2020. It is now read-only.

Maid 22 bad alloc #14

Open
wants to merge 15 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions src/maidsafe/rudp/connection_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,9 @@ ConnectionManager::ConnectionManager(std::shared_ptr<Transport> transport,
ConnectionManager::~ConnectionManager() { Close(); }

void ConnectionManager::Close() {
{
std::lock_guard<std::mutex> lock(mutex_);
for (auto connection : connections_)
strand_.post(std::bind(&Connection::Close, connection));
}
// Ugly, but we must not reset dispatcher until he's done
while (multiplexer_->dispatcher_.use_count()) std::this_thread::yield();
multiplexer_->dispatcher_.SetConnectionManager(nullptr);
for (auto& connection : connections_)
strand_.post(std::bind(&Connection::Close, connection));
}

void ConnectionManager::Connect(const NodeId& peer_id, const Endpoint& peer_endpoint,
Expand Down
32 changes: 6 additions & 26 deletions src/maidsafe/rudp/core/dispatcher.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,45 +38,25 @@ namespace rudp {

namespace detail {

Dispatcher::Dispatcher() : use_count_(std::make_shared<int>()),
connection_manager_(nullptr) {}
Dispatcher::Dispatcher() : connection_manager_(nullptr) {}

void Dispatcher::SetConnectionManager(ConnectionManager *connection_manager) {
std::lock_guard<decltype(mutex_)> guard(mutex_);
connection_manager_ = std::move(connection_manager);
}

uint32_t Dispatcher::AddSocket(Socket* socket) {
auto in_use(use_count_);
ConnectionManager *connection_manager;
{
std::lock_guard<decltype(mutex_)> guard(mutex_);
connection_manager = connection_manager_;
}
return connection_manager ? connection_manager->AddSocket(socket) : 0;
return connection_manager_ ? connection_manager_->AddSocket(socket) : 0;
}

void Dispatcher::RemoveSocket(uint32_t id) {
auto in_use(use_count_);
ConnectionManager *connection_manager;
{
std::lock_guard<decltype(mutex_)> guard(mutex_);
connection_manager = connection_manager_;
}
if (connection_manager)
connection_manager->RemoveSocket(id);
if (connection_manager_)
connection_manager_->RemoveSocket(id);
}

void Dispatcher::HandleReceiveFrom(const asio::const_buffer& data,
const ip::udp::endpoint& endpoint) {
auto in_use(use_count_);
ConnectionManager* connection_manager;
{
std::lock_guard<decltype(mutex_)> guard(mutex_);
connection_manager = connection_manager_;
}
if (connection_manager) {
Socket* socket(connection_manager->GetSocket(data, endpoint));
if (connection_manager_) {
Socket* socket(connection_manager_->GetSocket(data, endpoint));
if (socket) {
socket->HandleReceiveFrom(data, endpoint);
}
Expand Down
4 changes: 0 additions & 4 deletions src/maidsafe/rudp/core/dispatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ class Dispatcher {

void SetConnectionManager(ConnectionManager* connection_manager);

size_t use_count() const { return use_count_.use_count()-1; }

// Add a socket. Returns a new unique id for the socket.
uint32_t AddSocket(Socket* socket);

Expand All @@ -59,8 +57,6 @@ class Dispatcher {
Dispatcher(const Dispatcher&);
Dispatcher& operator=(const Dispatcher&);

std::mutex mutex_;
std::shared_ptr<int> use_count_;
ConnectionManager* connection_manager_;
};

Expand Down
2 changes: 1 addition & 1 deletion src/maidsafe/rudp/parameters.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace maidsafe {

namespace rudp {

uint32_t Parameters::thread_count(2);
uint32_t Parameters::thread_count(1);
int Parameters::max_transports(10);
const uint32_t Parameters::maximum_segment_size(16);
uint32_t Parameters::default_window_size(4*Parameters::maximum_segment_size);
Expand Down
13 changes: 11 additions & 2 deletions src/maidsafe/rudp/transport.cc
Original file line number Diff line number Diff line change
Expand Up @@ -259,15 +259,24 @@ void Transport::Close() {
on_connection_added_ = nullptr;
on_connection_lost_ = nullptr;
}
if (connection_manager_)
connection_manager_->Close();
if (connection_manager_) {
std::promise<void> _done;
std::future<void> done=_done.get_future();
strand_.dispatch(std::bind(&Transport::DoClose, this, std::ref(_done)));
done.wait();
}
if (multiplexer_) {
strand_.post(std::bind(&Multiplexer::Close, multiplexer_));
while (IsValid(multiplexer_->external_endpoint()))
boost::this_thread::yield();
}
}

void Transport::DoClose(std::promise<void>& done) {
connection_manager_->Close();
done.set_value();
}

void Transport::Connect(const NodeId& peer_id, const EndpointPair& peer_endpoint_pair,
const std::string& validation_data) {
strand_.dispatch(std::bind(&Transport::DoConnect, shared_from_this(), peer_id, peer_endpoint_pair,
Expand Down
2 changes: 2 additions & 0 deletions src/maidsafe/rudp/transport.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ class Transport : public std::enable_shared_from_this<Transport> {
const boost::asio::ip::udp::endpoint& bootstrap_endpoint,
const boost::posix_time::time_duration& lifespan);
void DetectNatType(NodeId const& peer_id);

void DoClose(std::promise<void>& done);

void DoConnect(const NodeId& peer_id, const EndpointPair& peer_endpoint_pair,
const std::string& validation_data);
Expand Down