Search in sources :

Example 1 with Session

use of com.lipisoft.toyshark.Session in project ToyShark by LipiLee.

the class SocketDataReaderWorker method run.

@Override
public void run() {
    Session session = SessionManager.INSTANCE.getSessionByKey(sessionKey);
    if (session == null) {
        Log.e(TAG, "Session NOT FOUND");
        return;
    }
    AbstractSelectableChannel channel = session.getChannel();
    if (channel instanceof SocketChannel) {
        readTCP(session);
    } else if (channel instanceof DatagramChannel) {
        readUDP(session);
    } else {
        return;
    }
    if (session.isAbortingConnection()) {
        Log.d(TAG, "removing aborted connection -> " + sessionKey);
        session.getSelectionKey().cancel();
        if (channel instanceof SocketChannel) {
            try {
                SocketChannel socketChannel = (SocketChannel) channel;
                if (socketChannel.isConnected()) {
                    socketChannel.close();
                }
            } catch (IOException e) {
                Log.e(TAG, e.toString());
            }
        } else {
            try {
                DatagramChannel datagramChannel = (DatagramChannel) channel;
                if (datagramChannel.isConnected()) {
                    datagramChannel.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        SessionManager.INSTANCE.closeSession(session);
    } else {
        session.setBusyread(false);
    }
}
Also used : SocketChannel(java.nio.channels.SocketChannel) DatagramChannel(java.nio.channels.DatagramChannel) IOException(java.io.IOException) Session(com.lipisoft.toyshark.Session) AbstractSelectableChannel(java.nio.channels.spi.AbstractSelectableChannel)

Example 2 with Session

use of com.lipisoft.toyshark.Session in project ToyShark by LipiLee.

the class SocketNIODataService method processTCPSelectionKey.

private void processTCPSelectionKey(SelectionKey key) throws IOException {
    if (!key.isValid()) {
        Log.d(TAG, "Invalid SelectionKey for TCP");
        return;
    }
    SocketChannel channel = (SocketChannel) key.channel();
    Session session = SessionManager.INSTANCE.getSessionByChannel(channel);
    if (session == null) {
        return;
    }
    if (!session.isConnected() && key.isConnectable()) {
        String ips = PacketUtil.intToIPAddress(session.getDestIp());
        int port = session.getDestPort();
        SocketAddress address = new InetSocketAddress(ips, port);
        Log.d(TAG, "connecting to remote tcp server: " + ips + ":" + port);
        boolean connected = false;
        if (!channel.isConnected() && !channel.isConnectionPending()) {
            try {
                connected = channel.connect(address);
            } catch (ClosedChannelException | UnresolvedAddressException | UnsupportedAddressTypeException | SecurityException e) {
                Log.e(TAG, e.toString());
                session.setAbortingConnection(true);
            } catch (IOException e) {
                Log.e(TAG, e.toString());
                session.setAbortingConnection(true);
            }
        }
        if (connected) {
            session.setConnected(connected);
            Log.d(TAG, "connected immediately to remote tcp server: " + ips + ":" + port);
        } else {
            if (channel.isConnectionPending()) {
                connected = channel.finishConnect();
                session.setConnected(connected);
                Log.d(TAG, "connected to remote tcp server: " + ips + ":" + port);
            }
        }
    }
    if (channel.isConnected()) {
        processSelector(key, session);
    }
}
Also used : SocketChannel(java.nio.channels.SocketChannel) ClosedChannelException(java.nio.channels.ClosedChannelException) UnsupportedAddressTypeException(java.nio.channels.UnsupportedAddressTypeException) InetSocketAddress(java.net.InetSocketAddress) IOException(java.io.IOException) UnresolvedAddressException(java.nio.channels.UnresolvedAddressException) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) Session(com.lipisoft.toyshark.Session)

Example 3 with Session

use of com.lipisoft.toyshark.Session in project ToyShark by LipiLee.

the class SocketDataWriterWorker method run.

@Override
public void run() {
    final Session session = SessionManager.INSTANCE.getSessionByKey(sessionKey);
    if (session == null) {
        Log.d(TAG, "No session related to " + sessionKey + "for write");
        return;
    }
    session.setBusywrite(true);
    AbstractSelectableChannel channel = session.getChannel();
    if (channel instanceof SocketChannel) {
        writeTCP(session);
    } else if (channel instanceof DatagramChannel) {
        writeUDP(session);
    } else {
        return;
    }
    session.setBusywrite(false);
    if (session.isAbortingConnection()) {
        Log.d(TAG, "removing aborted connection -> " + sessionKey);
        session.getSelectionKey().cancel();
        if (channel instanceof SocketChannel) {
            try {
                SocketChannel socketChannel = (SocketChannel) channel;
                if (socketChannel.isConnected()) {
                    socketChannel.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else if (channel instanceof DatagramChannel) {
            try {
                DatagramChannel datagramChannel = (DatagramChannel) channel;
                if (datagramChannel.isConnected()) {
                    datagramChannel.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        SessionManager.INSTANCE.closeSession(session);
    }
}
Also used : SocketChannel(java.nio.channels.SocketChannel) DatagramChannel(java.nio.channels.DatagramChannel) IOException(java.io.IOException) Session(com.lipisoft.toyshark.Session) AbstractSelectableChannel(java.nio.channels.spi.AbstractSelectableChannel)

Example 4 with Session

use of com.lipisoft.toyshark.Session in project ToyShark by LipiLee.

the class SocketNIODataService method processUDPSelectionKey.

private void processUDPSelectionKey(SelectionKey key) {
    if (!key.isValid()) {
        Log.d(TAG, "Invalid SelectionKey for UDP");
        return;
    }
    DatagramChannel channel = (DatagramChannel) key.channel();
    Session session = SessionManager.INSTANCE.getSessionByChannel(channel);
    if (session == null) {
        return;
    }
    if (!session.isConnected() && key.isConnectable()) {
        String ips = PacketUtil.intToIPAddress(session.getDestIp());
        int port = session.getDestPort();
        SocketAddress address = new InetSocketAddress(ips, port);
        try {
            Log.d(TAG, "selector: connecting to remote UDP server: " + ips + ":" + port);
            channel = channel.connect(address);
            session.setChannel(channel);
            session.setConnected(channel.isConnected());
        } catch (Exception e) {
            e.printStackTrace();
            session.setAbortingConnection(true);
        }
    }
    if (channel.isConnected()) {
        processSelector(key, session);
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) DatagramChannel(java.nio.channels.DatagramChannel) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) ClosedChannelException(java.nio.channels.ClosedChannelException) UnsupportedAddressTypeException(java.nio.channels.UnsupportedAddressTypeException) IOException(java.io.IOException) UnresolvedAddressException(java.nio.channels.UnresolvedAddressException) Session(com.lipisoft.toyshark.Session)

Aggregations

Session (com.lipisoft.toyshark.Session)4 IOException (java.io.IOException)4 DatagramChannel (java.nio.channels.DatagramChannel)3 SocketChannel (java.nio.channels.SocketChannel)3 InetSocketAddress (java.net.InetSocketAddress)2 SocketAddress (java.net.SocketAddress)2 ClosedChannelException (java.nio.channels.ClosedChannelException)2 UnresolvedAddressException (java.nio.channels.UnresolvedAddressException)2 UnsupportedAddressTypeException (java.nio.channels.UnsupportedAddressTypeException)2 AbstractSelectableChannel (java.nio.channels.spi.AbstractSelectableChannel)2