From ef808e4f3778610bee5c5ccc5995f9a6056802f5 Mon Sep 17 00:00:00 2001 From: James Packer Date: Mon, 8 Jun 2026 19:22:17 +0100 Subject: [PATCH 01/10] Check that address has resolved before using --- src/NetworkPrimary.cpp | 50 ++++++++++++++++++---------------- src/multiplayerHub/Network.cpp | 42 +++++++++++++++------------- 2 files changed, 50 insertions(+), 42 deletions(-) diff --git a/src/NetworkPrimary.cpp b/src/NetworkPrimary.cpp index 26bdcf007..a4ce614e1 100644 --- a/src/NetworkPrimary.cpp +++ b/src/NetworkPrimary.cpp @@ -112,30 +112,34 @@ void NetworkPrimary::connectToServer(std::string hostnames) //std::cout << "In NetworkPrimary, trying to set up to " << address.port << std::endl; /* Connect to some.server.net:18304. */ - enet_address_set_host (& address, thisHostname.c_str()); - - /* Initiate the connection, allocating the maximum number of channels. */ - peer = enet_host_connect (client, & address, ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT, 0); - //Note we don't store peer pointer, as we broadcast to all connected peers. - if (peer == NULL) - { - std::cerr << "No available peers for initiating an ENet connection." << std::endl; - enet_deinitialize(); - exit(EXIT_FAILURE); - } - /* Wait up to 1 second for the connection attempt to succeed. */ - if (enet_host_service (client, & event, 1000) > 0 && event.type == ENET_EVENT_TYPE_CONNECT) { - //std::string logMessage = "ENet connection succeeded to: "; - //logMessage.append(thisHostname); - device->getLogger()->log("ENet connection succeeded to:"); - device->getLogger()->log(thisHostname.c_str()); + if (enet_address_set_host (& address, thisHostname.c_str()) == 0) { + + /* Initiate the connection, allocating the maximum number of channels. */ + peer = enet_host_connect (client, & address, ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT, 0); + //Note we don't store peer pointer, as we broadcast to all connected peers. + if (peer == NULL) + { + std::cerr << "No available peers for initiating an ENet connection." << std::endl; + enet_deinitialize(); + exit(EXIT_FAILURE); + } + /* Wait up to 1 second for the connection attempt to succeed. */ + if (enet_host_service (client, & event, 1000) > 0 && event.type == ENET_EVENT_TYPE_CONNECT) { + //std::string logMessage = "ENet connection succeeded to: "; + //logMessage.append(thisHostname); + device->getLogger()->log("ENet connection succeeded to:"); + device->getLogger()->log(thisHostname.c_str()); + } else { + /* Either the 1 second is up or a disconnect event was */ + /* received. Reset the peer in the event the 1 second */ + /* had run out without any significant event. */ + enet_peer_reset (peer); + device->getLogger()->log("ENet connection failed to:"); + device->getLogger()->log(thisHostname.c_str()); + } } else { - /* Either the 1 second is up or a disconnect event was */ - /* received. Reset the peer in the event the 1 second */ - /* had run out without any significant event. */ - enet_peer_reset (peer); - device->getLogger()->log("ENet connection failed to:"); - device->getLogger()->log(thisHostname.c_str()); + // Failed to look up address with enet_address_set_host + std::cout << "ENet could not resolve hostname:" << thisHostname << std::endl; } } } diff --git a/src/multiplayerHub/Network.cpp b/src/multiplayerHub/Network.cpp index 12b938b95..a84eb0548 100644 --- a/src/multiplayerHub/Network.cpp +++ b/src/multiplayerHub/Network.cpp @@ -97,28 +97,32 @@ void Network::connectToServer(std::string hostnames) } } - enet_address_set_host (& address, thisHostname.c_str()); + if (enet_address_set_host (& address, thisHostname.c_str()) == 0) { - /* Initiate the connection, allocating the maximum number of channels. */ - peer = enet_host_connect (client, & address, ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT, 0); + /* Initiate the connection, allocating the maximum number of channels. */ + peer = enet_host_connect (client, & address, ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT, 0); - if (peer == NULL) - { - std::cout << "No available peers for initiating an ENet connection." << std::endl; - exit (EXIT_FAILURE); - } - /* Wait up to 1 second for the connection attempt to succeed. */ - if (enet_host_service (client, & event, 1000) > 0 && event.type == ENET_EVENT_TYPE_CONNECT) { - std::cout << "ENet connection succeeded to: " << thisHostname << std::endl; - //Store peer, and initialise the vector of latest strings received - peers.push_back(peer); - latestMessageFromPeer.push_back(""); + if (peer == NULL) + { + std::cout << "No available peers for initiating an ENet connection." << std::endl; + //exit (EXIT_FAILURE); + } + /* Wait up to 1 second for the connection attempt to succeed. */ + if (enet_host_service (client, & event, 1000) > 0 && event.type == ENET_EVENT_TYPE_CONNECT) { + std::cout << "ENet connection succeeded to: " << thisHostname << std::endl; + //Store peer, and initialise the vector of latest strings received + peers.push_back(peer); + latestMessageFromPeer.push_back(""); + } else { + /* Either the 1 second is up or a disconnect event was */ + /* received. Reset the peer in the event the 1 second */ + /* had run out without any significant event. */ + enet_peer_reset (peer); + std::cout << "ENet connection failed to:" << thisHostname << std::endl; + } } else { - /* Either the 1 second is up or a disconnect event was */ - /* received. Reset the peer in the event the 1 second */ - /* had run out without any significant event. */ - enet_peer_reset (peer); - std::cout << "ENet connection failed to:" << thisHostname << std::endl; + // Failed to look up address with enet_address_set_host + std::cout << "ENet could not resolve hostname:" << thisHostname << std::endl; } } } From 6ffb48e5cd88b80c4ad9069cbfc9fb4d7dc5eb49 Mon Sep 17 00:00:00 2001 From: James Packer Date: Mon, 8 Jun 2026 19:26:10 +0100 Subject: [PATCH 02/10] Increase timeout on enet_host_service at initial connection to 10s (from 1s) --- src/NetworkPrimary.cpp | 2 +- src/multiplayerHub/Network.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/NetworkPrimary.cpp b/src/NetworkPrimary.cpp index a4ce614e1..4e4448fd5 100644 --- a/src/NetworkPrimary.cpp +++ b/src/NetworkPrimary.cpp @@ -124,7 +124,7 @@ void NetworkPrimary::connectToServer(std::string hostnames) exit(EXIT_FAILURE); } /* Wait up to 1 second for the connection attempt to succeed. */ - if (enet_host_service (client, & event, 1000) > 0 && event.type == ENET_EVENT_TYPE_CONNECT) { + if (enet_host_service (client, & event, 10000) > 0 && event.type == ENET_EVENT_TYPE_CONNECT) { //std::string logMessage = "ENet connection succeeded to: "; //logMessage.append(thisHostname); device->getLogger()->log("ENet connection succeeded to:"); diff --git a/src/multiplayerHub/Network.cpp b/src/multiplayerHub/Network.cpp index a84eb0548..8a0f2f5e6 100644 --- a/src/multiplayerHub/Network.cpp +++ b/src/multiplayerHub/Network.cpp @@ -108,7 +108,7 @@ void Network::connectToServer(std::string hostnames) //exit (EXIT_FAILURE); } /* Wait up to 1 second for the connection attempt to succeed. */ - if (enet_host_service (client, & event, 1000) > 0 && event.type == ENET_EVENT_TYPE_CONNECT) { + if (enet_host_service (client, & event, 10000) > 0 && event.type == ENET_EVENT_TYPE_CONNECT) { std::cout << "ENet connection succeeded to: " << thisHostname << std::endl; //Store peer, and initialise the vector of latest strings received peers.push_back(peer); From 84c47745e8174195cf933db6fb4a88a98c869c78 Mon Sep 17 00:00:00 2001 From: James Packer Date: Tue, 9 Jun 2026 18:09:40 +0100 Subject: [PATCH 03/10] Improved networking reliability by using different channels. --- src/NetworkPrimary.cpp | 52 +++++++++++++++++++++++----------- src/NetworkSecondary.cpp | 16 ++++++++--- src/controller/Network.cpp | 18 +++++++----- src/controller/Network.hpp | 2 +- src/multiplayerHub/Network.cpp | 42 +++++++++++++++++---------- 5 files changed, 86 insertions(+), 44 deletions(-) diff --git a/src/NetworkPrimary.cpp b/src/NetworkPrimary.cpp index 4e4448fd5..31e48d6ca 100644 --- a/src/NetworkPrimary.cpp +++ b/src/NetworkPrimary.cpp @@ -123,13 +123,23 @@ void NetworkPrimary::connectToServer(std::string hostnames) enet_deinitialize(); exit(EXIT_FAILURE); } - /* Wait up to 1 second for the connection attempt to succeed. */ - if (enet_host_service (client, & event, 10000) > 0 && event.type == ENET_EVENT_TYPE_CONNECT) { - //std::string logMessage = "ENet connection succeeded to: "; - //logMessage.append(thisHostname); - device->getLogger()->log("ENet connection succeeded to:"); - device->getLogger()->log(thisHostname.c_str()); - } else { + + bool connectedPeer = false; + + // Retry connection 10 times + for (unsigned int j = 0; j < 10; j++) { + /* Wait up to 1 second for the connection attempt to succeed. */ + if (enet_host_service(client, &event, 1000) > 0 && event.type == ENET_EVENT_TYPE_CONNECT) { + //std::string logMessage = "ENet connection succeeded to: "; + //logMessage.append(thisHostname); + device->getLogger()->log("ENet connection succeeded to:"); + device->getLogger()->log(thisHostname.c_str()); + connectedPeer = true; + break; + } + } + + if (!connectedPeer) { /* Either the 1 second is up or a disconnect event was */ /* received. Reset the peer in the event the 1 second */ /* had run out without any significant event. */ @@ -137,6 +147,7 @@ void NetworkPrimary::connectToServer(std::string hostnames) device->getLogger()->log("ENet connection failed to:"); device->getLogger()->log(thisHostname.c_str()); } + } else { // Failed to look up address with enet_address_set_host std::cout << "ENet could not resolve hostname:" << thisHostname << std::endl; @@ -459,16 +470,23 @@ void NetworkPrimary::sendNetwork(std::string aManualCmd) packetFlag = ENET_PACKET_FLAG_RELIABLE; } - /* Create a packet */ - ENetPacket * packet = enet_packet_create (stringToSend.c_str(), - strlen (stringToSend.c_str()) + 1, - packetFlag); - - /* Send the packet to all connected peers over channel id 0. */ - enet_host_broadcast(client, 0, packet); - - /* One could just use enet_host_service() instead. */ - enet_host_flush (client); + // Send data to connected peers + for (int i = 0; i < client->peerCount; i++) { + /* Create a packet */ + ENetPacket* packet = enet_packet_create(stringToSend.c_str(), + strlen(stringToSend.c_str()) + 1, + packetFlag); + + /* Send the packet to each peer over channel id i. */ + if (enet_peer_send(&client->peers[i], min(i, ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT - 1), packet) == 0) { + /* One could just use enet_host_service() instead. */ + enet_host_flush(client); + } + else { + enet_packet_destroy(packet); + // TODO: Add warning/error message here? + } + } } } diff --git a/src/NetworkSecondary.cpp b/src/NetworkSecondary.cpp index 2bd3b7c56..cf2cb68e7 100644 --- a/src/NetworkSecondary.cpp +++ b/src/NetworkSecondary.cpp @@ -530,8 +530,11 @@ void NetworkSecondary::receiveMessage() //Send back to event.peer ENetPacket* packet = enet_packet_create (multiplayerFeedback.c_str(), strlen (multiplayerFeedback.c_str()) + 1,0/*reliable flag*/); if (packet!=0) { - enet_peer_send (event.peer, 0, packet); - enet_host_flush (server); + if (enet_peer_send(event.peer, event.channelID, packet) == 0) { + enet_host_flush(server); + } else { + enet_packet_destroy(packet); + } } } @@ -586,8 +589,13 @@ void NetworkSecondary::receiveMessage() //Send back to event.peer ENetPacket* packet = enet_packet_create (controlOverride.c_str(), strlen (controlOverride.c_str()) + 1,0/*reliable flag*/); if (packet!=0) { - enet_peer_send (event.peer, 0, packet); - enet_host_flush (server); + if (enet_peer_send(event.peer, event.channelID, packet) == 0) { + enet_host_flush(server); + } + else { + enet_packet_destroy(packet); + } + } } diff --git a/src/controller/Network.cpp b/src/controller/Network.cpp index f92bde924..9c0a18724 100644 --- a/src/controller/Network.cpp +++ b/src/controller/Network.cpp @@ -138,7 +138,7 @@ void Network::update(irr::f32& time, ShipData& ownShipData, std::vectorstringToSend.append("|"); } -void Network::sendMessage(ENetPeer* peer) +void Network::sendMessage(ENetPeer* peer, enet_uint8 channel) { //Assumes that event contains a received message @@ -173,14 +173,18 @@ void Network::sendMessage(ENetPeer* peer) strlen (stringToSend.c_str()) + 1, /*ENET_PACKET_FLAG_RELIABLE*/1); - /* Send the packet to the peer over channel id 0. */ + /* Send the packet to the peer over specified channel id. */ /* One could also broadcast the packet by */ /* enet_host_broadcast (host, 0, packet); */ - enet_peer_send (peer, 0, packet); - /* One could just use enet_host_service() instead. */ - enet_host_flush (server); + if (enet_peer_send(peer, channel, packet) == 0) { + /* One could just use enet_host_service() instead. */ + enet_host_flush(server); + stringToSend = ""; //Sent message, so clear it + } else { + enet_packet_destroy(packet); + } - stringToSend = ""; //Sent message, so clear it + } } diff --git a/src/controller/Network.hpp b/src/controller/Network.hpp index 2b0aa9d14..959c1baae 100644 --- a/src/controller/Network.hpp +++ b/src/controller/Network.hpp @@ -57,7 +57,7 @@ class Network void findOtherShipData(const std::vector& otherShipsDataString, std::vector& otherShipsData); void findBuoyPositionData(const std::vector& buoysDataString, std::vector& buoysData); - void sendMessage(ENetPeer * peer); + void sendMessage(ENetPeer * peer, enet_uint8 channel); }; #endif // __NETWORK_HPP_INCLUDED__ diff --git a/src/multiplayerHub/Network.cpp b/src/multiplayerHub/Network.cpp index 8a0f2f5e6..c9149d8c9 100644 --- a/src/multiplayerHub/Network.cpp +++ b/src/multiplayerHub/Network.cpp @@ -107,22 +107,32 @@ void Network::connectToServer(std::string hostnames) std::cout << "No available peers for initiating an ENet connection." << std::endl; //exit (EXIT_FAILURE); } - /* Wait up to 1 second for the connection attempt to succeed. */ - if (enet_host_service (client, & event, 10000) > 0 && event.type == ENET_EVENT_TYPE_CONNECT) { - std::cout << "ENet connection succeeded to: " << thisHostname << std::endl; - //Store peer, and initialise the vector of latest strings received - peers.push_back(peer); - latestMessageFromPeer.push_back(""); - } else { + + bool connectedPeer = false; + + // Retry connection 10 times + for (unsigned int j = 0; j < 10; j++) { + /* Wait up to 1 second for the connection attempt to succeed. */ + if (enet_host_service(client, &event, 1000) > 0 && event.type == ENET_EVENT_TYPE_CONNECT) { + std::cout << "ENet multiplayer connection succeeded to: " << thisHostname << std::endl; + //Store peer, and initialise the vector of latest strings received + peers.push_back(peer); + latestMessageFromPeer.push_back(""); + connectedPeer = true; + break; + } + } + if (!connectedPeer) { /* Either the 1 second is up or a disconnect event was */ /* received. Reset the peer in the event the 1 second */ /* had run out without any significant event. */ - enet_peer_reset (peer); - std::cout << "ENet connection failed to:" << thisHostname << std::endl; + enet_peer_reset(peer); + std::cout << "ENet multiplayer connection failed to:" << thisHostname << std::endl; } + } else { // Failed to look up address with enet_address_set_host - std::cout << "ENet could not resolve hostname:" << thisHostname << std::endl; + std::cout << "ENet multiplayer could not resolve hostname:" << thisHostname << std::endl; } } } @@ -148,11 +158,13 @@ void Network::sendString(std::string stringToSend, bool reliable, unsigned int p strlen (stringToSend.c_str()) + 1, reliableFlag); //Flag - // Send the packet to peer over channel id 0. - enet_peer_send(peers.at(peerNumber), 0, packet); - - // One could just use enet_host_service() instead. - enet_host_flush (client); + // Send the packet to peer over channel id matching the peer number (one channel per peer). + if (enet_peer_send(peers.at(peerNumber), min(peerNumber, ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT - 1), packet) == 0) { + // One could just use enet_host_service() instead. + enet_host_flush(client); + } else { + enet_packet_destroy(packet); + } } } } From 3fc005679dfa44185fd06fb5d57a6c9bff76702f Mon Sep 17 00:00:00 2001 From: James Packer Date: Tue, 9 Jun 2026 19:18:05 +0100 Subject: [PATCH 04/10] Process multiple messages while waiting for scenario. --- src/NetworkPrimary.cpp | 8 +++++++- src/NetworkSecondary.cpp | 6 +++++- src/multiplayerHub/Network.cpp | 1 + 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/NetworkPrimary.cpp b/src/NetworkPrimary.cpp index 31e48d6ca..341097549 100644 --- a/src/NetworkPrimary.cpp +++ b/src/NetworkPrimary.cpp @@ -472,6 +472,12 @@ void NetworkPrimary::sendNetwork(std::string aManualCmd) // Send data to connected peers for (int i = 0; i < client->peerCount; i++) { + + // Skip non-connected peers + if (client->peers[i].state != ENET_PEER_STATE_CONNECTED) { + continue; + } + /* Create a packet */ ENetPacket* packet = enet_packet_create(stringToSend.c_str(), strlen(stringToSend.c_str()) + 1, @@ -484,7 +490,7 @@ void NetworkPrimary::sendNetwork(std::string aManualCmd) } else { enet_packet_destroy(packet); - // TODO: Add warning/error message here? + std::cout << "Could not send packet to peer " << i << std::endl; } } } diff --git a/src/NetworkSecondary.cpp b/src/NetworkSecondary.cpp index cf2cb68e7..49565048d 100644 --- a/src/NetworkSecondary.cpp +++ b/src/NetworkSecondary.cpp @@ -87,7 +87,7 @@ void NetworkSecondary::connectToServer(std::string hostnames) void NetworkSecondary::getScenarioFromNetwork(std::string& dataString) //Not used by primary { - if (enet_host_service (server, & event, 10) > 0) { //Wait 10ms for event + while (enet_host_service (server, & event, 10) > 0) { //Wait 10ms for event, and process multiple events if needed if (event.type ==ENET_EVENT_TYPE_RECEIVE) { //receive it @@ -104,6 +104,8 @@ void NetworkSecondary::getScenarioFromNetwork(std::string& dataString) //Not use (receivedString.substr(0,4) == "SCN5")) { //Check if it starts with SCN1-SCN5 //If valid, use this string dataString = receivedString; + // Break out of loop so we don't overwrite the scenario data + break; } } } @@ -534,6 +536,7 @@ void NetworkSecondary::receiveMessage() enet_host_flush(server); } else { enet_packet_destroy(packet); + std::cout << "Could not send multiplayer feedback packet on channel " << event.channelID << std::endl; } } } @@ -594,6 +597,7 @@ void NetworkSecondary::receiveMessage() } else { enet_packet_destroy(packet); + std::cout << "Could not send control override packet on channel " << event.channelID << std::endl; } } diff --git a/src/multiplayerHub/Network.cpp b/src/multiplayerHub/Network.cpp index c9149d8c9..7efd49cf0 100644 --- a/src/multiplayerHub/Network.cpp +++ b/src/multiplayerHub/Network.cpp @@ -164,6 +164,7 @@ void Network::sendString(std::string stringToSend, bool reliable, unsigned int p enet_host_flush(client); } else { enet_packet_destroy(packet); + std::cout << "Could not send packet to peer " << peerNumber << std::endl; } } } From 6be20b2b3edcea94a88483e207ff7f816508d3b7 Mon Sep 17 00:00:00 2001 From: James Packer Date: Tue, 9 Jun 2026 19:25:47 +0100 Subject: [PATCH 05/10] Also apply while loop to get scenario name in the map controller. --- src/controller/Network.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/controller/Network.cpp b/src/controller/Network.cpp index 9c0a18724..445265b86 100644 --- a/src/controller/Network.cpp +++ b/src/controller/Network.cpp @@ -89,7 +89,7 @@ std::string Network::findWorldName() std::string worldName = ""; - if (enet_host_service (server, & event, 10) > 0) { + while (worldName == "" && enet_host_service (server, & event, 10) > 0) { if (event.type == ENET_EVENT_TYPE_RECEIVE) { //receive it char tempString[8192]; //Fixme: Think if this is long enough From ccdd4b977110238d8834768d471e942ce56cb212 Mon Sep 17 00:00:00 2001 From: James Packer Date: Tue, 9 Jun 2026 20:26:46 +0100 Subject: [PATCH 06/10] Avoid using min because of clash between std::min and min macro on Windows. --- src/NetworkPrimary.cpp | 6 +++++- src/multiplayerHub/Network.cpp | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/NetworkPrimary.cpp b/src/NetworkPrimary.cpp index 341097549..b0ebe373e 100644 --- a/src/NetworkPrimary.cpp +++ b/src/NetworkPrimary.cpp @@ -484,7 +484,11 @@ void NetworkPrimary::sendNetwork(std::string aManualCmd) packetFlag); /* Send the packet to each peer over channel id i. */ - if (enet_peer_send(&client->peers[i], min(i, ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT - 1), packet) == 0) { + enet_uint8 channelNumber = i; + if (channelNumber >= ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT) { + channelNumber = ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT - 1; + } + if (enet_peer_send(&client->peers[i], channelNumber, packet) == 0) { /* One could just use enet_host_service() instead. */ enet_host_flush(client); } diff --git a/src/multiplayerHub/Network.cpp b/src/multiplayerHub/Network.cpp index 7efd49cf0..1b75b5333 100644 --- a/src/multiplayerHub/Network.cpp +++ b/src/multiplayerHub/Network.cpp @@ -159,7 +159,11 @@ void Network::sendString(std::string stringToSend, bool reliable, unsigned int p reliableFlag); //Flag // Send the packet to peer over channel id matching the peer number (one channel per peer). - if (enet_peer_send(peers.at(peerNumber), min(peerNumber, ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT - 1), packet) == 0) { + enet_uint8 channelNumber = peerNumber; + if (channelNumber >= ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT) { + channelNumber = ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT - 1; + } + if (enet_peer_send(peers.at(peerNumber), channelNumber, packet) == 0) { // One could just use enet_host_service() instead. enet_host_flush(client); } else { From 1e8c738bc3e3d721d49feec795030e9d07666a52 Mon Sep 17 00:00:00 2001 From: James Packer Date: Tue, 9 Jun 2026 23:39:50 +0100 Subject: [PATCH 07/10] Also use while loop in network primary for enet_host_service --- src/NetworkPrimary.cpp | 2 +- src/NetworkSecondary.cpp | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/NetworkPrimary.cpp b/src/NetworkPrimary.cpp index b0ebe373e..aad2e658f 100644 --- a/src/NetworkPrimary.cpp +++ b/src/NetworkPrimary.cpp @@ -190,7 +190,7 @@ void NetworkPrimary::receiveNetwork() std::cerr << "Network not linked to model" << std::endl; return; } - if (enet_host_service (client, & event, 10) > 0) { + while (enet_host_service (client, & event, 10) > 0) { if (event.type==ENET_EVENT_TYPE_RECEIVE) { //Convert into a string, max length 8192 diff --git a/src/NetworkSecondary.cpp b/src/NetworkSecondary.cpp index 49565048d..67de1a2f4 100644 --- a/src/NetworkSecondary.cpp +++ b/src/NetworkSecondary.cpp @@ -95,6 +95,8 @@ void NetworkSecondary::getScenarioFromNetwork(std::string& dataString) //Not use snprintf(tempString,8192,"%s",event.packet -> data); std::string receivedString(tempString); + std::cout << "Received message: " << receivedString << std::endl; + //Basic checks if (receivedString.length() > 4) { //Check if more than 4 chars long, ie we have at least some data if ((receivedString.substr(0,4) == "SCN1") || From 4ed7df1b5e7f8af071f2ef1f00cedb714c20ada8 Mon Sep 17 00:00:00 2001 From: James Packer Date: Wed, 10 Jun 2026 00:13:14 +0100 Subject: [PATCH 08/10] More debugging outputs, and don't destroy packet if not required. --- src/NetworkSecondary.cpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/NetworkSecondary.cpp b/src/NetworkSecondary.cpp index 67de1a2f4..d648b418c 100644 --- a/src/NetworkSecondary.cpp +++ b/src/NetworkSecondary.cpp @@ -87,7 +87,9 @@ void NetworkSecondary::connectToServer(std::string hostnames) void NetworkSecondary::getScenarioFromNetwork(std::string& dataString) //Not used by primary { - while (enet_host_service (server, & event, 10) > 0) { //Wait 10ms for event, and process multiple events if needed + std::cout << "Waiting for scenario." << std::endl; // TODO: Remove after debugging + while (enet_host_service (server, & event, 10) > 0) { //Wait 10ms for event, and process multiple events if needed + std::cout << "event.type:" << event.type << std::endl; // TODO: Remove after debugging if (event.type ==ENET_EVENT_TYPE_RECEIVE) { //receive it @@ -95,7 +97,7 @@ void NetworkSecondary::getScenarioFromNetwork(std::string& dataString) //Not use snprintf(tempString,8192,"%s",event.packet -> data); std::string receivedString(tempString); - std::cout << "Received message: " << receivedString << std::endl; + std::cout << "Received message: " << receivedString << std::endl; // TODO: Remove after debugging //Basic checks if (receivedString.length() > 4) { //Check if more than 4 chars long, ie we have at least some data @@ -110,9 +112,10 @@ void NetworkSecondary::getScenarioFromNetwork(std::string& dataString) //Not use break; } } + /* Clean up the packet now that we're done using it. */ + enet_packet_destroy(event.packet); } - /* Clean up the packet now that we're done using it. */ - enet_packet_destroy (event.packet); + } } From 5ff35aee5db8d94c33f29542276ce994be202ce3 Mon Sep 17 00:00:00 2001 From: James Packer Date: Wed, 10 Jun 2026 21:54:44 +0100 Subject: [PATCH 09/10] Delay starting multiplayer extra network until after the scenario is known (to avoid long delay). --- src/main.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 1500512f4..c120964e7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -897,14 +897,6 @@ int main(int argc, char ** argv) //Network network(&model); network->connectToServer(hostname); - // If in multiplayer mode, also start 'normal' network, so we can send data to secondary displays - Network* extraNetwork = 0; - if ((mode == OperatingMode::Multiplayer) && (hostname.length() > 0 )) { - extraNetwork = Network::createNetwork(OperatingMode::Normal, udpPort, device); - extraNetwork->connectToServer(hostname); - //std::cout << "Starting extra network to " << hostname << " on " << udpPort << std::endl; - } - //Read in scenario data (work in progress) ScenarioData scenarioData; if (mode == OperatingMode::Normal) { @@ -934,6 +926,14 @@ int main(int argc, char ** argv) //Note: We could use this serialised format as a scenario import/export format or for online distribution + // If in multiplayer mode, also start 'normal' network, so we can send data to secondary displays + Network* extraNetwork = 0; + if ((mode == OperatingMode::Multiplayer) && (hostname.length() > 0 )) { + extraNetwork = Network::createNetwork(OperatingMode::Normal, udpPort, device); + extraNetwork->connectToServer(hostname); + //std::cout << "Starting extra network to " << hostname << " on " << udpPort << std::endl; + } + // Check VR mode bool vr3dMode = false; if (IniFile::iniFileTou32(iniFilename, "vr_mode")==1) { From 54dd9609d5bb20e59a51298b47c00effbf4310ed Mon Sep 17 00:00:00 2001 From: James Packer Date: Wed, 10 Jun 2026 22:20:50 +0100 Subject: [PATCH 10/10] Remove debugging outputs. --- src/NetworkSecondary.cpp | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/NetworkSecondary.cpp b/src/NetworkSecondary.cpp index d648b418c..625a06648 100644 --- a/src/NetworkSecondary.cpp +++ b/src/NetworkSecondary.cpp @@ -87,9 +87,7 @@ void NetworkSecondary::connectToServer(std::string hostnames) void NetworkSecondary::getScenarioFromNetwork(std::string& dataString) //Not used by primary { - std::cout << "Waiting for scenario." << std::endl; // TODO: Remove after debugging while (enet_host_service (server, & event, 10) > 0) { //Wait 10ms for event, and process multiple events if needed - std::cout << "event.type:" << event.type << std::endl; // TODO: Remove after debugging if (event.type ==ENET_EVENT_TYPE_RECEIVE) { //receive it @@ -97,8 +95,6 @@ void NetworkSecondary::getScenarioFromNetwork(std::string& dataString) //Not use snprintf(tempString,8192,"%s",event.packet -> data); std::string receivedString(tempString); - std::cout << "Received message: " << receivedString << std::endl; // TODO: Remove after debugging - //Basic checks if (receivedString.length() > 4) { //Check if more than 4 chars long, ie we have at least some data if ((receivedString.substr(0,4) == "SCN1") ||