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 //-----------------------------------------------------------------------------
21 class nsSocketTransportService
;
23 class nsUDPSocket final
: public nsASocketHandler
, public nsIUDPSocket
{
25 NS_DECL_THREADSAFE_ISUPPORTS
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
; }
40 virtual ~nsUDPSocket();
45 // try attaching our socket (mFD) to the STS's poll list.
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
);
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};
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
{
78 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
79 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(nsUDPMessage
)
82 nsUDPMessage(NetAddr
* aAddr
, nsIOutputStream
* aOutputStream
,
83 FallibleTArray
<uint8_t>&& aData
);
86 virtual ~nsUDPMessage();
89 nsCOMPtr
<nsIOutputStream
> mOutputStream
;
90 FallibleTArray
<uint8_t> mData
;
91 JS::Heap
<JSObject
*> mJsobj
;
94 //-----------------------------------------------------------------------------
96 class nsUDPOutputStream
: public nsIOutputStream
{
98 NS_DECL_THREADSAFE_ISUPPORTS
99 NS_DECL_NSIOUTPUTSTREAM
101 nsUDPOutputStream(nsUDPSocket
* aSocket
, PRFileDesc
* aFD
,
102 PRNetAddr
& aPrClientAddr
);
105 virtual ~nsUDPOutputStream() = default;
107 RefPtr
<nsUDPSocket
> mSocket
;
109 PRNetAddr mPrClientAddr
;
114 } // namespace mozilla
116 #endif // nsUDPSocket_h__