本文整理汇总了Java中org.apache.mina.core.session.IoSession.isConnected方法的典型用法代码示例。如果您正苦于以下问题:Java IoSession.isConnected方法的具体用法?Java IoSession.isConnected怎么用?Java IoSession.isConnected使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.mina.core.session.IoSession
的用法示例。
在下文中一共展示了IoSession.isConnected方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: main
import org.apache.mina.core.session.IoSession; //导入方法依赖的package包/类
public static void main(String[] args) {
IoConnector connector = new NioSocketConnector();
connector.getFilterChain().addLast("logger", new LoggingFilter());
connector.getFilterChain().addLast("codec",
new ProtocolCodecFilter(new PrefixedStringCodecFactory(Charset.forName("UTF-8"))));
connector.setHandler(new TimeClientHander());
ConnectFuture connectFuture = connector.connect(new InetSocketAddress("127.0.0.1", PORT));
// 等待建立连接
connectFuture.awaitUninterruptibly();
System.out.println("连接成功");
IoSession session = connectFuture.getSession();
Scanner sc = new Scanner(System.in);
boolean quit = false;
while (!quit) {
String str = sc.next();
if (str.equalsIgnoreCase("quit")) {
quit = true;
}
session.write(str);
}
// 关闭
if (session != null) {
if (session.isConnected()) {
session.getCloseFuture().awaitUninterruptibly();
}
connector.dispose(true);
}
}
示例2: writeData
import org.apache.mina.core.session.IoSession; //导入方法依赖的package包/类
/**
* Actually write data. Queues the data up unless it relates to the handshake or the
* handshake is done.
*
* @param nextFilter the next filter in filter chain
* @param session the session object
* @param writeRequest the data to write
* @param isHandshakeData true if writeRequest is written by the proxy classes.
*/
public void writeData(final NextFilter nextFilter, final IoSession session, final WriteRequest writeRequest,
final boolean isHandshakeData) {
ProxyLogicHandler handler = getProxyHandler(session);
synchronized (handler) {
if (handler.isHandshakeComplete()) {
// Handshake is done - write data as normal
nextFilter.filterWrite(session, writeRequest);
} else if (isHandshakeData) {
LOGGER.debug(" handshake data: {}", writeRequest.getMessage());
// Writing handshake data
nextFilter.filterWrite(session, writeRequest);
} else {
// Writing non-handshake data before the handshake finished
if (!session.isConnected()) {
// Not even connected - ignore
LOGGER.debug(" Write request on closed session. Request ignored.");
} else {
// Queue the data to be sent as soon as the handshake completes
LOGGER.debug(" Handshaking is not complete yet. Buffering write request.");
handler.enqueueWriteRequest(nextFilter, writeRequest);
}
}
}
}
示例3: filterWrite
import org.apache.mina.core.session.IoSession; //导入方法依赖的package包/类
@Override
public void filterWrite(NextFilter nextFilter, IoSession session, WriteRequest writeRequest) throws SSLException {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("{}: Writing Message : {}", getSessionInfo(session), writeRequest);
}
boolean needsFlush = true;
SslHandler handler = getSslSessionHandler(session);
synchronized (handler) {
if (!isSslStarted(session)) {
handler.scheduleFilterWrite(nextFilter, writeRequest);
}
// Don't encrypt the data if encryption is disabled.
else if (session.containsAttribute(DISABLE_ENCRYPTION_ONCE)) {
// Remove the marker attribute because it is temporary.
session.removeAttribute(DISABLE_ENCRYPTION_ONCE);
handler.scheduleFilterWrite(nextFilter, writeRequest);
} else {
// Otherwise, encrypt the buffer.
IoBuffer buf = (IoBuffer) writeRequest.getMessage();
if (handler.isWritingEncryptedData()) {
// data already encrypted; simply return buffer
handler.scheduleFilterWrite(nextFilter, writeRequest);
} else if (handler.isHandshakeComplete()) {
// SSL encrypt
int pos = buf.position();
handler.encrypt(buf.buf());
buf.position(pos);
IoBuffer encryptedBuffer = handler.fetchOutNetBuffer();
handler.scheduleFilterWrite(nextFilter, new EncryptedWriteRequest(writeRequest, encryptedBuffer));
} else {
if (session.isConnected()) {
// Handshake not complete yet.
handler.schedulePreHandshakeWriteRequest(nextFilter, writeRequest);
}
needsFlush = false;
}
}
}
if (needsFlush) {
handler.flushScheduledEvents();
}
}
示例4: onSessionClosed
import org.apache.mina.core.session.IoSession; //导入方法依赖的package包/类
@Override
public void onSessionClosed(IoSession session) {
if (session != null && session.isConnected())
session.closeOnFlush();
}