Beispiel #1
0
        static void Main(string[] args)
        {
            startup();
            NetIncomingMessage msg;
            while (true)
            {

                foreach (GameObj obj in ObjList)
                {
                    obj.onUpdate(0.03333f);

                }

                while ((msg = server.ReadMessage()) != null)
                {
                    switch (msg.MessageType)
                    {

                        case NetIncomingMessageType.Data:
                            Console.WriteLine(msg.ToString() + msg.SenderConnection.ToString());
                            InputPacket dat = new InputPacket();
                            XmlSerializer bin = new XmlSerializer(dat.GetType());
                            var daat = (string)msg.ReadString();

                            StringReader read = new StringReader(daat);

                            dat = (InputPacket)bin.Deserialize(read);
                            foreach (ConClient cl in ClientList)
                            {
                                if (msg.SenderConnection == cl.con)
                                {
                                    cl.LastInput = dat;
                                    break;
                                }

                            }

                            Console.WriteLine(daat);
                            //str.Write(, 0, msg.Data.Length);
                            break;
                        case NetIncomingMessageType.ConnectionApproval:
                            msg.SenderConnection.Approve();
                            //  NetOutgoingMessage msgs=server.CreateMessage();
                            ConClient c = new ConClient();
                            c.id = clientidc;
                            c.con = msg.SenderConnection;
                            ClientList.Add(c);
                            Pobj po = new Pobj(c);
                            po.Sprite = "test";
                            ObjList.Add(po);
                            //msgs.Write(clientidc);

                            //server.SendMessage(msgs, msg.SenderConnection, NetDeliveryMethod.Unreliable);
                            clientidc++;

                            break;
                        default:
                            break;
                    }

                    //(string) bin.Deserialize(str);

                }

                DataPacket p= new DataPacket();
                foreach (GameObj va in ObjList)
                {
                    p.addGameObj(va);

                }

                foreach (ConClient va in ClientList)
                {

                    NetOutgoingMessage msgs = server.CreateMessage();
                    XmlSerializer s = new XmlSerializer(p.GetType());
                      using (var stream = new StringWriter())
            {
                s.Serialize(stream, p);
                var str = stream.ToString();

                msgs.Write(str);
                Console.WriteLine("sending: " + str+"size in bits: "+msgs.LengthBits);
                server.SendMessage(msgs, va.con, NetDeliveryMethod.UnreliableSequenced);

                      }

                }

            }
        }
Beispiel #2
0
        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
                Exit();
             pk = new InputPacket();
            // TODO: Add your update logic here
             XmlSerializer bin = new XmlSerializer(pk.GetType());

            base.Update(gameTime);
            NetOutgoingMessage msg=client.CreateMessage();
            using (var stream = new StringWriter())
            {

                bin.Serialize(stream,pk);
                var aarr = stream.ToString();
            msg.Write( stream.ToString());
            var arrarr = msg.ReadString();
            client.SendMessage(msg, NetDeliveryMethod.UnreliableSequenced);

            }

            NetIncomingMessage cmsg;

            while ((cmsg = client.ReadMessage()) != null)
            {

                if (cmsg.MessageType == NetIncomingMessageType.Data)
                {

                    XmlSerializer dbin = new XmlSerializer(typeof(DataPacket)); //desserilizer
                    var daat = (string)cmsg.ReadString();

                    StringReader read = new StringReader(daat);
                    try {
                        DataPacket a;
                        if ((a=(DataPacket)dbin.Deserialize(read)) != null)
                        {

                            objlist = a.ObjList;
                        }
                    }
                    catch(System.InvalidOperationException)
                    {
                        break;
                    }

                }

            }
        }