public static void UpdatePlayerList() { // There's probably a better way to get all current active P2P connections, // but I couldn't find one. The SessionInfo pointers are not reliable as // players can spoof their Steam ID there. int cnt = SteamFriends.GetCoplayFriendCount(); for (int i = 0; i < cnt; i++) { CSteamID id = SteamFriends.GetCoplayFriend(i); P2PSessionState_t session = new P2PSessionState_t(); if (!SteamNetworking.GetP2PSessionState(id, out session) || (session.m_bConnectionActive == 0 && session.m_bConnecting == 0)) { if (activePlayers.ContainsKey(id)) { ETWPingMonitor.Unregister(activePlayers[id].NetId); activePlayers.Remove(id); } continue; } if (!activePlayers.ContainsKey(id)) { activePlayers[id] = new Player(id); } activePlayers[id].UpdateNetInfo(session); } }
private void UpdateNetInfo(P2PSessionState_t session) { bool endpointChanged = sessionState.m_nRemoteIP != session.m_nRemoteIP || sessionState.m_nRemotePort != session.m_nRemotePort; sessionState = session; if (endpointChanged) { // If IP/port changed for whatever reason ETWPingMonitor.Unregister(NetId); Region = "..."; byte[] ipBytes = BitConverter.GetBytes(sessionState.m_nRemoteIP).Reverse().ToArray(); NetId = (ulong)sessionState.m_nRemotePort << 32 | BitConverter.ToUInt32(ipBytes, 0); ETWPingMonitor.Register(NetId); if (session.m_bUsingRelay == 0) { IpLocationAPI.GetLocationAsync(new IPAddress(ipBytes).ToString(), r => Region = r); } else { Region = "[STEAM RELAY]"; } } }