Ejemplo n.º 1
0
        public async Task WaitForData()
        {
            var mainPageViewModel = MainPageViewModel.GetInstance();

            using (var dr = new DataReader(ServerProxy.TcpSocket.InputStream))
            {
                while (mainPageViewModel.ConnectionStatus)
                {
                    var stringHeader = await dr.LoadAsync(4);

                    if (stringHeader == 0)
                    {
                        mainPageViewModel.ConnectionStatus = false;
                        return;
                    }

                    int messageLength = dr.ReadInt32();
                    uint numBytes = await dr.LoadAsync((uint)messageLength);

                    var packetBaseBuffer = new byte[numBytes];

                    dr.ReadBytes(packetBaseBuffer);

                    var packetBase = new PacketBase();

                    using (var stream = new MemoryStream(packetBaseBuffer))
                    {
                        try
                        {
                            var reader = new BinaryReader(stream);

                            packetBase.Read(reader);
                        }
                        catch (Exception e)
                        {
#if DEBUG
                            throw;
#endif
                        }
                    }


                    var incomingMessage = IncomingMessageFactory.GetMessage(
                        packetBase.Data as PacketV1);

                    incomingMessage.HandleMessage();
                }
            }
        }
Ejemplo n.º 2
0
        public void HandleMessage(ref Client client, byte[] msgBytes)
        {
            var packetBase = new PacketBase();

            using (var stream = new MemoryStream(msgBytes))
            {
                try
                {
                    var reader = new BinaryReader(stream);

                    packetBase.Read(reader);
                }
                catch (Exception e)
                {
            #if DEBUG
                    throw;
            #else
                    Trace.WriteLine(string.Format("[IncomingMessageHandler.HandleMessage] - " +
                            "Invalid packet from client! Deserialization failed: {0}, Trace: {1}",
                            e.Message,
                            e.StackTrace));
            #endif
                }
            }

            var handler = PacketHandlerFactory.GetPacketHandler(packetBase);

            if (handler != null)
            {
                handler.DoActions(ref client);
            }
        }