Permalink
Browse files

Add UnixSocketChannel.pair() to create a linked pair of unix sockets …

…(ie. socketpair(2))
  • Loading branch information...
1 parent 4c8b2c6 commit 876339b62cff6bc444327cd22a052f96886dabba Wayne Meissner committed Nov 12, 2009
Showing with 17 additions and 0 deletions.
  1. +8 −0 src/com/kenai/jnr/unixsocket/Native.java
  2. +9 −0 src/com/kenai/jnr/unixsocket/UnixSocketChannel.java
@@ -44,6 +44,7 @@
int connect(int s, @In @Transient SockAddrUnix name, int namelen);
int getsockname(int fd, @Out SockAddrUnix addr, @In @Out IntByReference len);
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);
String strerror(int error);
}
@@ -64,6 +65,13 @@ static int socket(ProtocolFamily domain, Sock type, int protocol) throws IOExcep
return fd;
}
+ static int socketpair(ProtocolFamily domain, Sock type, int protocol, int[] sv) throws IOException {
+ if (libsocket().socketpair(domain.value(), type.value(), protocol, sv) < 0) {
+ throw new IOException("socketpair(2) failed " + Native.getLastErrorString());
+ }
+ return 0;
+ }
+
static int listen(int fd, int backlog) {
return libsocket().listen(fd, backlog);
}
@@ -50,6 +50,15 @@ public static final UnixSocketChannel open(UnixSocketAddress remote) throws IOEx
return channel;
}
+ public static final UnixSocketChannel[] pair() throws IOException {
+ int[] sockets = { -1, -1 };
+ Native.socketpair(ProtocolFamily.PF_UNIX, Sock.SOCK_STREAM, 0, sockets);
+ return new UnixSocketChannel[] {
+ new UnixSocketChannel(sockets[0], SelectionKey.OP_READ | SelectionKey.OP_WRITE),
+ new UnixSocketChannel(sockets[1], SelectionKey.OP_READ | SelectionKey.OP_WRITE)
+ };
+ }
+
private UnixSocketChannel() throws IOException {
super(Native.socket(ProtocolFamily.PF_UNIX, Sock.SOCK_STREAM, 0),
SelectionKey.OP_CONNECT | SelectionKey.OP_READ | SelectionKey.OP_WRITE);

0 comments on commit 876339b

Please sign in to comment.