⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 socketsession.java

📁 JAVA解析MSNP15协议
💻 JAVA
字号:
package jm.jmmnet.sf.jml.protocol;
import java.io.IOException;
import java.net.ConnectException;
import java.net.SocketAddress;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;

import net.sf.cindy.impl.StreamChannelSession;
public class SocketSession extends StreamChannelSession
{

    public SocketSession()
    {
    }

    public void setSocketAddress(SocketAddress socketaddress)
        throws IllegalStateException
    {
        if(isStarted())
        {
            throw new IllegalStateException("can't set socket address after session started");
        } else
        {
            socketAddress = socketaddress;
            return;
        }
    }

    public void setChannel(SocketChannel socketchannel)
        throws IllegalStateException
    {
        if(isStarted())
        {
            throw new IllegalStateException("can't set socket channel after session started");
        } else
        {
            channel = socketchannel;
            return;
        }
    }

    public SocketChannel getChannel()
    {
        return channel;
    }

    public synchronized void start(boolean flag)
        throws IllegalStateException
    {
        if(isStarted())
            return;
        if(socketAddress == null && channel == null)
            throw new IllegalStateException("must specify socket address or socket channel before start");
        if(channel == null)
            try
            {
                channel = SocketChannel.open();
                channel.configureBlocking(false);
                channel.connect(socketAddress);
            }
            catch(IOException ioexception)
            {
                dispatchException(ioexception);
                if(channel != null)
                {
                    try
                    {
                        channel.close();
                    }
                    catch(IOException ioexception1) { }
                    channel = null;
                }
                dispatchSessionClosed();
                return;
            }
        startSession(channel, channel, flag);
    }

    public boolean isConnected()
    {
        if(channel == null)
            return false;
        else
            return channel.isConnected();
    }

    public void onEvent(Object obj, Object obj1)
    {
//        if(obj == Constants.EV_CONNECTABLE)
            onConnectable();
        super.onEvent(obj, obj1);
    }

    protected void onConnectable()
    {
        try
        {
            channel.finishConnect();
            selectionKey.interestOps(selectionKey.interestOps() & -9 | 1);
            dispatchSessionEstablished();
        }
        catch(ConnectException connectexception)
        {
            close();
        }
        catch(IOException ioexception)
        {
            dispatchException(ioexception);
            close();
        }
    }

    protected void onRegister(Selector selector)
    {
        try
        {
            if(isConnected())
            {
                selectionKey = channel.register(selector, 1, this);
                super.onRegister(selector);
                dispatchSessionEstablished();
            } else
            {
                selectionKey = channel.register(selector, 8, this);
                super.onRegister(selector);
            }
        }
        catch(ClosedChannelException closedchannelexception)
        {
            close();
        }
    }

    protected void onUnregister()
    {
        channel = null;
        selectionKey = null;
        super.onUnregister();
    }

    private SocketAddress socketAddress;
    private SocketChannel channel;
    private SelectionKey selectionKey;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -