Skip to content

Commit fc323cc

Browse files
authored
Added compatibility with asio 1.33 and up (#77)
Mostly replaced deprecated 'io_service' with 'io_context'. The deprecated API was removed with asio 1.33.
1 parent 1f08ffa commit fc323cc

File tree

4 files changed

+28
-28
lines changed

4 files changed

+28
-28
lines changed

fineftp-server/src/ftp_session.cpp

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,18 @@
4444
namespace fineftp
4545
{
4646

47-
FtpSession::FtpSession(asio::io_service& io_service, const UserDatabase& user_database, const std::function<void()>& completion_handler, std::ostream& output, std::ostream& error)
47+
FtpSession::FtpSession(asio::io_context& io_context, const UserDatabase& user_database, const std::function<void()>& completion_handler, std::ostream& output, std::ostream& error)
4848
: completion_handler_ (completion_handler)
4949
, user_database_ (user_database)
50-
, io_service_ (io_service)
51-
, command_strand_ (io_service)
52-
, command_socket_ (io_service)
50+
, io_context_ (io_context)
51+
, command_strand_ (io_context)
52+
, command_socket_ (io_context)
5353
, data_type_binary_ (false)
5454
, shutdown_requested_ (false)
5555
, ftp_working_directory_("/")
56-
, data_acceptor_ (io_service)
57-
, data_socket_strand_ (io_service)
58-
, timer_ (io_service)
56+
, data_acceptor_ (io_context)
57+
, data_socket_strand_ (io_context)
58+
, timer_ (io_context)
5959
, output_(output)
6060
, error_(error)
6161
{
@@ -96,7 +96,7 @@ namespace fineftp
9696
command_socket_.set_option(asio::ip::tcp::no_delay(true), ec);
9797
if (ec) error_ << "Unable to set socket option tcp::no_delay: " << ec.message() << std::endl;
9898

99-
command_strand_.post([me = shared_from_this()]() { me->readFtpCommand(); });
99+
asio::post(command_strand_, [me = shared_from_this()]() { me->readFtpCommand(); });
100100
sendFtpMessage(FtpMessage(FtpReplyCode::SERVICE_READY_FOR_NEW_USER, "Welcome to fineFTP Server"));
101101
}
102102

@@ -116,7 +116,7 @@ namespace fineftp
116116

117117
void FtpSession::sendRawFtpMessage(const std::string& raw_message)
118118
{
119-
command_strand_.post([me = shared_from_this(), raw_message]()
119+
asio::post(command_strand_, [me = shared_from_this(), raw_message]()
120120
{
121121
const bool write_in_progress = !me->command_output_queue_.empty();
122122
me->command_output_queue_.push_back(raw_message);
@@ -188,7 +188,7 @@ namespace fineftp
188188
me->data_acceptor_.close(ec_);
189189
}
190190

191-
me->data_socket_strand_.post([me]()
191+
asio::post(me->data_socket_strand_, [me]()
192192
{
193193
auto data_socket = me->data_socket_weakptr_.lock();
194194
if (data_socket)
@@ -449,7 +449,7 @@ namespace fineftp
449449
}
450450
{
451451
asio::error_code ec;
452-
data_acceptor_.listen(asio::socket_base::max_connections, ec);
452+
data_acceptor_.listen(asio::socket_base::max_listen_connections, ec);
453453
if (ec)
454454
{
455455
error_ << "Error listening on data acceptor: " << ec.message() << std::endl;
@@ -1205,7 +1205,7 @@ namespace fineftp
12051205

12061206
void FtpSession::sendDirectoryListing(const std::map<std::string, Filesystem::FileStatus>& directory_content)
12071207
{
1208-
auto data_socket = std::make_shared<asio::ip::tcp::socket>(io_service_);
1208+
auto data_socket = std::make_shared<asio::ip::tcp::socket>(io_context_);
12091209

12101210
data_acceptor_.async_accept(*data_socket
12111211
, data_socket_strand_.wrap([data_socket, directory_content, me = shared_from_this()](auto ec)
@@ -1248,7 +1248,7 @@ namespace fineftp
12481248

12491249
void FtpSession::sendNameList(const std::map<std::string, Filesystem::FileStatus>& directory_content)
12501250
{
1251-
auto data_socket = std::make_shared<asio::ip::tcp::socket>(io_service_);
1251+
auto data_socket = std::make_shared<asio::ip::tcp::socket>(io_context_);
12521252

12531253
data_acceptor_.async_accept(*data_socket
12541254
, data_socket_strand_.wrap([data_socket, directory_content, me = shared_from_this()](auto ec)
@@ -1283,7 +1283,7 @@ namespace fineftp
12831283

12841284
void FtpSession::sendFile(const std::shared_ptr<ReadableFile>& file)
12851285
{
1286-
auto data_socket = std::make_shared<asio::ip::tcp::socket>(io_service_);
1286+
auto data_socket = std::make_shared<asio::ip::tcp::socket>(io_context_);
12871287

12881288
data_acceptor_.async_accept(*data_socket
12891289
, data_socket_strand_.wrap([data_socket, file, me = shared_from_this()](auto ec)
@@ -1352,7 +1352,7 @@ namespace fineftp
13521352

13531353
void FtpSession::addDataToBufferAndSend(const std::shared_ptr<std::vector<char>>& data, const std::shared_ptr<asio::ip::tcp::socket>& data_socket)
13541354
{
1355-
data_socket_strand_.post([me = shared_from_this(), data, data_socket]()
1355+
asio::post(data_socket_strand_, [me = shared_from_this(), data, data_socket]()
13561356
{
13571357
const bool write_in_progress = (!me->data_buffer_.empty());
13581358

@@ -1367,7 +1367,7 @@ namespace fineftp
13671367

13681368
void FtpSession::writeDataToSocket(const std::shared_ptr<asio::ip::tcp::socket>& data_socket)
13691369
{
1370-
data_socket_strand_.post(
1370+
asio::post(data_socket_strand_,
13711371
[me = shared_from_this(), data_socket]()
13721372
{
13731373
auto data = me->data_buffer_.front();
@@ -1417,7 +1417,7 @@ namespace fineftp
14171417

14181418
void FtpSession::receiveFile(const std::shared_ptr<WriteableFile>& file)
14191419
{
1420-
auto data_socket = std::make_shared<asio::ip::tcp::socket>(io_service_);
1420+
auto data_socket = std::make_shared<asio::ip::tcp::socket>(io_context_);
14211421

14221422
data_acceptor_.async_accept(*data_socket
14231423
, data_socket_strand_.wrap([data_socket, file, me = shared_from_this()](auto ec)
@@ -1469,7 +1469,7 @@ namespace fineftp
14691469

14701470
void FtpSession::endDataReceiving(const std::shared_ptr<WriteableFile>& file, const std::shared_ptr<asio::ip::tcp::socket>& data_socket)
14711471
{
1472-
data_socket_strand_.post([me = shared_from_this(), file, data_socket]()
1472+
asio::post(data_socket_strand_, [me = shared_from_this(), file, data_socket]()
14731473
{
14741474
file->close();
14751475
me->sendFtpMessage(FtpReplyCode::CLOSING_DATA_CONNECTION, "Done");

fineftp-server/src/ftp_session.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ namespace fineftp
3333
// Public API
3434
////////////////////////////////////////////////////////
3535
public:
36-
FtpSession(asio::io_service& io_service, const UserDatabase& user_database, const std::function<void()>& completion_handler, std::ostream& output, std::ostream& error);
36+
FtpSession(asio::io_context& io_context, const UserDatabase& user_database, const std::function<void()>& completion_handler, std::ostream& output, std::ostream& error);
3737

3838
// Copy (disabled, as we are inheriting from shared_from_this)
3939
FtpSession(const FtpSession&) = delete;
@@ -182,11 +182,11 @@ namespace fineftp
182182
std::shared_ptr<FtpUser> logged_in_user_;
183183

184184
// "Global" io service
185-
asio::io_service& io_service_;
185+
asio::io_context& io_context_;
186186

187187
// Command Socket.
188188
// Note that the command_strand_ is used to serialize access to all of the 9 member variables following it.
189-
asio::io_service::strand command_strand_;
189+
asio::io_context::strand command_strand_;
190190
asio::ip::tcp::socket command_socket_;
191191
asio::streambuf command_input_stream_;
192192
std::deque<std::string> command_output_queue_;
@@ -204,7 +204,7 @@ namespace fineftp
204204
asio::ip::tcp::acceptor data_acceptor_;
205205

206206
// Note that the data_socket_strand_ is used to serialize access to the 2 member variables following it.
207-
asio::io_service::strand data_socket_strand_;
207+
asio::io_context::strand data_socket_strand_;
208208
std::weak_ptr<asio::ip::tcp::socket> data_socket_weakptr_;
209209
std::deque<std::shared_ptr<std::vector<char>>> data_buffer_;
210210

fineftp-server/src/server_impl.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ namespace fineftp
2020
: ftp_users_ (output, error)
2121
, port_ (port)
2222
, address_ (address)
23-
, acceptor_ (io_service_)
23+
, acceptor_ (io_context_)
2424
, open_connection_count_(0)
2525
, output_ (output)
2626
, error_ (error)
@@ -43,7 +43,7 @@ namespace fineftp
4343

4444
bool FtpServerImpl::start(size_t thread_count)
4545
{
46-
auto ftp_session = std::make_shared<FtpSession>(io_service_, ftp_users_, [this]() { open_connection_count_--; }, output_, error_);
46+
auto ftp_session = std::make_shared<FtpSession>(io_context_, ftp_users_, [this]() { open_connection_count_--; }, output_, error_);
4747

4848
// set up the acceptor to listen on the tcp port
4949
asio::error_code make_address_ec;
@@ -108,15 +108,15 @@ namespace fineftp
108108

109109
for (size_t i = 0; i < thread_count; i++)
110110
{
111-
thread_pool_.emplace_back([this] {io_service_.run(); });
111+
thread_pool_.emplace_back([this] {io_context_.run(); });
112112
}
113113

114114
return true;
115115
}
116116

117117
void FtpServerImpl::stop()
118118
{
119-
io_service_.stop();
119+
io_context_.stop();
120120
for (std::thread& thread : thread_pool_)
121121
{
122122
thread.join();
@@ -140,7 +140,7 @@ namespace fineftp
140140

141141
ftp_session->start();
142142

143-
auto new_session = std::make_shared<FtpSession>(io_service_, ftp_users_, [this]() { open_connection_count_--; }, output_, error_);
143+
auto new_session = std::make_shared<FtpSession>(io_context_, ftp_users_, [this]() { open_connection_count_--; }, output_, error_);
144144

145145
acceptor_.async_accept(new_session->getSocket()
146146
, [this, new_session](auto ec)

fineftp-server/src/server_impl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ namespace fineftp
5555
const std::string address_;
5656

5757
std::vector<std::thread> thread_pool_;
58-
asio::io_service io_service_;
58+
asio::io_context io_context_;
5959
asio::ip::tcp::acceptor acceptor_;
6060

6161
std::atomic<int> open_connection_count_;

0 commit comments

Comments
 (0)