@@ -84,13 +84,16 @@ void NetworkMesh::SendToMesh(NetworkPacket& packet, std::vector<int64_t> vecTarg
8484 {
8585 int ret = enet_peer_send (peer, 0 , pENetPacket);
8686
87+ char ip[INET_ADDRSTRLEN + 1 ] = { 0 };
88+ enet_address_get_host_ip (&peer->address , ip, sizeof (ip));
89+
8790 if (ret == 0 )
8891 {
89- NetworkLog (" Packet Sent!" );
92+ NetworkLog (" Packet Sent! %s:%d " , ip, peer-> address . port );
9093 }
9194 else
9295 {
93- NetworkLog (" Packet Failed To Send!" );
96+ NetworkLog (" Packet Failed To Send! %s:%d " , ip, peer-> address . port );
9497 }
9598 }
9699 }
@@ -136,15 +139,26 @@ void NetworkMesh::ConnectToSingleUser(LobbyMemberEntry& lobbyMember, bool bIsRec
136139 char ip[INET_ADDRSTRLEN + 1 ] = { 0 };
137140 enet_address_get_host_ip (&addr, ip, sizeof (ip));
138141
139- NetworkLog (" Connecting to %s:%d." , ip, addr.port );
142+ NetworkLog (" Connecting to %s:%d. (pref port was actually %d) " , ip, addr.port , lobbyMember. preferredPort );
140143
141144 ConnectToSingleUser (addr, lobbyMember.user_id , bIsReconnect);
142145}
143146
144147void NetworkMesh::ConnectToSingleUser (ENetAddress addr, Int64 user_id, bool bIsReconnect /* = false*/ )
145148{
149+ // is it already connected?
150+ if (m_mapConnections.contains (user_id))
151+ {
152+ NetworkLog (" NetworkMesh::ConnectToSingleUser - Duplicate connection for user %lld, not making new connection and returning." , user_id);
153+ return ;
154+ }
146155
147- // TODO_NGMP: Connect to correct IP + the remote preferred port
156+ // is it local?
157+ if (user_id == NGMP_OnlineServicesManager::GetInstance ()->GetAuthInterface ()->GetUserID ())
158+ {
159+ // TODO_NGMP: Is this really necessary
160+ enet_address_set_host (&addr, " 127.0.0.1" );
161+ }
148162
149163 /* Initiate the connection, allocating the two channels 0 and 1. */
150164 ENetPeer* peer = enet_host_connect (enetInstance, &addr, 2 , 0 );
@@ -201,7 +215,7 @@ void NetworkMesh::ConnectToMesh(LobbyEntry& lobby)
201215 {
202216 server_address.host = ENET_HOST_ANY;
203217 server_address.port = NGMP_OnlineServicesManager::GetInstance ()->GetPortMapper ().GetOpenPort ();
204-
218+ NetworkLog ( " Network Listening on port %d! " , server_address. port );
205219 // TODO_NGMP: Correct values here
206220 enetInstance = enet_host_create (&server_address,
207221 32 , // max game size is 8 and we are p2p and fake a connection to ourselves // TODO_NGMP: Do we need to support more, e.g. spectators?
@@ -304,6 +318,27 @@ void NetworkMesh::Tick()
304318
305319 // TODO_NGMP: Add a timeout for connections
306320 pConnection->m_State = EConnectionState::CONNECTED_DIRECT;
321+
322+ // did the endpoint change? we should just use that, thats what the other side is talking to us on
323+ if (pConnection->m_address .host != event.peer ->address .host
324+ || pConnection->m_address .port != event.peer ->address .port )
325+ {
326+ char oldIp[INET_ADDRSTRLEN + 1 ] = { 0 };
327+ enet_address_get_host_ip (&pConnection->m_address , oldIp, sizeof (oldIp));
328+ char newIp[INET_ADDRSTRLEN + 1 ] = { 0 };
329+ enet_address_get_host_ip (&event.peer ->address , newIp, sizeof (newIp));
330+
331+
332+ NetworkLog (" Endpoint for user %lld changed. Before: %s:%d, now %s:%d" , pConnection->m_userID , oldIp, pConnection->m_address .port , newIp, event.peer ->address .port );
333+ pConnection->m_address = event.peer ->address ;
334+ pConnection->m_peer ->address = event.peer ->address ;
335+ //
336+ // ENetAddress addr;
337+ // enet_address_set_host(&addr, newIp);
338+ // addr.port = event.peer->address.port;
339+ //
340+ // UpdatePeerConnection(event.peer, addr);
341+ }
307342 }
308343
309344 break ;
@@ -345,8 +380,17 @@ void NetworkMesh::Tick()
345380 // process
346381 // TODO_NGMP: Reject any packets from members not in the room? or mesh
347382
383+
384+ int64_t connUserID = -1 ;
385+ PlayerConnection* pConnection = GetConnectionForPeer (event.peer );
386+
387+ if (pConnection != nullptr )
388+ {
389+ connUserID = pConnection->m_userID ;
390+ }
391+
348392 CBitStream bitstream (event.packet ->data , event.packet ->dataLength , (EPacketID)event.packet ->data [0 ]);
349- NetworkLog (" [NGMP]: Received %d bytes from user %d" , event.packet ->dataLength , event.peer ->incomingPeerID );
393+ NetworkLog (" [NGMP]: Received %d bytes from peer %d (user id is %lld) " , event.packet ->dataLength , event.peer ->incomingPeerID , connUserID );
350394
351395
352396 bitstream.Decrypt (currentLobby.EncKey , currentLobby.EncIV );
@@ -425,42 +469,36 @@ void NetworkMesh::Tick()
425469 // get host ID
426470 int64_t localID = NGMP_OnlineServicesManager::GetInstance ()->GetAuthInterface ()->GetUserID ();
427471
428- // find user
429- for (auto & connectionData : m_mapConnections)
472+ PlayerConnection* pConnection = GetConnectionForPeer (event.peer );
473+
474+ if (pConnection != nullptr )
430475 {
431- if (connectionData. second . m_peer -> address . host == event. peer -> address . host
432- && connectionData. second . m_peer -> address . port == event. peer -> address . port )
476+ auto lobbyUsers = NGMP_OnlineServicesManager::GetInstance ()-> GetLobbyInterface ()-> GetMembersListForCurrentRoom ();
477+ for ( const auto & lobbyUser : lobbyUsers )
433478 {
434- auto lobbyUsers = NGMP_OnlineServicesManager::GetInstance ()->GetLobbyInterface ()->GetMembersListForCurrentRoom ();
435- for (const auto & lobbyUser : lobbyUsers)
479+ if (lobbyUser.user_id == pConnection->m_userID )
436480 {
437- if (lobbyUser.user_id == connectionData.first )
481+ // if its an announce, dont show it to the sender, they did something locally instead
482+ if (chatPacket.IsAnnouncement ())
438483 {
439- // if its an announce, dont show it to the sender, they did something locally instead
440- if (chatPacket.IsAnnouncement ())
441- {
442- // if its not us, show the message
443- if (chatPacket.ShowAnnouncementToHost () || lobbyUser.user_id != localID)
444- {
445- UnicodeString str;
446- str.format (L" %hs" , chatPacket.GetMsg ().c_str ());
447- NGMP_OnlineServicesManager::GetInstance ()->GetLobbyInterface ()->m_OnChatCallback (str);
448- }
449- }
450- else
484+ // if its not us, show the message
485+ if (chatPacket.ShowAnnouncementToHost () || lobbyUser.user_id != localID)
451486 {
452487 UnicodeString str;
453- str.format (L" %hs: %hs " , lobbyUser. display_name . c_str () , chatPacket.GetMsg ().c_str ());
488+ str.format (L" %hs" , chatPacket.GetMsg ().c_str ());
454489 NGMP_OnlineServicesManager::GetInstance ()->GetLobbyInterface ()->m_OnChatCallback (str);
455490 }
456-
457-
458- break ;
459491 }
460- }
492+ else
493+ {
494+ UnicodeString str;
495+ str.format (L" %hs: %hs" , lobbyUser.display_name .c_str (), chatPacket.GetMsg ().c_str ());
496+ NGMP_OnlineServicesManager::GetInstance ()->GetLobbyInterface ()->m_OnChatCallback (str);
497+ }
498+
461499
462- break ;
463- // NetworkLog("Found connection for user %d", connectionData.first);
500+ break ;
501+ }
464502 }
465503 }
466504
0 commit comments