Пример #1
0
    public override void OnReceiveData(NetworkFrame frame, IPEndPoint point, byte[] bytes)
    {
        base.OnReceiveData(frame, point, bytes);
        NetworkPlayer player = NetworkManager.Instance.GetPlayer(frame.m_SenderId);

        if (!IsUserAuthorized(frame.m_SenderId) && frame.m_Type != NetworkFrame.NetworkFrameType.Authentication)
        {
            return;
        }

        switch (frame.m_Type)
        {
        case NetworkFrame.NetworkFrameType.Handshake:
        {
            NetworkHandshakeFrame handshake = NetworkFrame.Parse <NetworkHandshakeFrame>(bytes);
            player = NetworkManager.Instance.AddPlayer(handshake.m_SenderId, new NetworkPlayer()
                {
                    m_Id   = handshake.m_SenderId,
                    m_Name = handshake.m_DisplayName
                });

            NetworkSpawnRPC spawnRpc = new NetworkSpawnRPC(-1, true);
            OnRPCCommand(player, spawnRpc.ToString());
            UpdatePlayer(player);
        } break;

        case NetworkFrame.NetworkFrameType.Authentication:     // TODO: Clean this? Its terrible lol
        {
            NetworkAuthenticationFrame authenticationFrame = NetworkFrame.Parse <NetworkAuthenticationFrame>(bytes);

            if (m_Password != "" && m_Password != null && authenticationFrame.m_Password != m_Password)
            {
                Debug.Log(m_Password);
                authenticationFrame.m_Response = NetworkAuthenticationFrame.NetworkAuthenticationResponse.IncorrectPassword;
            }
            else if (NetworkManager.Instance.GetPlayerCount() == NetworkManager.Instance.m_Settings.m_MaxPlayers && NetworkManager.Instance.m_Settings.m_MaxPlayers > 0)
            {
                authenticationFrame.m_Response = NetworkAuthenticationFrame.NetworkAuthenticationResponse.LobbyFull;
            }
            else
            {
                Debug.Log("bop");
                m_AuthorizedUsers.Add(frame.m_SenderId);
                authenticationFrame.m_Response = NetworkAuthenticationFrame.NetworkAuthenticationResponse.Connected;
            }
            authenticationFrame.m_TargetId = authenticationFrame.m_SenderId;
            authenticationFrame.m_SenderId = "server";
            QueueFrame(authenticationFrame);
        } break;

        case NetworkFrame.NetworkFrameType.Ping:
        {
            NetworkFrame pingFrame = new NetworkFrame(NetworkFrame.NetworkFrameType.Ping, "server", frame.m_SenderId);
            QueueFrame(pingFrame);
        } break;

        case NetworkFrame.NetworkFrameType.RPC:
        {
            NetworkRPCFrame rpcFrame = NetworkFrame.Parse <NetworkRPCFrame>(bytes);
            OnRPCCommand(player, rpcFrame.m_RPC);
        } break;

        case NetworkFrame.NetworkFrameType.Heartbeat:
        {
            if (m_Heartbeats.ContainsKey(frame.m_SenderId))
            {
                m_Heartbeats[frame.m_SenderId] = 0;
            }
            else
            {
                m_Heartbeats.Add(frame.m_SenderId, 0);
            }
        } break;
        }
    }
Пример #2
0
    public override void OnReceiveData(NetworkFrame frame, IPEndPoint point, byte[] bytes)
    {
        base.OnReceiveData(frame, point, bytes);

        switch (frame.m_Type)
        {
        case NetworkFrame.NetworkFrameType.Ping:
        {
            if (m_Stopwatch != null)
            {
                m_Stopwatch.Stop();
                m_LastPing  = m_Stopwatch.ElapsedMilliseconds;
                m_Stopwatch = null;
            }
        } break;

        case NetworkFrame.NetworkFrameType.Authentication:
        {
            NetworkAuthenticationFrame authenticationFrame = NetworkFrame.Parse <NetworkAuthenticationFrame>(bytes);
            if (authenticationFrame.m_Response == NetworkAuthenticationFrame.NetworkAuthenticationResponse.Connected)
            {
                SendHandshake();
                StartCoroutine(PingChecker());
                StartCoroutine(HeartbeatWorker());
            }
            else
            {
                if (authenticationFrame.m_Response == NetworkAuthenticationFrame.NetworkAuthenticationResponse.Banned)
                {
                    Debug.LogWarning($"[Client] Failed to connect to server. We have been banned from that server: {authenticationFrame.m_Message}!");
                }
                else if (authenticationFrame.m_Response == NetworkAuthenticationFrame.NetworkAuthenticationResponse.LobbyFull)
                {
                    Debug.LogWarning($"[Client] Failed to connect to server. That server is full!");
                }
                else if (authenticationFrame.m_Response == NetworkAuthenticationFrame.NetworkAuthenticationResponse.IncorrectPassword)
                {
                    Debug.LogWarning($"[Client] Failed to connect to server. The password was incorrect!");
                }
                else if (authenticationFrame.m_Response == NetworkAuthenticationFrame.NetworkAuthenticationResponse.Error)
                {
                    Debug.LogWarning($"[Client] Failed to connect to server. We have received an error: {authenticationFrame.m_Message}");
                }
                else
                {
                    Debug.LogWarning($"[Client] Failed to connect to server. Unknown reason: {authenticationFrame.m_Message}");
                }
                Clean();
            }
        } break;

        case NetworkFrame.NetworkFrameType.RPC:
        {
            NetworkRPCFrame rpcFrame = NetworkFrame.Parse <NetworkRPCFrame>(bytes);
            OnRPCCommand(rpcFrame.m_RPC);
        } break;

        case NetworkFrame.NetworkFrameType.Heartbeat:
        {
            m_ServerHeartbeats = 0;
        } break;
        }
    }