Exemplo n.º 1
0
        /// <summary>
        /// Execute the packet handler for the client addon data.
        /// </summary>
        /// <param name="addonId">The ID of the addon.</param>
        /// <param name="packetId">The ID of the packet data for the addon.</param>
        /// <param name="packetData">The packet data instance.</param>
        private void ExecuteClientAddonPacketHandler(
            byte addonId,
            byte packetId,
            IPacketData packetData
            )
        {
            var addonPacketIdMessage    = $"for addon ID: {addonId} and packet ID: {packetId}";
            var noHandlerWarningMessage =
                $"There is no client addon packet handler registered {addonPacketIdMessage}";

            if (!_clientAddonPacketHandlers.TryGetValue(addonId, out var addonPacketHandlers))
            {
                Logger.Get().Warn(this, noHandlerWarningMessage);
                return;
            }

            if (!addonPacketHandlers.TryGetValue(packetId, out var handler))
            {
                Logger.Get().Warn(this, noHandlerWarningMessage);
                return;
            }

            // Invoke the packet handler on the Unity main thread
            ThreadUtil.RunActionOnMainThread(() => {
                try {
                    handler.Invoke(packetData);
                } catch (Exception e) {
                    Logger.Get().Error(this,
                                       $"Exception occurred while executing client addon packet handler {addonPacketIdMessage}, type: {e.GetType()}, message: {e.Message}, stacktrace: {e.StackTrace}");
                }
            });
        }
Exemplo n.º 2
0
        /**
         * Executes the correct packet handler corresponding to this packet.
         * Assumes that the packet is not read yet.
         */
        private void ExecuteClientPacketHandler(Packet packet)
        {
            var packetId = packet.ReadPacketId();

            if (!_clientPacketHandlers.ContainsKey(packetId))
            {
                Logger.Warn(this, $"There is no client packet handler registered for ID: {packetId}");
                return;
            }

            var instantiatedPacket = InstantiateClientPacket(packetId, packet);

            if (instantiatedPacket == null)
            {
                Logger.Warn(this, $"Could not instantiate client packet with ID: {packetId}");
                return;
            }

            // Read the packet data into the packet object before sending it to the packet handler
            instantiatedPacket.ReadPacket();

            // Invoke the packet handler for this ID on the Unity main thread
            ThreadUtil.RunActionOnMainThread(() => {
                try {
                    _clientPacketHandlers[packetId].Invoke(instantiatedPacket);
                } catch (Exception e) {
                    Logger.Error(this, $"Exception occured while executing client packet handler for packet ID: {packetId}, message: {e.Message}, stacktrace: {e.StackTrace}");
                }
            });
        }
Exemplo n.º 3
0
        /// <summary>
        /// Callback method for when the client connection fails.
        /// </summary>
        /// <param name="result">The connection failed result.</param>
        private void OnConnectFailed(ConnectFailedResult result)
        {
            Logger.Get().Info(this, $"Connection to server failed, cause: {result.Type}");

            UpdateManager?.StopUdpUpdates();

            IsConnected = false;

            // Invoke callback if it exists on the main thread of Unity
            ThreadUtil.RunActionOnMainThread(() => {
                ConnectFailedEvent?.Invoke(result);
            });
        }
Exemplo n.º 4
0
        private void OnClientConnect()
        {
            // We should only be able to connect during a gameplay scene,
            // which is when the player is spawned already, so we can add the username
            ThreadUtil.RunActionOnMainThread(() => {
                _playerManager.AddNameToPlayer(HeroController.instance.gameObject, _username,
                                               _playerManager.LocalPlayerTeam);
            });

            Logger.Get().Info(this, "Client is connected, sending Hello packet");

            // If we are in a non-gameplay scene, we transmit that we are not active yet
            var currentSceneName = SceneUtil.GetCurrentSceneName();

            if (SceneUtil.IsNonGameplayScene(currentSceneName))
            {
                Logger.Get().Error(this,
                                   $"Client connected during a non-gameplay scene named {currentSceneName}, this should never happen!");
                return;
            }

            var transform = HeroController.instance.transform;
            var position  = transform.position;

            Logger.Get().Info(this, "Sending Hello packet");

            _netClient.UpdateManager.SetHelloServerData(
                _username,
                SceneUtil.GetCurrentSceneName(),
                new Math.Vector2(position.x, position.y),
                transform.localScale.x > 0,
                (ushort)_animationManager.GetCurrentAnimationClip()
                );

            // Since we are probably in the pause menu when we connect, set the timescale so the game
            // is running while paused
            PauseManager.SetTimeScale(1.0f);

            // We have established a TCP connection so we should receive heart beats now
            _heartBeatReceiveStopwatch.Reset();
            _heartBeatReceiveStopwatch.Start();

            MonoBehaviourUtil.Instance.OnUpdateEvent += CheckHeartBeat;

            Ui.UiManager.InfoBox.AddMessage("You are connected to the server");
        }
Exemplo n.º 5
0
        /// <summary>
        /// Executes the correct packet handler corresponding to this packet data.
        /// </summary>
        /// <param name="packetId">The client packet ID for this data.</param>
        /// <param name="packetData">The packet data instance.</param>
        private void ExecuteClientPacketHandler(ClientPacketId packetId, IPacketData packetData)
        {
            if (!_clientPacketHandlers.ContainsKey(packetId))
            {
                Logger.Get().Warn(this, $"There is no client packet handler registered for ID: {packetId}");
                return;
            }

            // Invoke the packet handler for this ID on the Unity main thread
            ThreadUtil.RunActionOnMainThread(() => {
                try {
                    _clientPacketHandlers[packetId].Invoke(packetData);
                } catch (Exception e) {
                    Logger.Get().Error(this,
                                       $"Exception occured while executing client packet handler for packet ID: {packetId}, message: {e.Message}, stacktrace: {e.StackTrace}");
                }
            });
        }
Exemplo n.º 6
0
        /// <summary>
        /// Callback method for when the client receives a login response from a server connection.
        /// </summary>
        /// <param name="loginResponse">The LoginResponse packet data.</param>
        private void OnConnect(LoginResponse loginResponse)
        {
            Logger.Get().Info(this, "Connection to server success");

            // De-register the connect failed and register the actual timeout handler if we time out
            UpdateManager.OnTimeout -= OnConnectTimedOut;
            UpdateManager.OnTimeout += () => {
                ThreadUtil.RunActionOnMainThread(() => {
                    TimeoutEvent?.Invoke();
                });
            };

            // Invoke callback if it exists on the main thread of Unity
            ThreadUtil.RunActionOnMainThread(() => {
                ConnectEvent?.Invoke(loginResponse);
            });

            IsConnected = true;
        }