use of com.baidu.hugegraph.computer.core.network.buffer.NetworkBuffer in project hugegraph-computer by hugegraph.
the class MessageEncoder method writeMessage.
private void writeMessage(ChannelHandlerContext ctx, Message message, ChannelPromise promise, ByteBufAllocator allocator) throws TransportException {
ByteBuf bufHeader = null;
try {
PromiseCombiner combiner = new PromiseCombiner(ctx.executor());
bufHeader = allocator.directBuffer(AbstractMessage.HEADER_LENGTH);
NetworkBuffer bodyBuffer = message.encode(bufHeader);
ChannelFuture headerWriteFuture = ctx.write(bufHeader);
/*
* Released bufHeader after in ctx.write(), set bufHeader = null
* to not release again
*/
bufHeader = null;
combiner.add(headerWriteFuture);
if (bodyBuffer != null) {
ByteBuf bodyBuf = bodyBuffer.nettyByteBuf();
// Will call bodyBuf.release() in ctx.write(), retain() first
bodyBuffer.retain();
combiner.add(ctx.write(bodyBuf));
}
combiner.finish(promise);
} catch (Throwable e) {
throw new TransportException("Failed to encode message, " + "message type: %s", e, message.type());
} finally {
if (bufHeader != null) {
bufHeader.release();
}
message.release();
}
}
use of com.baidu.hugegraph.computer.core.network.buffer.NetworkBuffer in project hugegraph-computer by hugegraph.
the class ClientSession method sendAsync.
public synchronized void sendAsync(MessageType messageType, int partition, ByteBuffer buffer) {
E.checkArgument(this.state == TransportState.ESTABLISHED, "The state must be ESTABLISHED instead of %s " + "at sendAsync()", this.state);
int requestId = this.nextRequestId();
NetworkBuffer networkBuffer = new NioBuffer(buffer);
DataMessage dataMessage = new DataMessage(messageType, requestId, partition, networkBuffer);
this.sendFunction.apply(dataMessage);
this.updateFlowBlocking();
}
use of com.baidu.hugegraph.computer.core.network.buffer.NetworkBuffer in project hugegraph-computer by hugegraph.
the class NettyServerHandler method processDataMessage.
@Override
protected void processDataMessage(ChannelHandlerContext ctx, Channel channel, DataMessage dataMessage) {
NetworkBuffer body = dataMessage.body();
try {
int requestId = dataMessage.requestId();
this.serverSession.onRecvData(requestId);
if (body instanceof FileRegionBuffer) {
this.processFileRegionBuffer(ctx, channel, dataMessage, (FileRegionBuffer) body);
} else {
this.handler.handle(dataMessage.type(), dataMessage.partition(), dataMessage.body());
this.serverSession.onHandledData(requestId);
}
} finally {
body.release();
}
}
Aggregations