Пример #1
0
        public async Task SendPacket(UncompressedPacket packet)
        {
            dynamic innerPacket = new object();

            switch (_state)
            {
            case SessionState.Handshaking:
                innerPacket = DeserializeHandshakingPacket(packet);
                break;

            case SessionState.Status:
                innerPacket = DeserializeStatusPacket(packet);
                break;

            case SessionState.Login:
                innerPacket = DeserializeLoginPacket(packet);
                break;

            case SessionState.Play:
                await _user.ForwardPacket(packet);

                return;

            case SessionState.Closed:
                break;

            default:
                break;
            }

            await DispatchPacket(innerPacket);
        }
Пример #2
0
 public Task ForwardPacket(UncompressedPacket packet)
 {
     return(_player.Tell(new ServerboundPacketMessage
     {
         Packet = packet
     }));
 }
Пример #3
0
        private object DeserializePlayPacket(UncompressedPacket packet)
        {
            var    br = new SpanReader(packet.Data);
            object innerPacket;

            switch (packet.PacketId)
            {
            // Entity Look And Relative Move
            case 0x27:
                innerPacket = EntityLookAndRelativeMove.Deserialize(ref br);
                break;

            // Chunk Data
            case 0x20:
                innerPacket = ChunkData.Deserialize(ref br, true);
                break;

            default:
                // Debug.LogWarning($"Unrecognizable packet id: 0x{packet.PacketId:X2}.");
                return(null);
            }

            if (!br.IsCosumed)
            {
                throw new InvalidDataException($"Packet data is not fully consumed.");
            }
            return(innerPacket);
        }
Пример #4
0
        private Task DispatchLoginPackets(UncompressedPacket packet)
        {
            var  br = new SpanReader(packet.Data);
            Task task;

            switch (packet.PacketId)
            {
            // Login Start
            case 0x00:
                task = DispatchPacket(PacketDeserializer.Deserialize <LoginStart>(ref br));
                break;

            // Encryption Response
            case 0x01:
                task = DispatchPacket(PacketDeserializer.Deserialize <EncryptionResponse>(ref br));
                break;

            default:
                throw new InvalidDataException($"Unrecognizable packet id: 0x{packet.PacketId:X2}.");
            }

            if (!br.IsCosumed)
            {
                throw new InvalidDataException($"Packet data is not fully consumed.");
            }
            return(task);
        }
Пример #5
0
        private async Task DispatchIncomingPacket()
        {
            using (var bufferScope = _bufferPool.CreateScope())
            {
                var packet = _uncompressedPacketObjectPool.Get();
                try
                {
                    if (_useCompression)
                    {
                        var compressedPacket = await CompressedPacket.DeserializeAsync(_remoteStream, null);

                        packet = PacketCompress.Decompress(compressedPacket, bufferScope, 0, packet);
                    }
                    else
                    {
                        packet = await UncompressedPacket.DeserializeAsync(_remoteStream, bufferScope, packet);
                    }

                    await DispatchIncomingPacket(packet);
                }
                finally
                {
                    _uncompressedPacketObjectPool.Return(packet);
                }
            }
        }
Пример #6
0
        private object DeserializeLoginPacket(UncompressedPacket packet)
        {
            var    br = new SpanReader(packet.Data);
            object innerPacket;

            switch (packet.PacketId)
            {
            // Disconnect
            case 0x00:
                innerPacket = LoginDisconnect.Deserialize(ref br);
                break;

            // Login Success
            case 0x02:
                innerPacket = LoginSuccess.Deserialize(ref br);
                break;

            default:
                throw new InvalidDataException($"Unrecognizable packet id: 0x{packet.PacketId:X2}.");
            }

            if (!br.IsCosumed)
            {
                throw new InvalidDataException($"Packet data is not fully consumed.");
            }
            return(innerPacket);
        }
Пример #7
0
        public Task SendPacket(UncompressedPacket packet)
        {
            switch (_state)
            {
            case SessionState.Handshaking:
                return(DispatchHandshakingPackets(packet));

            case SessionState.Status:
                return(DispatchStatusPackets(packet));

            case SessionState.Login:
                return(DispatchLoginPackets(packet));

            case SessionState.Play:
                return(_user.ForwardPacket(packet));

            case SessionState.Closed:
                break;

            default:
                break;
            }

            return(Task.CompletedTask);
        }
Пример #8
0
 public Task ForwardPacket(UncompressedPacket packet)
 {
     _player.InvokeOneWay(p => p.Tell(new ServerboundPacketMessage
     {
         Packet = packet
     }));
     return(Task.CompletedTask);
 }
Пример #9
0
        private void InvokeDispatchInvalidPacket(SessionStatus status, UncompressedPacket packet)
        {
            if (onDispatchInvalidPacket != null)
            {
                onDispatchInvalidPacket(status, packet);
            }

            this.OnDispatchInvalidPacket(status, packet);
        }
        private void OnReceivePacket(UncompressedPacket rawPacket)
        {
            var packet = DeserializePlayPacket(rawPacket);

            if (packet != null)
            {
                _deferredPacket.Enqueue(packet);
            }
        }
