-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use AsynchronousFileChannel
for npipe
#1408
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
159 changes: 159 additions & 0 deletions
159
docker-java-transport-okhttp/src/main/java/com/github/dockerjava/okhttp/NamedPipeSocket.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
package com.github.dockerjava.okhttp; | ||
|
||
import com.sun.jna.Native; | ||
import com.sun.jna.win32.StdCallLibrary; | ||
import com.sun.jna.win32.W32APIOptions; | ||
|
||
import java.io.FileNotFoundException; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
import java.net.Socket; | ||
import java.net.SocketAddress; | ||
import java.nio.ByteBuffer; | ||
import java.nio.channels.AsynchronousByteChannel; | ||
import java.nio.channels.AsynchronousCloseException; | ||
import java.nio.channels.AsynchronousFileChannel; | ||
import java.nio.channels.Channels; | ||
import java.nio.channels.CompletionHandler; | ||
import java.nio.file.Paths; | ||
import java.nio.file.StandardOpenOption; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.Future; | ||
|
||
class NamedPipeSocket extends Socket { | ||
|
||
private final String socketFileName; | ||
|
||
private AsynchronousFileByteChannel channel; | ||
|
||
NamedPipeSocket(String socketFileName) { | ||
this.socketFileName = socketFileName; | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
if (channel != null) { | ||
channel.close(); | ||
} | ||
} | ||
|
||
@Override | ||
public void connect(SocketAddress endpoint) throws IOException { | ||
connect(endpoint, 0); | ||
} | ||
|
||
@Override | ||
public void connect(SocketAddress endpoint, int timeout) throws IOException { | ||
long startedAt = System.currentTimeMillis(); | ||
|
||
timeout = Math.max(timeout, 10_000); | ||
while (true) { | ||
try { | ||
channel = new AsynchronousFileByteChannel( | ||
AsynchronousFileChannel.open( | ||
Paths.get(socketFileName), | ||
StandardOpenOption.READ, | ||
StandardOpenOption.WRITE | ||
) | ||
); | ||
break; | ||
} catch (FileNotFoundException e) { | ||
if (System.currentTimeMillis() - startedAt >= timeout) { | ||
throw new RuntimeException(e); | ||
} else { | ||
Kernel32.INSTANCE.WaitNamedPipe(socketFileName, 100); | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public InputStream getInputStream() { | ||
return Channels.newInputStream(channel); | ||
} | ||
|
||
@Override | ||
public OutputStream getOutputStream() { | ||
return Channels.newOutputStream(channel); | ||
} | ||
|
||
interface Kernel32 extends StdCallLibrary { | ||
|
||
Kernel32 INSTANCE = Native.load("kernel32", Kernel32.class, W32APIOptions.DEFAULT_OPTIONS); | ||
|
||
@SuppressWarnings("checkstyle:methodname") | ||
boolean WaitNamedPipe(String lpNamedPipeName, int nTimeOut); | ||
} | ||
|
||
private static class AsynchronousFileByteChannel implements AsynchronousByteChannel { | ||
private final AsynchronousFileChannel fileChannel; | ||
|
||
AsynchronousFileByteChannel(AsynchronousFileChannel fileChannel) { | ||
this.fileChannel = fileChannel; | ||
} | ||
|
||
@Override | ||
public <A> void read(ByteBuffer dst, A attachment, CompletionHandler<Integer, ? super A> handler) { | ||
fileChannel.read(dst, 0, attachment, new CompletionHandler<Integer, A>() { | ||
@Override | ||
public void completed(Integer read, A attachment) { | ||
handler.completed(read > 0 ? read : -1, attachment); | ||
} | ||
|
||
@Override | ||
public void failed(Throwable exc, A attachment) { | ||
if (exc instanceof AsynchronousCloseException) { | ||
handler.completed(-1, attachment); | ||
return; | ||
} | ||
handler.failed(exc, attachment); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public Future<Integer> read(ByteBuffer dst) { | ||
CompletableFutureHandler future = new CompletableFutureHandler(); | ||
fileChannel.read(dst, 0, null, future); | ||
return future; | ||
} | ||
|
||
@Override | ||
public <A> void write(ByteBuffer src, A attachment, CompletionHandler<Integer, ? super A> handler) { | ||
fileChannel.write(src, 0, attachment, handler); | ||
} | ||
|
||
@Override | ||
public Future<Integer> write(ByteBuffer src) { | ||
return fileChannel.write(src, 0); | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
fileChannel.close(); | ||
} | ||
|
||
@Override | ||
public boolean isOpen() { | ||
return fileChannel.isOpen(); | ||
} | ||
|
||
private static class CompletableFutureHandler extends CompletableFuture<Integer> implements CompletionHandler<Integer, Object> { | ||
|
||
@Override | ||
public void completed(Integer read, Object attachment) { | ||
complete(read > 0 ? read : -1); | ||
} | ||
|
||
@Override | ||
public void failed(Throwable exc, Object attachment) { | ||
if (exc instanceof AsynchronousCloseException) { | ||
complete(-1); | ||
return; | ||
} | ||
completeExceptionally(exc); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if that should catch
NoSuchFileException
now.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice catch, thanks! 👍