public override void DisconnectRemoteClient(ulong clientId)
        {
            if (NetworkingManager.Singleton.LogLevel <= LogLevel.Developer)
            {
                NetworkLog.LogInfoServer("SteamP2PTransport - DisconnectRemoteClient clientId: " + clientId);
            }

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

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

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

            connectedUsers.Remove(clientId);
        }
示例#2
0
        public override void Shutdown()
        {
            NetworkLog.LogInfoServer("SteamP2PTransport - Shutdown");

            if (_p2PSessionRequestCallback != null)
            {
                _p2PSessionRequestCallback.Dispose();
            }
            if (_p2PSessionConnectFailCallback != null)
            {
                _p2PSessionConnectFailCallback.Dispose();
            }

            sendPings = false;
            isServer  = false;
            connectionAttemptFailed = false;
            channelSendTypes.Clear();
            channelCounter     = 0;
            currentPollChannel = 0;

            sentPings.Clear();
            pingIdCounter = 0;

            if (NetworkingManager.Singleton != null)
            {
                NetworkingManager.Singleton.StartCoroutine(Delay(100, () =>
                {//Need to delay the closing of the p2p sessions to not block the disconect message before it is sent.
                    CloseP2PSessions();
                }));
            }
            else
            {
                CloseP2PSessions();
            }
        }
 public override void DisconnectLocalClient()
 {
     if (NetworkingManager.Singleton.LogLevel <= LogLevel.Developer)
     {
         NetworkLog.LogInfoServer("SteamP2PTransport - DisconnectLocalClient");
     }
     SteamNetworking.SendP2PPacket(serverUser.SteamId, new byte[] { 0 }, 1, EP2PSend.k_EP2PSendReliable, (int)InternalChannelType.Disconnect);
 }
 private void OnP2PSessionConnectFail(P2PSessionConnectFail_t request)
 {
     if (NetworkingManager.Singleton.LogLevel <= LogLevel.Developer)
     {
         NetworkLog.LogInfoServer("SteamP2PTransport - OnP2PSessionConnectFail - m_steamIDRemote: " + request.m_eP2PSessionError.ToString() + " Error: " + request.m_eP2PSessionError.ToString());
     }
     connectionAttemptFailed         = true;
     connectionAttemptFailedClientId = request.m_steamIDRemote.m_SteamID;
 }
        private void OnP2PSessionRequest(P2PSessionRequest_t request)
        {
            if (NetworkingManager.Singleton.LogLevel <= LogLevel.Developer)
            {
                NetworkLog.LogInfoServer("SteamP2PTransport - OnP2PSessionRequest - m_steamIDRemote: " + request.m_steamIDRemote);
            }

            CSteamID userId = request.m_steamIDRemote;

            //Todo: Might want to check if we expect the user before just accepting it.
            SteamNetworking.AcceptP2PSessionWithUser(userId);
        }
        public override void DisconnectLocalClient()
        {
            if (NetworkManager.Singleton.LogLevel <= LogLevel.Developer)
            {
                NetworkLog.LogInfoServer(nameof(SteamNetworkingTransport) + " - DisconnectLocalClient");
            }

#if UNITY_SERVER
            SteamGameServerNetworking.SendP2PPacket(serverUser.SteamId, new byte[] { 0 }, 1, EP2PSend.k_EP2PSendReliable, (int)InternalChannelType.Disconnect);
#else
            SteamNetworking.SendP2PPacket(serverUser.SteamId, new byte[] { 0 }, 1, EP2PSend.k_EP2PSendReliable, (int)InternalChannelType.Disconnect);
#endif
        }
