src/fd_util.cpp
std::pair<native_socket_type, native_socket_type> create_pipe() {
    socklen_t addrlen = sizeof(sockaddr_in);
    native_socket_type socks[2] = {invalid_socket, invalid_socket};
    auto listener = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (listener == invalid_socket) throw_io_failure("socket() failed");
    union {
        sockaddr_in inaddr;
        sockaddr addr;
    } a;
    memset(&a, 0, sizeof(a));
    a.inaddr.sin_family = AF_INET;
    a.inaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
    a.inaddr.sin_port = 0;
    bool success = false;
    // makes sure all sockets are closed in case of an error
    auto guard = detail::make_scope_guard([&] {
        if (success) return; // everyhting's fine
        auto e = WSAGetLastError();
        closesocket(listener);
        closesocket(socks[0]);
        closesocket(socks[1]);
        WSASetLastError(e);
    });
    // bind listener to a local port
    int reuse = 1;
    ccall("setsockopt() failed", ::setsockopt, listener,
          SOL_SOCKET, SO_REUSEADDR, reinterpret_cast<char*>(&reuse),
          static_cast<socklen_t>(sizeof(reuse)));
    ccall("bind() failed", ::bind, listener, &a.addr, sizeof(a.inaddr));
    // read the port in use: win32 getsockname may only set the port number
    // (http://msdn.microsoft.com/library/ms738543.aspx):
    memset(&a, 0, sizeof(a));
    ccall("getsockname() failed", ::getsockname, listener, &a.addr, &addrlen);
    a.inaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
    a.inaddr.sin_family = AF_INET;
    // set listener to listen mode
    ccall("listen() failed", ::listen, listener, 1);
    // create read-only end of the pipe
    DWORD flags = 0;
    auto read_fd = WSASocket(AF_INET, SOCK_STREAM, 0, NULL, 0, flags);
    if (read_fd == invalid_socket) {
        throw_io_failure("cannot create read handle: WSASocket() failed");
    }
    ccall("connect() failed", ::connect, read_fd, &a.addr, sizeof(a.inaddr));
    // get write-only end of the pipe
    auto write_fd = accept(listener, NULL, NULL);
    if (write_fd == invalid_socket) {
        throw_io_failure("cannot create write handle: accept() failed");
    }
    closesocket(listener);
    success = true;
    return {read_fd, write_fd};
}
/******************************************************************************\
 * Based on work of others;                                                   *
 * original header:                                                           *
 *                                                                            *
 * Copyright 2007, 2010 by Nathan C. Myers <ncm@cantrip.org>                  *
 * Redistribution and use in source and binary forms, with or without         *
 * modification, are permitted provided that the following conditions are met:*
 *                                                                            *
 * Redistributions of source code must retain the above copyright notice,     *
 * this list of conditions and the following disclaimer.                      *
 *                                                                            *
 * 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.                     *
 *                                                                            *
 * The name of the author must not be used to endorse or promote products     *
 * derived from this software without specific prior written permission.      *
 *                                                                            *
 * 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 HOLDER 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.                                 *
\******************************************************************************/
\
Based on work of others;
original header.



Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.

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.

The name of the author must not be used to endorse or promote products
derived from this software without specific prior written permission.


