use of io.netty.buffer.ByteBuf in project netty by netty.
the class GlobalChannelTrafficShapingHandler method handlerRemoved.
@Override
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
trafficCounter.resetCumulativeTime();
Channel channel = ctx.channel();
Integer key = channel.hashCode();
PerChannel perChannel = channelQueues.remove(key);
if (perChannel != null) {
// write operations need synchronization
synchronized (perChannel) {
if (channel.isActive()) {
for (ToSend toSend : perChannel.messagesQueue) {
long size = calculateSize(toSend.toSend);
trafficCounter.bytesRealWriteFlowControl(size);
perChannel.channelTrafficCounter.bytesRealWriteFlowControl(size);
perChannel.queueSize -= size;
queuesSize.addAndGet(-size);
ctx.write(toSend.toSend, toSend.promise);
}
} else {
queuesSize.addAndGet(-perChannel.queueSize);
for (ToSend toSend : perChannel.messagesQueue) {
if (toSend.toSend instanceof ByteBuf) {
((ByteBuf) toSend.toSend).release();
}
}
}
perChannel.messagesQueue.clear();
}
}
releaseWriteSuspended(ctx);
releaseReadSuspended(ctx);
super.handlerRemoved(ctx);
}
use of io.netty.buffer.ByteBuf in project netty by netty.
the class GlobalTrafficShapingHandler method handlerRemoved.
@Override
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
Channel channel = ctx.channel();
Integer key = channel.hashCode();
PerChannel perChannel = channelQueues.remove(key);
if (perChannel != null) {
// write operations need synchronization
synchronized (perChannel) {
if (channel.isActive()) {
for (ToSend toSend : perChannel.messagesQueue) {
long size = calculateSize(toSend.toSend);
trafficCounter.bytesRealWriteFlowControl(size);
perChannel.queueSize -= size;
queuesSize.addAndGet(-size);
ctx.write(toSend.toSend, toSend.promise);
}
} else {
queuesSize.addAndGet(-perChannel.queueSize);
for (ToSend toSend : perChannel.messagesQueue) {
if (toSend.toSend instanceof ByteBuf) {
((ByteBuf) toSend.toSend).release();
}
}
}
perChannel.messagesQueue.clear();
}
}
releaseWriteSuspended(ctx);
releaseReadSuspended(ctx);
super.handlerRemoved(ctx);
}
use of io.netty.buffer.ByteBuf in project netty by netty.
the class OptionalSslHandlerTest method handlerReplaced.
@Test
public void handlerReplaced() throws Exception {
final ChannelHandler nonSslHandler = Mockito.mock(ChannelHandler.class);
OptionalSslHandler handler = new OptionalSslHandler(sslContext) {
@Override
protected ChannelHandler newNonSslHandler(ChannelHandlerContext context) {
return nonSslHandler;
}
@Override
protected String newNonSslHandlerName() {
return HANDLER_NAME;
}
};
final ByteBuf payload = Unpooled.copiedBuffer("plaintext".getBytes());
try {
handler.decode(context, payload, null);
verify(pipeline).replace(handler, HANDLER_NAME, nonSslHandler);
} finally {
payload.release();
}
}
use of io.netty.buffer.ByteBuf in project netty by netty.
the class OptionalSslHandlerTest method sslHandlerReplaced.
@Test
public void sslHandlerReplaced() throws Exception {
final SslHandler sslHandler = Mockito.mock(SslHandler.class);
OptionalSslHandler handler = new OptionalSslHandler(sslContext) {
@Override
protected SslHandler newSslHandler(ChannelHandlerContext context, SslContext sslContext) {
return sslHandler;
}
@Override
protected String newSslHandlerName() {
return SSL_HANDLER_NAME;
}
};
final ByteBuf payload = Unpooled.wrappedBuffer(new byte[] { 22, 3, 1, 0, 5 });
try {
handler.decode(context, payload, null);
verify(pipeline).replace(handler, SSL_HANDLER_NAME, sslHandler);
} finally {
payload.release();
}
}
use of io.netty.buffer.ByteBuf in project netty by netty.
the class OptionalSslHandlerTest method decodeBuffered.
@Test
public void decodeBuffered() throws Exception {
OptionalSslHandler handler = new OptionalSslHandler(sslContext);
final ByteBuf payload = Unpooled.wrappedBuffer(new byte[] { 22, 3 });
try {
handler.decode(context, payload, null);
verifyZeroInteractions(pipeline);
} finally {
payload.release();
}
}
Aggregations