Skip to content

Commit

Permalink
Wesnothd Connection: clarified data queues' nature by wrapping them i…
Browse files Browse the repository at this point in the history
…n std::queue

I kept the underlying container an std::list like before.
  • Loading branch information
Vultraz committed Aug 5, 2019
1 parent 2051e7f commit 1e9d649
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
8 changes: 4 additions & 4 deletions src/wesnothd_connection.cpp
Expand Up @@ -182,7 +182,7 @@ void wesnothd_connection::send_data(const configr_of& request)
// TODO: should I capture a shared_ptr for this?
io_service_.post([this, buf_ptr]() {
DBG_NW << "In wesnothd_connection::send_data::lambda\n";
send_queue_.push_back(buf_ptr);
send_queue_.push(buf_ptr);

if(send_queue_.size() == 1) {
send();
Expand Down Expand Up @@ -248,7 +248,7 @@ void wesnothd_connection::handle_write(const boost::system::error_code& ec, std:
MPTEST_LOG;
DBG_NW << "Written " << bytes_transferred << " bytes.\n";

send_queue_.pop_front();
send_queue_.pop();

if(ec) {
{
Expand Down Expand Up @@ -332,7 +332,7 @@ void wesnothd_connection::handle_read(const boost::system::error_code& ec, std::

{
std::lock_guard<std::mutex> lock(recv_queue_mutex_);
recv_queue_.emplace_back(std::move(data));
recv_queue_.emplace(std::move(data));
}

recv();
Expand Down Expand Up @@ -395,7 +395,7 @@ bool wesnothd_connection::receive_data(config& result)
std::lock_guard<std::mutex> lock(recv_queue_mutex_);
if(!recv_queue_.empty()) {
result.swap(recv_queue_.front());
recv_queue_.pop_front();
recv_queue_.pop();
return true;
}
}
Expand Down
8 changes: 6 additions & 2 deletions src/wesnothd_connection.hpp
Expand Up @@ -40,6 +40,7 @@
#include <list>
#include <thread>
#include <mutex>
#include <queue>

class config;
enum class loading_stage;
Expand Down Expand Up @@ -159,8 +160,11 @@ class wesnothd_connection
void send();
void recv();

std::list<std::shared_ptr<boost::asio::streambuf>> send_queue_;
std::list<config> recv_queue_;
template<typename T>
using data_queue = std::queue<T, std::list<T>>;

data_queue<std::shared_ptr<boost::asio::streambuf>> send_queue_;
data_queue<config> recv_queue_;

std::mutex recv_queue_mutex_;

Expand Down

0 comments on commit 1e9d649

Please sign in to comment.