1

我正在尝试运行原始游戏服务器。到目前为止,我到了这一点(注释行)并且服务器运行顺利。但是如果我再次从客户端发送对象,它不会更新超过一次。例如,客户端发送具有新位置 Vector2(400,50) 的序列化播放器对象,但服务器将其反序列化到具有旧位置的对象。


播放器代码

namespace Commons.Game
{
    [Serializable]
    public class Unit
    {
        #region Fields
        public int ID;
        public Vector2 position;
        public string name;
        public int HP;
        public int XP;
        public int Lvl;
        public bool active;
        public float speed;
        public string password;
        #endregion

        public Unit(Vector2 position, int HP, float speed, string name, string password, int ID)
        {
            active = true;
            this.position = position;
            this.HP = HP;
            this.XP = 0;
            this.speed = speed;
            this.name = name;
            this.Lvl = 1;
            this.password = password;
            this.ID = ID;
        }


服务器代码

namespace SocketServer.Connection
{
    class Server
    {
        #region Fields
        Unit[] players;

        UdpClient playersData;

        Thread INHandlePlayers;

        BinaryFormatter bf;

        IPEndPoint playersEP;

        #endregion

        public Server()
        {
            this.players = new Unit[5];

            bf = new BinaryFormatter();

            this.playersData = new UdpClient(new IPEndPoint(IPAddress.Any, 3001));
            this.playersEP = new IPEndPoint(IPAddress.Any, 3001);

            this.INHandlePlayers = new Thread(new ThreadStart(HandleIncomePlayers));
            this.INHandlePlayers.Name = "Handle income players.";

            this.INHandlePlayers.Start();
        }

        private void HandleIncomePlayers()
        {
            Console.Out.WriteLine("Players income handler started.");
            MemoryStream ms = new MemoryStream();

            while (true)
            {
                byte[] data = playersData.Receive(ref playersEP);
                ms.Write(data, 0, data.Length); 
                ms.Position = 0;
                Unit player = null; 
                player = bf.Deserialize(ms) as Unit; //<-- 1st deserialization is OK, bu  after another client update, object doesn't change. So I change Vector with position at client, that sends correct position I want, but at server side position doesn't change after first deserialization.
                Console.Out.WriteLine(player.name + " " + player.position.X + " " + player.position.Y);
                ms.Flush();
                for (int i = 0; i < players.Length; i++)
                {
                    if (players[i] != null && player.ID == players[i].ID)
                    {
                        players[i] = player;
                        break;
                    }
                }
            }
        }
}
4

2 回答 2

2

伙计,我不太确定,但我会在你的循环中实例化 MemoryStream 对象,如下所示。

我无法正确尝试此代码,对此感到抱歉。

    private void HandleIncomePlayers()
    {
        Console.Out.WriteLine("Players income handler started.");
        MemoryStream ms;

        while (true)
        {
            ms = new MemoryStream(); //here
            byte[] data = playersData.Receive(ref playersEP);
            ms.Write(data, 0, data.Length); 
            ms.Position = 0;
            Unit player = null; 
            player = bf.Deserialize(ms) as Unit;

            Console.Out.WriteLine(player.name + " " + player.position.X + " " + player.position.Y);
            ms.Dispose(); //and here
            for (int i = 0; i < players.Length; i++)
            {
                if (players[i] != null && player.ID == players[i].ID)
                {
                    players[i] = player;
                    break;
                }
            }
        }
    }
于 2012-06-27T07:21:08.570 回答
1

美好的一天用户1181369,

我自己之前没有使用过 MemoryStream,但我一直在通过 MSDN 库进行一些研究,我怀疑“ms”没有清除,您加载的所有数据实际上都被添加到流中,而不是更换它。

例如,MemorySteam.Flush() 实际上并没有做任何事情 ( http://msdn.microsoft.com/en-us/library/system.io.memorystream.flush.aspx )。如果是这种情况,那么 for 循环将在找到特定玩家 ID 的第一个实例时中断,而不是找到较新的版本。

另外,我不确定您提供的代码如何反序列化多个玩家,但这超出了这个问题的范围,也可能超出了我目前的知识领域。

遗憾的是,虽然我认为我可能已经诊断出问题,但我目前还没有能力提供除了在每个循环中实例化一个新的内存流之外的解决方案。

于 2012-06-27T07:15:15.480 回答