Skip to content

Commit

Permalink
Fixed handling of asio::io_context
Browse files Browse the repository at this point in the history
  • Loading branch information
kosekmi committed Jan 24, 2025
1 parent 601a165 commit d53f8ad
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
12 changes: 8 additions & 4 deletions core/sdl/dreamconn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

void createDreamConnDevices(std::shared_ptr<DreamConn> dreamconn, bool gameStart);

static asio::error_code sendMsg(const MapleMsg& msg, asio::ip::tcp::iostream& stream, asio::serial_port& serial_handler, int dreamcastControllerType)
static asio::error_code sendMsg(const MapleMsg& msg, asio::ip::tcp::iostream& stream, asio::io_context& io_context, asio::serial_port& serial_handler, int dreamcastControllerType)
{
std::ostringstream s;
s.fill('0');
Expand Down Expand Up @@ -69,6 +69,9 @@ static asio::error_code sendMsg(const MapleMsg& msg, asio::ip::tcp::iostream& st
}
else if (dreamcastControllerType == TYPE_DREAMCASTCONTROLLERUSB)
{
io_context.run();
io_context.reset();

if (!serial_handler.is_open())
return asio::error::not_connected;
asio::async_write(serial_handler, asio::buffer(s.str()), asio::transfer_exactly(s.str().size()), [ &serial_handler](const asio::error_code& error, size_t bytes_transferred)
Expand Down Expand Up @@ -228,7 +231,7 @@ void DreamConn::connect()
usleep(500000);
#endif

serial_handler = asio::serial_port(io_service);
serial_handler = asio::serial_port(io_context);

// select first available serial device
std::string serial_device = getFirstSerialDevice();
Expand All @@ -255,7 +258,7 @@ void DreamConn::connect()
msg.originAP = bus << 6;
msg.setData(MFID_0_Input);

ec = sendMsg(msg, iostream, serial_handler, dreamcastControllerType);
ec = sendMsg(msg, iostream, io_context, serial_handler, dreamcastControllerType);
if (ec)
{
WARN_LOG(INPUT, "DreamcastController[%d] connection failed: %s", bus, ec.message().c_str());
Expand Down Expand Up @@ -300,6 +303,7 @@ void DreamConn::disconnect()
if (serial_handler.is_open())
serial_handler.cancel();
serial_handler.close();
io_context.stop();
}

maple_io_connected = false;
Expand All @@ -312,7 +316,7 @@ bool DreamConn::send(const MapleMsg& msg)
asio::error_code ec;

if (maple_io_connected)
ec = sendMsg(msg, iostream, serial_handler, dreamcastControllerType);
ec = sendMsg(msg, iostream, io_context, serial_handler, dreamcastControllerType);
else
return false;
if (ec) {
Expand Down
1 change: 0 additions & 1 deletion core/sdl/dreamconn.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ class DreamConn
#ifdef USE_DREAMCASTCONTROLLER
asio::ip::tcp::iostream iostream;
asio::io_context io_context;
asio::io_service io_service;
asio::serial_port serial_handler{io_context};
#endif
bool maple_io_connected;
Expand Down

0 comments on commit d53f8ad

Please sign in to comment.