Bug 1845134 - Part 4: Update existing ui-icons to use the latest source from acorn...
[gecko.git] / netwerk / base / nsUDPSocket.h
blobbd616d52069ad23abce40f0446fc92d531c1ac65
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 void AddOutputBytes(int32_t aBytes);
39 nsUDPSocket();
41 private:
42 virtual ~nsUDPSocket();
44 void OnMsgClose();
45 void OnMsgAttach();
47 // try attaching our socket (mFD) to the STS's poll list.
48 nsresult TryAttach();
50 friend class SetSocketOptionRunnable;
51 nsresult SetSocketOption(const PRSocketOptionData& aOpt);
52 nsresult JoinMulticastInternal(const PRNetAddr& aAddr,
53 const PRNetAddr& aIface);
54 nsresult LeaveMulticastInternal(const PRNetAddr& aAddr,
55 const PRNetAddr& aIface);
56 nsresult SetMulticastInterfaceInternal(const PRNetAddr& aIface);
58 void CloseSocket();
60 // lock protects access to mListener;
61 // so mListener is not cleared while being used/locked.
62 Mutex mLock MOZ_UNANNOTATED{"nsUDPSocket.mLock"};
63 PRFileDesc* mFD{nullptr};
64 NetAddr mAddr;
65 OriginAttributes mOriginAttributes;
66 nsCOMPtr<nsIUDPSocketListener> mListener;
67 nsCOMPtr<nsIUDPSocketSyncListener> mSyncListener;
68 nsCOMPtr<nsIEventTarget> mListenerTarget;
69 bool mAttached{false};
70 RefPtr<nsSocketTransportService> mSts;
72 uint64_t mByteReadCount{0};
73 uint64_t mByteWriteCount{0};
76 //-----------------------------------------------------------------------------
78 class nsUDPMessage : public nsIUDPMessage {
79 public:
80 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
81 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(nsUDPMessage)
82 NS_DECL_NSIUDPMESSAGE
84 nsUDPMessage(NetAddr* aAddr, nsIOutputStream* aOutputStream,
85 FallibleTArray<uint8_t>&& aData);
87 private:
88 virtual ~nsUDPMessage();
90 NetAddr mAddr;
91 nsCOMPtr<nsIOutputStream> mOutputStream;
92 FallibleTArray<uint8_t> mData;
93 JS::Heap<JSObject*> mJsobj;
96 //-----------------------------------------------------------------------------
98 class nsUDPOutputStream : public nsIOutputStream {
99 public:
100 NS_DECL_THREADSAFE_ISUPPORTS
101 NS_DECL_NSIOUTPUTSTREAM
103 nsUDPOutputStream(nsUDPSocket* aSocket, PRFileDesc* aFD,
104 PRNetAddr& aPrClientAddr);
106 private:
107 virtual ~nsUDPOutputStream() = default;
109 RefPtr<nsUDPSocket> mSocket;
110 PRFileDesc* mFD;
111 PRNetAddr mPrClientAddr;
112 bool mIsClosed;
115 } // namespace net
116 } // namespace mozilla
118 #endif // nsUDPSocket_h__