Permalink
Browse files

add UnixSocket#setKeepAlive()

  • Loading branch information...
1 parent 876339b commit ce845201098d40eb6e1954057e6495a3f661ecdf Wayne Meissner committed Nov 12, 2009
Showing with 32 additions and 0 deletions.
  1. +13 −0 src/com/kenai/jnr/unixsocket/Native.java
  2. +19 −0 src/com/kenai/jnr/unixsocket/UnixSocket.java
@@ -18,6 +18,8 @@
import com.kenai.constantine.platform.ProtocolFamily;
import com.kenai.constantine.platform.Sock;
+import com.kenai.constantine.platform.SocketLevel;
+import com.kenai.constantine.platform.SocketOption;
import com.kenai.jaffl.LastError;
import com.kenai.jaffl.Library;
import com.kenai.jaffl.Platform;
@@ -26,6 +28,8 @@
import com.kenai.jaffl.annotations.Transient;
import com.kenai.jaffl.byref.IntByReference;
import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
class Native {
static final String[] libnames = Platform.getPlatform().getOS() == Platform.OS.SOLARIS
@@ -46,6 +50,8 @@
int getpeername(int fd, @Out SockAddrUnix addr, @In @Out IntByReference len);
int socketpair(int domain, int type, int protocol, @Out int[] sv);
int fcntl(int fd, int cmd, int data);
+ int getsockopt(int s, int level, int optname, @Out ByteBuffer optval, @In @Out IntByReference optlen);
+ int setsockopt(int s, int level, int optname, @In ByteBuffer optval, int optlen);
String strerror(int error);
}
@@ -105,4 +111,11 @@ public static void setBlocking(int fd, boolean block) {
}
libc().fcntl(fd, LibC.F_SETFL, flags);
}
+
+ public static int setsockopt(int s, SocketLevel level, SocketOption optname, boolean optval) {
+ ByteBuffer buf = ByteBuffer.allocate(4);
+ buf.order(ByteOrder.BIG_ENDIAN);
+ buf.putInt(optval ? 1 : 0).flip();
+ return libsocket().setsockopt(s, level.value(), optname.value(), buf, buf.remaining());
+ }
}
@@ -16,7 +16,12 @@
package com.kenai.jnr.unixsocket;
+import com.kenai.constantine.platform.SocketLevel;
+import com.kenai.constantine.platform.SocketOption;
+import com.kenai.jaffl.byref.IntByReference;
import com.kenai.jnr.enxio.channels.NativeSocketChannel;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
import java.nio.channels.Channel;
public class UnixSocket {
@@ -29,4 +34,18 @@
public final Channel getChannel() {
return channel;
}
+
+ public final void setKeepAlive(boolean on) {
+ Native.setsockopt(channel.getFD(), SocketLevel.SOL_SOCKET, SocketOption.SO_KEEPALIVE, on);
+ }
+
+ public final boolean getKeepAlive() {
+ ByteBuffer buf = ByteBuffer.allocate(4);
+ buf.order(ByteOrder.BIG_ENDIAN);
+ IntByReference ref = new IntByReference(4);
+
+ Native.libsocket().getsockopt(channel.getFD(), SocketLevel.SOL_SOCKET.value(), SocketOption.SO_KEEPALIVE.value(), buf, ref);
+
+ return buf.getInt(0) != 0;
+ }
}

0 comments on commit ce84520

Please sign in to comment.