Example #1
0
        public void Update()
        {
            NetIncomingMessage msg;

            while ((msg = NetworkClient.ReadMessage()) != null)
            {
                float packetTime = (float)DateTime.UtcNow.Subtract(_lastPacketTime).TotalMilliseconds;
                if (packetTime > 3f)
                {
                    _averagePacketTimes.Add(packetTime);

                    if (_averagePacketTimes.Count > 300)
                    {
                        _averagePacketTimes.RemoveAt(0);
                    }

                    Tickrate = (float)Math.Round(1000f / _averagePacketTimes.Average(), 2);
                }
                _lastPacketTime = DateTime.UtcNow;
                switch (msg.MessageType)
                {
                case NetIncomingMessageType.StatusChanged:
                {
                    NetConnectionStatus status = (NetConnectionStatus)msg.ReadByte();

                    if (status == NetConnectionStatus.Connected && !Connected)
                    {
                        Connected = true;
                        ConnectedToServerHub?.Invoke();
                    }
                    else
                    {
                        Misc.Logger.Info("New connection state: " + status);
                    }
                }
                break;

                case NetIncomingMessageType.Data:
                {
                    CommandType commandType = (CommandType)msg.PeekByte();

                    if (commandType == CommandType.Disconnect)
                    {
#if DEBUG
                        Misc.Logger.Info("Disconnecting...");
#endif
                        Disconnect();

                        foreach (Action <NetIncomingMessage> nextDel in MessageReceived.GetInvocationList())
                        {
                            try
                            {
                                msg.Position = 0;
                                nextDel.Invoke(msg);
                            }
                            catch (Exception e)
                            {
                                Misc.Logger.Error($"Exception in {nextDel.Method.Name} on message received event: {e}");
                            }
                        }

                        return;
                    }
                    else if (commandType == CommandType.SendEventMessage)
                    {
                        string header = msg.ReadString();
                        string data   = msg.ReadString();

#if DEBUG
                        Misc.Logger.Info($"Received event message! Header=\"{header}\", Data=\"{data}\"");
#endif
                        EventMessageReceived?.Invoke(header, data);
                    }
                    else
                    {
                        foreach (Action <NetIncomingMessage> nextDel in MessageReceived.GetInvocationList())
                        {
                            try
                            {
                                msg.Position = 0;
                                nextDel.Invoke(msg);
                            }
                            catch (Exception e)
                            {
                                Misc.Logger.Error($"Exception in {nextDel.Method.Name} on message received event: {e}");
                            }
                        }
                    }


                    if (commandType == CommandType.JoinRoom)
                    {
                        msg.Position = 8;
                        if (msg.PeekByte() == 0)
                        {
                            ClientJoinedRoom?.Invoke();
                        }
                    }
                    else if (commandType == CommandType.StartLevel)
                    {
                        if (playerInfo.playerState == PlayerState.Room)
                        {
                            ClientLevelStarted?.Invoke();
                        }
                    }
                };
                    break;

                case NetIncomingMessageType.WarningMessage:
                    Misc.Logger.Warning(msg.ReadString());
                    break;

                case NetIncomingMessageType.ErrorMessage:
                    Misc.Logger.Error(msg.ReadString());
                    break;

#if DEBUG
                case NetIncomingMessageType.VerboseDebugMessage:
                case NetIncomingMessageType.DebugMessage:
                    Misc.Logger.Info(msg.ReadString());
                    break;

                default:
                    Misc.Logger.Info("Unhandled message type: " + msg.MessageType);
                    break;
#endif
                }
                NetworkClient.Recycle(msg);
            }
        }
