コード例 #1
0
ファイル: NettyUtils.java プロジェクト: AmyStorm/game-server
 /**
  * converts a bytebuf to byte array.
  *
  * @param buf
  * @param isReadDestroy if true then the reader index of the bytebuf will be modified.
  * @return
  */
 public static byte[] toByteArray(ByteBuf buf, boolean isReadDestroy) {
   byte[] arr = new byte[buf.readableBytes()];
   if (isReadDestroy) {
     buf.readBytes(arr);
   } else {
     buf.getBytes(buf.readerIndex(), arr);
   }
   return arr;
 }
コード例 #2
0
 @Override
 public byte[] get() {
   if (byteBuf == null) {
     return EMPTY_BUFFER.array();
   }
   byte[] array = new byte[byteBuf.readableBytes()];
   byteBuf.getBytes(byteBuf.readerIndex(), array);
   return array;
 }
コード例 #3
0
 @Override
 protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
   // TODO Auto-generated method stub
   final byte[] array;
   final int length = in.readableBytes();
   array = new byte[length];
   in.getBytes(in.readerIndex(), array, 0, length);
   MessagePack messagePack = new MessagePack();
   out.add(messagePack.read(array));
 }
コード例 #4
0
 public void writeObject(Object var1) throws IOException {
   ByteBufOutputStream var2 = new ByteBufOutputStream(Unpooled.buffer(this.estimatedLength));
   CompactObjectOutputStream var3 = new CompactObjectOutputStream(var2);
   var3.writeObject(var1);
   var3.flush();
   var3.close();
   ByteBuf var4 = var2.buffer();
   int var5 = var4.readableBytes();
   this.writeInt(var5);
   var4.getBytes(0, (OutputStream) this, var5);
 }
コード例 #5
0
ファイル: NioDatagramChannel.java プロジェクト: JiniKim/netty
  @Override
  protected int doWriteMessages(MessageBuf<Object> buf, boolean lastSpin) throws Exception {
    DatagramPacket packet = (DatagramPacket) buf.peek();
    ByteBuf data = packet.data();
    int dataLen = data.readableBytes();
    ByteBuffer nioData;
    if (data.nioBufferCount() == 1) {
      nioData = data.nioBuffer();
    } else {
      nioData = ByteBuffer.allocate(dataLen);
      data.getBytes(data.readerIndex(), nioData);
      nioData.flip();
    }

    final int writtenBytes = javaChannel().send(nioData, packet.remoteAddress());

    final SelectionKey key = selectionKey();
    final int interestOps = key.interestOps();
    if (writtenBytes <= 0 && dataLen > 0) {
      // Did not write a packet.
      // 1) If 'lastSpin' is false, the caller will call this method again real soon.
      //    - Do not update OP_WRITE.
      // 2) If 'lastSpin' is true, the caller will not retry.
      //    - Set OP_WRITE so that the event loop calls flushForcibly() later.
      if (lastSpin) {
        if ((interestOps & SelectionKey.OP_WRITE) == 0) {
          key.interestOps(interestOps | SelectionKey.OP_WRITE);
        }
      }
      return 0;
    }

    // Wrote a packet.
    buf.remove();

    // packet was written free up buffer
    packet.free();

    if (buf.isEmpty()) {
      // Wrote the outbound buffer completely - clear OP_WRITE.
      if ((interestOps & SelectionKey.OP_WRITE) != 0) {
        key.interestOps(interestOps & ~SelectionKey.OP_WRITE);
      }
    }
    return 1;
  }
コード例 #6
0
  @Override
  protected void doWriteMessages(MessageBuf<Object> buf) throws Exception {
    DatagramPacket p = (DatagramPacket) buf.poll();
    ByteBuf data = p.data();
    int length = data.readableBytes();
    InetSocketAddress remote = p.remoteAddress();
    if (remote != null) {
      tmpPacket.setSocketAddress(remote);
    }
    if (data.hasArray()) {
      tmpPacket.setData(data.array(), data.arrayOffset() + data.readerIndex(), length);
    } else {
      byte[] tmp = new byte[length];
      data.getBytes(data.readerIndex(), tmp);
      tmpPacket.setData(tmp);
    }

    socket.send(tmpPacket);
  }
コード例 #7
0
  public ByteBuf setBytes(int var1, ByteBuf var2, int var3, int var4) {
    this.checkSrcIndex(var1, var4, var3, var2.capacity());
    if (var2.hasArray()) {
      this.setBytes(var1, var2.array(), var2.arrayOffset() + var3, var4);
    } else if (var2.nioBufferCount() > 0) {
      ByteBuffer[] var5 = var2.nioBuffers(var3, var4);
      int var6 = var5.length;

      for (int var7 = 0; var7 < var6; ++var7) {
        ByteBuffer var8 = var5[var7];
        int var9 = var8.remaining();
        this.setBytes(var1, var8);
        var1 += var9;
      }
    } else {
      var2.getBytes(var3, (ByteBuf) this, var1, var4);
    }

    return this;
  }
コード例 #8
0
  /** 解密 */
  @Override
  protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    if (in.readableBytes() < 4) {
      return;
    }
    int header = in.readShort();
    int packetLength = in.readShort();
    // 预解密长度信息成功,回溯位置
    in.readerIndex(in.readerIndex() - 4);
    // 如果不是标识头,发送给客户端说,断开连接
    if (header != Message.HEADER || packetLength < Message.HEAD_SIZE) {
      // 数据包长度错误,断开连接
      LogUtil.error(
          String.format(
              "error packet length: packetlength=%d Packet.HDR_SIZE=%d",
              packetLength, Message.HEAD_SIZE));
      LogUtil.error(
          String.format(
              "Disconnect the client:%s",
              ((InetSocketAddress) ctx.channel().remoteAddress()).getHostName()));
      ctx.channel().close();
      return;
    }

    if (in.readableBytes() < packetLength) {
      // 数据长度不足,等待下次接收
      return;
    }
    // 读取数据并解密数据
    byte[] data = new byte[packetLength];
    in.getBytes(in.readerIndex(), data, 0, packetLength);
    in.readerIndex(in.readerIndex() + packetLength);
    Message packet = Message.build(data);
    if (packet != null) {
      out.add(packet);
    }
    return;
  }
コード例 #9
0
 @Override
 public ByteBuf getBytes(int index, ByteBuffer dst) {
   return buf.getBytes(index, dst);
 }
コード例 #10
0
 @Override
 public ByteBuf getBytes(int index, byte[] dst, int dstIndex, int length) {
   return buf.getBytes(index, dst, dstIndex, length);
 }
コード例 #11
0
 @Override
 public ByteBuf getBytes(int index, byte[] dst) {
   return buf.getBytes(index, dst);
 }
コード例 #12
0
 @Override
 public ByteBuf getBytes(int index, ByteBuf dst, int length) {
   return buf.getBytes(index, dst, length);
 }
コード例 #13
0
 @Override
 public ByteBuf getBytes(int var1, byte[] var2, int var3, int var4) {
   return a.getBytes(var1, var2, var3, var4);
 }
コード例 #14
0
ファイル: MulticastEndpoint.java プロジェクト: cvgaviao/yajsw
 public boolean checkMessage(ByteBuf e) {
   byte[] eId = new byte[id.length];
   e.getBytes(0, eId, 0, eId.length);
   return (!Arrays.equals(id, eId));
 }
コード例 #15
0
 @Override
 public ByteBuf getBytes(int var1, OutputStream var2, int var3) throws IOException {
   return a.getBytes(var1, var2, var3);
 }
コード例 #16
0
 @Override
 public ByteBuf getBytes(int var1, byte[] var2) {
   return a.getBytes(var1, var2);
 }
コード例 #17
0
 @Override
 public int getBytes(int var1, GatheringByteChannel var2, int var3) throws IOException {
   return a.getBytes(var1, var2, var3);
 }
コード例 #18
0
 @Override
 public ByteBuf getBytes(int index, OutputStream out, int length) throws IOException {
   return buf.getBytes(index, out, length);
 }
コード例 #19
0
ファイル: Buffer.java プロジェクト: hgschmie/vert.x
 /**
  * Returns a copy of a sub-sequence the Buffer as a {@code byte[]} starting at position {@code
  * start} and ending at position {@code end - 1}
  */
 public byte[] getBytes(int start, int end) {
   byte[] arr = new byte[end - start];
   buffer.getBytes(start, arr, 0, end - start);
   return arr;
 }
コード例 #20
0
 @Override
 public int getBytes(int index, GatheringByteChannel out, int length) throws IOException {
   return buf.getBytes(index, out, length);
 }
コード例 #21
0
 @Override
 public ByteBuf getBytes(int var1, ByteBuffer var2) {
   return a.getBytes(var1, var2);
 }
コード例 #22
0
ファイル: Buffer.java プロジェクト: hgschmie/vert.x
 /** Returns a copy of the entire Buffer as a {@code byte[]} */
 public byte[] getBytes() {
   byte[] arr = new byte[buffer.writerIndex()];
   buffer.getBytes(0, arr);
   return arr;
 }
コード例 #23
0
 @Override
 public ByteBuf getBytes(int var1, ByteBuf var2, int var3) {
   return a.getBytes(var1, var2, var3);
 }