1 /* vim:set ts=2 sw=2 et cindent: */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 
6 #ifndef nsServerSocket_h__
7 #define nsServerSocket_h__
8 
9 #include "prio.h"
10 #include "nsASocketHandler.h"
11 #include "nsIServerSocket.h"
12 #include "mozilla/Mutex.h"
13 
14 //-----------------------------------------------------------------------------
15 
16 class nsIEventTarget;
17 namespace mozilla {
18 namespace net {
19 union NetAddr;
20 
21 class nsServerSocket : public nsASocketHandler, public nsIServerSocket {
22  public:
23   NS_DECL_THREADSAFE_ISUPPORTS
24   NS_DECL_NSISERVERSOCKET
25 
26   // nsASocketHandler methods:
27   virtual void OnSocketReady(PRFileDesc* fd, int16_t outFlags) override;
28   virtual void OnSocketDetached(PRFileDesc* fd) override;
29   virtual void IsLocal(bool* aIsLocal) override;
30   virtual void KeepWhenOffline(bool* aKeepWhenOffline) override;
31 
ByteCountSent()32   virtual uint64_t ByteCountSent() override { return 0; }
ByteCountReceived()33   virtual uint64_t ByteCountReceived() override { return 0; }
34   nsServerSocket();
35 
36   virtual void CreateClientTransport(PRFileDesc* clientFD,
37                                      const mozilla::net::NetAddr& clientAddr);
SetSocketDefaults()38   virtual nsresult SetSocketDefaults() { return NS_OK; }
OnSocketListen()39   virtual nsresult OnSocketListen() { return NS_OK; }
40 
41  protected:
42   virtual ~nsServerSocket();
43   PRFileDesc* mFD{nullptr};
44   nsCOMPtr<nsIServerSocketListener> mListener;
45 
46  private:
47   void OnMsgClose();
48   void OnMsgAttach();
49 
50   // try attaching our socket (mFD) to the STS's poll list.
51   nsresult TryAttach();
52 
53   // lock protects access to mListener; so it is not cleared while being used.
54   mozilla::Mutex mLock{"nsServerSocket.mLock"};
55   PRNetAddr mAddr = {.raw = {0, {0}}};
56   nsCOMPtr<nsIEventTarget> mListenerTarget;
57   bool mAttached{false};
58   bool mKeepWhenOffline{false};
59 };
60 
61 }  // namespace net
62 }  // namespace mozilla
63 
64 //-----------------------------------------------------------------------------
65 
66 #endif  // nsServerSocket_h__
67