mirror of
https://github.com/corda/corda.git
synced 2025-06-12 20:28:18 +00:00
Implemented a basic NIO socket channel interface. Non-blocking socket channels
and server socket channels are implemented. This version works but only when libnative is linked with g++ (because of C++ object creation code that fails without this linking)
This commit is contained in:
78
classpath/java/nio/channels/SocketChannel.java
Normal file
78
classpath/java/nio/channels/SocketChannel.java
Normal file
@ -0,0 +1,78 @@
|
||||
package java.nio.channels;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
public class SocketChannel extends SelectableChannel
|
||||
implements ReadableByteChannel, WritableByteChannel
|
||||
{
|
||||
public static final int InvalidSocket = -1;
|
||||
|
||||
protected int socket = InvalidSocket;
|
||||
protected boolean open = true;
|
||||
protected boolean connected = false;
|
||||
|
||||
public static SocketChannel open() {
|
||||
return new SocketChannel();
|
||||
}
|
||||
|
||||
public void configureBlocking(boolean v) {
|
||||
if (v) throw new IllegalArgumentException();
|
||||
}
|
||||
|
||||
public void connect(InetSocketAddress address) throws Exception {
|
||||
socket = doConnect(address.getHostName(), address.getPort());
|
||||
}
|
||||
|
||||
public void close() throws IOException {
|
||||
super.close();
|
||||
if (! open) return;
|
||||
closeSocket();
|
||||
open = false;
|
||||
}
|
||||
|
||||
public boolean isOpen() {
|
||||
return open;
|
||||
}
|
||||
|
||||
private int doConnect(String host, int port) throws Exception {
|
||||
boolean b[] = new boolean[1];
|
||||
int s = natDoConnect(host, port, b);
|
||||
connected = b[0];
|
||||
return s;
|
||||
}
|
||||
|
||||
public int read(ByteBuffer b) throws IOException {
|
||||
if (b.remaining() == 0) return 0;
|
||||
int r = natRead(socket, b.array(), b.arrayOffset() + b.position(), b.remaining());
|
||||
b.position(b.position() + r);
|
||||
return r;
|
||||
}
|
||||
|
||||
public int write(ByteBuffer b) throws IOException {
|
||||
if (! connected) {
|
||||
natThrowWriteError(socket);
|
||||
}
|
||||
int w = natWrite(socket, b.array(), b.arrayOffset() + b.position(), b.remaining());
|
||||
b.position(b.position() + w);
|
||||
return w;
|
||||
}
|
||||
|
||||
private void closeSocket() {
|
||||
natCloseSocket(socket);
|
||||
}
|
||||
|
||||
int socketFD() {
|
||||
return socket;
|
||||
}
|
||||
|
||||
private static native int natDoConnect(String host, int port, boolean[] connected)
|
||||
throws Exception;
|
||||
private static native int natRead(int socket, byte[] buffer, int offset, int length)
|
||||
throws IOException;
|
||||
private static native int natWrite(int socket, byte[] buffer, int offset, int length)
|
||||
throws IOException;
|
||||
private static native void natThrowWriteError(int socket) throws IOException;
|
||||
private static native void natCloseSocket(int socket);
|
||||
}
|
Reference in New Issue
Block a user