\ Based on work of others ; original header . Redistributions of source code must retain the above copyright notice this list of conditions and the following disclaimer . 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 . The name of the author must not be used to endorse or promote products derived from this software without specific prior written permission .
std::pair<native_socket_type, native_socket_type> create_pipe() {
    socklen_t addrlen = sizeof(sockaddr_in);
    native_socket_type socks[2] = {invalid_socket, invalid_socket};
    auto listener = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (listener == invalid_socket) throw_io_failure("socket() failed");
    union {
        sockaddr_in inaddr;
        sockaddr addr;
    } a;
    memset(&a, 0, sizeof(a));
    a.inaddr.sin_family = AF_INET;
    a.inaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
    a.inaddr.sin_port = 0;
    bool success = false;
    auto guard = detail::make_scope_guard([&] {
        if (success) return; 
        auto e = WSAGetLastError();
        closesocket(listener);
        closesocket(socks[0]);
        closesocket(socks[1]);
        WSASetLastError(e);
    });
    int reuse = 1;
    ccall("setsockopt() failed", ::setsockopt, listener,
          SOL_SOCKET, SO_REUSEADDR, reinterpret_cast<char*>(&reuse),
          static_cast<socklen_t>(sizeof(reuse)));
    ccall("bind() failed", ::bind, listener, &a.addr, sizeof(a.inaddr));
    memset(&a, 0, sizeof(a));
    ccall("getsockname() failed", ::getsockname, listener, &a.addr, &addrlen);
    a.inaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
    a.inaddr.sin_family = AF_INET;
    ccall("listen() failed", ::listen, listener, 1);
    DWORD flags = 0;
    auto read_fd = WSASocket(AF_INET, SOCK_STREAM, 0, NULL, 0, flags);
    if (read_fd == invalid_socket) {
        throw_io_failure("cannot create read handle: WSASocket() failed");
    }
    ccall("connect() failed", ::connect, read_fd, &a.addr, sizeof(a.inaddr));
    auto write_fd = accept(listener, NULL, NULL);
    if (write_fd == invalid_socket) {
        throw_io_failure("cannot create write handle: accept() failed");
    }
    closesocket(listener);
    success = true;
    return {read_fd, write_fd};
}
std :: pair < native_socket_type , native_socket_type > create_pipe ( ) { socklen_t addrlen = sizeof ( sockaddr_in ) ; native_socket_type socks [ 2 ] = { invalid_socket , invalid_socket } ; auto listener = socket ( AF_INET , SOCK_STREAM , IPPROTO_TCP ) ; if ( listener == invalid_socket ) throw_io_failure ( " " ) ; union { sockaddr_in inaddr ; sockaddr addr ; } a ; memset ( & a , 0 , sizeof ( a ) ) ; a . inaddr . sin_family = AF_INET ; a . inaddr . sin_addr . s_addr = htonl ( INADDR_LOOPBACK ) ; a . inaddr . sin_port = 0 ; bool success = false ; auto guard = detail :: make_scope_guard ( [ & ] { if ( success ) return ; auto e = WSAGetLastError ( ) ; closesocket ( listener ) ; closesocket ( socks [ 0 ] ) ; closesocket ( socks [ 1 ] ) ; WSASetLastError ( e ) ; } ) ; int reuse = 1 ; ccall ( " " , :: setsockopt , listener , SOL_SOCKET , SO_REUSEADDR , reinterpret_cast < char * > ( & reuse ) , static_cast < socklen_t > ( sizeof ( reuse ) ) ) ; ccall ( " " , :: bind , listener , & a . addr , sizeof ( a . inaddr ) ) ; memset ( & a , 0 , sizeof ( a ) ) ; ccall ( " " , :: getsockname , listener , & a . addr , & addrlen ) ; a . inaddr . sin_addr . s_addr = htonl ( INADDR_LOOPBACK ) ; a . inaddr . sin_family = AF_INET ; ccall ( " " , :: listen , listener , 1 ) ; DWORD flags = 0 ; auto read_fd = WSASocket ( AF_INET , SOCK_STREAM , 0 , NULL , 0 , flags ) ; if ( read_fd == invalid_socket ) { throw_io_failure ( " " ) ; } ccall ( " " , :: connect , read_fd , & a . addr , sizeof ( a . inaddr ) ) ; auto write_fd = accept ( listener , NULL , NULL ) ; if ( write_fd == invalid_socket ) { throw_io_failure ( " " ) ; } closesocket ( listener ) ; success = true ; return { read_fd , write_fd } ; }
\
 Based on work of others;
 original header:
\ Based on work of others ; original header :
// makes sure all sockets are closed in case of an error // everyhting's fine // bind listener to a local port // read the port in use: win32 getsockname may only set the port number // (http://msdn.microsoft.com/library/ms738543.aspx): // set listener to listen mode // create read-only end of the pipe // get write-only end of the pipe