private void CloseP2PSessions()
        {
            foreach (User user in connectedUsers.Values)
            {
#if UNITY_SERVER
                SteamGameServerNetworking.CloseP2PSessionWithUser(user.SteamId);
#else
                SteamNetworking.CloseP2PSessionWithUser(user.SteamId);
#endif
            }

            if (serverUser != null)
            {
#if UNITY_SERVER
                SteamGameServerNetworking.CloseP2PSessionWithUser(serverUser.SteamId);
#else
                SteamNetworking.CloseP2PSessionWithUser(serverUser.SteamId);
#endif
            }

            connectedUsers.Clear();
            serverUser = null;
            if (NetworkManager.Singleton.LogLevel <= LogLevel.Developer)
            {
                UnityEngine.Debug.Log(nameof(SteamNetworkingTransport) + " - CloseP2PSessions - has Closed P2P Sessions With all Users");
            }
        }
Esempio n. 2
0
        protected void CloseP2PSessionWithUser(CSteamID clientSteamID)
        {
#if UNITY_SERVER
            SteamGameServerNetworking.CloseP2PSessionWithUser(clientSteamID);
#else
            SteamNetworking.CloseP2PSessionWithUser(clientSteamID);
#endif
        }
        public override void DisconnectRemoteClient(ulong clientId)
        {
            if (NetworkManager.Singleton.LogLevel <= LogLevel.Developer)
            {
                NetworkLog.LogInfoServer(nameof(SteamNetworkingTransport) + " - DisconnectRemoteClient clientId: " + clientId);
            }

            if (!connectedUsers.ContainsKey(clientId))
            {
                if (NetworkManager.Singleton.LogLevel <= LogLevel.Error)
                {
                    NetworkLog.LogErrorServer(nameof(SteamNetworkingTransport) + " - Can't disconect client, client not connected, clientId: " + clientId);
                }
                return;
            }

#if UNITY_SERVER
            SteamGameServerNetworking.SendP2PPacket(connectedUsers[clientId].SteamId, new byte[] { 0 }, 1, EP2PSend.k_EP2PSendReliable, (int)InternalChannelType.Disconnect);
#else
            SteamNetworking.SendP2PPacket(connectedUsers[clientId].SteamId, new byte[] { 0 }, 1, EP2PSend.k_EP2PSendReliable, (int)InternalChannelType.Disconnect);
#endif
            CSteamID steamId = connectedUsers[clientId].SteamId;

            NetworkManager.Singleton.StartCoroutine(Delay(100, () =>
            {
                //Need to delay the closing of the p2p sessions to not block the disconect message before it is sent.
#if UNITY_SERVER
                SteamGameServerNetworking.CloseP2PSessionWithUser(steamId);
#else
                SteamNetworking.CloseP2PSessionWithUser(steamId);
#endif
                if (NetworkManager.Singleton.LogLevel <= LogLevel.Developer)
                {
                    NetworkLog.LogInfoServer(nameof(SteamNetworkingTransport) + " - DisconnectRemoteClient - has Closed P2P Session With clientId: " + clientId);
                }
            }));

            connectedUsers.Remove(clientId);
        }
 protected void CloseP2PSessionWithUser(CSteamID clientSteamID) => SteamGameServerNetworking.CloseP2PSessionWithUser(clientSteamID);