Example #2
0
        public void Update()
        {
            if (_receivedMessages.Count > 0)
            {
                networkClient.Recycle(_receivedMessages);
                _receivedMessages.Clear();
            }

            if (networkClient.ReadMessages(_receivedMessages) > 0)
            {
                for (int i = _receivedMessages.Count - 1; i >= 0; i--)
                {
                    if (_receivedMessages[i].MessageType == NetIncomingMessageType.Data && _receivedMessages[i].PeekByte() == (byte)CommandType.UpdatePlayerInfo)
                    {
                        if (_packets > 150)
                        {
                            _averagePacketTime = 0f;
                            _packets           = 0;
                        }

                        if (Time.realtimeSinceStartup - _lastPacketTime < 0.2f)
                        {
                            _averagePacketTime += Time.realtimeSinceStartup - _lastPacketTime;
                            _packets++;

                            if (_averagePacketTime != 0f && _packets != 0)
                            {
                                tickrate = 1f / (_averagePacketTime / _packets);
                            }
                        }
                        else
                        {
                            _averagePacketTime = 0f;
                            _packets           = 0;
                        }
                        _lastPacketTime = Time.realtimeSinceStartup;

                        MessageReceived?.Invoke(_receivedMessages[i]);
                        _receivedMessages[i].Position = 0;

                        break;
                    }
                }

                if (Config.Instance.SpectatorMode)
                {
                    for (int i = 0; i < _receivedMessages.Count; i++)
                    {
                        _receivedMessages[i].Position = 0;
                        if (_receivedMessages[i].MessageType == NetIncomingMessageType.Data && _receivedMessages[i].PeekByte() == (byte)CommandType.GetPlayerUpdates)
                        {
                            PlayerInfoUpdateReceived?.Invoke(_receivedMessages[i]);
                            _receivedMessages[i].Position = 0;
                        }
                    }
                }

                foreach (NetIncomingMessage msg in _receivedMessages)
                {
                    switch (msg.MessageType)
                    {
                    case NetIncomingMessageType.StatusChanged:
                    {
                        NetConnectionStatus status = (NetConnectionStatus)msg.ReadByte();

                        if (status == NetConnectionStatus.Connected && !connected)
                        {
                            connected = true;
                            ConnectedToServerHub?.Invoke();
                        }
                        else if (status == NetConnectionStatus.Disconnected && connected)
                        {
#if DEBUG
                            Plugin.log.Info("Disconnecting...");
#endif
                            Disconnect();

                            MessageReceived?.Invoke(null);
                        }
                        else if (status == NetConnectionStatus.Disconnected && !connected)
                        {
                            Plugin.log.Error("ServerHub refused connection! Reason: " + msg.ReadString());
                        }
                    }
                    break;

                    case NetIncomingMessageType.Data:
                    {
                        CommandType commandType = (CommandType)msg.PeekByte();

                        switch (commandType)
                        {
                        case CommandType.Disconnect:
                        {
#if DEBUG
                            Plugin.log.Info("Disconnecting...");
#endif
                            Disconnect();

                            MessageReceived?.Invoke(msg);
                        }
                        break;

                        case CommandType.SendEventMessage:
                        {
                            string header = msg.ReadString();
                            string data   = msg.ReadString();

#if DEBUG
                            Plugin.log.Info($"Received event message! Header=\"{header}\", Data=\"{data}\"");
#endif

                            foreach (Action <string, string> nextDel in EventMessageReceived.GetInvocationList())
                            {
                                try
                                {
                                    nextDel?.Invoke(header, data);
                                }
                                catch (Exception e)
                                {
                                    if (nextDel != null)
                                    {
                                        Plugin.log.Error($"Exception in {nextDel.Target.GetType()}.{nextDel.Method.Name} on event message received event: {e}");
                                    }
                                    else
                                    {
                                        Plugin.log.Error($"Exception in {nextDel.Target.GetType()}.{nextDel.Method.Name} on event message received event: {e}");
                                    }
                                }
                            }
                        }
                        break;

                        case CommandType.JoinRoom:
                        {
                            msg.Position = 8;
                            if (msg.PeekByte() == 0 && ClientJoinedRoom != null)
                            {
                                foreach (Action nextDel in ClientJoinedRoom.GetInvocationList())
                                {
                                    try
                                    {
                                        nextDel?.Invoke();
                                    }
                                    catch (Exception e)
                                    {
                                        if (nextDel != null)
                                        {
                                            Plugin.log.Error($"Exception in {nextDel.Target.GetType()}.{nextDel.Method.Name} on client joined room event: {e}");
                                        }
                                        else
                                        {
                                            Plugin.log.Error($"Exception on client joined room event: {e}");
                                        }
                                    }
                                }
                            }

                            MessageReceived?.Invoke(msg);
                        }
                        break;

                        case CommandType.StartLevel:
                        {
#if DEBUG
                            startNewDump = true;
                            packetsBuffer.Clear();
                            msg.Position = 8;
                            LevelOptionsInfo levelInfo = new LevelOptionsInfo(msg);
                            SongInfo         songInfo  = new SongInfo(msg);
                            List <byte>      buffer    = new List <byte>();
                            buffer.AddRange(levelInfo.ToBytes());
                            buffer.AddRange(HexConverter.ConvertHexToBytesX(songInfo.hash));

                            Plugin.log.Info("LevelID: " + songInfo.levelId + ", Bytes: " + BitConverter.ToString(buffer.ToArray()));

                            packetsBuffer.Enqueue(buffer.ToArray());
                            msg.Position = 0;
#endif
                            if (playerInfo != null && playerInfo.updateInfo.playerState == PlayerState.Room && ClientLevelStarted != null)
                            {
                                foreach (Action nextDel in ClientLevelStarted.GetInvocationList())
                                {
                                    try
                                    {
                                        nextDel?.Invoke();
                                    }
                                    catch (Exception e)
                                    {
                                        if (nextDel != null)
                                        {
                                            Plugin.log.Error($"Exception in {nextDel.Target.GetType()}.{nextDel.Method.Name} on client level started event: {e}");
                                        }
                                        else
                                        {
                                            Plugin.log.Error($"Exception on client level started event: {e}");
                                        }
                                    }
                                }
                            }

                            MessageReceived?.Invoke(msg);
                        }
                        break;

                        case CommandType.UpdatePlayerInfo:
                            break;

                        default:
                        {
                            MessageReceived?.Invoke(msg);
                        }
                        break;
                        }
                    };
                        break;

                    case NetIncomingMessageType.WarningMessage:
                        Plugin.log.Warn(msg.ReadString());
                        break;

                    case NetIncomingMessageType.ErrorMessage:
                        Plugin.log.Error(msg.ReadString());
                        break;

#if DEBUG
                    case NetIncomingMessageType.VerboseDebugMessage:
                    case NetIncomingMessageType.DebugMessage:
                        Plugin.log.Info(msg.ReadString());
                        break;

                    default:
                        Plugin.log.Info("Unhandled message type: " + msg.MessageType);
                        break;
#endif
                    }
                }

                networkClient.Recycle(_receivedMessages);
                _receivedMessages.Clear();
            }

            if (connected && networkClient.ConnectionsCount == 0)
            {
#if DEBUG
                Plugin.log.Info("Connection lost! Disconnecting...");
#endif
                Disconnect();

                MessageReceived?.Invoke(null);
            }

            if ((Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl)) && Input.GetKeyDown(KeyCode.F1))
            {
                GarbageCollector.GCMode = GarbageCollector.GCMode == GarbageCollector.Mode.Enabled ? GarbageCollector.Mode.Disabled : GarbageCollector.Mode.Enabled;
                Plugin.log.Notice($"Garbage collector {GarbageCollector.GCMode.ToString()}!");
            }
        }
