~widelands-dev/widelands/trunk

« back to all changes in this revision

Viewing changes to src/network/bufferedconnection.cc

  • Committer: The Widelands Bunnybot
  • Date: 2021-12-30 20:11:15 UTC
  • Revision ID: bunnybot@widelands.org-20211230201115-636kyp607bco11an
Replace Boost::Asio → Asio (#5164)

Co-authored-by: Tóth András <txa-dev@posteo.hu>

(by Noordfrees)
7841615f6a955b941058acc5d9deb98e6e1b2dc0

Show diffs side-by-side

added added

removed removed

Lines of Context:
97
97
        return ptr;
98
98
}
99
99
 
100
 
std::pair<std::unique_ptr<BufferedConnection>, boost::asio::ip::tcp::socket*>
 
100
std::pair<std::unique_ptr<BufferedConnection>, asio::ip::tcp::socket*>
101
101
BufferedConnection::create_unconnected() {
102
102
        std::unique_ptr<BufferedConnection> ptr(new BufferedConnection());
103
103
        assert(!ptr->is_connected());
114
114
}
115
115
 
116
116
void BufferedConnection::close() {
117
 
        boost::system::error_code ec;
118
 
        boost::asio::ip::tcp::endpoint remote = socket_.remote_endpoint(ec);
 
117
        std::error_code ec;
 
118
        asio::ip::tcp::endpoint remote = socket_.remote_endpoint(ec);
119
119
        if (!ec) {
120
120
                verb_log_info("[BufferedConnection] Closing network socket connected to %s:%i.",
121
121
                              remote.address().to_string().c_str(), remote.port());
140
140
        assert(!asio_thread_.joinable());
141
141
        // Close the socket
142
142
        if (socket_.is_open()) {
143
 
                socket_.shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec);
 
143
                socket_.shutdown(asio::ip::tcp::socket::shutdown_both, ec);
144
144
                socket_.close(ec);
145
145
        }
146
146
}
228
228
        // Start writing to the socket. This might block if the network buffer within
229
229
        // the operating system is currently full.
230
230
        // When done with sending, call the lambda method defined below
231
 
        boost::asio::async_write(
232
 
           socket_, boost::asio::buffer(nonempty_queue->front()),
 
231
        asio::async_write(
 
232
           socket_, asio::buffer(nonempty_queue->front()),
233
233
#ifndef NDEBUG
234
 
           [this, nonempty_queue](boost::system::error_code ec, std::size_t length) {
 
234
           [this, nonempty_queue](std::error_code ec, std::size_t length) {
235
235
#else
236
 
           [this, nonempty_queue](boost::system::error_code ec, std::size_t /*length*/) {
 
236
           [this, nonempty_queue](std::error_code ec, std::size_t /*length*/) {
237
237
#endif
238
238
                   std::unique_lock<std::mutex> lock2(mutex_send_);
239
239
                   currently_sending_ = false;
251
251
                                   log_err("[BufferedConnection] Error when sending packet to host (error %i: %s)\n",
252
252
                                           ec.value(), ec.message().c_str());
253
253
                                   log_err("[BufferedConnection] Closing socket\n");
254
 
                                   socket_.shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec);
 
254
                                   socket_.shutdown(asio::ip::tcp::socket::shutdown_both, ec);
255
255
                                   socket_.close();
256
256
                           }
257
257
                   }
266
266
        }
267
267
 
268
268
        socket_.async_read_some(
269
 
           boost::asio::buffer(asio_receive_buffer_, kNetworkBufferSize),
270
 
           [this](boost::system::error_code ec, std::size_t length) {
 
269
           asio::buffer(asio_receive_buffer_, kNetworkBufferSize),
 
270
           [this](std::error_code ec, std::size_t length) {
271
271
                   if (!ec) {
272
272
                           assert(length > 0);
273
273
                           assert(length <= kNetworkBufferSize);
284
284
                                   log_err("[BufferedConnection] Error when receiving data from host (error %i: %s)\n",
285
285
                                           ec.value(), ec.message().c_str());
286
286
                                   log_err("[BufferedConnection] Closing socket\n");
287
 
                                   socket_.shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec);
 
287
                                   socket_.shutdown(asio::ip::tcp::socket::shutdown_both, ec);
288
288
                                   socket_.close();
289
289
                           }
290
290
                   }
291
291
           });
292
292
}
293
293
 
294
 
void BufferedConnection::reduce_send_buffer(boost::asio::ip::tcp::socket& socket) {
 
294
void BufferedConnection::reduce_send_buffer(asio::ip::tcp::socket& socket) {
295
295
        // Reduce the size of the send buffer. This will result in (slightly) slower
296
296
        // file transfers but keeps the program responsive (e.g., chat messages are
297
297
        // displayed) while transmitting files
298
 
        boost::system::error_code ec;
299
 
        boost::asio::socket_base::send_buffer_size send_buffer_size;
 
298
        std::error_code ec;
 
299
        asio::socket_base::send_buffer_size send_buffer_size;
300
300
        socket.get_option(send_buffer_size, ec);
301
301
        if (!ec && send_buffer_size.value() > 20 * static_cast<int>(kNetworkBufferSize)) {
302
 
                const boost::asio::socket_base::send_buffer_size new_buffer_size(20 * kNetworkBufferSize);
 
302
                const asio::socket_base::send_buffer_size new_buffer_size(20 * kNetworkBufferSize);
303
303
                socket.set_option(new_buffer_size, ec);
304
304
                // Ignore error. When it fails, chat messages will lag while transmitting files,
305
305
                // but nothing really bad happens
312
312
BufferedConnection::BufferedConnection(const NetAddress& host)
313
313
   : socket_(io_service_), currently_sending_(false) {
314
314
 
315
 
        const boost::asio::ip::tcp::endpoint destination(host.ip, host.port);
 
315
        const asio::ip::tcp::endpoint destination(host.ip, host.port);
316
316
 
317
317
        verb_log_info("[BufferedConnection] Trying to connect to %s:%u ... ",
318
318
                      host.ip.to_string().c_str(), host.port);
319
 
        boost::system::error_code ec;
 
319
        std::error_code ec;
320
320
        socket_.connect(destination, ec);
321
321
        if (!ec && is_connected()) {
322
322
                verb_log_info("success.\n");