Esempio n. 5
0
 public override void CloseConnection(Identity user)
 {
     SteamGameServerNetworking.CloseP2PSessionWithUser((CSteamID)(SteamIdentity)user);
 }
        public override NetworkEvent PollEvent(out ulong clientId, out ArraySegment <byte> payload, out float receiveTime)
        {
            //Connect fail disconnect
            if (connectionAttemptFailed)
            {
                clientId = connectionAttemptFailedClientId;
                payload  = new ArraySegment <byte>();
                connectionAttemptFailed = false;
                receiveTime             = Time.realtimeSinceStartup;
                return(NetworkEvent.Disconnect);
            }

            var currentPollChannel = 0;

            while (currentPollChannel < (int)InternalChannelType.InternalChannelsCount)
            {
#if UNITY_SERVER
                if (SteamGameServerNetworking.IsP2PPacketAvailable(out uint msgSize, currentPollChannel))
#else
                if (SteamNetworking.IsP2PPacketAvailable(out uint msgSize, currentPollChannel))
#endif
                {
                    uint     bytesRead;
                    CSteamID remoteId;
                    if (messageBuffer.Length < msgSize)
                    {
                        messageBuffer = new byte[msgSize];
                        if (NetworkManager.Singleton.LogLevel <= LogLevel.Developer)
                        {
                            NetworkLog.LogInfoServer(nameof(SteamNetworkingTransport) + " - PollEvent - Increase buffer size to: " + msgSize);
                        }
                    }

#if UNITY_SERVER
                    if (SteamGameServerNetworking.ReadP2PPacket(messageBuffer, msgSize, out bytesRead, out remoteId, currentPollChannel))
#else
                    if (SteamNetworking.ReadP2PPacket(messageBuffer, msgSize, out bytesRead, out remoteId, currentPollChannel))
#endif
                    {
                        clientId = remoteId.m_SteamID;

                        payload = new ArraySegment <byte>();

                        switch (currentPollChannel)
                        {
                        case (byte)InternalChannelType.Disconnect:

                            connectedUsers.Remove(clientId);
#if UNITY_SERVER
                            SteamGameServerNetworking.CloseP2PSessionWithUser(remoteId);
#else
                            SteamNetworking.CloseP2PSessionWithUser(remoteId);
#endif
                            receiveTime = Time.realtimeSinceStartup;
                            return(NetworkEvent.Disconnect);

                        case (byte)InternalChannelType.Connect:

                            if (isServer)
                            {
#if UNITY_SERVER
                                SteamGameServerNetworking.SendP2PPacket(remoteId, new byte[] { 0 }, 1, EP2PSend.k_EP2PSendReliable, (int)InternalChannelType.Connect);
#else
                                SteamNetworking.SendP2PPacket(remoteId, new byte[] { 0 }, 1, EP2PSend.k_EP2PSendReliable, (int)InternalChannelType.Connect);
#endif
                            }

                            if (connectedUsers.ContainsKey(remoteId.m_SteamID) == false)
                            {
                                clientId = remoteId.m_SteamID;
                                connectedUsers.Add(clientId, new User(remoteId));
                                receiveTime = Time.realtimeSinceStartup;

                                if (!isServer)
                                {
                                    OnConnected();
                                }

                                return(NetworkEvent.Connect);
                            }

                            break;

                        case (byte)InternalChannelType.Ping:

                            pingPongMessageBuffer[0] = messageBuffer[0];
#if UNITY_SERVER
                            SteamGameServerNetworking.SendP2PPacket(remoteId, pingPongMessageBuffer, msgSize, EP2PSend.k_EP2PSendUnreliableNoDelay, (int)InternalChannelType.Pong);
#else
                            SteamNetworking.SendP2PPacket(remoteId, pingPongMessageBuffer, msgSize, EP2PSend.k_EP2PSendUnreliableNoDelay, (int)InternalChannelType.Pong);
#endif
                            receiveTime = Time.realtimeSinceStartup;
                            break;

                        case (byte)InternalChannelType.Pong:

                            uint pingValue = sentPings[messageBuffer[0]].getPingTime();
                            if (isServer)
                            {
                                connectedUsers[remoteId.m_SteamID].Ping.SetPing(pingValue);
                            }
                            else
                            {
                                serverUser.Ping.SetPing(pingValue);
                            }

                            receiveTime = Time.realtimeSinceStartup;
                            break;

                        case (byte)InternalChannelType.NetcodeData:
                            payload     = new ArraySegment <byte>(messageBuffer, 0, (int)msgSize);
                            receiveTime = Time.realtimeSinceStartup;
                            return(NetworkEvent.Data);

                        default:
                            throw new InvalidOperationException();
                        }
                    }
                    else
                    {
                        currentPollChannel++;
                    }
                }
                else
                {
                    currentPollChannel++;
                }
            }

            currentPollChannel = 0;
            payload            = new ArraySegment <byte>();
            clientId           = 0;
            receiveTime        = Time.realtimeSinceStartup;
            return(NetworkEvent.Nothing);
        }