public interface IoSession
A handle which represents connection between two end-points regardless of transport types.
IoSession
provides user-defined attributes. User-defined attributes
are application-specific data which are associated with a session.
It often contains objects that represents the state of a higher-level protocol
and becomes a way to exchange data between filters and handlers.
You can simply downcast the session to an appropriate subclass.
IoSession
is thread-safe. But please note that performing
more than one write(Object)
calls at the same time will
cause the IoFilter.filterWrite(IoFilter.NextFilter,IoSession,WriteRequest)
to be executed simultaneously, and therefore you have to make sure the
IoFilter
implementations you're using are thread-safe, too.
Object.equals(Object)
and Object.hashCode()
shall not be overriden
to the default behavior that is defined in Object
.限定符和类型 | 方法和说明 |
---|---|
CloseFuture |
close()
已过时。
use
close(boolean) |
CloseFuture |
close(boolean immediately)
已过时。
Use either the closeNow() or the flushAndClose() methods
|
CloseFuture |
closeNow()
Closes this session immediately.
|
CloseFuture |
closeOnFlush()
Closes this session after all queued write requests are flushed.
|
boolean |
containsAttribute(java.lang.Object key) |
java.lang.Object |
getAttachment()
已过时。
Use
getAttribute(Object) instead. |
java.lang.Object |
getAttribute(java.lang.Object key)
Returns the value of the user-defined attribute of this session.
|
java.lang.Object |
getAttribute(java.lang.Object key,
java.lang.Object defaultValue)
Returns the value of user defined attribute associated with the
specified key.
|
java.util.Set<java.lang.Object> |
getAttributeKeys() |
int |
getBothIdleCount() |
CloseFuture |
getCloseFuture() |
IoSessionConfig |
getConfig() |
long |
getCreationTime() |
java.lang.Object |
getCurrentWriteMessage()
Returns the message which is being written by
IoService . |
WriteRequest |
getCurrentWriteRequest()
Returns the
WriteRequest which is being processed by
IoService . |
IoFilterChain |
getFilterChain() |
IoHandler |
getHandler() |
long |
getId() |
int |
getIdleCount(IdleStatus status) |
long |
getLastBothIdleTime() |
long |
getLastIdleTime(IdleStatus status) |
long |
getLastIoTime() |
long |
getLastReaderIdleTime() |
long |
getLastReadTime() |
long |
getLastWriterIdleTime() |
long |
getLastWriteTime() |
java.net.SocketAddress |
getLocalAddress() |
long |
getReadBytes() |
double |
getReadBytesThroughput() |
int |
getReaderIdleCount() |
long |
getReadMessages() |
double |
getReadMessagesThroughput() |
java.net.SocketAddress |
getRemoteAddress() |
long |
getScheduledWriteBytes() |
int |
getScheduledWriteMessages() |
IoService |
getService() |
java.net.SocketAddress |
getServiceAddress() |
TransportMetadata |
getTransportMetadata() |
WriteRequestQueue |
getWriteRequestQueue()
Get the queue that contains the message waiting for being written.
|
int |
getWriterIdleCount() |
long |
getWrittenBytes() |
double |
getWrittenBytesThroughput() |
long |
getWrittenMessages() |
double |
getWrittenMessagesThroughput() |
boolean |
isActive() |
boolean |
isBothIdle() |
boolean |
isClosing() |
boolean |
isConnected() |
boolean |
isIdle(IdleStatus status) |
boolean |
isReaderIdle() |
boolean |
isReadSuspended()
Is read operation is suspended for this session.
|
boolean |
isSecured() |
boolean |
isWriterIdle() |
boolean |
isWriteSuspended()
Is write operation is suspended for this session.
|
ReadFuture |
read()
TODO This javadoc is wrong.
|
java.lang.Object |
removeAttribute(java.lang.Object key)
Removes a user-defined attribute with the specified key.
|
boolean |
removeAttribute(java.lang.Object key,
java.lang.Object value)
Removes a user defined attribute with the specified key if the current
attribute value is equal to the specified value.
|
boolean |
replaceAttribute(java.lang.Object key,
java.lang.Object oldValue,
java.lang.Object newValue)
Replaces a user defined attribute with the specified key if the
value of the attribute is equals to the specified old value.
|
void |
resumeRead()
Resumes read operations for this session.
|
void |
resumeWrite()
Resumes write operations for this session.
|
java.lang.Object |
setAttachment(java.lang.Object attachment)
已过时。
Use
setAttribute(Object, Object) instead. |
java.lang.Object |
setAttribute(java.lang.Object key)
Sets a user defined attribute without a value.
|
java.lang.Object |
setAttribute(java.lang.Object key,
java.lang.Object value)
Sets a user-defined attribute.
|
java.lang.Object |
setAttributeIfAbsent(java.lang.Object key)
Sets a user defined attribute without a value if the attribute with
the specified key is not set yet.
|
java.lang.Object |
setAttributeIfAbsent(java.lang.Object key,
java.lang.Object value)
Sets a user defined attribute if the attribute with the specified key
is not set yet.
|
void |
setCurrentWriteRequest(WriteRequest currentWriteRequest)
Associate the current write request with the session
|
void |
suspendRead()
Suspends read operations for this session.
|
void |
suspendWrite()
Suspends write operations for this session.
|
void |
updateThroughput(long currentTime,
boolean force)
Update all statistical properties related with throughput assuming
the specified time is the current time.
|
WriteFuture |
write(java.lang.Object message)
Writes the specified
message to remote peer. |
WriteFuture |
write(java.lang.Object message,
java.net.SocketAddress destination)
(Optional) Writes the specified message to the specified destination.
|
long getId()
IoSessionConfig getConfig()
IoFilterChain getFilterChain()
WriteRequestQueue getWriteRequestQueue()
TransportMetadata getTransportMetadata()
TransportMetadata
that this session runs on.ReadFuture read()
ReadFuture
which is notified when a new message is
received, the connection is closed or an exception is caught. This
operation is especially useful when you implement a client application.
TODO : Describe here how we enable this feature.
However, please note that this operation is disabled by default and
throw IllegalStateException
because all received events must be
queued somewhere to support this operation, possibly leading to memory
leak. This means you have to keep calling read()
once you
enabled this operation. To enable this operation, please call
IoSessionConfig.setUseReadOperation(boolean)
with true.java.lang.IllegalStateException
- if
useReadOperation
option has not been enabled.WriteFuture write(java.lang.Object message)
message
to remote peer. This
operation is asynchronous; IoHandler.messageSent(IoSession,Object)
will be invoked when the message is actually sent to remote peer.
You can also wait for the returned WriteFuture
if you want
to wait for the message actually written.message
- The message to writeWriteFuture write(java.lang.Object message, java.net.SocketAddress destination)
IoHandler.messageSent(IoSession, Object)
will be invoked when the message is actually sent to remote peer. You can
also wait for the returned WriteFuture
if you want to wait for
the message actually written.
When you implement a client that receives a broadcast message from a server
such as DHCP server, the client might need to send a response message for the
broadcast message the server sent. Because the remote address of the session
is not the address of the server in case of broadcasting, there should be a
way to specify the destination when you write the response message.
This interface provides write(Object, SocketAddress)
method so you
can specify the destination.
message
- The message to writedestination
- null if you want the message sent to the
default remote addressCloseFuture close(boolean immediately)
CloseFuture
if you want to wait for the session actually closed.immediately
- true
to close this session immediately
. The pending write requests
will simply be discarded.
false
to close this session after all queued
write requests are flushed.CloseFuture closeNow()
CloseFuture
.CloseFuture closeOnFlush()
CloseFuture
if you want to wait
for the session actually closed.@Deprecated CloseFuture close()
close(boolean)
CloseFuture
if you want to wait for the session actually closed.@Deprecated java.lang.Object getAttachment()
getAttribute(Object)
instead.@Deprecated java.lang.Object setAttachment(java.lang.Object attachment)
setAttribute(Object, Object)
instead.attachment
- The attachmentjava.lang.Object getAttribute(java.lang.Object key)
key
- the key of the attributejava.lang.Object getAttribute(java.lang.Object key, java.lang.Object defaultValue)
if (containsAttribute(key)) { return getAttribute(key); } else { setAttribute(key, defaultValue); return defaultValue; }
key
- the key of the attribute we want to retreivedefaultValue
- the default value of the attributejava.lang.Object setAttribute(java.lang.Object key, java.lang.Object value)
key
- the key of the attributevalue
- the value of the attributejava.lang.Object setAttribute(java.lang.Object key)
Boolean.TRUE
.key
- the key of the attributejava.lang.Object setAttributeIfAbsent(java.lang.Object key, java.lang.Object value)
if (containsAttribute(key)) { return getAttribute(key); } else { return setAttribute(key, value); }
key
- The key of the attribute we want to setvalue
- The value we want to setjava.lang.Object setAttributeIfAbsent(java.lang.Object key)
Boolean.TRUE
.
This method is same with the following code except that the operation
is performed atomically.
if (containsAttribute(key)) { return getAttribute(key); // might not always be Boolean.TRUE. } else { return setAttribute(key); }
key
- The key of the attribute we want to setjava.lang.Object removeAttribute(java.lang.Object key)
key
- The key of the attribute we want to removeboolean removeAttribute(java.lang.Object key, java.lang.Object value)
if (containsAttribute(key) && getAttribute(key).equals(value)) { removeAttribute(key); return true; } else { return false; }
key
- The key we want to removevalue
- The value we want to removeboolean replaceAttribute(java.lang.Object key, java.lang.Object oldValue, java.lang.Object newValue)
if (containsAttribute(key) && getAttribute(key).equals(oldValue)) { setAttribute(key, newValue); return true; } else { return false; }
key
- The key we want to replaceoldValue
- The previous valuenewValue
- The new valueboolean containsAttribute(java.lang.Object key)
key
- The key of the attribute we are looking for in the sessionjava.util.Set<java.lang.Object> getAttributeKeys()
boolean isConnected()
boolean isActive()
boolean isClosing()
boolean isSecured()
CloseFuture getCloseFuture()
CloseFuture
of this session. This method returns
the same instance whenever user calls it.java.net.SocketAddress getRemoteAddress()
java.net.SocketAddress getLocalAddress()
java.net.SocketAddress getServiceAddress()
IoService
listens to to manage
this session. If this session is managed by IoAcceptor
, it
returns the SocketAddress
which is specified as a parameter of
IoAcceptor.bind()
. If this session is managed by
IoConnector
, this method returns the same address with
that of getRemoteAddress()
.void setCurrentWriteRequest(WriteRequest currentWriteRequest)
currentWriteRequest
- the current write request to associatevoid suspendRead()
void suspendWrite()
void resumeRead()
void resumeWrite()
boolean isReadSuspended()
boolean isWriteSuspended()
void updateThroughput(long currentTime, boolean force)
calculation interval
.
If, however, force is specified as true, this method
updates the throughput properties immediately.currentTime
- the current time in millisecondsforce
- Force the update if truelong getReadBytes()
long getWrittenBytes()
long getReadMessages()
long getWrittenMessages()
double getReadBytesThroughput()
double getWrittenBytesThroughput()
double getReadMessagesThroughput()
double getWrittenMessagesThroughput()
int getScheduledWriteMessages()
long getScheduledWriteBytes()
java.lang.Object getCurrentWriteMessage()
IoService
.WriteRequest getCurrentWriteRequest()
WriteRequest
which is being processed by
IoService
.long getCreationTime()
long getLastIoTime()
long getLastReadTime()
long getLastWriteTime()
boolean isIdle(IdleStatus status)
status
- The researched idle statusIdleStatus
.boolean isReaderIdle()
IdleStatus.READER_IDLE
.isIdle(IdleStatus)
boolean isWriterIdle()
IdleStatus.WRITER_IDLE
.isIdle(IdleStatus)
boolean isBothIdle()
IdleStatus.BOTH_IDLE
.isIdle(IdleStatus)
int getIdleCount(IdleStatus status)
status
- The researched idle statusIdleStatus
.
If sessionIdle event is fired first after some time after I/O, idleCount becomes 1. idleCount resets to 0 if any I/O occurs again, otherwise it increases to 2 and so on if sessionIdle event is fired again without any I/O between two (or more) sessionIdle events.
int getReaderIdleCount()
IdleStatus.READER_IDLE
.getIdleCount(IdleStatus)
int getWriterIdleCount()
IdleStatus.WRITER_IDLE
.getIdleCount(IdleStatus)
int getBothIdleCount()
IdleStatus.BOTH_IDLE
.getIdleCount(IdleStatus)
long getLastIdleTime(IdleStatus status)
status
- The researched idle statusIdleStatus
.long getLastReaderIdleTime()
IdleStatus.READER_IDLE
.getLastIdleTime(IdleStatus)
long getLastWriterIdleTime()
IdleStatus.WRITER_IDLE
.getLastIdleTime(IdleStatus)
long getLastBothIdleTime()
IdleStatus.BOTH_IDLE
.getLastIdleTime(IdleStatus)