io.github.yawenok.apns.http2.impl.handler.Http2PingHandler Maven / Gradle / Ivy
package io.github.yawenok.apns.http2.impl.handler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelOutboundHandlerAdapter;
import io.netty.channel.ChannelPromise;
import io.netty.handler.codec.http2.Http2CodecUtil;
import io.netty.handler.codec.http2.Http2ConnectionEncoder;
import java.io.Serializable;
public class Http2PingHandler extends ChannelOutboundHandlerAdapter {
private final Integer maxIdle;
private final Http2ConnectionEncoder encoder;
public Http2PingHandler(final Http2ConnectionEncoder encoder) {
this(30, encoder);
}
public Http2PingHandler(final Integer maxIdle, final Http2ConnectionEncoder encoder) {
this.maxIdle = maxIdle;
this.encoder = encoder;
}
public Integer getMaxIdle() {
return maxIdle;
}
@Override
public void write(final ChannelHandlerContext ctx, final Object msg, final ChannelPromise promise) throws Exception {
if (!(msg instanceof Ping)) {
ctx.write(msg, promise);
return;
}
// Send ping frame
encoder.writePing(ctx, false, Http2CodecUtil.emptyPingBuf(), promise);
ctx.channel().flush();
}
public static final Ping PING = new Ping();
public static class Ping implements Serializable {}
}