Пример #1
0
        async void MessageReceived(DatagramSocket sender, DatagramSocketMessageReceivedEventArgs args)
        {
            using (DataReader dataReader = args.GetDataReader())
            {
                dataReader.ByteOrder = ByteOrder.LittleEndian;
                Debug.WriteLine("buffer lntgh: " + dataReader.UnconsumedBufferLength);
                while (dataReader.UnconsumedBufferLength != 0)
                {
                    Code   code = (Code)dataReader.ReadInt32();
                    Packet p;
                    switch (code)
                    {
                    case Code.Connecting:
                    {
                        Connected = true;
                        hostName  = args.RemoteAddress;
                        Debug.WriteLine("Connected to: " + hostName);
                        try
                        {
                            Stream output = (await socket.GetOutputStreamAsync(hostName, Port)).AsStreamForWrite();
                            writer = new BinaryWriter(output);
                        }
                        catch (Exception e)
                        {
                            Debug.WriteLine(e.Message);
                        }
                        send_buffer.Add(new Connected());
                        p = null;
                        break;
                    }

                    case Code.Ping:
                    {
                        p = Ping.ConstructPacket(dataReader);
                        break;
                    }

                    case Code.Pong:
                    {
                        p = Pong.ConstructPacket(dataReader);
                        break;
                    }

                    case Code.Acknowledge:
                    {
                        p = Acknowledge.ConstructPacket(dataReader);
                        break;
                    }

                    case Code.OtherPlayerCreationData:
                    {
                        p = AddOtherPlayer.ConstructPacket(dataReader);
                        send_buffer.Add(new Acknowledge(p.Code));
                        break;
                    }

                    case Code.Input:
                    {
                        p = Input.ConstructPacket(dataReader);
                        break;
                    }

                    case Code.EntityXYCorrection:
                    {
                        p = EntityCorrection.ConstructPacket(dataReader);
                        break;
                    }

                    default: p = null; break;
                    }

                    if (p != null)
                    {
                        receive_buffer.Add(p);
                    }
                    else
                    {
                        Debug.WriteLine("Unrecognized packet " + code);
                        break;
                    }
                }
            }
        }
Пример #2
0
        void MessageReceived(DatagramSocket sender, DatagramSocketMessageReceivedEventArgs args)
        {
            using (DataReader dataReader = args.GetDataReader())
            {
                dataReader.ByteOrder = ByteOrder.LittleEndian;
                while (dataReader.UnconsumedBufferLength != 0)
                {
                    Code   code = (Code)dataReader.ReadInt32();
                    Packet p;
                    switch (code)
                    {
                    case Code.Connected:
                    {
                        Connected = true;
                        p         = null;
                        break;
                    }

                    case Code.Ping:
                    {
                        p = Ping.ConstructPacket(dataReader);
                        break;
                    }

                    case Code.Pong:
                    {
                        p = Pong.ConstructPacket(dataReader);
                        break;
                    }

                    case Code.LevelGenerationData:
                    {
                        p = LevelGenData.ConstructPacket(dataReader);
                        send_buffer.Add(new Acknowledge(p.Code));
                        break;
                    }

                    case Code.Acknowledge:
                    {
                        p = Acknowledge.ConstructPacket(dataReader);
                        break;
                    }

                    case Code.OtherPlayerCreationData:
                    {
                        p = AddOtherPlayer.ConstructPacket(dataReader);
                        break;
                    }

                    case Code.OtherPlayerID:
                    {
                        p = OtherPlayerID.ConstructPacket(dataReader);
                        break;
                    }

                    case Code.Input:
                    {
                        p = Input.ConstructPacket(dataReader);
                        break;
                    }

                    case Code.EntityXYCorrection:
                    {
                        p = EntityCorrection.ConstructPacket(dataReader);
                        break;
                    }

                    default: p = null; break;
                    }

                    if (p != null)
                    {
                        receive_buffer.Add(p);
                    }
                    else
                    {
                        Debug.WriteLine("Unrecognized packet " + code);
                        break;
                    }
                }
            }
        }