@@ -2706,6 +2706,58 @@ pub fn socket(domain: u32, socket_type: u32, protocol: u32) SocketError!socket_t
2706
2706
}
2707
2707
}
2708
2708
2709
+ pub const ShutdownError = error {
2710
+ ConnectionAborted ,
2711
+
2712
+ /// Connection was reset by peer, application should close socket as it is no longer usable.
2713
+ ConnectionResetByPeer ,
2714
+
2715
+ BlockingOperationInProgress ,
2716
+
2717
+ /// The network subsystem has failed.
2718
+ NetworkSubsystemFailed ,
2719
+
2720
+ /// The socket is not connected (connection-oriented sockets only).
2721
+ SocketNotConnected ,
2722
+
2723
+ SystemResources
2724
+ } || UnexpectedError ;
2725
+
2726
+ pub const ShutdownHow = enum {
2727
+ recv = if (builtin .os .tag == .windows ) windows .SD_RECEIVE else SHUT_RD ,
2728
+ send = if (builtin .os .tag == .windows ) windows .SD_SEND else SHUT_WR ,
2729
+ both = if (builtin .os .tag == .windows ) windows .SD_BOTH else SHUT_RDWR ,
2730
+ };
2731
+
2732
+ /// Shutdown socket send/receive operations
2733
+ pub fn shutdown (sock : socket_t , how : ShutdownHow ) ShutdownError ! void {
2734
+ if (builtin .os .tag == .windows ) {
2735
+ const result = windows .ws2_32 .shutdown (sock , @enumToInt (how ));
2736
+ if (0 != result ) switch (windows .ws2_32 .WSAGetLastError ()) {
2737
+ .WSAECONNABORTED = > return error .ConnectionAborted ,
2738
+ .WSAECONNRESET = > return error .ConnectionResetByPeer ,
2739
+ .WSAEINPROGRESS = > return error .BlockingOperationInProgress ,
2740
+ .WSAEINVAL = > unreachable ,
2741
+ .WSAENETDOWN = > return error .NetworkSubsystemFailed ,
2742
+ .WSAENOTCONN = > return error .SocketNotConnected ,
2743
+ .WSAENOTSOCK = > unreachable ,
2744
+ .WSANOTINITIALISED = > unreachable ,
2745
+ else = > | err | return windows .unexpectedWSAError (err ),
2746
+ };
2747
+ } else {
2748
+ const rc = system .shutdown (sock , @enumToInt (how ));
2749
+ switch (errno (rc )) {
2750
+ 0 = > return ,
2751
+ EBADF = > unreachable ,
2752
+ EINVAL = > unreachable ,
2753
+ ENOTCONN = > return error .SocketNotConnected ,
2754
+ ENOTSOCK = > unreachable ,
2755
+ ENOBUFS = > return error .SystemResources ,
2756
+ else = > | err | return unexpectedErrno (err ),
2757
+ }
2758
+ }
2759
+ }
2760
+
2709
2761
pub fn closeSocket (sock : socket_t ) void {
2710
2762
if (builtin .os .tag == .windows ) {
2711
2763
windows .closesocket (sock ) catch unreachable ;
0 commit comments