Example #1
0
	    // 接受消息
	    public void readMessage(byte[] bytes)
	    {
	        //消息读取完成后开始解析 
	        MemoryStream ms = new MemoryStream(bytes, 0, bytes.Length);
	        VitByteArray arr = new VitByteArray(ms);
	        while (arr.Postion < bytes.Length)
	        {
	            
				int messageLength = Utils.BigtoLittle32 (arr.ReadInt());

	            if ((arr.Postion + messageLength - 4 + MsgHeadSize) > bytes.Length)
	            {
	                int lastCount = arr.Length - arr.Postion + 4;
	                allCount -= lastCount;
	                Middle = new Byte[lastCount];
	                Buffer.BlockCopy(bytes, arr.Postion - 4, Middle, 0, lastCount);
	                break;
	            }
				int messageID =  Utils.BigtoLittle32( arr.ReadInt());
	            //转换为Socket消息模型
				VitSocketModel model = new VitSocketModel();
	            model.bodyLength = messageLength-4;
	            model.messageID = messageID;
	            byte[] data = new byte[messageLength-4];
	            arr.ReadBytes(data, 0, (uint)messageLength);
	            model.message = data;

				messages.Add(model);
	        }

	    }
Example #2
0
	    // 解析消息
	    void parseMessage(byte[] bytes)
	    {
	        //消息读取完成后开始解析 
	        MemoryStream ms = new MemoryStream(bytes, 0, bytes.Length);
	        VitByteArray arr = new VitByteArray(ms);
	        while (arr.Postion < bytes.Length)
	        {
	            int messageLength = arr.ReadInt();
				messageLength = Utils.BigtoLittle32(messageLength);
	            if ((arr.Postion + messageLength - 4 + MsgHeadSize) > bytes.Length)
	            {
	                Debug.LogError("解析消息长度不一致[ " + messageLength + " ][ " +  bytes.Length + " ]");
	                break;
	            }
	            int messageID = arr.ReadInt();
				messageID = Utils.BigtoLittle32(messageID);
	            //转换为Socket消息模型
	            VitSocketModel model = new VitSocketModel();
	            model.bodyLength = messageLength;
	            model.messageID = messageID;
	            byte[] pdata = new byte[messageLength];
				arr.ReadBytes(pdata, 0, (uint)messageLength);
				model.message = pdata;
	            // 解析proto对象
				VitProtobufDispatcher.Instance.OnMessage(model);
	        }
	    }