1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "mozilla/Assertions.h"
8 #include "ShutdownLayer.h"
10 #include "private/pprio.h"
14 static PRDescIdentity sWinSockShutdownLayerIdentity
;
15 static PRIOMethods sWinSockShutdownLayerMethods
;
16 static PRIOMethods
*sWinSockShutdownLayerMethodsPtr
= nullptr;
21 extern PRDescIdentity nsNamedPipeLayerIdentity
;
24 } // namespace mozilla
26 PRStatus
WinSockClose(PRFileDesc
*aFd
) {
27 MOZ_RELEASE_ASSERT(aFd
->identity
== sWinSockShutdownLayerIdentity
,
28 "Windows shutdown layer not on the top of the stack");
30 PROsfd osfd
= PR_FileDesc2NativeHandle(aFd
);
32 shutdown(osfd
, SD_BOTH
);
35 aFd
->identity
= PR_INVALID_IO_LAYER
;
38 return aFd
->lower
->methods
->close(aFd
->lower
);
44 nsresult
mozilla::net::AttachShutdownLayer(PRFileDesc
*aFd
) {
45 if (!sWinSockShutdownLayerMethodsPtr
) {
46 sWinSockShutdownLayerIdentity
=
47 PR_GetUniqueIdentity("windows shutdown call layer");
48 sWinSockShutdownLayerMethods
= *PR_GetDefaultIOMethods();
49 sWinSockShutdownLayerMethods
.close
= WinSockClose
;
50 sWinSockShutdownLayerMethodsPtr
= &sWinSockShutdownLayerMethods
;
53 if (mozilla::net::nsNamedPipeLayerIdentity
&&
54 PR_GetIdentitiesLayer(aFd
, mozilla::net::nsNamedPipeLayerIdentity
)) {
55 // Do not attach shutdown layer on named pipe layer,
56 // it is for PR_NSPR_IO_LAYER only.
63 layer
= PR_CreateIOLayerStub(sWinSockShutdownLayerIdentity
,
64 sWinSockShutdownLayerMethodsPtr
);
69 status
= PR_PushIOLayer(aFd
, PR_NSPR_IO_LAYER
, layer
);
71 if (status
== PR_FAILURE
) {
72 PR_Free(layer
); // PR_CreateIOLayerStub() uses PR_Malloc().
73 return NS_ERROR_FAILURE
;