Index: TODO
===================================================================
--- TODO	(revision 11)
+++ TODO	(revision 11)
@@ -0,0 +1,1 @@
+- POSIX: make the Waitable Failable
Index: src/xplcommon/posix/Acceptor.h
===================================================================
--- src/xplcommon/posix/Acceptor.h	(revision 6)
+++ src/xplcommon/posix/Acceptor.h	(revision 11)
@@ -52,5 +52,5 @@
 class Acceptor : public ::xplcommon::Failable
 {
-private:
+protected:
     /**
      * The socket we belong to.
@@ -58,5 +58,4 @@
     ServerSocket& socket;
 
-protected:
     /**
      * The file descriptor accepted the last time.
Index: src/xplcommon/posix/LocalAcceptor.h
===================================================================
--- src/xplcommon/posix/LocalAcceptor.h	(revision 11)
+++ src/xplcommon/posix/LocalAcceptor.h	(revision 11)
@@ -0,0 +1,122 @@
+// Copyright (c) 2012 by István Váradi
+
+// This file is part of libxplcommon, a common utility library for
+// development related to X-Plane
+
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+
+// 1. Redistributions of source code must retain the above copyright notice, this
+//    list of conditions and the following disclaimer.
+// 2. Redistributions in binary form must reproduce the above copyright notice,
+//    this list of conditions and the following disclaimer in the documentation
+//    and/or other materials provided with the distribution.
+
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+// The views and conclusions contained in the software and documentation are those
+// of the authors and should not be interpreted as representing official policies,
+// either expressed or implied, of the FreeBSD Project.
+
+#ifndef XPLCOMMON_POSIX_LOCALACCEPTOR_H
+#define XPLCOMMON_POSIX_LOCALACCEPTOR_H
+//------------------------------------------------------------------------------
+
+#include "Acceptor.h"
+
+#include "LocalSocket.h"
+
+//------------------------------------------------------------------------------
+
+namespace xplcommon { namespace posix {
+
+//------------------------------------------------------------------------------
+
+/**
+ * An acceptor that provides a LocalSocket for each accepted
+ * connection.
+ */
+class LocalAcceptor : public Acceptor
+{
+private:
+    /**
+     * Construct the acceptor.
+     *
+     * @param addressLength the length of the buffer for the peer
+     * address. If 0, not buffer will be created.
+     */
+    LocalAcceptor(ServerSocket* socket);
+
+public:
+    /**
+     * Get the local socket accepted last. If no socket was accepted,
+     * return 0. The local socket's waiter will be the given one
+     * (which can be 0).
+     */
+    LocalSocket* getSocket(Waiter* waiter,
+                           size_t readingCapacity = LocalSocket::DEFAULT_CAPACITY,
+                           size_t writingCapacity = LocalSocket::DEFAULT_CAPACITY);
+    /**
+     * Get the local socket accepted last. If no socket was accepted,
+     * return 0. The local socket's waiter will be the same as that of
+     * the associated server socket.
+     */
+    LocalSocket* getSocket(size_t readingCapacity = LocalSocket::DEFAULT_CAPACITY,
+                           size_t writingCapacity = LocalSocket::DEFAULT_CAPACITY);
+
+
+    friend class LocalServerSocket;
+};
+
+//------------------------------------------------------------------------------
+// Inline definitions
+//------------------------------------------------------------------------------
+
+inline LocalAcceptor::LocalAcceptor(ServerSocket* socket) :
+    Acceptor(socket)
+{
+}
+
+//------------------------------------------------------------------------------
+
+inline LocalSocket* LocalAcceptor::getSocket(Waiter* waiter,
+                                             size_t readingCapacity,
+                                             size_t writingCapacity)
+{
+    if (acceptedFD<0) return 0;
+
+    LocalSocket* socket = new LocalSocket(acceptedFD, waiter,
+                                          readingCapacity, writingCapacity);
+    connectionProcessed();
+    return socket;
+}
+
+//------------------------------------------------------------------------------
+
+inline LocalSocket* LocalAcceptor::getSocket(size_t readingCapacity,
+                                             size_t writingCapacity)
+{
+    return getSocket(socket.getWaiter(), readingCapacity, writingCapacity);
+}
+
+//------------------------------------------------------------------------------
+
+} /* namespace xplcommon::posix */ } /* namespace xplcommon */
+
+//------------------------------------------------------------------------------
+#endif // XPLCOMMON_POSIX_LOCALACCEPTOR_H
+
+// Local Variables:
+// mode: C++
+// c-basic-offset: 4
+// indent-tabs-mode: nil
+// End:
Index: src/xplcommon/posix/LocalServerSocket.cc
===================================================================
--- src/xplcommon/posix/LocalServerSocket.cc	(revision 11)
+++ src/xplcommon/posix/LocalServerSocket.cc	(revision 11)
@@ -0,0 +1,74 @@
+// Copyright (c) 2012 by István Váradi
+
+// This file is part of libxplcommon, a common utility library for
+// development related to X-Plane
+
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+
+// 1. Redistributions of source code must retain the above copyright notice, this
+//    list of conditions and the following disclaimer.
+// 2. Redistributions in binary form must reproduce the above copyright notice,
+//    this list of conditions and the following disclaimer in the documentation
+//    and/or other materials provided with the distribution.
+
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+// The views and conclusions contained in the software and documentation are those
+// of the authors and should not be interpreted as representing official policies,
+// either expressed or implied, of the FreeBSD Project.
+
+//------------------------------------------------------------------------------
+
+#include "LocalServerSocket.h"
+
+#include <cerrno>
+
+#include <unistd.h>
+
+#include <sys/socket.h>
+#include <sys/un.h>
+
+//------------------------------------------------------------------------------
+
+using xplcommon::posix::LocalServerSocket;
+
+//------------------------------------------------------------------------------
+
+LocalServerSocket::LocalServerSocket(const char* name, Waiter* waiter) :
+    ServerSocket(AF_UNIX, SOCK_STREAM, waiter),
+    acceptor(this)
+{
+    struct sockaddr_un sun;
+    sun.sun_family = AF_UNIX;
+    setupPath(sun, name);
+
+    unlink(sun.sun_path);
+
+    if (bind(reinterpret_cast<struct sockaddr*>(&sun), sizeof(sun), false)<0) {
+        setErrorCode(errno);
+        return;
+    }
+
+    if (listen()<0) {
+        setErrorCode(errno);
+        return;
+    }
+}
+
+//------------------------------------------------------------------------------
+
+// Local Variables:
+// mode: C++
+// c-basic-offset: 4
+// indent-tabs-mode: nil
+// End:
Index: src/xplcommon/posix/LocalServerSocket.h
===================================================================
--- src/xplcommon/posix/LocalServerSocket.h	(revision 11)
+++ src/xplcommon/posix/LocalServerSocket.h	(revision 11)
@@ -0,0 +1,110 @@
+// Copyright (c) 2012 by István Váradi
+
+// This file is part of libxplcommon, a common utility library for
+// development related to X-Plane
+
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+
+// 1. Redistributions of source code must retain the above copyright notice, this
+//    list of conditions and the following disclaimer.
+// 2. Redistributions in binary form must reproduce the above copyright notice,
+//    this list of conditions and the following disclaimer in the documentation
+//    and/or other materials provided with the distribution.
+
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+// The views and conclusions contained in the software and documentation are those
+// of the authors and should not be interpreted as representing official policies,
+// either expressed or implied, of the FreeBSD Project.
+
+#ifndef XPLCOMMON_POSIX_LOCALSERVERSOCKET_H
+#define XPLCOMMON_POSIX_LOCALSERVERSOCKET_H
+//------------------------------------------------------------------------------
+
+#include "ServerSocket.h"
+#include "../Failable.h"
+
+#include "LocalAcceptor.h"
+
+//------------------------------------------------------------------------------
+
+struct sockaddr_un;
+
+//------------------------------------------------------------------------------
+
+namespace xplcommon { namespace posix {
+
+//------------------------------------------------------------------------------
+
+/**
+ * A server socket that works locally, i.e. a Unix socket.
+ *
+ * It is identified by a name that should be unique for the user. The
+ * actual socket will be created with the name /tmp/<name>-<username>.
+ */
+class LocalServerSocket : public ServerSocket, public ::xplcommon::Failable
+{
+private:
+    /**
+     * Fill in the path of the socket with the given name.
+     */
+    static void setupPath(struct sockaddr_un& addr, const char* name);
+
+    /**
+     * The acceptor to use for accepting connections.
+     */
+    LocalAcceptor acceptor;
+
+public:
+    /**
+     * Construct the socket with the given name.
+     *
+     * It will be bound to the name and able to listen for incoming
+     * connections.
+     *
+     * @param waiter the waiter to use for waiting a possible event
+     * occuring on the socket. If given, the socket will be
+     * non-blocking. Otherwise, it will be blocking, and the accept()
+     * function of the Acceptor might block.
+     */
+    LocalServerSocket(const char* name, Waiter* waiter = 0);
+
+    /**
+     * Get the acceptor.
+     */
+    LocalAcceptor& getAcceptor();
+
+    friend class LocalAcceptor;
+};
+
+//------------------------------------------------------------------------------
+// Inline definitions
+//------------------------------------------------------------------------------
+
+inline LocalAcceptor& LocalServerSocket::getAcceptor()
+{
+    return acceptor;
+}
+
+//------------------------------------------------------------------------------
+
+} /* namespace xplcommon::posix */ } /* namespace xplcommon */
+
+//------------------------------------------------------------------------------
+#endif // XPLCOMMON_POSIX_LOCALSERVERSOCKET_H
+
+// Local Variables:
+// mode: C++
+// c-basic-offset: 4
+// indent-tabs-mode: nil
+// End:
Index: src/xplcommon/posix/LocalSocket.h
===================================================================
--- src/xplcommon/posix/LocalSocket.h	(revision 11)
+++ src/xplcommon/posix/LocalSocket.h	(revision 11)
@@ -0,0 +1,80 @@
+// Copyright (c) 2012 by István Váradi
+
+// This file is part of libxplcommon, a common utility library for
+// development related to X-Plane
+
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+
+// 1. Redistributions of source code must retain the above copyright notice, this
+//    list of conditions and the following disclaimer.
+// 2. Redistributions in binary form must reproduce the above copyright notice,
+//    this list of conditions and the following disclaimer in the documentation
+//    and/or other materials provided with the distribution.
+
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+// The views and conclusions contained in the software and documentation are those
+// of the authors and should not be interpreted as representing official policies,
+// either expressed or implied, of the FreeBSD Project.
+
+#ifndef XPLCOMMON_POSIX_LOCALSOCKET_H
+#define XPLCOMMON_POSIX_LOCALSOCKET_H
+//------------------------------------------------------------------------------
+
+#include "Socket.h"
+
+//------------------------------------------------------------------------------
+
+namespace xplcommon { namespace posix {
+
+//------------------------------------------------------------------------------
+
+/**
+ * A socket that works on a local (Unix) connection.
+ */
+class LocalSocket : public Socket
+{
+private:
+    /**
+     * Construct the socket with the given parameters.
+     */
+    LocalSocket(int fd, Waiter* waiter = 0,
+                size_t readingCapacity = DEFAULT_CAPACITY,
+                size_t writingCapacity = DEFAULT_CAPACITY);
+
+    friend class LocalAcceptor;
+};
+
+//------------------------------------------------------------------------------
+// Inline definitions
+//------------------------------------------------------------------------------
+
+inline LocalSocket::LocalSocket(int fd, Waiter* waiter,
+                                size_t readingCapacity,
+                                size_t writingCapacity) :
+    Socket(fd, waiter, readingCapacity, writingCapacity)
+{
+}
+
+//------------------------------------------------------------------------------
+
+} /* namespace xplcommon::posix */ } /* namespace xplcommon */
+
+//------------------------------------------------------------------------------
+#endif // XPLCOMMON_POSIX_LOCALSOCKET_H
+
+// Local Variables:
+// mode: C++
+// c-basic-offset: 4
+// indent-tabs-mode: nil
+// End:
Index: src/xplcommon/posix/Makefile.am
===================================================================
--- src/xplcommon/posix/Makefile.am	(revision 6)
+++ src/xplcommon/posix/Makefile.am	(revision 11)
@@ -13,5 +13,6 @@
 	Socket.cc		\
 	Connector.cc		\
