示例#1
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}");
                    }
                }
            }
        }
示例#2
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}");
                        }
                    }
                }
            }
        }