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);
}
}
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);
}
}
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);
}
}
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);
}
}
Aggregations