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

denizzzka / dpq2 / #911

01 Sep 2025 11:34AM UTC coverage: 86.823% (-0.03%) from 86.849%
#911

push

coveralls-ruby

web-flow
Merge pull request #220 from denizzzka/socket_dup_deal

Socket duplication code moved to the dedicated module

4 of 5 new or added lines in 2 files covered. (80.0%)

1555 of 1791 relevant lines covered (86.82%)

32.25 hits per line

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

75.0
/src/dpq2/socket_stuff.d
1
///
2
module dpq2.socket_stuff;
3

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

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

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

15
    int ret = dup(socket);
4✔
16

17
    if(ret == -1)
4✔
NEW
18
        throw new ConnectionException("Socket duplication error");
×
19

20
    return cast(socket_t) ret;
4✔
21
}
22

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

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

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

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

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

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

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

52
    return s;
53
}
54

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

62
    alias GROUP = uint;
63

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

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

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

99
    extern(Windows) nothrow @nogc
100
    {
101
        import core.sys.windows.winsock2: WSAGetLastError;
102
        int WSADuplicateSocketW(SOCKET s, DWORD dwProcessId, WSAPROTOCOL_INFOW* lpProtocolInfo);
103
        SOCKET WSASocketW(int af, int type, int protocol, WSAPROTOCOL_INFOW*, GROUP, DWORD dwFlags);
104
    }
105
}
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