preCICE v3.1.1
Loading...
Searching...
No Matches
SocketSendQueue.cpp
Go to the documentation of this file.
1#include <algorithm>
2#include <boost/asio.hpp>
3#include <iosfwd>
4#include <new>
5#include <utility>
6
7#include "SocketSendQueue.hpp"
9#include "utils/assertion.hpp"
10
11namespace precice::com {
12namespace asio = boost::asio;
13
16{
17 PRECICE_ASSERT(_itemQueue.empty(), "The SocketSendQueue is not empty upon destruction. "
18 "Make sure it always outlives all the requests pushed onto it.");
19}
20
22 boost::asio::const_buffers_1 data,
23 std::function<void()> callback)
24{
26 _itemQueue.push_back({std::move(sock), std::move(data), std::move(callback)});
27 process(); // if queue was previously empty, start it now.
28}
29
31{
33 _ready = true;
34 process(); // if queue was previously empty, start it now.
35}
36
38{
39 if (!_ready || _itemQueue.empty()) {
40 return;
41 }
42
43 auto item = _itemQueue.front();
44 _itemQueue.pop_front();
45 _ready = false;
46 asio::async_write(*(item.sock),
47 item.data,
48 [item, this](boost::system::error_code const &, std::size_t) {
49 item.callback();
50 this->sendCompleted();
51 });
52}
53
54} // namespace precice::com
#define PRECICE_ASSERT(...)
Definition assertion.hpp:87
bool _ready
Is the queue allowed to start another asynchronous send?
void sendCompleted()
Notifies the queue that the last asynchronous send operation has completed.
std::mutex _queueMutex
The mutex protecting access to the queue.
std::deque< SendItem > _itemQueue
The queue, containing items to asynchronously send using boost.asio.
void dispatch(std::shared_ptr< Socket > sock, boost::asio::const_buffers_1 data, std::function< void()> callback)
Put data in the queue, start processing the queue.
~SocketSendQueue()
If items are left in the queue upon destruction, something went really wrong.
void process()
This method can be called arbitrarily many times, but enough times to ensure the queue makes progress...
contains the data communication abstraction layer.