Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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

…(ie. socketpair(2))
  • Loading branch information...
commit 876339b62cff6bc444327cd22a052f96886dabba 1 parent 4c8b2c6
Wayne Meissner authored
8 src/com/kenai/jnr/unixsocket/Native.java
@@ -44,6 +44,7 @@
44 44 int connect(int s, @In @Transient SockAddrUnix name, int namelen);
45 45 int getsockname(int fd, @Out SockAddrUnix addr, @In @Out IntByReference len);
46 46 int getpeername(int fd, @Out SockAddrUnix addr, @In @Out IntByReference len);
  47 + int socketpair(int domain, int type, int protocol, @Out int[] sv);
47 48 int fcntl(int fd, int cmd, int data);
48 49 String strerror(int error);
49 50 }
@@ -64,6 +65,13 @@ static int socket(ProtocolFamily domain, Sock type, int protocol) throws IOExcep
64 65 return fd;
65 66 }
66 67
  68 + static int socketpair(ProtocolFamily domain, Sock type, int protocol, int[] sv) throws IOException {
  69 + if (libsocket().socketpair(domain.value(), type.value(), protocol, sv) < 0) {
  70 + throw new IOException("socketpair(2) failed " + Native.getLastErrorString());
  71 + }
  72 + return 0;
  73 + }
  74 +
67 75 static int listen(int fd, int backlog) {
68 76 return libsocket().listen(fd, backlog);
69 77 }
9 src/com/kenai/jnr/unixsocket/UnixSocketChannel.java
@@ -50,6 +50,15 @@ public static final UnixSocketChannel open(UnixSocketAddress remote) throws IOEx
50 50 return channel;
51 51 }
52 52
  53 + public static final UnixSocketChannel[] pair() throws IOException {
  54 + int[] sockets = { -1, -1 };
  55 + Native.socketpair(ProtocolFamily.PF_UNIX, Sock.SOCK_STREAM, 0, sockets);
  56 + return new UnixSocketChannel[] {
  57 + new UnixSocketChannel(sockets[0], SelectionKey.OP_READ | SelectionKey.OP_WRITE),
  58 + new UnixSocketChannel(sockets[1], SelectionKey.OP_READ | SelectionKey.OP_WRITE)
  59 + };
  60 + }
  61 +
53 62 private UnixSocketChannel() throws IOException {
54 63 super(Native.socket(ProtocolFamily.PF_UNIX, Sock.SOCK_STREAM, 0),
55 64 SelectionKey.OP_CONNECT | SelectionKey.OP_READ | SelectionKey.OP_WRITE);

0 comments on commit 876339b

Please sign in to comment.
Something went wrong with that request. Please try again.