Пример #11
0
        public Task SendPacket(uint packetId, Immutable <byte[]> data)
        {
            var packet = new UncompressedPacket
            {
                PacketId = packetId,
                Data     = new ArraySegment <byte>(data.Value)
            };

            _subsManager.Notify(n => n.ReceivePacket(packet));
            return(Task.CompletedTask);
        }
Пример #12
0
        private Task SendPacket(uint packetId, byte[] data)
        {
            var packet = new UncompressedPacket
            {
                PacketId = packetId,
                Data     = data
            };

            _subsManager.Notify(n => n.ReceivePacket(packet));
            return(Task.CompletedTask);
        }
Пример #13
0
        public Task SendPacket(uint packetId, byte[] data)
        {
            var packet = new UncompressedPacket
            {
                PacketId = packetId,
                Data     = new ArraySegment <byte>(data)
            };

            _observers.ForEach(o => o.ReceivePacket(packet));
            return(Task.CompletedTask);
        }
Пример #14
0
 private async Task SendOutcomingPacket(UncompressedPacket packet)
 {
     if (_useCompression)
     {
         var newPacket = PacketCompress.Compress(ref packet);
         await newPacket.SerializeAsync(_remoteStream);
     }
     else
     {
         await packet.SerializeAsync(_remoteStream);
     }
 }
Пример #15
0
 public async Task SendOutcomingUncompressedPacket(UncompressedPacket packet)
 {
     if (packet == null)
     {
         _tcpClient.Client.Shutdown(SocketShutdown.Send);
     }
     else
     {
         var newPacket = _packetCompress.Compress(packet);
         await newPacket.SerializeAsync(_tcpClient.GetStream());
     }
 }
Пример #16
0
 private async void DispatchOutcomingPacket(UncompressedPacket packet)
 {
     try
     {
         if (!_outcomingPacketDispatcher.Completion.IsCompleted)
         {
             await _outcomingPacketDispatcher.SendAsync(packet);
         }
     }
     catch
     {
         _outcomingPacketDispatcher.Complete();
     }
 }
        private object DeserializeHandshakingPacket(UncompressedPacket packet)
        {
            var br = new SpanReader(packet.Data);

            switch (packet.PacketId)
            {
            // Handshake
            case 0x00:
                return(Handshake.Deserialize(ref br));

            default:
                throw new InvalidDataException($"Unrecognizable packet id: 0x{packet.PacketId:X2}.");
            }
        }
        private Task DispatchHandshakingPackets(UncompressedPacket packet)
        {
            var br = new SpanReader(packet.Data);

            switch (packet.PacketId)
            {
            // Handshake
            case 0x00:
                return(DispatchPacket(PacketDeserializer.Deserialize <Handshake>(ref br)));

            default:
                throw new InvalidDataException($"Unrecognizable packet id: 0x{packet.PacketId:X2}.");
            }
        }
        private object DeserializeHandshakingPacket(ref UncompressedPacket packet)
        {
            using (var br = new BinaryReader(new MemoryStream(packet.Data)))
            {
                switch (packet.PacketId)
                {
                // Handshake
                case 0x00:
                    return(Handshake.Deserialize(br));

                default:
                    throw new InvalidDataException($"Unrecognizable packet id: 0x{packet.PacketId:X}.");
                }
            }
        }
Пример #20
0
        private object DeserializePlayPacket(ref UncompressedPacket packet)
        {
            using (var br = new BinaryReader(new MemoryStream(packet.Data)))
            {
                object innerPacket;
                switch (packet.PacketId)
                {
                // Teleport Confirm
                case 0x00:
                    innerPacket = TeleportConfirm.Deserialize(br);
                    break;

                // Chat Message
                case 0x03:
                    innerPacket = ServerboundChatMessage.Deserialize(br);
                    break;

                // Client Settings
                case 0x05:
                    innerPacket = ClientSettings.Deserialize(br);
                    break;

                // Plugin Message
                case 0x0A:
                    innerPacket = ServerboundPluginMessage.Deserialize(br);
                    break;

                // Keep Alive
                case 0x0C:
                    innerPacket = ServerboundKeepAlive.Deserialize(br);
                    break;

                // Position And Look
                case 0x0F:
                    innerPacket = ServerboundPositionAndLook.Deserialize(br);
                    break;

                default:
                    throw new InvalidDataException($"Unrecognizable packet id: 0x{packet.PacketId:X}.");
                }

                if (br.BaseStream.Position != br.BaseStream.Length)
                {
                    throw new InvalidDataException($"Packet data is not fully consumed.");
                }
                return(innerPacket);
            }
        }