示例#7
0
 private void CloseP2PSessions()
 {
     foreach (User user in connectedUsers.Values)
     {
         SteamNetworking.CloseP2PSessionWithUser(user.SteamId);
     }
     if (serverUser != null)
     {
         SteamNetworking.CloseP2PSessionWithUser(serverUser.SteamId);
     }
     connectedUsers.Clear();
     serverUser = null;
     NetworkLog.LogInfoServer("SteamP2PTransport - CloseP2PSessions - has Closed P2P Sessions With all Users");
 }
        private void OnP2PSessionRequest(P2PSessionRequest_t request)
        {
            if (NetworkManager.Singleton.LogLevel <= LogLevel.Developer)
            {
                NetworkLog.LogInfoServer(nameof(SteamNetworkingTransport) + " - OnP2PSessionRequest - m_steamIDRemote: " + request.m_steamIDRemote);
            }

            CSteamID userId = request.m_steamIDRemote;

            //Todo: Might want to check if we expect the user before just accepting it.
#if UNITY_SERVER
            SteamGameServerNetworking.AcceptP2PSessionWithUser(userId);
#else
            SteamNetworking.AcceptP2PSessionWithUser(userId);
#endif
        }
        public override SocketTasks StartServer()
        {
            isServer = true;

            // setup the callback method
            _p2PSessionRequestCallback = Callback <P2PSessionRequest_t> .Create(OnP2PSessionRequest);

            _p2PSessionConnectFailCallback = Callback <P2PSessionConnectFail_t> .Create(OnP2PSessionConnectFail);

            OnConnected();

            if (NetworkingManager.Singleton.LogLevel <= LogLevel.Developer)
            {
                NetworkLog.LogInfoServer("SteamP2PTransport - StartServer - ConnectToCSteamID: " + SteamUser.GetSteamID().m_SteamID.ToString());
            }

            return(SocketTask.Done.AsTasks());
        }
        public override NetEventType PollEvent(out ulong clientId, out string channelName, out ArraySegment <byte> payload, out float receiveTime)
        {
            //Connect fail disconnect
            if (connectionAttemptFailed)
            {
                clientId                = connectionAttemptFailedClientId;
                channelName             = null;
                payload                 = new ArraySegment <byte>();
                connectionAttemptFailed = false;
                receiveTime             = Time.realtimeSinceStartup;
                return(NetEventType.Disconnect);
            }

            while (currentPollChannel < channelSendTypes.Count)
            {
                if (SteamNetworking.IsP2PPacketAvailable(out uint msgSize, currentPollChannel))
                {
                    uint     bytesRead;
                    CSteamID remoteId;
                    if (messageBuffer.Length < msgSize)
                    {
                        messageBuffer = new byte[msgSize];
                        if (NetworkingManager.Singleton.LogLevel <= LogLevel.Developer)
                        {
                            NetworkLog.LogInfoServer("SteamP2PTransport - PollEvent - Increase buffer size to: " + msgSize);
                        }
                    }

                    if (SteamNetworking.ReadP2PPacket(messageBuffer, msgSize, out bytesRead, out remoteId, currentPollChannel))
                    {
                        clientId = remoteId.m_SteamID;

                        if (currentPollChannel < (int)InternalChannelType.InternalChannelsCount)
                        {
                            channelName = null;
                            payload     = new ArraySegment <byte>();

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

                                connectedUsers.Remove(clientId);
                                SteamNetworking.CloseP2PSessionWithUser(remoteId);
                                receiveTime = Time.realtimeSinceStartup;
                                return(NetEventType.Disconnect);

                            case (byte)InternalChannelType.Connect:

                                if (isServer)
                                {
                                    SteamNetworking.SendP2PPacket(remoteId, new byte[] { 0 }, 1, EP2PSend.k_EP2PSendReliable, (int)InternalChannelType.Connect);
                                }
                                if (connectedUsers.ContainsKey(remoteId.m_SteamID) == false)
                                {
                                    clientId = remoteId.m_SteamID;
                                    connectedUsers.Add(clientId, new User(remoteId));
                                    receiveTime = Time.realtimeSinceStartup;

                                    if (!isServer)
                                    {
                                        OnConnected();
                                    }


                                    return(NetEventType.Connect);
                                }
                                break;

                            case (byte)InternalChannelType.Ping:

                                pingPongMessageBuffer[0] = messageBuffer[0];
                                SteamNetworking.SendP2PPacket(remoteId, pingPongMessageBuffer, msgSize, EP2PSend.k_EP2PSendUnreliableNoDelay, (int)InternalChannelType.Pong);
                                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;
                            }
                        }
                        else
                        {
                            payload     = new ArraySegment <byte>(messageBuffer, 0, (int)msgSize);
                            channelName = channelIdToName[currentPollChannel];
                            receiveTime = Time.realtimeSinceStartup;
                            return(NetEventType.Data);
                        }
                    }
                    else
                    {
                        currentPollChannel++;
                    }
                }
                else
                {
                    currentPollChannel++;
                }
            }
            currentPollChannel = 0;
            payload            = new ArraySegment <byte>();
            channelName        = null;
            clientId           = 0;
            receiveTime        = Time.realtimeSinceStartup;
            return(NetEventType.Nothing);
        }
        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);
        }
示例#12
0
 private void ServerOnCollide()
 {
     NetworkLog.LogInfoServer("HELLO THERE WAS A COLLISION");
 }