Пример #1
0
        public void BroadcastState(GameState state)
        {
            GameMessage msg = new GameMessage();
            msg.messageType = "ServerUpdate";
            msg.gs = state;
            //Create game state

            Debug.WriteLine("Broadcasting state");
            BinaryFormatter format = new BinaryFormatter();

            MemoryStream ms = new MemoryStream();

            format.Serialize(ms, msg);

            ms.Position = 0;

            GameMessage msgout = (GameMessage)format.Deserialize(ms);
            Debug.WriteLine("Message:{0}", msgout);

            lock (Sockets)
            {
                List<Socket> kept = new List<Socket>();
                foreach(Socket s in Sockets)
                {
                    try
                    {
                        s.Send(ms.GetBuffer());
                        kept.Add(s);
                    }
                    catch (Exception e)
                    {
                        Debug.WriteLine("Exception: " + e.Message);
                    }
                }
                Sockets = kept;
            }
        }
Пример #2
0
 private void processMessage(GameMessage gm, Socket sender)
 {
     if(gm.messageType.CompareTo("PlayerUpdate") == 0)
     {
         lock (UpdateLock)
         {
             Updates.Add(gm.PlayerState);
         }
     }
     else if (gm.messageType.CompareTo("Join") == 0)
     {
         Player p;
         lock (CurIdLock)
         {
             p = new Player("Player", new Microsoft.Xna.Framework.Vector2(500, 500), 0.0f, GameObject.Directions.N, CurId);
             CurId++;
         }
         lock (UpdateLock)
         {
             Updates.Add(p);
         }
         GameMessage response = new GameMessage();
         response.messageType = "JoinResponse";
         response.PlayerState = p;
         BinaryFormatter bf = new BinaryFormatter();
         MemoryStream ms = new MemoryStream();
         bf.Serialize(ms, response);
         sender.Send(ms.GetBuffer());
     }
 }