Added IPv6 support, added option to disable IPv6 (IPv4-Only mode)
This commit is contained in:
@@ -21,9 +21,39 @@ namespace ColumnLynx::Net::TCP {
|
||||
|
||||
class TCPServer {
|
||||
public:
|
||||
TCPServer(asio::io_context& ioContext, uint16_t port, Utils::LibSodiumWrapper* sodiumWrapper, bool* hostRunning)
|
||||
: mIoContext(ioContext), mAcceptor(ioContext, asio::ip::tcp::endpoint(asio::ip::tcp::v4(), port)), mSodiumWrapper(sodiumWrapper), mHostRunning(hostRunning)
|
||||
TCPServer(asio::io_context& ioContext,
|
||||
uint16_t port,
|
||||
Utils::LibSodiumWrapper* sodiumWrapper,
|
||||
bool* hostRunning, bool ipv4Only = false)
|
||||
: mIoContext(ioContext),
|
||||
mAcceptor(ioContext),
|
||||
mSodiumWrapper(sodiumWrapper),
|
||||
mHostRunning(hostRunning)
|
||||
{
|
||||
asio::error_code ec;
|
||||
|
||||
if (!ipv4Only) {
|
||||
// Try IPv6 first (dual-stack check)
|
||||
asio::ip::tcp::endpoint endpoint_v6(asio::ip::tcp::v6(), port);
|
||||
mAcceptor.open(endpoint_v6.protocol(), ec);
|
||||
if (!ec) {
|
||||
mAcceptor.set_option(asio::ip::v6_only(false), ec); // Allow dual-stack if possible
|
||||
mAcceptor.bind(endpoint_v6, ec);
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback to IPv4 if anything failed
|
||||
if (ec || ipv4Only) {
|
||||
Utils::warn("TCP: IPv6 unavailable (" + ec.message() + "), falling back to IPv4 only");
|
||||
|
||||
asio::ip::tcp::endpoint endpoint_v4(asio::ip::tcp::v4(), port);
|
||||
mAcceptor.close(); // ensure clean state
|
||||
mAcceptor.open(endpoint_v4.protocol());
|
||||
mAcceptor.bind(endpoint_v4);
|
||||
}
|
||||
|
||||
// Start listening
|
||||
mAcceptor.listen();
|
||||
Utils::log("Started TCP server on port " + std::to_string(port));
|
||||
mStartAccept();
|
||||
}
|
||||
|
||||
@@ -12,9 +12,31 @@
|
||||
namespace ColumnLynx::Net::UDP {
|
||||
class UDPServer {
|
||||
public:
|
||||
UDPServer(asio::io_context& ioContext, uint16_t port, bool* hostRunning)
|
||||
: mSocket(ioContext, asio::ip::udp::endpoint(asio::ip::udp::v4(), port)), mHostRunning(hostRunning)
|
||||
UDPServer(asio::io_context& ioContext, uint16_t port, bool* hostRunning, bool ipv4Only = false)
|
||||
: mSocket(ioContext), mHostRunning(hostRunning)
|
||||
{
|
||||
asio::error_code ec;
|
||||
|
||||
if (!ipv4Only) {
|
||||
// Try IPv6 first (dual-stack check)
|
||||
asio::ip::udp::endpoint endpoint_v6(asio::ip::udp::v6(), port);
|
||||
mSocket.open(endpoint_v6.protocol(), ec);
|
||||
if (!ec) {
|
||||
mSocket.set_option(asio::ip::v6_only(false), ec); // Allow dual-stack if possible
|
||||
mSocket.bind(endpoint_v6, ec);
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback to IPv4 if anything failed
|
||||
if (ec || ipv4Only) {
|
||||
Utils::warn("UDP: IPv6 unavailable (" + ec.message() + "), falling back to IPv4 only");
|
||||
|
||||
asio::ip::udp::endpoint endpoint_v4(asio::ip::udp::v4(), port);
|
||||
mSocket.close(); // ensure clean state
|
||||
mSocket.open(endpoint_v4.protocol());
|
||||
mSocket.bind(endpoint_v4);
|
||||
}
|
||||
|
||||
Utils::log("Started UDP server on port " + std::to_string(port));
|
||||
mStartReceive();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user