public abstract class Channel extends Object
This abstract class provides the basic functions of an SSH2 channel. All terminal sessions, forwarded connections etc, are channels. Either side may open a channel and multiple channels are multiplexed into a single connection. Channels are flow-controlled so that no data may be sent to a channel until a message is received to indicate that a window space is available.
Channel implementations are identified by name and to support the opening of
a channel your server should add the channel's unique name to the
com.maverick.sshd.ConfigurationContext#supportedChannels()
. This can
be completed in your
com.maverick.sshd.SshDaemon#configure(ConfigurationContext)
method.
protected void configure(ConfigurationContext context) { context.supportedChannels().add("chat@sshtools.com", Class.forName("com.sshtools.ChatChannel")); }
Data sent by the channel is buffered and sent when window space is available
on the remote side. The incoming window space is decremented whenever data is
received with the abstract evaluateWindowSpace(int)
method being
called to enable the channel implementation to add further window space
should it require.
Modifier and Type | Class and Description |
---|---|
protected class |
Channel.DataBlock |
protected class |
Channel.QueuedData |
Modifier and Type | Field and Description |
---|---|
protected ConnectionProtocol |
connection
The Connection Protocol instance managing this session, use this instance
to disconnect the session or obtain the ip address of the remote client.
|
protected int |
localwindow |
protected Object |
localWindowLock |
Constructor and Description |
---|
Channel(String channelType,
int maximumPacketSize,
int initialWindowSize)
Construct a channel with the specified settings.
|
Modifier and Type | Method and Description |
---|---|
void |
addEventListener(ChannelEventListener listener)
Enable other objects to receive channel events
|
void |
addInputListener(OutputStream out)
Add an OutputStream to receive all channel data input from the remote
side of the connection.
|
void |
addOutputListener(OutputStream out)
Add an OutputStream to receive all of the channels output.
|
protected boolean |
canClose()
Can this channel close?
|
void |
clearIdleState(IdleStateListener listener)
Clear an idle state listener.
|
void |
close() |
protected void |
close(boolean forceClose)
This method closes the channel and free's its resources.
|
protected void |
consumeWindowSpace(int length) |
protected abstract byte[] |
createChannel()
Called when the channel is being created.
|
protected ConnectionAwareTask |
createCloseTask(boolean forceClose) |
protected abstract void |
evaluateWindowSpace(int remaining)
Called to evaluate the window space available.
|
protected void |
executeTask(ConnectionAwareTask task) |
String |
getChannelType()
The name of this channel.
|
ConnectionProtocol |
getConnection()
Returns the
ConnectionProtocol associated with this channel. |
SshContext |
getContext()
Get the current configuration from the underlying connection.
|
int |
getLocalId()
The local channel id
|
int |
getLocalPacket()
The maximum size of a single packet that the local side will accept.
|
int |
getLocalWindow()
The current size of the local data window.
|
int |
getQueueSize() |
int |
getRemoteId()
The remote sides channel id
|
int |
getRemotePacket()
The maximum size of a single packet that the remote side will accept.
|
int |
getRemoteWindow()
The current size of the remote data window.
|
String |
getSessionIdentifier()
Get the session id for the current connection.
|
boolean |
isClosed()
Indicates the channel has been closed
|
boolean |
isClosing()
Indicates whether the channel is currently performing a close operation
|
boolean |
isEOF()
Indicates that the channel is EOF (it will not be receiving any more data
from the remote side).
|
protected boolean |
isOpen()
Get the open state of the channel.
|
protected boolean |
isSelfManagedWindowSpace() |
protected void |
markEOF() |
protected abstract void |
onChannelClosed()
Called when the channel has been closed to enable resources to be freed.
|
protected abstract void |
onChannelClosing()
Called before the channel is closed
|
protected abstract void |
onChannelData(byte[] data)
Called by the channel when data arrives from the remote side.
|
protected abstract void |
onChannelFree()
This method is called when the channel has been closed and all its
resources are no longer required.
|
protected abstract void |
onChannelOpen()
Called when the channel has been opened - this method is only called when
the remote side requests a channel open.
|
protected abstract void |
onChannelOpenConfirmation()
Called when the channel has been confirmed as open by the remote side -
this method is only called when the channel has been requested by this
side of the connection
|
protected void |
onChannelOpenFailure()
Called when the remote side fails to open a channel in response to our
request.
|
protected abstract void |
onChannelRequest(String type,
boolean wantreply,
byte[] requestdata)
Called when a channel request is received.
|
protected abstract void |
onExtendedData(byte[] data,
int type)
Called by the channel when extended data arrives
|
protected abstract void |
onLocalEOF()
Called when the local side is EOF.
|
protected void |
onRemoteClose()
Called when the remote side closes the channel.
|
protected abstract void |
onRemoteEOF()
Called when the remote side reports its OutputStream as EOF.
|
protected void |
onWindowAdjust(int count) |
protected abstract byte[] |
openChannel(byte[] requestdata)
Called when the channel is being opened.
|
protected int |
queueSize() |
protected void |
registerExtendedData(int type)
Call this method to register an extended data type.
|
void |
resetIdleState(IdleStateListener listener)
Allows a channel to register and receive idle state events.
|
void |
sendChannelData(byte[] data)
Send channel data to the remote side of the channel.
|
void |
sendChannelData(byte[] data,
int off,
int len)
Send channel data to the remote side of the channel.
|
void |
sendChannelData(byte[] data,
int off,
int len,
Runnable r) |
void |
sendChannelData(byte[] data,
Runnable r) |
void |
sendChannelDataAndBlock(byte[] data,
int off,
int len) |
protected void |
sendChannelRequest(String type,
boolean wantreply,
byte[] requestdata)
Send a channel request.
|
protected void |
sendEOF()
You can send EOF when you have no more data to send.
|
protected void |
sendExtendedData(byte[] data,
int type)
Send extended channel data.
|
protected void |
sendExtendedData(byte[] data,
int off,
int len,
int type)
Send extended channel data.
|
protected void |
sendRequestResponse(boolean succeeded)
Send a channel request.
|
protected void |
sendWindowAdjust(int count)
Adjust the local window by adding more bytes.
|
protected ConnectionProtocol connection
protected int localwindow
protected Object localWindowLock
public Channel(String channelType, int maximumPacketSize, int initialWindowSize)
channelType
- the name of the channel, for example "session" or
"tcpip-forward"maximumPacketSize
- the maximum size of an individual packet that the remote side
can send through the channelinitialWindowSize
- the initial size of the local window.public boolean isClosed()
public boolean isEOF()
public int getQueueSize()
public void resetIdleState(IdleStateListener listener)
IdleStateListener
instance passed as a parameter
will receive notification once the channel reaches an idle state.
NOTE: It is the callers responsibility to call clearIdleState method to
ensure references are released.listener
- IdleStateListenerpublic void clearIdleState(IdleStateListener listener)
listener
- public void addEventListener(ChannelEventListener listener)
listener
- public void addInputListener(OutputStream out)
out
- OutputStreampublic void addOutputListener(OutputStream out)
out
- OutputStreampublic String getChannelType()
public int getRemoteWindow()
public int getLocalWindow()
public int getLocalPacket()
public int getRemotePacket()
public int getLocalId()
public int getRemoteId()
protected int queueSize()
protected void registerExtendedData(int type) throws IOException
type
- IOException
public String getSessionIdentifier()
protected void onWindowAdjust(int count)
public ConnectionProtocol getConnection()
ConnectionProtocol
associated with this channel.protected void consumeWindowSpace(int length)
protected boolean isSelfManagedWindowSpace()
public void sendChannelData(byte[] data)
data
- public void sendChannelData(byte[] data, Runnable r)
public void sendChannelData(byte[] data, int off, int len)
data
- off
- len
- public void sendChannelData(byte[] data, int off, int len, Runnable r)
public void sendChannelDataAndBlock(byte[] data, int off, int len)
public SshContext getContext()
protected void sendExtendedData(byte[] data, int type)
data
- type
- protected void sendExtendedData(byte[] data, int off, int len, int type)
data
- off
- len
- type
- protected abstract void onChannelData(byte[] data)
data
- protected abstract void onExtendedData(byte[] data, int type)
data
- protected void sendChannelRequest(String type, boolean wantreply, byte[] requestdata)
Send a channel request. Many channel types have extensions that are specific to that particular channel type. An example is requresting a pty (pseudo terminal) for an interactive session. This method enables the sending of channel requests but does not support receiving responses. This should not present a problem as server implementations tend to send messages as one way information, for example the exit-status request of an interactive session.
To handle requests from a client implement
onChannelRequest(String,boolean,byte[])
.
type
- wantreply
- requestdata
- protected void onChannelOpenFailure()
protected void onRemoteClose()
public boolean isClosing()
public void close()
protected void executeTask(ConnectionAwareTask task)
protected void close(boolean forceClose)
protected ConnectionAwareTask createCloseTask(boolean forceClose)
protected abstract void onChannelFree()
protected abstract byte[] createChannel() throws IOException
IOException
protected abstract byte[] openChannel(byte[] requestdata) throws WriteOperationRequest, ChannelOpenException
requestdata
- WriteOperationRequest
ChannelOpenException
protected abstract void onChannelOpenConfirmation()
protected abstract void onChannelClosed()
protected abstract void onChannelOpen()
protected abstract void onChannelClosing()
protected abstract void onChannelRequest(String type, boolean wantreply, byte[] requestdata)
type
- wantreply
- requestdata
- protected abstract void evaluateWindowSpace(int remaining)
remaining
- protected abstract void onRemoteEOF()
protected void sendEOF()
protected void markEOF()
protected boolean canClose()
protected abstract void onLocalEOF()
protected boolean isOpen()
protected void sendRequestResponse(boolean succeeded)
succeeded
- protected void sendWindowAdjust(int count)
count
- Copyright © 2024. All rights reserved.