Example #3
0
        public void Update()
        {
            if (NetworkClient.ReadMessages(_receivedMessages) > 0)
            {
                try
                {
                    if (_receivedMessages.Any(x => x.PeekByte() != (byte)CommandType.UpdateVoIPData))
                    {
                        float packetTime = (float)DateTime.UtcNow.Subtract(_lastPacketTime).TotalMilliseconds;
                        if (packetTime > 2f)
                        {
                            _averagePacketTimes.Add(packetTime);

                            if (_averagePacketTimes.Count > 150)
                            {
                                _averagePacketTimes.RemoveAt(0);
                            }

                            Tickrate = (float)Math.Round(1000f / _averagePacketTimes.Where(x => x > 2f).Average(), 2);
                        }
                        _lastPacketTime = DateTime.UtcNow;
                    }

                    NetIncomingMessage lastUpdate = _receivedMessages.LastOrDefault(x => x.MessageType == NetIncomingMessageType.Data && x.PeekByte() == (byte)CommandType.UpdatePlayerInfo);

                    if (lastUpdate != null)
                    {
                        foreach (NetIncomingMessage msg in _receivedMessages.Where(x => x.MessageType == NetIncomingMessageType.Data && x.PeekByte() == (byte)CommandType.UpdatePlayerInfo))
                        {
                            foreach (Action <NetIncomingMessage> nextDel in PlayerInfoUpdateReceived.GetInvocationList())
                            {
                                try
                                {
                                    msg.Position = 0;
                                    nextDel.Invoke(msg);
                                }
                                catch (Exception e)
                                {
                                    Misc.Logger.Error($"Exception in {nextDel.Target.GetType()}.{nextDel.Method.Name} on update received event: {e}");
                                }
                            }
                        }

                        _receivedMessages.RemoveAll(x => x.MessageType == NetIncomingMessageType.Data && x.PeekByte() == (byte)CommandType.UpdatePlayerInfo);

                        foreach (Action <NetIncomingMessage> nextDel in MessageReceived.GetInvocationList())
                        {
                            try
                            {
                                lastUpdate.Position = 0;
                                nextDel.Invoke(lastUpdate);
                            }
                            catch (Exception e)
                            {
                                Misc.Logger.Error($"Exception in {nextDel.Target.GetType()}.{nextDel.Method.Name} on message received event: {e}");
                            }
                        }
                    }

                    foreach (NetIncomingMessage msg in _receivedMessages)
                    {
                        switch (msg.MessageType)
                        {
                        case NetIncomingMessageType.StatusChanged:
                        {
                            NetConnectionStatus status = (NetConnectionStatus)msg.ReadByte();

                            if (status == NetConnectionStatus.Connected && !Connected)
                            {
                                Connected = true;
                                ConnectedToServerHub?.Invoke();
                            }
                            else if (status == NetConnectionStatus.Disconnected && Connected)
                            {
#if DEBUG
                                Misc.Logger.Info("Disconnecting...");
#endif
                                Disconnect();

                                foreach (Action <NetIncomingMessage> nextDel in MessageReceived.GetInvocationList())
                                {
                                    try
                                    {
                                        nextDel.Invoke(null);
                                    }
                                    catch (Exception e)
                                    {
                                        Misc.Logger.Error($"Exception in {nextDel.Target.GetType()}.{nextDel.Method.Name} on message received event: {e}");
                                    }
                                }
                            }
                            else if (status == NetConnectionStatus.Disconnected && !Connected)
                            {
                                Misc.Logger.Info("ServerHub refused connection! Reason: " + msg.ReadString());
                            }
                        }
                        break;

                        case NetIncomingMessageType.Data:
                        {
                            CommandType commandType = (CommandType)msg.PeekByte();

                            if (commandType == CommandType.Disconnect)
                            {
#if DEBUG
                                Misc.Logger.Info("Disconnecting...");
#endif
                                Disconnect();

                                foreach (Action <NetIncomingMessage> nextDel in MessageReceived.GetInvocationList())
                                {
                                    try
                                    {
                                        msg.Position = 0;
                                        nextDel.Invoke(msg);
                                    }
                                    catch (Exception e)
                                    {
                                        Misc.Logger.Error($"Exception in {nextDel.Target.GetType()}.{nextDel.Method.Name} on message received event: {e}");
                                    }
                                }

                                return;
                            }
                            else if (commandType == CommandType.SendEventMessage)
                            {
                                string header = msg.ReadString();
                                string data   = msg.ReadString();

#if DEBUG
                                Misc.Logger.Info($"Received event message! Header=\"{header}\", Data=\"{data}\"");
#endif
                                foreach (Action <string, string> nextDel in EventMessageReceived.GetInvocationList())
                                {
                                    try
                                    {
                                        nextDel.Invoke(header, data);
                                    }
                                    catch (Exception e)
                                    {
                                        Misc.Logger.Error($"Exception in {nextDel.Target.GetType()}.{nextDel.Method.Name} on event message received event: {e}");
                                    }
                                }
                            }
                            else
                            {
                                if (MessageReceived != null)
                                {
                                    foreach (Action <NetIncomingMessage> nextDel in MessageReceived.GetInvocationList())
                                    {
                                        try
                                        {
                                            msg.Position = 0;
                                            nextDel.Invoke(msg);
                                        }
                                        catch (Exception e)
                                        {
                                            Misc.Logger.Error($"Exception in {nextDel.Target.GetType()}.{nextDel.Method.Name} on message received event: {e}");
                                        }
                                    }
                                }
                            }


                            if (commandType == CommandType.JoinRoom)
                            {
                                msg.Position = 8;
                                if (msg.PeekByte() == 0 && ClientJoinedRoom != null)
                                {
                                    foreach (Action nextDel in ClientJoinedRoom.GetInvocationList())
                                    {
                                        try
                                        {
                                            nextDel.Invoke();
                                        }
                                        catch (Exception e)
                                        {
                                            Misc.Logger.Error($"Exception in {nextDel.Target.GetType()}.{nextDel.Method.Name} on client joined room event: {e}");
                                        }
                                    }
                                }
                            }
                            else if (commandType == CommandType.StartLevel)
                            {
                                if (playerInfo.playerState == PlayerState.Room)
                                {
                                    foreach (Action nextDel in ClientLevelStarted.GetInvocationList())
                                    {
                                        try
                                        {
                                            nextDel.Invoke();
                                        }
                                        catch (Exception e)
                                        {
                                            Misc.Logger.Error($"Exception in {nextDel.Target.GetType()}.{nextDel.Method.Name} on client level started event: {e}");
                                        }
                                    }
                                }
                            }
                        };
                            break;

                        case NetIncomingMessageType.WarningMessage:
                            Misc.Logger.Warning(msg.ReadString());
                            break;

                        case NetIncomingMessageType.ErrorMessage:
                            Misc.Logger.Error(msg.ReadString());
                            break;

#if DEBUG
                        case NetIncomingMessageType.VerboseDebugMessage:
                        case NetIncomingMessageType.DebugMessage:
                            Misc.Logger.Info(msg.ReadString());
                            break;

                        default:
                            Misc.Logger.Info("Unhandled message type: " + msg.MessageType);
                            break;
#endif
                        }
                        NetworkClient.Recycle(msg);
                    }
                }catch (Exception e)
                {
#if DEBUG
                    Misc.Logger.Exception($"Exception on message received event: {e}");
#endif
                }
                _receivedMessages.Clear();
            }

            if (Connected && NetworkClient.ConnectionsCount == 0)
            {
#if DEBUG
                Misc.Logger.Info("Connection lost! Disconnecting...");
#endif
                Disconnect();

                foreach (Action <NetIncomingMessage> nextDel in MessageReceived.GetInvocationList())
                {
                    try
                    {
                        nextDel.Invoke(null);
                    }
                    catch (Exception e)
                    {
                        Misc.Logger.Error($"Exception in {nextDel.Target.GetType()}.{nextDel.Method.Name} on message received event: {e}");
                    }
                }
            }
        }
