Skip to content

Commit

Permalink
Merge pull request #6576 from capital-G/boost-asio-context
Browse files Browse the repository at this point in the history
Boost 1.87 support
  • Loading branch information
dyfer authored Jan 1, 2025
2 parents e2c01d9 + e085858 commit 297ed7d
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 52 deletions.
2 changes: 1 addition & 1 deletion QtCollider/LanguageClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ void LangClient::customEvent(QEvent* e) {

case Event_SCRequest_Work:
QApplication::removePostedEvents(this, Event_SCRequest_Work);
mIoService.poll();
mIoContext.poll();
break;
case Event_SCRequest_Quit: {
int code = static_cast<SCRequestEvent*>(e)->data.toInt();
Expand Down
4 changes: 2 additions & 2 deletions lang/LangPrimSource/OSCData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -745,7 +745,7 @@ static PyrObject* ConvertReplyAddress(ReplyAddress* inReply) {
VMGlobals* g = gMainVMGlobals;
PyrObject* obj = instantiateObject(g->gc, s_netaddr->u.classobj, 2, true, false);
PyrSlot* slots = obj->slots;
SetInt(slots + 0, inReply->mAddress.to_v4().to_ulong());
SetInt(slots + 0, inReply->mAddress.to_v4().to_uint());
SetInt(slots + 1, inReply->mPort);
return obj;
}
Expand Down Expand Up @@ -934,7 +934,7 @@ void cleanup_OSC() {
#endif
}

extern boost::asio::io_service ioService;
extern boost::asio::io_context ioContext;

static int prGetHostByName(VMGlobals* g, int numArgsPushed) {
PyrSlot* a = g->sp;
Expand Down
4 changes: 2 additions & 2 deletions lang/LangPrimSource/PyrSerialPrim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
using boost::uint8_t;
using boost::asio::serial_port;

extern boost::asio::io_service ioService; // defined in SC_ComPort.cpp
extern boost::asio::io_context ioContext; // defined in SC_ComPort.cpp

/**
* \brief Serial port abstraction
Expand Down Expand Up @@ -104,7 +104,7 @@ class SerialPort {
*/
SerialPort(PyrObject* obj, const char* serialport, const Options& options):
m_obj(obj),
m_port(ioService, serialport),
m_port(ioContext, serialport),
m_options(options),
m_rxErrors(0) {
using namespace boost::asio;
Expand Down
21 changes: 11 additions & 10 deletions lang/LangPrimSource/SC_ComPort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,13 @@ void ProcessRawMessage(std::unique_ptr<char[]> inData, size_t inSize, ReplyAddre
//////////////////////////////////////////////////////////////////////////////////////////////////////////

SC_Thread gAsioThread;
boost::asio::io_service ioService;
boost::asio::io_context ioContext;


static void asioFunction() {
boost::asio::io_service::work work(ioService);
ioService.run();
boost::asio::executor_work_guard<boost::asio::io_context::executor_type> work =
boost::asio::make_work_guard(ioContext);
ioContext.run();
}

void startAsioThread() {
Expand All @@ -60,7 +61,7 @@ void startAsioThread() {
}

void stopAsioThread() {
ioService.stop();
ioContext.stop();
gAsioThread.join();
}

Expand Down Expand Up @@ -110,8 +111,8 @@ template <> struct MessageHandler<HandlerType::Raw> {

namespace Detail {

TCPConnection::TCPConnection(boost::asio::io_service& ioService, int portNum, HandlerType handlerType):
mSocket(ioService),
TCPConnection::TCPConnection(boost::asio::io_context& ioContext, int portNum, HandlerType handlerType):
mSocket(ioContext),
mOSCMsgLength(0),
mPortNum(portNum) {
initHandler(handlerType);
Expand Down Expand Up @@ -186,7 +187,7 @@ void TCPConnection::handleMsgReceived(const boost::system::error_code& error, si

namespace InPort {

UDP::UDP(int inPortNum, HandlerType handlerType, int portsToCheck): mPortNum(inPortNum), mUdpSocket(ioService) {
UDP::UDP(int inPortNum, HandlerType handlerType, int portsToCheck): mPortNum(inPortNum), mUdpSocket(ioContext) {
using namespace boost::asio;

BOOST_AUTO(protocol, ip::udp::v4());
Expand Down Expand Up @@ -271,7 +272,7 @@ UDPCustom::UDPCustom(int inPortNum, HandlerType handlerType): UDP(inPortNum, han
//////////////////////////////////////////////////////////////////////////////////////////////////////////

TCP::TCP(int inPortNum, int inMaxConnections, int inBacklog, HandlerType handlerType):
mAcceptor(ioService, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), inPortNum)),
mAcceptor(ioContext, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), inPortNum)),
mPortNum(inPortNum),
mHandlerType(handlerType) {
// FIXME: handle max connections
Expand All @@ -281,7 +282,7 @@ TCP::TCP(int inPortNum, int inMaxConnections, int inBacklog, HandlerType handler
}

void TCP::startAccept() {
const auto newConnection = std::make_shared<Detail::TCPConnection>(ioService, mPortNum, mHandlerType);
const auto newConnection = std::make_shared<Detail::TCPConnection>(ioContext, mPortNum, mHandlerType);

mAcceptor.async_accept(newConnection->getSocket(),
[this, newConnection](auto error) { handleAccept(newConnection, error); });
Expand All @@ -300,7 +301,7 @@ void TCP::handleAccept(Detail::TCPConnection::pointer newConnection, const boost
namespace OutPort {

TCP::TCP(unsigned long inAddress, int inPort, HandlerType handlerType, ClientNotifyFunc notifyFunc, void* clientData):
mSocket(ioService),
mSocket(ioContext),
mEndpoint(boost::asio::ip::address_v4(inAddress), inPort),
mClientNotifyFunc(notifyFunc),
mClientData(clientData) {
Expand Down
2 changes: 1 addition & 1 deletion lang/LangPrimSource/SC_ComPort.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class TCPConnection : public std::enable_shared_from_this<TCPConnection>, privat
public:
using pointer = std::shared_ptr<TCPConnection>;

TCPConnection(boost::asio::io_service& ioService, int portNum, HandlerType);
TCPConnection(boost::asio::io_context& ioContext, int portNum, HandlerType);

void start();
auto& getSocket() { return mSocket; }
Expand Down
25 changes: 13 additions & 12 deletions lang/LangSource/SC_TerminalClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ SC_TerminalClient::SC_TerminalClient(const char* name):
SC_LanguageClient(name),
mReturnCode(0),
mUseReadline(false),
mWork(mIoService),
mTimer(mIoService),
mWork(boost::asio::make_work_guard(mIoContext)),
mTimer(mIoContext),
#ifndef _WIN32
mStdIn(mInputService, STDIN_FILENO)
mStdIn(mInputContext, STDIN_FILENO)
#else
mStdIn(mInputService, GetStdHandle(STD_INPUT_HANDLE))
mStdIn(mInputContext, GetStdHandle(STD_INPUT_HANDLE))
#endif
{
}
Expand Down Expand Up @@ -360,19 +360,19 @@ void SC_TerminalClient::onLibraryStartup() {
void SC_TerminalClient::sendSignal(Signal sig) {
switch (sig) {
case sig_input:
mIoService.post(boost::bind(&SC_TerminalClient::interpretInput, this));
boost::asio::post(boost::bind(&SC_TerminalClient::interpretInput, this));
break;

case sig_recompile:
mIoService.post(boost::bind(&SC_TerminalClient::recompileLibrary, this));
boost::asio::post(boost::bind(&SC_TerminalClient::recompileLibrary, this));
break;

case sig_sched:
mIoService.post(boost::bind(&SC_TerminalClient::tick, this, boost::system::error_code()));
boost::asio::post(boost::bind(&SC_TerminalClient::tick, this, boost::system::error_code()));
break;

case sig_stop:
mIoService.post(boost::bind(&SC_TerminalClient::stopMain, this));
boost::asio::post(boost::bind(&SC_TerminalClient::stopMain, this));
break;
}
}
Expand Down Expand Up @@ -447,7 +447,7 @@ void SC_TerminalClient::tick(const boost::system::error_code& error) {
}
}

void SC_TerminalClient::commandLoop() { mIoService.run(); }
void SC_TerminalClient::commandLoop() { mIoContext.run(); }

void SC_TerminalClient::daemonLoop() { commandLoop(); }

Expand Down Expand Up @@ -614,8 +614,9 @@ void SC_TerminalClient::inputThreadFn() {

startInputRead();

boost::asio::io_service::work work(mInputService);
mInputService.run();
boost::asio::executor_work_guard<boost::asio::io_context::executor_type> work =
boost::asio::make_work_guard(mInputContext);
mInputContext.run();
}


Expand Down Expand Up @@ -662,7 +663,7 @@ void SC_TerminalClient::startInput() {
}

void SC_TerminalClient::endInput() {
mInputService.stop();
mInputContext.stop();
mStdIn.cancel();
#ifdef _WIN32
// Note this breaks Windows XP compatibility, since this function is only defined in Vista and later
Expand Down
8 changes: 4 additions & 4 deletions lang/LangSource/SC_TerminalClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class SCLANG_DLLEXPORT SC_TerminalClient : public SC_LanguageClient {
// NOTE: It may be called from any thread, and with interpreter locked.
virtual void sendSignal(Signal code);

void stop() { mIoService.stop(); }
void stop() { mIoContext.stop(); }

protected:
bool parseOptions(int& argc, char**& argv, Options& opt);
Expand Down Expand Up @@ -151,14 +151,14 @@ class SCLANG_DLLEXPORT SC_TerminalClient : public SC_LanguageClient {

// app-clock io service
protected:
boost::asio::io_service mIoService;
boost::asio::io_context mIoContext;

private:
boost::asio::io_service::work mWork;
boost::asio::executor_work_guard<boost::asio::io_context::executor_type> mWork;
boost::asio::basic_waitable_timer<std::chrono::system_clock> mTimer;

// input io service
boost::asio::io_service mInputService;
boost::asio::io_context mInputContext;
SC_Thread mInputThread;
void inputThreadFn();

Expand Down
21 changes: 11 additions & 10 deletions server/scsynth/SC_ComPort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ static bool UnrollOSCPacket(World* inWorld, int inSize, char* inData, OSC_Packet
/////////////////////////////////////////////////////////////////////////////////////////////////////////

SC_Thread gAsioThread;
boost::asio::io_service ioService;
boost::asio::io_context ioContext;

const int kTextBufSize = 65536;

Expand Down Expand Up @@ -252,12 +252,12 @@ class SC_UdpInPort {
mWorld(world),
mPortNum(inPortNum),
mbindTo(bindTo),
udpSocket(ioService) {
udpSocket(ioContext) {
using namespace boost::asio;
BOOST_AUTO(protocol, ip::udp::v4());
udpSocket.open(protocol);

udpSocket.bind(ip::udp::endpoint(boost::asio::ip::address::from_string(bindTo), inPortNum));
udpSocket.bind(ip::udp::endpoint(boost::asio::ip::make_address(bindTo), inPortNum));
if (inPortNum == 0)
mPortNum = udpSocket.local_endpoint().port();

Expand All @@ -282,9 +282,9 @@ class SC_TcpConnection : public boost::enable_shared_from_this<SC_TcpConnection>
typedef boost::shared_ptr<SC_TcpConnection> pointer;
boost::asio::ip::tcp::socket socket;

SC_TcpConnection(struct World* world, boost::asio::io_service& ioService, class SC_TcpInPort* parent):
SC_TcpConnection(struct World* world, boost::asio::io_context& ioContext, class SC_TcpInPort* parent):
mWorld(world),
socket(ioService),
socket(ioContext),
mParent(parent) {}

~SC_TcpConnection();
Expand Down Expand Up @@ -400,7 +400,7 @@ class SC_TcpInPort {
public:
SC_TcpInPort(struct World* world, const std::string& bindTo, int inPortNum, int inMaxConnections, int inBacklog):
mWorld(world),
acceptor(ioService, boost::asio::ip::tcp::endpoint(boost::asio::ip::address::from_string(bindTo), inPortNum)),
acceptor(ioContext, boost::asio::ip::tcp::endpoint(boost::asio::ip::make_address(bindTo), inPortNum)),
mAvailableConnections(inMaxConnections) {
// FIXME: backlog???

Expand All @@ -419,7 +419,7 @@ class SC_TcpInPort {
void startAccept() {
if (mAvailableConnections > 0) {
--mAvailableConnections;
SC_TcpConnection::pointer newConnection(new SC_TcpConnection(mWorld, ioService, this));
SC_TcpConnection::pointer newConnection(new SC_TcpConnection(mWorld, ioContext, this));

acceptor.async_accept(
newConnection->socket,
Expand Down Expand Up @@ -453,8 +453,9 @@ static void asioFunction() {
nova::thread_set_priority_rt(priority);
#endif

boost::asio::io_service::work work(ioService);
ioService.run();
boost::asio::executor_work_guard<boost::asio::io_context::executor_type> work =
boost::asio::make_work_guard(ioContext);
ioContext.run();
}

void startAsioThread() {
Expand All @@ -463,7 +464,7 @@ void startAsioThread() {
}

void stopAsioThread() {
ioService.stop();
ioContext.stop();
gAsioThread.join();
}

Expand Down
6 changes: 3 additions & 3 deletions server/supernova/sc/sc_osc_handler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class sc_notify_observers {
public:
typedef enum { no_error = 0, already_registered = -1, not_registered = -2 } error_code;

sc_notify_observers(boost::asio::io_service& io_service): udp_socket(io_service) {}
sc_notify_observers(boost::asio::io_context& io_context): udp_socket(io_context) {}

int add_observer(endpoint_ptr const& ep);
int remove_observer(endpoint_ptr const& ep);
Expand Down Expand Up @@ -186,8 +186,8 @@ class sc_osc_handler : private detail::network_thread, public sc_notify_observer

public:
sc_osc_handler(server_arguments const& args):
sc_notify_observers(detail::network_thread::io_service_),
tcp_acceptor_(detail::network_thread::io_service_),
sc_notify_observers(detail::network_thread::io_context_),
tcp_acceptor_(detail::network_thread::io_context_),
tcp_password_(args.server_password.size() ? args.server_password.c_str() : nullptr) {
if (!args.non_rt) {
if (args.tcp_port && !open_socket(IPPROTO_TCP, args.socket_address, args.tcp_port))
Expand Down
14 changes: 7 additions & 7 deletions server/supernova/utilities/osc_server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
# define BOOST_ASIO_HAS_STD_STRING_VIEW 1
#endif

#include <boost/asio/io_service.hpp>
#include <boost/asio/ip/udp.hpp>

#include "branch_hints.hpp"
Expand All @@ -56,29 +55,30 @@ class network_thread {
name_thread("Network Receive");

sem.post();
io_service::work work(io_service_);
io_service_.run();
boost::asio::executor_work_guard<boost::asio::io_context::executor_type> work =
boost::asio::make_work_guard(io_context_);
io_context_.run();
});
sem.wait();
}

~network_thread(void) {
if (!thread_.joinable())
return;
io_service_.stop();
io_context_.stop();
thread_.join();
}

io_service& get_io_service(void) { return io_service_; }
io_context& get_io_context(void) { return io_context_; }

void send_udp(const char* data, unsigned int size, udp::endpoint const& receiver) {
udp::socket socket(io_service_);
udp::socket socket(io_context_);
socket.open(udp::v4());
socket.send_to(boost::asio::buffer(data, size), receiver);
}

protected:
io_service io_service_;
io_context io_context_;

private:
semaphore sem;
Expand Down

0 comments on commit 297ed7d

Please sign in to comment.