Пример #21
0
        private async Task DispatchIncomingPacket()
        {
            UncompressedPacket packet;

            if (_useCompression)
            {
                var compressedPacket = await CompressedPacket.DeserializeAsync(_remoteStream);

                packet = PacketCompress.Decompress(ref compressedPacket);
            }
            else
            {
                packet = await UncompressedPacket.DeserializeAsync(_remoteStream);
            }
            await DispatchIncomingPacket(packet);
        }
        private Task DispatchStatusPackets(UncompressedPacket packet)
        {
            var br = new SpanReader(packet.Data);

            switch (packet.PacketId)
            {
            // Request
            case 0x00:
                return(DispatchPacket(PacketDeserializer.Deserialize <Request>(ref br)));

            // Ping
            case 0x01:
                return(DispatchPacket(PacketDeserializer.Deserialize <Ping>(ref br)));

            default:
                throw new InvalidDataException($"Unrecognizable packet id: 0x{packet.PacketId:X2}.");
            }
        }
Пример #23
0
 private async Task SendOutcomingPacket(UncompressedPacket packet)
 {
     // Close
     if (packet == null)
     {
         _tcpClient.Client.Shutdown(SocketShutdown.Send);
         _outcomingPacketDispatcher.Complete();
     }
     else if (_useCompression)
     {
         var newPacket = PacketCompress.Compress(ref packet);
         await newPacket.SerializeAsync(_remoteStream);
     }
     else
     {
         await packet.SerializeAsync(_remoteStream);
     }
 }
Пример #24
0
        private object DeserializeStatusPacket(UncompressedPacket packet)
        {
            var br = new SpanReader(packet.Data);

            switch (packet.PacketId)
            {
            // Request
            case 0x00:
                return(Request.Deserialize(ref br));

            // Ping
            case 0x01:
                return(Ping.Deserialize(ref br));

            default:
                throw new InvalidDataException($"Unrecognizable packet id: 0x{packet.PacketId:X2}.");
            }
        }
Пример #25
0
        public Task SendPacket(uint packetId, Immutable <byte[]> data)
        {
            var packet = new UncompressedPacket
            {
                PacketId = packetId,
                Data     = new ArraySegment <byte>(data.Value)
            };

            if (_subsManager.Count == 0)
            {
                DeactivateOnIdle();
            }
            else
            {
                _subsManager.Notify(n => n.ReceivePacket(packet));
            }
            return(Task.CompletedTask);
        }
Пример #26
0
        private async Task DispatchIncomingPacket()
        {
            using (var bufferScope = _bufferPool.CreateScope())
            {
                UncompressedPacket packet;
                if (_useCompression)
                {
                    var compressedPacket = await CompressedPacket.DeserializeAsync(_remoteStream, null);

                    packet = _packetCompress.Decompress(compressedPacket, _compressThreshold);
                }
                else
                {
                    packet = await UncompressedPacket.DeserializeAsync(_remoteStream, bufferScope);
                }

                await DispatchIncomingPacket(packet);
            }
        }
Пример #27
0
        public Task DispatchIncomingPacket(UncompressedPacket packet)
        {
            switch (status)
            {
            case SessionStatus.Status:
            case SessionStatus.Login:
            case SessionStatus.Play:
            {
                IPacketSerializable pack = null;

                foreach (var it in list[(int)status])
                {
                    if (it.packetId == packet.packetId)
                    {
                        pack = it.Clone() as IPacketSerializable;
                        break;
                    }
                }

                if (pack != null)
                {
                    try
                    {
                        using (var br = new NetworkReader(packet.data))
                            pack.Deserialize(br);

                        this.InvokeDispatchIncomingPacket(status, pack);
                    }
                    catch (System.NotImplementedException e)
                    {
                        UnityEngine.Debug.LogException(e);
                    }
                }
                else
                {
                    this.InvokeDispatchInvalidPacket(status, packet);
                }
            }
            break;
            }

            return(Task.CompletedTask);
        }
Пример #28
0
        private object DeserializeStatusPacket(ref UncompressedPacket packet)
        {
            using (var br = new BinaryReader(new MemoryStream(packet.Data)))
            {
                switch (packet.PacketId)
                {
                // Request
                case 0x00:
                    return(Request.Deserialize(br));

                // Ping
                case 0x01:
                    return(Ping.Deserialize(br));

                default:
                    throw new InvalidDataException($"Unrecognizable packet id: 0x{packet.PacketId:X}.");
                }
            }
        }
Пример #29
0
        private object DeserializeLoginPacket(ref UncompressedPacket packet)
        {
            using (var br = new BinaryReader(new MemoryStream(packet.Data)))
            {
                object innerPacket;
                switch (packet.PacketId)
                {
                // Login Start
                case 0x00:
                    innerPacket = LoginStart.Deserialize(br);
                    break;

                default:
                    throw new InvalidDataException($"Unrecognizable packet id: 0x{packet.PacketId:X}.");
                }

                if (br.BaseStream.Position != br.BaseStream.Length)
                {
                    throw new InvalidDataException($"Packet data is not fully consumed.");
                }
                return(innerPacket);
            }
        }
Пример #30
0
 private async Task DispatchIncomingPacket(UncompressedPacket packet)
 {
     var router = _grainFactory.GetGrain <IPacketRouter>(_sessionId);
     await router.SendPacket(packet);
 }