示例#1
0
        private void HandlePacket(Packet message)
        {
            if (message == null)
            {
                return;
            }

            try
            {
                RakOfflineHandler.TraceReceive(Log, message);

                if (message.Id < (int)DefaultMessageIdTypes.ID_USER_PACKET_ENUM)
                {
                    // Standard RakNet online message handlers
                    switch (message)
                    {
                    case ConnectedPing connectedPing:
                        HandleConnectedPing(connectedPing);
                        break;

                    case ConnectedPong connectedPong:
                        HandleConnectedPong(connectedPong);
                        break;

                    case DetectLostConnections _:
                        break;

                    case ConnectionRequest connectionRequest:
                        HandleConnectionRequest(connectionRequest);
                        break;

                    case ConnectionRequestAccepted connectionRequestAccepted:
                        HandleConnectionRequestAccepted(connectionRequestAccepted);
                        break;

                    case NewIncomingConnection newIncomingConnection:
                        HandleNewIncomingConnection(newIncomingConnection);
                        break;

                    case DisconnectionNotification _:
                        HandleDisconnectionNotification();
                        break;

                    default:
                        Log.Error($"Unhandled packet: {message.GetType().Name} 0x{message.Id:X2} for user: {Username}, IP {EndPoint.Address}");
                        if (Log.IsDebugEnabled)
                        {
                            Log.Warn($"Unknown packet 0x{message.Id:X2}\n{Packet.HexDump(message.Bytes)}");
                        }
                        break;
                    }
                }
                else
                {
                    try
                    {
                        CustomMessageHandler.HandlePacket(message);
                    }
                    catch (Exception e)
                    {
                        // ignore
                        Log.Warn($"Custom message handler error", e);
                    }
                }

                if (message.Timer.IsRunning)
                {
                    long elapsedMilliseconds = message.Timer.ElapsedMilliseconds;
                    if (elapsedMilliseconds > 1000)
                    {
                        Log.WarnFormat("Packet (0x{1:x2}) handling too long {0}ms for {2}", elapsedMilliseconds, message.Id, Username);
                    }
                }
                else
                {
                    Log.WarnFormat("Packet (0x{0:x2}) timer not started for {1}.", message.Id, Username);
                }
            }
            catch (Exception e)
            {
                Log.Error("Packet handling", e);
                throw;
            }
            finally
            {
                message?.PutPool();
            }
        }