Exemplo n.º 1
0
 public static IoMessageHandler getInstance()
 {
     if (instance == null)
     {
         instance = new IoMessageHandler();
     }
     return(instance);
 }
Exemplo n.º 2
0
        private void onData()
        {
            //消息长度小于数据基础长度说明包没完整
            if (ioBuffer.Length < BASE_LENGTH)
            {
                isRead = false;
                return;
            }

            //读取定义的消息长度
            while (true)
            {
                int datazie = ioBuffer.ReadInt();
                if (datazie == -777888)
                {
                    break;
                }
            }
            short cmd       = ioBuffer.ReadShort();
            short gameId    = ioBuffer.ReadShort();
            short srcGameId = ioBuffer.ReadShort();
            int   length    = ioBuffer.ReadInt();

            if (ioBuffer.Length < length + BASE_LENGTH)
            {
                //还原指针
                ioBuffer.Postion = 0;
                isRead           = false;
                //数据不全 还原指针等待下一数据包(分包)
                return;
            }
            ByteArray ioData = new ByteArray();

            ioData.WriteBytes(ioBuffer.Buffer, BASE_LENGTH, length);
            ioBuffer.Postion += length;
            byte[] buf = new byte[length];
            buf = ioData.ReadBytes();
            ClientTcpIoMessage clientTcpIoMessage = ProtostuffUtils.ProtobufDeserialize <ClientTcpIoMessage>(buf);

            Debug.Log("收到:" + clientTcpIoMessage.interfaceName + "::" + clientTcpIoMessage.methodName + "====" + clientTcpIoMessage.args);
            IoMessageHandler.getInstance().push(clientTcpIoMessage);
            ByteArray bytes = new ByteArray();

            bytes.WriteBytes(ioBuffer.Buffer, ioBuffer.Postion, ioBuffer.Length - ioBuffer.Postion);
            ioBuffer = bytes;
            onData();
        }