下面列出了io.netty.channel.ChannelPipeline#context ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
protected void removeDecoder(ChannelHandlerContext ctx) throws Exception {
ChannelPipeline p = ctx.pipeline();
if (p.context(decoderName) != null) {
p.remove(decoderName);
}
}
private String getHttpDecoderContextName(ChannelPipeline pipeline){
ChannelHandlerContext ctx = pipeline.context(HttpRequestDecoder.class);
if (ctx == null) {
ctx = pipeline.context(HttpServerCodec.class);
}
return ctx == null? null : ctx.name();
}
/**
* Get the context for a the channel handler before a certain name.
*
* @param name The name of the channel handler
* @param pipeline The pipeline to target
* @return The ChannelHandler before the one requested.
*/
public static ChannelHandlerContext getContextBefore(String name, ChannelPipeline pipeline) {
boolean mark = false;
for (String s : pipeline.names()) {
if (mark) {
return pipeline.context(pipeline.get(s));
}
if (s.equalsIgnoreCase(name))
mark = true;
}
return null;
}
public static ChannelHandlerContext getPreviousContext(String name, ChannelPipeline pipeline) {
String previous = null;
for (String entry : pipeline.toMap().keySet()) {
if (entry.equals(name)) {
return pipeline.context(previous);
}
previous = entry;
}
return null;
}
@SuppressWarnings("deprecation")
private void remove(ChannelHandlerContext ctx) {
try {
ctx.channel().attr(OneTime.channelConfig).remove();
ChannelPipeline pipeline = ctx.pipeline();
if (pipeline.context(this) != null) {
pipeline.remove(this);
}
} finally {
negotiateMap.remove(ctx);
}
}
@Override
public void channelRegistered(ChannelHandlerContext channelHandlerContext) throws Exception {
try {
initChannel(channelHandlerContext.channel());
} catch (Throwable t) {
exceptionCaught(channelHandlerContext, t);
} finally {
ChannelPipeline pipeline = channelHandlerContext.pipeline();
if (pipeline.context(this) != null) {
pipeline.remove(this);
}
}
channelHandlerContext.pipeline().fireChannelRegistered();
}
@Override
public void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress, SocketAddress localAddress,
ChannelPromise promise) throws Exception {
// Remember the requested remote address for later use.
final InetSocketAddress inetRemoteAddr = (InetSocketAddress) remoteAddress;
this.remoteAddress = inetRemoteAddr;
// Configure the pipeline.
final Channel ch = ctx.channel();
ChannelUtil.disableWriterBufferWatermark(ch);
final ChannelPipeline p = ch.pipeline();
p.addLast(new FlushConsolidationHandler());
p.addLast(ReadSuppressingAndChannelDeactivatingHandler.INSTANCE);
try {
if (sslCtx != null) {
configureAsHttps(ch, inetRemoteAddr);
} else {
configureAsHttp(ch);
}
} catch (Throwable t) {
promise.tryFailure(t);
ctx.close();
} finally {
if (p.context(this) != null) {
p.remove(this);
}
}
ctx.connect(remoteAddress, localAddress, promise);
}
public final ChannelFuture handshake(Channel channel, boolean sendResponse, ChannelPromise promise) {
ChannelPipeline p = channel.pipeline();
ChannelHandlerContext ctx = p.context(RfbClientDecoder.class);
p.addBefore(ctx.name(), "rfb-security-decoder", newSecurityDecoder());
ChannelHandlerContext ctx2 = p.context(RfbClientEncoder.class);
p.addBefore(ctx2.name(), "rfb-security-encoder", newSecurityEncoder());
if (!sendResponse) {
return promise.setSuccess();
}
channel.writeAndFlush(Unpooled.buffer(1).writeByte(securityType.getType()), promise);
return promise;
}
public void initCodec(IPacketIdCodec packetIdCodec, AbstractPacketEncoder encoder, AbstractPacketDecoder decoder) {
ChannelPipeline pipeline = networkmanager.getChannel().pipeline();
this.logicCtx = pipeline.context(ChannelHandlers.LOGIC);
this.rawSendCtx = pipeline.context(ChannelHandlers.RAW_CAPTURE_SEND);
this.rawRecvCtx = pipeline.context(ChannelHandlers.RAW_CAPTURE_RECEIVE);
this.codec = new PacketDataCodecImpl(this, packetIdCodec, pipeline.context(encoder), pipeline.context(decoder));
encoder.init(codec);
decoder.init(codec);
}
@Override
public void channelRegistered(ChannelHandlerContext channelHandlerContext) throws Exception {
try {
initChannel(channelHandlerContext.channel());
} catch (Throwable t) {
exceptionCaught(channelHandlerContext, t);
} finally {
ChannelPipeline pipeline = channelHandlerContext.pipeline();
if (pipeline.context(this) != null) {
pipeline.remove(this);
}
}
channelHandlerContext.pipeline().fireChannelRegistered();
}
protected void releaseHandlers(PooledConnection conn) {
final ChannelPipeline pipeline = conn.getChannel().pipeline();
removeHandlerFromPipeline(OriginResponseReceiver.CHANNEL_HANDLER_NAME, pipeline);
// The Outbound handler is always after the inbound handler, so look for it.
ChannelHandlerContext passportStateHttpClientHandlerCtx =
pipeline.context(PassportStateHttpClientHandler.OutboundHandler.class);
pipeline.addAfter(passportStateHttpClientHandlerCtx.name(), IDLE_STATE_HANDLER_NAME,
new IdleStateHandler(0, 0, connPoolConfig.getIdleTimeout(), TimeUnit.MILLISECONDS));
}
/**
* Validates and finishes the opening handshake initiated by {@link #handshake}}.
*
* @param channel
* Channel
* @param response
* HTTP response containing the closing handshake details
*/
public final void finishHandshake(Channel channel, FullHttpResponse response) {
verify(response);
// Verify the subprotocol that we received from the server.
// This must be one of our expected subprotocols - or null/empty if we didn't want to speak a subprotocol
String receivedProtocol = response.headers().get(HttpHeaders.Names.SEC_WEBSOCKET_PROTOCOL);
receivedProtocol = receivedProtocol != null ? receivedProtocol.trim() : null;
String expectedProtocol = expectedSubprotocol != null ? expectedSubprotocol : "";
boolean protocolValid = false;
if (expectedProtocol.isEmpty() && receivedProtocol == null) {
// No subprotocol required and none received
protocolValid = true;
setActualSubprotocol(expectedSubprotocol); // null or "" - we echo what the user requested
} else if (!expectedProtocol.isEmpty() && receivedProtocol != null && !receivedProtocol.isEmpty()) {
// We require a subprotocol and received one -> verify it
for (String protocol : StringUtil.split(expectedSubprotocol, ',')) {
if (protocol.trim().equals(receivedProtocol)) {
protocolValid = true;
setActualSubprotocol(receivedProtocol);
break;
}
}
} // else mixed cases - which are all errors
if (!protocolValid) {
throw new WebSocketHandshakeException(String.format(
"Invalid subprotocol. Actual: %s. Expected one of: %s",
receivedProtocol, expectedSubprotocol));
}
setHandshakeComplete();
ChannelPipeline p = channel.pipeline();
// Remove decompressor from pipeline if its in use
HttpContentDecompressor decompressor = p.get(HttpContentDecompressor.class);
if (decompressor != null) {
p.remove(decompressor);
}
// Remove aggregator if present before
HttpObjectAggregator aggregator = p.get(HttpObjectAggregator.class);
if (aggregator != null) {
p.remove(aggregator);
}
ChannelHandlerContext ctx = p.context(HttpResponseDecoder.class);
if (ctx == null) {
ctx = p.context(HttpClientCodec.class);
if (ctx == null) {
throw new IllegalStateException("ChannelPipeline does not contain " +
"a HttpRequestEncoder or HttpClientCodec");
}
p.replace(ctx.name(), "ws-decoder", newWebsocketDecoder());
} else {
if (p.get(HttpRequestEncoder.class) != null) {
p.remove(HttpRequestEncoder.class);
}
p.replace(ctx.name(),
"ws-decoder", newWebsocketDecoder());
}
}