예제 #1
0
        /// <summary>
        /// udp数据包解析函数
        /// </summary>
        /// <param name="buf">字节数组</param>
        private void Parse(byte[] buf)
        {
            MsgType msgType = MsgType.None;
            Msg     msg     = null;
            string  str     = Encoding.UTF32.GetString(buf);

            string[] strs = str.Split('|');
            msgType = (MsgType)Convert.ToInt32(strs[0]);
            //通过不同的消息类型,对消息进行解析
            switch (msgType)
            {
            case MsgType.TankNew:
                msg = new TankNewMsg(ctrl);
                msg.Parse(buf);
                break;

            case MsgType.TankMove:
                msg = new TankMoveMsg(ctrl);
                msg.Parse(buf);
                break;

            case MsgType.BulletNew:
                msg = new BulletNewMsg(ctrl);
                msg.Parse(buf);
                break;
            }
        }
예제 #2
0
 /// <summary>
 /// 连接服务器并开启udp接受线程
 /// </summary>
 /// <param name="ip">服务器ip地址</param>
 /// <param name="port">服务器端口号</param>
 public bool Connect(string ip, int port)
 {
     try
     {
         this.ip = ip;
         TcpClient client = new TcpClient();
         client.Connect(ip, port);
         Stream       ns = client.GetStream();
         BinaryWriter bw = new BinaryWriter(ns);
         bw.Write(udpPort);
         ns.Close();
         client.Close();
         TankNewMsg msg = new TankNewMsg(ctrl.myTank);
         Send(msg);
         //开启接收线程
         Thread t = new Thread(UDPRecvThread);
         t.IsBackground = true;
         t.Start();
         return(true);
     }
     catch
     {
         return(false);
     }
 }
예제 #3
0
        /// <summary>
        /// 解包
        /// </summary>
        /// <param name="b">二进制数据</param>
        public void Parse(byte[] b)
        {
            string str = Encoding.UTF32.GetString(b);

            string[] strs = str.Split('|');
            string   name = strs[1];

            //如果数据包里是自己的坦克,则不处理
            if (name == ctrl.myTank.Name)
            {
                return;
            }
            int       x   = Convert.ToInt32(strs[2]);
            int       y   = Convert.ToInt32(strs[3]);
            Direction dir = (Direction)Convert.ToInt32(strs[4]);

            float[] color = new float[3];
            color[0] = Convert.ToSingle(strs[5]);
            color[1] = Convert.ToSingle(strs[6]);
            color[2] = Convert.ToSingle(strs[7]);

            bool exist = false;

            for (int i = 0; i < ctrl.tanks.Count; i++)
            {
                Tank t = ctrl.tanks[i];
                if (t.Name == name)
                {
                    exist = true;
                    break;
                }
            }
            //如果坦克不存在就创建出来
            if (!exist)
            {
                TankNewMsg msg = new TankNewMsg(ctrl.myTank);
                ctrl.nc.Send(msg);
                Tank t = new Tank(name, x, y, dir, color);
                t.Name = name;
                ctrl.tanks.Add(t);
            }
        }