Skip to content

Commit

Permalink
Fix behavior of TunChannel#isOpen
Browse files Browse the repository at this point in the history
  • Loading branch information
HeikoBornholdt committed Nov 3, 2024
1 parent e69ecfb commit 31b23b5
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 17 deletions.
12 changes: 8 additions & 4 deletions src/main/java/org/drasyl/channel/tun/TunChannel.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public class TunChannel extends AbstractChannel {
private boolean readPending;
private final EventLoop readLoop = new DefaultEventLoop();
private TunDevice device;
private boolean closed;

public TunChannel() {
super(null);
Expand All @@ -85,12 +86,12 @@ public TunChannelConfig config() {

@Override
public boolean isOpen() {
return device == null || !device.isClosed();
return !closed;
}

@Override
public boolean isActive() {
return device != null && isOpen();
return !closed && device != null;
}

@Override
Expand Down Expand Up @@ -128,8 +129,11 @@ protected void doDisconnect() throws Exception {

@Override
protected void doClose() throws Exception {
if (device != null) {
device.close();
if (!closed) {
closed = true;
if (device != null) {
device.close();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

public abstract class AbstractTunDevice implements TunDevice {
protected final TunAddress localAddress;
protected boolean closed;

protected AbstractTunDevice(TunAddress localAddress) {
this.localAddress = requireNonNull(localAddress);
Expand All @@ -37,9 +36,4 @@ protected AbstractTunDevice(TunAddress localAddress) {
public TunAddress localAddress() {
return localAddress;
}

@Override
public boolean isClosed() {
return closed;
}
}
7 changes: 0 additions & 7 deletions src/main/java/org/drasyl/channel/tun/jna/TunDevice.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,4 @@ public interface TunDevice extends Closeable {
* @throws IOException if write failed
*/
void writePacket(final ByteBufAllocator alloc, final TunPacket msg) throws IOException;

/**
* Returns whether the device is closed or not.
*
* @return {@code true} if the device has been closed.
*/
boolean isClosed();
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public final class DarwinTunDevice extends AbstractTunDevice {
}));
private final int fd;
private final NativeLong readBytes;
protected boolean closed;

private DarwinTunDevice(final int fd, final int mtu, final TunAddress localAddress) {
super(localAddress);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public final class LinuxTunDevice extends AbstractTunDevice {
private static final IllegalArgumentException ILLEGAL_NAME_EXCEPTION = new IllegalArgumentException("Device name must be an ASCII string shorter than 16 characters or null.");
private final int fd;
private final NativeLong mtu;
protected boolean closed;

private LinuxTunDevice(final int fd, final int mtu, final TunAddress localAddress) {
super(localAddress);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public final class WindowsTunDevice extends AbstractTunDevice {
public static final WString TUNNEL_TYPE = new WString("drasyl");
private final WINTUN_ADAPTER_HANDLE adapter;
private final WINTUN_SESSION_HANDLE session;
protected boolean closed;

private WindowsTunDevice(final WINTUN_ADAPTER_HANDLE adapter,
final WINTUN_SESSION_HANDLE session,
Expand Down

0 comments on commit 31b23b5

Please sign in to comment.