-	ClientSocket.cc
+	ClientSocket.cc		\
+	LocalServerSocket.cc
 
 include_xplcommon_posixdir=$(includedir)/xplcommon/posix
@@ -29,3 +30,6 @@
 	Socket.h		\
 	Connector.h		\
-	ClientSocket.h
+	ClientSocket.h		\
+	LocalSocket.h		\
+	LocalAcceptor.h		\
+	LocalServerSocket.h
Index: src/xplcommon/posix/Socket.h
===================================================================
--- src/xplcommon/posix/Socket.h	(revision 6)
+++ src/xplcommon/posix/Socket.h	(revision 11)
@@ -67,4 +67,11 @@
            size_t writingCapacity = DEFAULT_CAPACITY);
 
+    /**
+     * Construct the socket with the given file descriptor.
+     */
+    Socket(int fd, Waiter* waiter = 0,
+           size_t readingCapacity = DEFAULT_CAPACITY,
+           size_t writingCapacity = DEFAULT_CAPACITY);
+
 public:
     /**
@@ -89,4 +96,12 @@
 //------------------------------------------------------------------------------
 
+inline Socket::Socket(int fd, Waiter* waiter,
+                      size_t readingCapacity, size_t writingCapacity) :
+    BufferedWaitable(waiter, fd, readingCapacity, writingCapacity)
+{
+}
+
+//------------------------------------------------------------------------------
+
 } /* namespace xplcommon::posix */ } /* namespace xplcommon */
 
Index: src/xplcommon/posix/Waitable.h
===================================================================
--- src/xplcommon/posix/Waitable.h	(revision 6)
+++ src/xplcommon/posix/Waitable.h	(revision 11)
@@ -82,4 +82,9 @@
 
     /**
+     * Get the waiter.
+     */
+    Waiter* getWaiter() const;
+
+    /**
      * Indicate if this waitable is valid, i.e. the file descriptor is
      * non-negative.
@@ -120,4 +125,11 @@
 //------------------------------------------------------------------------------
 
+inline Waiter* Waitable::getWaiter() const
+{
+    return waiter;
+}
+
+//------------------------------------------------------------------------------
+
 inline bool Waitable::isValid() const
 {
