Exemplo n.º 1
0
  public void Send(Packet pkt) {
    // old code
    //    	byte[] buf = new byte[pkt.blob.length + 4];
    //    	Utils.uint32e(pkt.type, buf, 0);
    //    	System.arraycopy(pkt.blob, 0, buf, 4, pkt.blob.length);

    byte[] buf = new byte[pkt.blob.length + 3];
    buf[0] = (byte) ((pkt.blob.length + 2) & 0xff);
    buf[1] = (byte) ((pkt.blob.length + 2) >> 8 & 0xff);
    buf[2] = (byte) pkt.type;
    System.arraycopy(pkt.blob, 0, buf, 3, pkt.blob.length);
    if (Config.debug_packets) Log.debug("send pkt: " + Utils.getHexString(buf));
    Send(buf);
  }
Exemplo n.º 2
0
    private void ProcessData(int readed) {
      byte[] buf;
      if (left_buf != null) {
        buf = new byte[left_buf.length + readed];
        System.arraycopy(left_buf, 0, buf, 0, left_buf.length);
        System.arraycopy(read_buffer, 0, buf, left_buf.length, readed);
        left_buf = null;
      } else {
        buf = new byte[readed];
        System.arraycopy(read_buffer, 0, buf, 0, readed);
      }
      // Log.info("process data readed="+readed);
      while (true) {

        // if (buf != null)
        // Log.info("buf len="+buf.length);
        // else
        // Log.info("buf null");

        if (is_header) {
          // Log.info("is header");

          if ((buf != null && buf.length < HEADER_SIZE) || buf == null) {
            left_buf = buf;
            break;
          }
          int data_len = Utils.uint16d(buf, 0) - HEADER_SIZE;
          int type = Utils.unsigned_byte(buf[2]);
          if (Config.debug_packets) Log.debug("new pkt: [" + type + "] len=" + data_len);
          cur_packet = new Packet(type, data_len);
          buf = cur_packet.add_data(buf, HEADER_SIZE, buf.length - HEADER_SIZE);
          if (cur_packet.is_ready()) {
            synchronized (Connection.this) {
              if (Config.debug_packets) Log.info("recv packet " + cur_packet.toString());
              cur_packet.offset = 0;
              synchronized (packets) {
                packets.addFirst(cur_packet);
              }
            }
            cur_packet = null;
          } else {
            is_header = false;
          }
        } else {
          // Log.info("not is header");
          if (buf == null) {
            left_buf = buf;
            break;
          }
          buf = cur_packet.add_data(buf, 0, buf.length);
          if (cur_packet.is_ready()) {
            synchronized (packets) {
              // Log.info("recv packet "+cur_packet.toString());
              cur_packet.offset = 0;
              packets.addFirst(cur_packet);
            }
            cur_packet = null;
            is_header = true;
          }
        }
      }
    }