Example #4
0
        public void Update()
        {
            if (networkClient.ReadMessages(_receivedMessages) > 0)
            {
                try
                {
                    if (_receivedMessages.Any(x => x.PeekByte() != (byte)CommandType.UpdateVoIPData))
                    {
                        float packetTime = (float)DateTime.UtcNow.Subtract(_lastPacketTime).TotalMilliseconds;
                        if (packetTime > 2f)
                        {
                            _averagePacketTimes.Add(packetTime);

                            if (_averagePacketTimes.Count > 150)
                            {
                                _averagePacketTimes.RemoveAt(0);
                            }

                            tickrate = (float)Math.Round(1000f / _averagePacketTimes.Where(x => x > 2f).Average(), 2);
                        }
                        _lastPacketTime = DateTime.UtcNow;
                    }

                    try
                    {
                        if (Config.Instance.SpectatorMode)
                        {
                            foreach (var lastPlayerHistoryUpdates in _receivedMessages.Where(x => x.MessageType == NetIncomingMessageType.Data && x.PeekByte() == (byte)CommandType.GetPlayerUpdates))
                            {
                                foreach (Action <NetIncomingMessage> nextDel in PlayerInfoUpdateReceived.GetInvocationList())
                                {
                                    try
                                    {
                                        lastPlayerHistoryUpdates.Position = 0;
                                        nextDel?.Invoke(lastPlayerHistoryUpdates);
                                    }
                                    catch (Exception e)
                                    {
                                        if (nextDel != null)
                                        {
                                            Plugin.log.Error($"Exception in {nextDel.Target.GetType()}.{nextDel.Method.Name} on update received event: {e}");
                                        }
                                        else
                                        {
                                            Plugin.log.Error($"Exception on update received event: {e}");
                                        }
                                    }
                                }
                                lastPlayerHistoryUpdates.Position = 0;
                            }
                        }
                    }catch (Exception e)
                    {
                        Plugin.log.Error("Unable to parse GetPlayerUpdates message! Exception: " + e);
                    }

                    try
                    {
                        NetIncomingMessage lastUpdate = _receivedMessages.LastOrDefault(x => x.MessageType == NetIncomingMessageType.Data && x.PeekByte() == (byte)CommandType.UpdatePlayerInfo);

                        if (lastUpdate != null)
                        {
                            _receivedMessages.RemoveAll(x => x.MessageType == NetIncomingMessageType.Data && x.PeekByte() == (byte)CommandType.UpdatePlayerInfo);

                            foreach (Action <NetIncomingMessage> nextDel in MessageReceived.GetInvocationList())
                            {
                                try
                                {
                                    lastUpdate.Position = 0;
                                    nextDel?.Invoke(lastUpdate);
                                }
                                catch (Exception e)
                                {
                                    if (nextDel != null)
                                    {
                                        Plugin.log.Error($"Exception in {nextDel.Target.GetType()}.{nextDel.Method.Name} on update received event: {e}");
                                    }
                                    else
                                    {
                                        Plugin.log.Error($"Exception on update received event: {e}");
                                    }
                                }
                            }
                            lastUpdate.Position = 0;
                        }
                    }catch (Exception e)
                    {
                        Plugin.log.Error("Unable to parse UpdatePlayerInfo message! Exception: " + e);
                    }

                    foreach (NetIncomingMessage msg in _receivedMessages)
                    {
                        switch (msg.MessageType)
                        {
                        case NetIncomingMessageType.StatusChanged:
                        {
                            NetConnectionStatus status = (NetConnectionStatus)msg.ReadByte();

                            if (status == NetConnectionStatus.Connected && !connected)
                            {
                                connected = true;
                                ConnectedToServerHub?.Invoke();
                            }
                            else if (status == NetConnectionStatus.Disconnected && connected)
                            {
#if DEBUG
                                Plugin.log.Info("Disconnecting...");
#endif
                                Disconnect();

                                foreach (Action <NetIncomingMessage> nextDel in MessageReceived.GetInvocationList())
                                {
                                    try
                                    {
                                        nextDel?.Invoke(null);
                                    }
                                    catch (Exception e)
                                    {
                                        if (nextDel != null)
                                        {
                                            Plugin.log.Error($"Exception in {nextDel.Target.GetType()}.{nextDel.Method.Name} on message received event: {e}");
                                        }
                                        else
                                        {
                                            Plugin.log.Error($"Exception on message received event: {e}");
                                        }
                                    }
                                }
                            }
                            else if (status == NetConnectionStatus.Disconnected && !connected)
                            {
                                Plugin.log.Error("ServerHub refused connection! Reason: " + msg.ReadString());
                            }
                        }
                        break;

                        case NetIncomingMessageType.Data:
                        {
                            CommandType commandType = (CommandType)msg.PeekByte();

                            if (commandType == CommandType.Disconnect)
                            {
#if DEBUG
                                Plugin.log.Info("Disconnecting...");
#endif
                                Disconnect();

                                foreach (Action <NetIncomingMessage> nextDel in MessageReceived.GetInvocationList())
                                {
                                    try
                                    {
                                        msg.Position = 0;
                                        nextDel?.Invoke(msg);
                                    }
                                    catch (Exception e)
                                    {
                                        if (nextDel != null)
                                        {
                                            Plugin.log.Error($"Exception in {nextDel.Target.GetType()}.{nextDel.Method.Name} on message received event: {e}");
                                        }
                                        else
                                        {
                                            Plugin.log.Error($"Exception on message received event: {e}");
                                        }
                                    }
                                }

                                return;
                            }
                            else if (commandType == CommandType.SendEventMessage)
                            {
                                string header = msg.ReadString();
                                string data   = msg.ReadString();

#if DEBUG
                                Plugin.log.Info($"Received event message! Header=\"{header}\", Data=\"{data}\"");
#endif
                                foreach (Action <string, string> nextDel in EventMessageReceived.GetInvocationList())
                                {
                                    try
                                    {
                                        nextDel?.Invoke(header, data);
                                    }
                                    catch (Exception e)
                                    {
                                        if (nextDel != null)
                                        {
                                            Plugin.log.Error($"Exception in {nextDel.Target.GetType()}.{nextDel.Method.Name} on event message received event: {e}");
                                        }
                                        else
                                        {
                                            Plugin.log.Error($"Exception in {nextDel.Target.GetType()}.{nextDel.Method.Name} on event message received event: {e}");
                                        }
                                    }
                                }
                            }
                            else
                            {
                                if (MessageReceived != null)
                                {
                                    foreach (Action <NetIncomingMessage> nextDel in MessageReceived.GetInvocationList())
                                    {
                                        try
                                        {
                                            msg.Position = 0;
                                            nextDel?.Invoke(msg);
                                        }
                                        catch (Exception e)
                                        {
                                            if (nextDel != null)
                                            {
                                                Plugin.log.Error($"Exception in {nextDel.Target.GetType()}.{nextDel.Method.Name} on message received event: {e}");
                                            }
                                            else
                                            {
                                                Plugin.log.Error($"Exception on message received event: {e}");
                                            }
                                        }
                                    }
                                }
                            }


                            if (commandType == CommandType.JoinRoom)
                            {
                                msg.Position = 8;
                                if (msg.PeekByte() == 0 && ClientJoinedRoom != null)
                                {
                                    foreach (Action nextDel in ClientJoinedRoom.GetInvocationList())
                                    {
                                        try
                                        {
                                            nextDel?.Invoke();
                                        }
                                        catch (Exception e)
                                        {
                                            if (nextDel != null)
                                            {
                                                Plugin.log.Error($"Exception in {nextDel.Target.GetType()}.{nextDel.Method.Name} on client joined room event: {e}");
                                            }
                                            else
                                            {
                                                Plugin.log.Error($"Exception on client joined room event: {e}");
                                            }
                                        }
                                    }
                                }
                            }
                            else if (commandType == CommandType.StartLevel)
                            {
#if DEBUG
                                startNewDump = true;
                                packetsBuffer.Clear();
                                msg.Position = 8;
                                LevelOptionsInfo levelInfo = new LevelOptionsInfo(msg);
                                SongInfo         songInfo  = new SongInfo(msg);
                                List <byte>      buffer    = new List <byte>();
                                buffer.AddRange(levelInfo.ToBytes());
                                buffer.AddRange(HexConverter.ConvertHexToBytesX(songInfo.levelId));

                                Plugin.log.Info("LevelID: " + songInfo.levelId + ", Bytes: " + BitConverter.ToString(buffer.ToArray()));

                                packetsBuffer.Enqueue(buffer.ToArray());
                                msg.Position = 0;
#endif
                                if (playerInfo.playerState == PlayerState.Room)
                                {
                                    foreach (Action nextDel in ClientLevelStarted.GetInvocationList())
                                    {
                                        try
                                        {
                                            nextDel?.Invoke();
                                        }
                                        catch (Exception e)
                                        {
                                            if (nextDel != null)
                                            {
                                                Plugin.log.Error($"Exception in {nextDel.Target.GetType()}.{nextDel.Method.Name} on client level started event: {e}");
                                            }
                                            else
                                            {
                                                Plugin.log.Error($"Exception on client level started event: {e}");
                                            }
                                        }
                                    }
                                }
                            }
                        };
                            break;

                        case NetIncomingMessageType.WarningMessage:
                            Plugin.log.Warn(msg.ReadString());
                            break;

                        case NetIncomingMessageType.ErrorMessage:
                            Plugin.log.Error(msg.ReadString());
                            break;

#if DEBUG
                        case NetIncomingMessageType.VerboseDebugMessage:
                        case NetIncomingMessageType.DebugMessage:
                            Plugin.log.Info(msg.ReadString());
                            break;

                        default:
                            Plugin.log.Info("Unhandled message type: " + msg.MessageType);
                            break;
#endif
                        }
                        networkClient.Recycle(msg);
                    }
                }catch (Exception e)
                {
#if DEBUG
                    Plugin.log.Critical($"Exception on message received event: {e}");
#endif
                }
                _receivedMessages.Clear();
            }

            if (connected && networkClient.ConnectionsCount == 0)
            {
#if DEBUG
                Plugin.log.Info("Connection lost! Disconnecting...");
#endif
                Disconnect();

                foreach (Action <NetIncomingMessage> nextDel in MessageReceived.GetInvocationList())
                {
                    try
                    {
                        nextDel.Invoke(null);
                    }
                    catch (Exception e)
                    {
                        if (nextDel != null)
                        {
                            Plugin.log.Error($"Exception in {nextDel.Target.GetType()}.{nextDel.Method.Name} on message received event: {e}");
                        }
                        else
                        {
                            Plugin.log.Error($"Exception on message received event: {e}");
                        }
                    }
                }
            }
        }