• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

denizzzka / dpq2 / #921

18 Jun 2026 02:10PM UTC coverage: 85.754% (-0.2%) from 85.959%
#921

push

coveralls-ruby

denizzzka
Merge remote-tracking branch 'origin/master'

1553 of 1811 relevant lines covered (85.75%)

31.89 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/dpq2/socket_stuff.d
1
///
2
deprecated("Posix/Windows socket duplication code will be removed in 2027. Please use https://github.com/denizzzka/misc instead")
3
module dpq2.socket_stuff;
4

5
import dpq2.connection: ConnectionException;
6
import std.socket;
7

8
/// Obtains duplicate file descriptor number of the socket
9
version(Posix)
10
socket_t duplicateSocket(int socket)
11
{
12
    import core.sys.posix.unistd: dup;
13

14
    static assert(socket_t.sizeof == int.sizeof);
15

16
    int ret = dup(socket);
×
17

18
    if(ret == -1)
×
19
        throw new ConnectionException("Socket duplication error");
×
20

21
    return cast(socket_t) ret;
×
22
}
23

24
/// Obtains duplicate file descriptor number of the socket
25
version(Windows)
26
SOCKET duplicateSocket(int socket)
27
{
28
    import core.stdc.stdlib: malloc, free;
29
    import core.sys.windows.winbase: GetCurrentProcessId;
30

31
    static assert(SOCKET.sizeof == socket_t.sizeof);
32

33
    auto protocolInfo = cast(WSAPROTOCOL_INFOW*) malloc(WSAPROTOCOL_INFOW.sizeof);
34
    scope(failure) free(protocolInfo);
35

36
    int dupStatus = WSADuplicateSocketW(socket, GetCurrentProcessId, protocolInfo);
37

38
    if(dupStatus)
39
        throw new ConnectionException("WSADuplicateSocketW error, code "~WSAGetLastError().to!string);
40

41
    SOCKET s = WSASocketW(
42
            FROM_PROTOCOL_INFO,
43
            FROM_PROTOCOL_INFO,
44
            FROM_PROTOCOL_INFO,
45
            protocolInfo,
46
            0,
47
            0
48
        );
49

50
    if(s == INVALID_SOCKET)
51
        throw new ConnectionException("WSASocket error, code "~WSAGetLastError().to!string);
52

53
    return s;
54
}
55

56
// Socket duplication structs for Win32
57
version(Windows)
58
private
59
{
60
    import core.sys.windows.windef;
61
    import core.sys.windows.basetyps: GUID;
62

63
    alias GROUP = uint;
64

65
    enum INVALID_SOCKET = 0;
66
    enum FROM_PROTOCOL_INFO =-1;
67
    enum MAX_PROTOCOL_CHAIN = 7;
68
    enum WSAPROTOCOL_LEN = 255;
69

70
    struct WSAPROTOCOLCHAIN
71
    {
72
        int ChainLen;
73
        DWORD[MAX_PROTOCOL_CHAIN] ChainEntries;
74
    }
75

76
    struct WSAPROTOCOL_INFOW
77
    {
78
        DWORD dwServiceFlags1;
79
        DWORD dwServiceFlags2;
80
        DWORD dwServiceFlags3;
81
        DWORD dwServiceFlags4;
82
        DWORD dwProviderFlags;
83
        GUID ProviderId;
84
        DWORD dwCatalogEntryId;
85
        WSAPROTOCOLCHAIN ProtocolChain;
86
        int iVersion;
87
        int iAddressFamily;
88
        int iMaxSockAddr;
89
        int iMinSockAddr;
90
        int iSocketType;
91
        int iProtocol;
92
        int iProtocolMaxOffset;
93
        int iNetworkByteOrder;
94
        int iSecurityScheme;
95
        DWORD dwMessageSize;
96
        DWORD dwProviderReserved;
97
        WCHAR[WSAPROTOCOL_LEN+1] szProtocol;
98
    }
99

100
    extern(Windows) nothrow @nogc
101
    {
102
        import core.sys.windows.winsock2: WSAGetLastError;
103
        int WSADuplicateSocketW(SOCKET s, DWORD dwProcessId, WSAPROTOCOL_INFOW* lpProtocolInfo);
104
        SOCKET WSASocketW(int af, int type, int protocol, WSAPROTOCOL_INFOW*, GROUP, DWORD dwFlags);
105
    }
106
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc