Package org.apache.sshd.common.io.nio2
Class Nio2Session
java.lang.Object
org.apache.sshd.common.util.logging.AbstractLoggingBean
org.apache.sshd.common.util.closeable.IoBaseCloseable
org.apache.sshd.common.util.closeable.AbstractCloseable
org.apache.sshd.common.io.nio2.Nio2Session
- All Implemented Interfaces:
Closeable,AutoCloseable,Channel,Closeable,IoSession,ConnectionEndpointsIndicator
-
Nested Class Summary
Nested classes/interfaces inherited from class org.apache.sshd.common.util.closeable.AbstractCloseable
AbstractCloseable.State -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final SocketAddressprivate final AtomicReference<Nio2DefaultIoWriteFuture> static final intprivate final longprivate final IoHandlerprivate final AtomicLongprivate final AtomicLongprivate final SocketAddressprivate final AtomicBooleanprivate final PropertyResolverprivate final AtomicLongprivate Threadprivate Runnableprivate final SocketAddressprivate final Nio2Serviceprivate static final AtomicLongprivate final AsynchronousSocketChannelprivate booleanprivate final Objectprivate final AtomicLongprivate final Queue<Nio2DefaultIoWriteFuture> Fields inherited from class org.apache.sshd.common.util.closeable.AbstractCloseable
closeFuture, futureLock, stateFields inherited from class org.apache.sshd.common.util.logging.AbstractLoggingBean
log -
Constructor Summary
ConstructorsConstructorDescriptionNio2Session(Nio2Service service, PropertyResolver propertyResolver, IoHandler handler, AsynchronousSocketChannel socket, SocketAddress acceptanceAddress) -
Method Summary
Modifier and TypeMethodDescriptionprotected Nio2CompletionHandler<Integer, Object> createReadCycleCompletionHandler(ByteBuffer buffer, Readable bufReader) protected Nio2CompletionHandler<Integer, Object> createWriteCycleCompletionHandler(Nio2DefaultIoWriteFuture future, AsynchronousSocketChannel socket, ByteBuffer buffer) protected CloseFutureprotected voiddoCloseImmediately is called once and only once with state == Immediateprotected voiddoReadCycle(ByteBuffer buffer, Nio2CompletionHandler<Integer, Object> completion) protected voiddoReadCycle(ByteBuffer buffer, Readable bufReader) protected voiddoShutdownOutputStream(Nio2DefaultIoWriteFuture future, AsynchronousSocketChannel socket) protected voiddoWriteCycle(ByteBuffer buffer, Nio2CompletionHandler<Integer, Object> completion) protected voidexceptionCaught(Throwable exc) protected voidfinishWrite(Nio2DefaultIoWriteFuture future) getAttribute(Object key) Returns the value of the user-defined attribute of this session.longgetId()protected voidhandleCompletedWriteCycle(Nio2DefaultIoWriteFuture future, AsynchronousSocketChannel socket, ByteBuffer buffer, int writeLen, Nio2CompletionHandler<Integer, Object> completionHandler, Integer result, Object attachment) protected voidhandleReadCycleCompletion(ByteBuffer buffer, Readable bufReader, Nio2CompletionHandler<Integer, Object> completionHandler, Integer result, Object attachment) protected voidhandleReadCycleFailure(ByteBuffer buffer, Readable bufReader, Throwable exc, Object attachment) protected voidhandleWriteCycleFailure(Nio2DefaultIoWriteFuture future, AsynchronousSocketChannel socket, ByteBuffer buffer, int writeLen, Throwable exc, Object attachment) removeAttribute(Object key) Removes a user-defined attribute with the specified key.voidResume read operations on this session.setAttribute(Object key, Object value) Sets a user-defined attribute.setAttributeIfAbsent(Object key, Object value) Sets a user defined attribute if the attribute with the specified key is not set yet.voidHandle received EOF.voidvoidstartReading(byte[] buf) voidstartReading(byte[] buf, int offset, int len) voidstartReading(int bufSize) voidstartReading(ByteBuffer buffer) protected voidvoidsuspend()Intended for tests simulating a sudden connection drop only! Do not call otherwise.voidSuspend read operations on this session.toString()writeBuffer(Buffer buffer) Write a packet on the socket.Methods inherited from class org.apache.sshd.common.util.closeable.AbstractCloseable
addCloseFutureListener, builder, close, getFutureLock, isClosed, isClosing, preClose, removeCloseFutureListenerMethods inherited from class org.apache.sshd.common.util.logging.AbstractLoggingBean
debug, debug, debug, debug, debug, error, error, error, error, error, getSimplifiedLogger, info, info, warn, warn, warn, warn, warn, warn, warn, warnMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface org.apache.sshd.common.Closeable
addCloseFutureListener, close, isClosed, isClosing, isOpen, removeCloseFutureListener
-
Field Details
-
DEFAULT_READBUF_SIZE
public static final int DEFAULT_READBUF_SIZE- See Also:
-
SESSION_ID_GENERATOR
-
id
private final long id -
service
-
ioHandler
-
socketChannel
-
attributes
-
localAddress
-
remoteAddress
-
acceptanceAddress
-
propertyResolver
-
writes
-
currentWrite
-
readCyclesCounter
-
lastReadCycleStart
-
writeCyclesCounter
-
lastWriteCycleStart
-
outputShutDown
-
suspendLock
-
suspend
private volatile boolean suspend -
readRunnable
-
readerThread
-
-
Constructor Details
-
Nio2Session
public Nio2Session(Nio2Service service, PropertyResolver propertyResolver, IoHandler handler, AsynchronousSocketChannel socket, SocketAddress acceptanceAddress) throws IOException - Throws:
IOException
-
-
Method Details
-
getId
public long getId() -
getAttribute
Description copied from interface:IoSessionReturns the value of the user-defined attribute of this session.- Specified by:
getAttributein interfaceIoSession- Parameters:
key- the key of the attribute- Returns:
nullif there is no attribute with the specified key
-
setAttribute
Description copied from interface:IoSessionSets a user-defined attribute.- Specified by:
setAttributein interfaceIoSession- Parameters:
key- the key of the attributevalue- the value of the attribute- Returns:
- The old value of the attribute -
nullif it is new.
-
setAttributeIfAbsent
Description copied from interface:IoSessionSets a user defined attribute if the attribute with the specified key is not set yet. This method is same with the following code except that the operation is performed atomically.if (containsAttribute(key)) { return getAttribute(key); } else { return setAttribute(key, value); }- Specified by:
setAttributeIfAbsentin interfaceIoSession- Parameters:
key- The key of the attribute we want to setvalue- The value we want to set- Returns:
- The old value of the attribute -
nullif not found.
-
removeAttribute
Description copied from interface:IoSessionRemoves a user-defined attribute with the specified key.- Specified by:
removeAttributein interfaceIoSession- Parameters:
key- The key of the attribute we want to remove- Returns:
- The old value of the attribute -
nullif not found.
-
getRemoteAddress
- Specified by:
getRemoteAddressin interfaceConnectionEndpointsIndicator- Returns:
- the socket address of remote peer.
-
getLocalAddress
- Specified by:
getLocalAddressin interfaceConnectionEndpointsIndicator- Returns:
- the socket address of local machine which is associated with this session.
-
getAcceptanceAddress
- Specified by:
getAcceptanceAddressin interfaceIoSession- Returns:
- The service address through which this session was accepted -
nullif session was initiated by this peer instead of being accepted
-
getSocket
-
getIoHandler
-
suspend
public void suspend()Intended for tests simulating a sudden connection drop only! Do not call otherwise. -
writeBuffer
Description copied from interface:IoSessionWrite a packet on the socket. Multiple writes can be issued concurrently and will be queued.- Specified by:
writeBufferin interfaceIoSession- Parameters:
buffer- the buffer send. NOTE: the buffer must not be touched until the returned write future is completed.- Returns:
- An
IoWriteFuturethat can be used to check when the packet has actually been sent - Throws:
IOException- if an error occurred when sending the packet
-
exceptionCaught
-
doCloseGracefully
- Overrides:
doCloseGracefullyin classAbstractCloseable
-
doCloseImmediately
protected void doCloseImmediately()Description copied from class:AbstractCloseabledoCloseImmediately is called once and only once with state == Immediate
Overriding methods should always call the base implementation. It may be called concurrently while preClose() or doCloseGracefully is executing
- Overrides:
doCloseImmediatelyin classAbstractCloseable
-
getService
- Specified by:
getServicein interfaceIoSession- Returns:
- the
IoServicethat created this session.
-
shutdownOutputStream
Description copied from interface:IoSessionHandle received EOF.- Specified by:
shutdownOutputStreamin interfaceIoSession- Throws:
IOException- If failed to shutdown the stream
-
doShutdownOutputStream
protected void doShutdownOutputStream(Nio2DefaultIoWriteFuture future, AsynchronousSocketChannel socket) throws IOException - Throws:
IOException
-
startReading
public void startReading() -
startReading
public void startReading(int bufSize) -
startReading
public void startReading(byte[] buf) -
startReading
public void startReading(byte[] buf, int offset, int len) -
startReading
-
doReadCycle
-
createReadCycleCompletionHandler
protected Nio2CompletionHandler<Integer,Object> createReadCycleCompletionHandler(ByteBuffer buffer, Readable bufReader) -
handleReadCycleCompletion
protected void handleReadCycleCompletion(ByteBuffer buffer, Readable bufReader, Nio2CompletionHandler<Integer, Object> completionHandler, Integer result, Object attachment) -
handleReadCycleFailure
protected void handleReadCycleFailure(ByteBuffer buffer, Readable bufReader, Throwable exc, Object attachment) -
suspendRead
public void suspendRead()Description copied from interface:IoSessionSuspend read operations on this session. If the session usage includes a graceful shutdown with messages being exchanged, the caller needs to take care of resuming reading the input in order to actually be able to carry on the conversation with the peer.- Specified by:
suspendReadin interfaceIoSession
-
resumeRead
public void resumeRead()Description copied from interface:IoSessionResume read operations on this session.- Specified by:
resumeReadin interfaceIoSession
-
doReadCycle
-
startWriting
protected void startWriting() -
doWriteCycle
-
createWriteCycleCompletionHandler
protected Nio2CompletionHandler<Integer,Object> createWriteCycleCompletionHandler(Nio2DefaultIoWriteFuture future, AsynchronousSocketChannel socket, ByteBuffer buffer) -
handleCompletedWriteCycle
protected void handleCompletedWriteCycle(Nio2DefaultIoWriteFuture future, AsynchronousSocketChannel socket, ByteBuffer buffer, int writeLen, Nio2CompletionHandler<Integer, Object> completionHandler, Integer result, Object attachment) -
handleWriteCycleFailure
protected void handleWriteCycleFailure(Nio2DefaultIoWriteFuture future, AsynchronousSocketChannel socket, ByteBuffer buffer, int writeLen, Throwable exc, Object attachment) -
finishWrite
-
toString
-