Skip to content

Conversation

@gfgit
Copy link
Contributor

@gfgit gfgit commented Oct 5, 2025

Hello,
these are a first set of changes rebased on current upstream.

Mainly UDP discovery and JSON comments.
I tried to increase TCP send butter to 8192 but problem is still present and async_write() doesn't seem to report error codes in callback so maybe the issue is elsewhere?

After this, if you agree, I removed completely the use of OpenGL to get thicker lines and to draw other objects like signals.

Comment on lines 367 to 381
m_socketUDP.open(boost::asio::ip::udp::v4(), ec);
if(ec)
assert(false);

m_socketUDP.set_option(boost::asio::socket_base::reuse_address(true), ec);
if(ec)
assert(false);

m_socketUDP.bind(boost::asio::ip::udp::endpoint(boost::asio::ip::address_v4::any(), defaultPort), ec);
if(ec)
{
std::cout << "UDP cannot bind" << std::endl;
}

std::cout << ec.message() << " END" << std::endl << std::flush;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest we change the return type from void to std::error_code, the start can return an error if something does wrong :) Then the callee can check it and report a proper error :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need some help on this one...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If something fails return an error (the ec value), if the function succeeds return no error: return {};

In the start function the boost functions will return the error code, we can just return that if it is an error.

// some boost function with an ec param
if(ec)
{
  return ec;
}

The caller can check if the start() succeeds:

if(auto ec = sim.start())
{
  // error
}

Traintastic server uses this in the Train::acquire function. This is really powerful as you can also return the reason why it failed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok but the logic is run in the worker thread so returned error would be async.
What should I do?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

worker thread should exit on error, the main thread then needs to check if the simulator is started or has an error. Those variables are accessed by two threads, so best is to use std::atomic<>:

std::atomic<bool> m_started = false;
std::atomic<std::error_code> m_error;

When start() set m_started to true else set m_error.

The main thread then can check (m_started || m_error), after the startup one of them must be true.


m_acceptor.listen(5, ec);

m_socketUDP.open(boost::asio::ip::udp::v4(), ec);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to have discovery optional, we can add a bool discoverable = false parameter to the start function for that. In most cases discovery isn't needed I'd expect.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Now we should add console argument to turn it on/off. For now it's still forced on because it needs testing.
Also another argument for localhost only server.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, command line options to control those would be great.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we use boost to parse command line?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I see there is already a parseCommandLine() function in main.cpp
I'll use that!

@reinder
Copy link
Member

reinder commented Oct 5, 2025

@gfgit OpenGL can be removed, I mainly did it as an experiment, if we even wanna have 3D we can built another view :)

gfgit added 3 commits October 15, 2025 20:11
We receive some garbeage chars at end.
Use "starts with" instead of exact comparison.

- Send TCP port in big endian.

- Change UDP port, cannot be same as TCP port

- failed attempt to increase send buffer size
@gfgit
Copy link
Contributor Author

gfgit commented Oct 16, 2025 via email

@reinder
Copy link
Member

reinder commented Oct 22, 2025

@gfgit the handshake you added, is that a ping/keep alive?

@gfgit
Copy link
Contributor Author

gfgit commented Oct 23, 2025 via email

@gfgit
Copy link
Contributor Author

gfgit commented Nov 22, 2025 via email

@reinder
Copy link
Member

reinder commented Nov 25, 2025

@gfgit sorry lost a bit track of this one due to other work, can you have a look at the build issues? Seem to be a few warning that can be resolved easily.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants