public staticString createMessageId(finalByteBuffer input,final ByteBuffer addr,final long offset) {
input.flip();
input.limit(MessageDecoder.MSG_ID_LENGTH);
input.put(addr);
input.putLong(offset);
return UtilAll.bytes2string(input.array());
}
如上代码,我们可以看到 messageId 分为两部分 :
ip地址 8 个字节
offset 8个字节
那么ip地址 如何转成 8个字节呢? 请看如下代码:
private staticByteBuffer socketAddress2ByteBuffer(final SocketAddress socketAddress, final ByteBuffer byteBuffer) {
InetSocketAddress inetSocketAddress = (InetSocketAddress) socketAddress;
byteBuffer.put(inetSocketAddress.getAddress().getAddress(),0,4);
byteBuffer.putInt(inetSocketAddress.getPort());
byteBuffer.flip();
return byteBuffer;
}
public staticByteBuffer socketAddress2ByteBuffer(SocketAddress socketAddress) {
ByteBuffer byteBuffer = ByteBuffer.allocate(8);
return socketAddress2ByteBuffer(socketAddress, byteBuffer);
}
执行方法后 Id 类似:AC110E7339B058644D464149A11D0004
网友评论