示例#1
0
        /// <inheritdoc cref="IMessageReader.ReadFrom"/>
        public IMessage ReadFrom(ref SpanBufferReader bufferReader, byte packetProperty)
        {
            if (packetProperty != 0x00)
            {
                var readPacketProperty = bufferReader.ReadUInt8();
                if (readPacketProperty != packetProperty)
                {
                    throw new InvalidDataContractException(
                              "Invalid packet property " +
                              $"(PacketProperty={readPacketProperty}, Expected={packetProperty})."
                              );
                }
            }
            var messageGroup = bufferReader.ReadUInt32();

            if (!_messageRegistries.TryGetValue(messageGroup, out var messageRegistry))
            {
                throw new InvalidDataContractException($"Invalid message group (MessageGroup={messageGroup}).");
            }
            var protocolVersion = bufferReader.ReadVarUInt();

            if (protocolVersion != ProtocolVersion)
            {
                throw new InvalidDataContractException($"Invalid message protocol version (ProtocolVersion={protocolVersion}).");
            }
            var length = bufferReader.ReadVarUInt();

            if (bufferReader.RemainingSize < length)
            {
                throw new InvalidDataContractException($"Message truncated (RemainingSize={bufferReader.RemainingSize}, Expected={length}).");
            }
            var messageId = bufferReader.ReadVarUInt();

            if (!messageRegistry.TryCreateMessage(messageId, out var message))
            {
                throw new InvalidDataContractException($"Invalid message identifier (MessageId={messageId}).");
            }
            if (message is IReliableRequest)
            {
                ((IReliableRequest)message).RequestId = bufferReader.ReadUInt32();
            }
            if (message is IReliableResponse)
            {
                ((IReliableResponse)message).ResponseId = bufferReader.ReadUInt32();
            }
            message.ReadFrom(ref bufferReader);
            return(message);
        }
        public override void ReadFrom(ref SpanBufferReader bufferReader)
        {
            Result = (ResultCode)bufferReader.ReadUInt8();
            if (!Success)
            {
                return;
            }

            UserId             = bufferReader.ReadString();
            UserName           = bufferReader.ReadString();
            Secret             = bufferReader.ReadString();
            DiscoveryPolicy    = (DiscoveryPolicy)bufferReader.ReadByte();
            InvitePolicy       = (InvitePolicy)bufferReader.ReadByte();
            MaximumPlayerCount = bufferReader.ReadVarInt();
            Configuration      = new GameplayServerConfiguration();
            Configuration.ReadFrom(ref bufferReader);
            var flags = bufferReader.ReadByte();

            IsConnectionOwner = (flags & 1) > 0;
            IsDedicatedServer = (flags & 2) > 0;
            RemoteEndPoint    = bufferReader.ReadIPEndPoint();
            Random            = bufferReader.ReadBytes(32).ToArray();
            PublicKey         = bufferReader.ReadVarBytes().ToArray();
        }