Backed out changeset 25e197bc4c13 (bug 1918706) for causing SM bustages on PlainDateT...
[gecko.git] / netwerk / base / nsUDPSocket.h
blobd58d1750a76a2850f41d490e147b2ec55106815e
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/. */
6 #ifndef nsUDPSocket_h__
7 #define nsUDPSocket_h__
9 #include "nsIUDPSocket.h"
10 #include "mozilla/Mutex.h"
11 #include "mozilla/net/DNS.h"
12 #include "nsIOutputStream.h"
13 #include "nsASocketHandler.h"
14 #include "nsCycleCollectionParticipant.h"
16 //-----------------------------------------------------------------------------
18 namespace mozilla {
19 namespace net {
21 class nsSocketTransportService;
23 class nsUDPSocket final : public nsASocketHandler, public nsIUDPSocket {
24 public:
25 NS_DECL_THREADSAFE_ISUPPORTS
26 NS_DECL_NSIUDPSOCKET
28 // nsASocketHandler methods:
29 virtual void OnSocketReady(PRFileDesc* fd, int16_t outFlags) override;
30 virtual void OnSocketDetached(PRFileDesc* fd) override;
31 virtual void IsLocal(bool* aIsLocal) override;
32 virtual nsresult GetRemoteAddr(NetAddr* addr) override;
34 uint64_t ByteCountSent() override { return mByteWriteCount; }
35 uint64_t ByteCountReceived() override { return mByteReadCount; }
37 nsUDPSocket();
39 private:
40 virtual ~nsUDPSocket();
42 void OnMsgClose();
43 void OnMsgAttach();
45 // try attaching our socket (mFD) to the STS's poll list.
46 nsresult TryAttach();
48 friend class SetSocketOptionRunnable;
49 nsresult SetSocketOption(const PRSocketOptionData& aOpt);
50 nsresult JoinMulticastInternal(const PRNetAddr& aAddr,
51 const PRNetAddr& aIface);
52 nsresult LeaveMulticastInternal(const PRNetAddr& aAddr,
53 const PRNetAddr& aIface);
54 nsresult SetMulticastInterfaceInternal(const PRNetAddr& aIface);
56 void CloseSocket();
58 // lock protects access to mListener;
59 // so mListener is not cleared while being used/locked.
60 Mutex mLock MOZ_UNANNOTATED{"nsUDPSocket.mLock"};
61 PRFileDesc* mFD{nullptr};
62 NetAddr mAddr;
63 OriginAttributes mOriginAttributes;
64 nsCOMPtr<nsIUDPSocketListener> mListener;
65 nsCOMPtr<nsIUDPSocketSyncListener> mSyncListener;
66 nsCOMPtr<nsIEventTarget> mListenerTarget;
67 bool mAttached{false};
68 RefPtr<nsSocketTransportService> mSts;
70 uint64_t mByteReadCount{0};
71 uint64_t mByteWriteCount{0};
74 //-----------------------------------------------------------------------------
76 class nsUDPMessage : public nsIUDPMessage {
77 public:
78 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
79 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(nsUDPMessage)
80 NS_DECL_NSIUDPMESSAGE
82 nsUDPMessage(NetAddr* aAddr, nsIOutputStream* aOutputStream,
83 FallibleTArray<uint8_t>&& aData);
85 private:
86 virtual ~nsUDPMessage();
88 NetAddr mAddr;
89 nsCOMPtr<nsIOutputStream> mOutputStream;
90 FallibleTArray<uint8_t> mData;
91 JS::Heap<JSObject*> mJsobj;
94 //-----------------------------------------------------------------------------
96 class nsUDPOutputStream : public nsIOutputStream {
97 public:
98 NS_DECL_THREADSAFE_ISUPPORTS
99 NS_DECL_NSIOUTPUTSTREAM
101 nsUDPOutputStream(nsUDPSocket* aSocket, PRFileDesc* aFD,
102 PRNetAddr& aPrClientAddr);
104 private:
105 virtual ~nsUDPOutputStream() = default;
107 RefPtr<nsUDPSocket> mSocket;
108 PRFileDesc* mFD;
109 PRNetAddr mPrClientAddr;
110 bool mIsClosed;
113 } // namespace net
114 } // namespace mozilla
116 #endif // nsUDPSocket_h__