Beispiel #1
0
        // 处理接收到的消息
        public void OnReceiveData()
        {
            // 半包的先不处理
            if (readbuff.Length <= 2)
            {
                return;
            }
            Int16 bodyLen = BitConverter.ToInt16(readbuff.bytes, 0);

            //UILog.log.Add("收到信息长度:" + bodyLen);
            if (readbuff.Length < 2 + bodyLen)
            {
                return;
            }

            // 处理消息
            string message = Encoding.UTF8.GetString(readbuff.bytes, 2, bodyLen);

            readbuff.readIdx += 2 + bodyLen;
            readbuff.CheckAndMoveBytes();
            Int16  typeLen = BitConverter.ToInt16(Encoding.UTF8.GetBytes(message), 0);
            string type    = typeLen.ToString();

            message = message.Substring(2, message.Length - 2);
            //UILog.log.Add("收到信息类型:" + ((netEventEnum)(int.Parse(type))).ToString());
            string protoType = "Net.Proto." + ((netEventEnum)(int.Parse(type))).ToString() + "Proto";

            if ((netEventEnum)(int.Parse(type)) != netEventEnum.Ping)
            {
                Console.WriteLine("收到信息类型:" + ((netEventEnum)(int.Parse(type))).ToString());
                if ((netEventEnum)(int.Parse(type)) == netEventEnum.Move)
                {
                    MoveProto mp = Packet.Decode(((netEventEnum)(int.Parse(type))).ToString() + "Proto", message) as MoveProto;
                    pos[0] = mp.x.ToString();
                    pos[1] = mp.y.ToString();
                    pos[2] = mp.z.ToString();
                }
                Server.broadcast((netEventEnum)(int.Parse(type)), message);  // 广播
            }
            else
            {
                lastPingTime = Tool.GetTimestamp();
                PongProto pp = new PongProto();
                pp.Timestamp = Tool.GetTimestamp();
                Send(netEventEnum.Pong, Encoding.UTF8.GetString(Packet.Encode(pp)));
            }
            // 粘包的继续解析
            if (readbuff.Length > 2)
            {
                OnReceiveData();
            }
        }
Beispiel #2
0
        public void Close()
        {
            isReceive = false;
            Server.clients.Remove(this);
            if (socket != null)
            {
                socket.Close();
            }

            lastPingTime = Tool.GetTimestamp();
            LeaveProto lp = new LeaveProto();

            lp.Timestamp = Tool.GetTimestamp();
            lp.id        = id;
            Server.broadcast(lp.protoType, Encoding.UTF8.GetString(Packet.Encode(lp)));  // 广播
        }