I - the type of "initial" message. This will be either HttpResponse or HttpRequest.public abstract class ProxyConnection<I> extends SimpleChannelInboundHandler<I>
Base class for objects that represent a connection to/from our proxy.
A ProxyConnection models a bidirectional message flow on top of a Netty Channel.
The readProxyIncoming(Object) method is called whenever a new message arrives on the underlying socket.
The writeToChannel(Object) method can be called by anyone wanting to write data out of the connection.
ProxyConnection has a lifecycle and its current state within that lifecycle is recorded as a ConnectionState.
The allowed states and transitions vary a little depending on the concrete implementation of ProxyConnection.
However, all ProxyConnections share the following lifecycle events:
connected() - Once the underlying channel is active, the ProxyConnection is considered connected and
moves into ConnectionState.AWAITING_INITIAL. The Channel is recorded at this time for later referencing.disconnected() - When the underlying channel goes inactive, the ProxyConnection moves into
ConnectionState.DISCONNECTEDbecameWritable() - When the underlying channel becomes writeable, this callback is invoked.
By default, incoming data on the underlying channel is automatically read and passed to the readProxyIncoming(Object)
method. Reading can be stopped and resumed using stopReading() and resumeReading().
| 限定符和类型 | 类和说明 |
|---|---|
protected class |
ProxyConnection.RequestReadMonitor
Utility handler for monitoring requests read on this connection.
|
protected class |
ProxyConnection.ResponseReadMonitor
Utility handler for monitoring responses read on this connection.
|
ChannelHandler.Sharable| 限定符和类型 | 字段和说明 |
|---|---|
private String |
applicationName |
protected Channel |
channel |
protected ChannelHandlerContext |
ctx |
private ConnectionState |
currentState |
protected long |
lastReadTime |
protected ProxyConnectionLogger |
log |
protected org.slf4j.Logger |
logger |
protected ProxyManager |
proxyManager |
protected SSLEngine |
sslEngine
If using encryption, this holds our
SSLEngine. |
| 限定符 | 构造器和说明 |
|---|---|
protected |
ProxyConnection(ConnectionState initialState,
ProxyManager proxyServer,
String applicationName)
Construct a new ProxyConnection.
|
| 限定符和类型 | 方法和说明 |
|---|---|
protected void |
becameSaturated()
Callback that's invoked if this connection becomes saturated.
|
protected void |
becameWritable()
Callback that's invoked when this connection becomes writeable again.
|
protected void |
become(ConnectionState state)
Udpates the current state to the given value.
|
void |
channelActive(ChannelHandlerContext ctx)
Only once the Netty Channel is active to we recognize the ProxyConnection as connected.
|
void |
channelInactive(ChannelHandlerContext ctx)
As soon as the Netty Channel is inactive, we recognize the ProxyConnection as disconnected.
|
protected void |
channelRead0(ChannelHandlerContext ctx,
Object msg)
Adapting the Netty API
|
void |
channelRegistered(ChannelHandlerContext ctx) |
protected void |
channelRegistered0(ChannelHandlerContext ctx) |
void |
channelWritabilityChanged(ChannelHandlerContext ctx) |
private void |
closeChannel(io.netty.util.concurrent.Promise<Void> promise) |
protected void |
connected()
This method is called as soon as the underlying
Channel is connected. |
(专用程序包) io.netty.util.concurrent.Future<Void> |
disconnect()
Disconnects.
|
protected void |
disconnected()
This method is called as soon as the underlying
Channel becomes disconnected. |
protected io.netty.util.concurrent.Future<Channel> |
encrypt(ChannelPipeline pipeline,
SSLEngine sslEngine)
Encrypts traffic on this connection with SSL/TLS.
|
protected io.netty.util.concurrent.Future<Channel> |
encrypt(SSLEngine sslEngine)
Encrypts traffic on this connection with SSL/TLS.
|
void |
exceptionCaught(ChannelHandlerContext ctx,
Throwable cause) |
protected void |
exceptionCaught(Throwable cause)
Override this to handle exceptions that occurred during asynchronous processing on the
Channel. |
protected String |
getApplicationName() |
Channel |
getChannel() |
protected ConnectionState |
getCurrentState() |
(专用程序包) ProxyConnectionLogger |
getLOG() |
SSLEngine |
getSslEngine() |
protected boolean |
is(ConnectionState state)
Utility for checking current state.
|
protected boolean |
isConnecting()
If this connection is currently in the process of going through a
ConnectionFlow, this will return true. |
protected boolean |
isSaturated()
Indicates whether or not this connection is saturated (i.e. not writeable).
|
protected void |
readIncoming(ChannelHandlerContext ctx,
Object msg)
Read is invoked automatically by Netty as messages arrive on the socket.
|
protected abstract void |
readProxyIncoming(Object msg) |
protected void |
resumeReading()
Call this to resume reading.
|
protected void |
stopReading()
Call this to stop reading.
|
protected void |
timedOut()
This method is called when the underlying
Channel times out due to an idle timeout. |
void |
userEventTriggered(ChannelHandlerContext ctx,
Object evt)
We're looking for
IdleStateEvents to see if we need to disconnect. |
protected void |
writeRaw(io.netty.buffer.ByteBuf buf)
Writes raw buffers to the connection.
|
protected ChannelFuture |
writeToChannel(Object msg) |
acceptInboundMessage, channelReadchannelReadComplete, channelUnregisteredensureNotSharable, handlerAdded, handlerRemoved, isSharableclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waithandlerAdded, handlerRemovedprotected final ProxyConnectionLogger log
protected org.slf4j.Logger logger
protected final ProxyManager proxyManager
protected volatile ChannelHandlerContext ctx
protected volatile Channel channel
private volatile ConnectionState currentState
protected volatile long lastReadTime
private final String applicationName
protected ProxyConnection(ConnectionState initialState, ProxyManager proxyServer, String applicationName)
initialState - the state in which this connection starts outproxyServer - the ProxyManager in which we're runningapplicationName - the application name in which we're runningprotected void readIncoming(ChannelHandlerContext ctx, Object msg)
ctx - handler contextmsg - messageprotected abstract void readProxyIncoming(Object msg)
protected void writeRaw(io.netty.buffer.ByteBuf buf)
buf - byte bufferprotected ChannelFuture writeToChannel(Object msg)
protected void connected()
Channel is connected. Note that for proxies with complex
ConnectionFlows that include SSL handshaking and other such things, just because the Channel is
connected doesn't mean that our connection is fully established.protected void disconnected()
Channel becomes disconnected.protected void timedOut()
Channel times out due to an idle timeout.protected io.netty.util.concurrent.Future<Channel> encrypt(SSLEngine sslEngine)
sslEngine - the SSLEngine for doing the encryptionprotected io.netty.util.concurrent.Future<Channel> encrypt(ChannelPipeline pipeline, SSLEngine sslEngine)
pipeline - the ChannelPipeline on which to enable encryptionsslEngine - the SSLEngine for doing the encryptionprotected void becameSaturated()
protected void becameWritable()
protected void exceptionCaught(Throwable cause)
Channel.cause - errorio.netty.util.concurrent.Future<Void> disconnect()
private void closeChannel(io.netty.util.concurrent.Promise<Void> promise)
protected boolean isSaturated()
protected boolean is(ConnectionState state)
state - connection stateprotected boolean isConnecting()
ConnectionFlow, this will return true.protected void become(ConnectionState state)
state - connection stateprotected ConnectionState getCurrentState()
public SSLEngine getSslEngine()
protected void stopReading()
protected void resumeReading()
ProxyConnectionLogger getLOG()
protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception
channelRead0 在类中 SimpleChannelInboundHandler<I>Exceptionpublic final void channelRegistered(ChannelHandlerContext ctx) throws Exception
channelRegistered 在接口中 ChannelInboundHandlerchannelRegistered 在类中 ChannelInboundHandlerAdapterExceptionprotected final void channelRegistered0(ChannelHandlerContext ctx) throws Exception
Exceptionpublic final void channelActive(ChannelHandlerContext ctx) throws Exception
channelActive 在接口中 ChannelInboundHandlerchannelActive 在类中 ChannelInboundHandlerAdapterExceptionpublic final void channelInactive(ChannelHandlerContext ctx) throws Exception
channelInactive 在接口中 ChannelInboundHandlerchannelInactive 在类中 ChannelInboundHandlerAdapterExceptionpublic final void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception
public final void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception
exceptionCaught 在接口中 ChannelHandlerexceptionCaught 在接口中 ChannelInboundHandlerexceptionCaught 在类中 ChannelInboundHandlerAdapterExceptionpublic void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception
We're looking for IdleStateEvents to see if we need to disconnect.
Note - we don't care what kind of IdleState we got. Thanks to qbast for pointing this out.
userEventTriggered 在接口中 ChannelInboundHandleruserEventTriggered 在类中 ChannelInboundHandlerAdapterExceptionpublic Channel getChannel()
protected String getApplicationName()
Copyright © 2022 Zodiac. All rights reserved.