本文主要是介绍Apache Mina 源码再读4 IoSession.write()源码剖析,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
在IoSession 中提供两个接口供写入数据。
Writes the specified 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.
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.
1、WriteFuture write(Object message)
2、WriteFuture write(Object message, SocketAddress destination)
1、写入指定的消息对象到远程对端。写入操作是异步的。 当消息实际被写入远程对端时,IoHandler.messageSent(IoSession,Object)方法将被执行。 也可以,等待返回WriteFuture直到消息被写入到远程对端。
2、当消息从一个服务器端被广播到客户端时,例如:消息从DHCP服务器端广播到客户端时,如果客户端想返回一个相应消息给服务器端,可以是用destination 指定目的地。
public WriteFuture write(Object message, SocketAddress remoteAddress) {if (message == null) {throw new IllegalArgumentException("Trying to write a null message : not allowed");}// We can't send a message to a connected session if we don't have// the remote addressif (!getTransportMetadata().isConnectionless() && (remoteAddress != null)) {throw new UnsupportedOperationException();}// If the session has been closed or is closing, we can't either// send a message to the remote side. We generate a future// containing an exception.if (isClosing() || !isConnected()) {WriteFuture future = new DefaultWriteFuture(this);WriteRequest request = new DefaultWriteRequest(message, future, remoteAddress);WriteException writeException = new WriteToClosedSessionException(request);future.setException(writeException);return future;}FileChannel openedFileChannel = null;// TODO: remove this code as soon as we use InputStream// instead of Object for the mes
这篇关于Apache Mina 源码再读4 IoSession.write()源码剖析的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!