Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,42 @@ boolean GetOverlappedResult(
boolean bWait
);

/**
* Variant taking the raw OVERLAPPED pointer. Prefer this one for overlapped I/O: passing a
* {@link WinBase.OVERLAPPED} Structure lets JNA auto-write its Java-side fields into native memory
* before the call, overwriting the completion status set by the kernel.
*/
boolean GetOverlappedResult(
WinNT.HANDLE hFile,
Pointer lpOverlapped,
IntByReference lpNumberOfBytesTransferred,
boolean bWait
);

/**
* ReadFile with a native buffer and raw OVERLAPPED pointer, both of which must stay valid
* until the (possibly asynchronous) operation has completed.
*/
boolean ReadFile(
WinNT.HANDLE hFile,
Pointer lpBuffer,
int nNumberOfBytesToRead,
IntByReference lpNumberOfBytesRead,
Pointer lpOverlapped
);

/**
* WriteFile with a native buffer and raw OVERLAPPED pointer, both of which must stay valid
* until the (possibly asynchronous) operation has completed.
*/
boolean WriteFile(
WinNT.HANDLE hFile,
Pointer lpBuffer,
int nNumberOfBytesToWrite,
IntByReference lpNumberOfBytesWritten,
Pointer lpOverlapped
);

/**
* Cancels all pending I/O operations on the specified file handle.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.net.SocketException;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
Expand All @@ -25,13 +26,16 @@ public class NamedPipeJniSocket extends FileSocket {
private static final Logger log = LoggerFactory.getLogger(NamedPipeJniSocket.class);

private WinNT.HANDLE handle;
private boolean connected = false;
private boolean closed = false;
private volatile boolean connected = false;
private volatile boolean closed = false;

private BufferedSource source;
private BufferedSink sink;

private final Timeout ioTimeout = new Timeout().timeout(1000, TimeUnit.MILLISECONDS);
// shared and mutable: setSoTimeout() updates this instance, which NamedPipeSource/NamedPipeSink
// read from on every call, so a timeout change takes effect immediately without recreating the streams.
// 0 means no timeout (blocks indefinitely), matching java.net.Socket's default SO_TIMEOUT.
private final Timeout ioTimeout = new Timeout();

@Override
public void connect(SocketAddress endpoint, int timeout) throws IOException {
Expand All @@ -42,20 +46,42 @@ public void connect(SocketAddress endpoint, int timeout) throws IOException {
InetSocketAddress inetSocketAddress = (InetSocketAddress) endpoint;
InetAddress address = inetSocketAddress.getAddress();
String socketPath = decodeHostname(address);
connect(socketPath);
connect(socketPath, timeout);
}

void connect(String socketPath) {
void connect(String socketPath) throws IOException {
connect(socketPath, 10_000);
}

void connect(String socketPath, int timeoutMs) throws IOException {
socketPath = socketPath.replace("/", "\\");
log.debug("connect via '{}'...", socketPath);

handle = NamedPipeUtils.connect(socketPath, 10_000, 500, 50);
handle = NamedPipeUtils.connect(socketPath, timeoutMs, 500, 50);

connected = true;
source = Okio.buffer(new NamedPipeSource(handle, ioTimeout));
sink = Okio.buffer(new NamedPipeSink(handle, ioTimeout));
}

@Override
public void setSoTimeout(int timeout) throws SocketException {
if (timeout < 0) {
throw new IllegalArgumentException("timeout can't be negative");
}
if (timeout == 0) {
ioTimeout.clearTimeout();
}
else {
ioTimeout.timeout(timeout, TimeUnit.MILLISECONDS);
}
}

@Override
public int getSoTimeout() throws SocketException {
return (int) (ioTimeout.timeoutNanos() / 1_000_000);
}

@Override
public InputStream getInputStream() throws IOException {
ensureOpen();
Expand Down Expand Up @@ -107,6 +133,27 @@ public boolean isClosed() {
return closed;
}

@Override
public boolean isBound() {
return connected;
}

@Override
public void shutdownOutput() throws SocketException {
// Named pipes have no half-close, see NamedPipeSocket#shutdownOutput(). Without this override, the
// inherited implementation would fail, because it relies on a SocketImpl that is never connected.
if (!connected) {
throw new SocketException("Socket is not connected");
}
}

@Override
public void shutdownInput() throws SocketException {
if (!connected) {
throw new SocketException("Socket is not connected");
}
}

private void ensureOpen() throws IOException {
if (closed) {
throw new IOException("NamedPipeSocket is closed");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@

import org.jetbrains.annotations.NotNull;

import com.sun.jna.platform.win32.Kernel32;
import com.sun.jna.platform.win32.WinError;
import com.sun.jna.platform.win32.WinNT;
import com.sun.jna.ptr.IntByReference;

import okio.Buffer;
import okio.Sink;
Expand All @@ -16,11 +13,11 @@
public class NamedPipeSink implements Sink {

private final WinNT.HANDLE handle;
private final int timeoutMillis;
private final Timeout timeout;

public NamedPipeSink(WinNT.HANDLE handle, Timeout timeout) {
this.handle = handle;
this.timeoutMillis = (int) timeout.timeoutNanos() / 1000;
this.timeout = timeout;
}

@Override
Expand All @@ -37,21 +34,8 @@ public void write(@NotNull Buffer source, long byteCount) throws IOException {
}

byte[] data = source.readByteArray(byteCount);
IntByReference bytesWritten = new IntByReference();
//boolean ok = NamedPipeUtils.writeFromBuffer(handle, data, data.length, bytesWritten);
boolean ok = NamedPipeUtils.writeOverlapped(handle, data, data.length, bytesWritten, timeoutMillis);
if (!ok) {
int err = Kernel32.INSTANCE.GetLastError();
if (err == WinError.ERROR_OPERATION_ABORTED) {
// Expected when CancelIoEx() is called during close()
return;
}
throw new IOException("Failed to write to Named Pipe. WinError=" + err);
}

if (bytesWritten.getValue() <= 0) {
throw new IOException("No bytes written to Named Pipe");
}
int timeoutMillis = (int) (timeout.timeoutNanos() / 1_000_000);
NamedPipeUtils.writeOverlapped(handle, data, data.length, timeoutMillis);
}

@Override
Expand Down
Loading
Loading