@Override
 public void run() {
   InputStream inputStream = null;
   MsgResponseHandle msgResponseHandle = new MsgResponseHandle();
   // 如果Socket连接实例不存在则退出
   while (InitMsgSocketServer.getInstance() != null) {
     try {
       // 睡眠0.5秒
       Thread.sleep(500);
       // TODO 检测Socket连接实例
       if (InitMsgSocketServer.getInstance().isConnected()) {
         // TODO 读取IO输入流数据
         if (inputStream == null) {
           inputStream = InitMsgSocketServer.getInputStream();
         }
         // 响应数据流解析
         if (msgResponseHandle == null) {
           msgResponseHandle = new MsgResponseHandle();
         }
         msgResponseHandle.decode(inputStream);
       } else {
         // TODO 跳出循环,退出线程
         IMLog.anlong("Socket处于断开状态,消息响应线程终止.");
         Utils.notifyMessage(3, HandleStaticValue.BCODE1000);
         break;
       }
     } catch (Exception e) {
       e.printStackTrace();
       IMLog.anlong("读取IO流时,解析响应数据异常");
       // TODO 通知页面更新提示
       Utils.notifyMessage(3, HandleStaticValue.BCODE1000);
       break;
     }
   }
   IMLog.anlong(
       "===================================================(消息响应)线程终止=========================================================");
 }
Exemplo n.º 2
0
  /**
   * @Title: decode @Description: TODO 解码器
   *
   * @author anlong
   * @throws
   */
  @SuppressWarnings("static-access")
  public void decode(InputStream stream) throws Exception {
    try {
      // 输入IO流
      inputStream = stream;
      if (inputStream == null) {
        IMLog.anlong("IO流中没有数据响应!");
        throw new IOException();
      }

      // 消息体字节大小,不包括表示大小的4个字节
      int limit = 0;

      // 消息字节大小,包括表示大小的4个字节
      int msgSize = 0;

      // 消息字节数组
      byte[] data = null;

      // 保证IO流中可读字节数大于 4个字节
      byte[] dataSize = new byte[HandleStaticValue.PROTOCOL_SIZE];

      if (inputStream.read(dataSize) == HandleStaticValue.PROTOCOL_SIZE) {
        msgSize = ByteAndInt.byteArray2Int(dataSize);
        IMLog.anlong("响应报文字节大小:" + msgSize);

        // 标识从该处读取剩余字节数
        limit = msgSize - HandleStaticValue.PROTOCOL_SIZE;
        IMLog.anlong("响应报文体字节数:" + limit + "=" + Utils.getFileSizeString((long) limit));

        // TODO 大于10M丢弃
        if (limit > 10000000) {
          IMLog.anlong("丢弃一个大小为 " + limit + "的数据包!");

          throw new IOException();
        }

        // 定义消息字节数组大小
        data = new byte[limit];
      }

      // 循环读取消息字节
      int len = 0;
      while (len < limit) {
        len += inputStream.read(data, len, (limit - len));
        IMLog.anlong("读取字节数:" + len);
      }

      if (data == null) throw new IOException();

      IMLog.anlong("读取流完成,byte[]的长度:" + data.length);
      dataInputStream = new DataInputStream(new ByteArrayInputStream(data));

      if (dataInputStream != null) {
        // 业务编码
        short bCode = dataInputStream.readShort();
        // 密钥
        int key = dataInputStream.readInt();
        // 用户ID
        int uid = dataInputStream.readInt();
        // 返回编码
        short rtCode = dataInputStream.readShort();
        // 错误编码
        String rtMsg = dataInputStream.readUTF();
        // 消息流水号
        int msgSerial = dataInputStream.readInt();

        // 消息头打印
        IMLog.anlong(
            "limit size:"
                + limit
                + " bCode:"
                + bCode
                + " key:"
                + key
                + " uid:"
                + uid
                + " rtCode:"
                + rtCode
                + " rtMsg:"
                + rtMsg
                + " msgSerial:"
                + msgSerial);

        if (bCode == 0) {
          IMLog.anlong("业务编码错误!");
          throw new IOException();
        }

        // 获取对象
        String requestPath = HandleStaticValue.RESPONSE_PACKEAGE + ".Response" + bCode;
        Class requestClass = Class.forName(requestPath);

        // 解析消息体
        Object obj = ReadValue(requestClass, dataInputStream);

        // 反射设置消息头
        ReflectionUtil.invokeMethod(
            obj, "init", msgSize, bCode, key, uid, rtCode, rtMsg, msgSerial);

        // 分发事件
        try {
          if (bCode != 0 && obj != null) {
            MessageEvent event = new MessageEvent(bCode, obj);
            new MessageEventSource().getSingleton().notifyMessageEvent(event);
          }
        } catch (Exception e) {
          IMLog.anlong("监听队列没有找到 " + bCode + "!");
          IMLog.anlong("已丢弃一个没有注册监听的数据包:" + bCode + "!");
          throw e;
        }

      } else {
        IMLog.anlong("没有数据响应!");
        throw new Exception();
      }

    } catch (Exception e) {
      throw e;
    }
  }