Line data Source code
1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim: set ts=8 sts=2 et sw=2 tw=80: */
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/. */
6 :
7 : #ifndef mozilla_dom_UDPSocket_h__
8 : #define mozilla_dom_UDPSocket_h__
9 :
10 : #include "mozilla/Attributes.h"
11 : #include "mozilla/DOMEventTargetHelper.h"
12 : #include "mozilla/ErrorResult.h"
13 : #include "mozilla/dom/Promise.h"
14 : #include "mozilla/dom/SocketCommonBinding.h"
15 : #include "nsIUDPSocket.h"
16 : #include "nsIUDPSocketChild.h"
17 : #include "nsTArray.h"
18 :
19 : struct JSContext;
20 :
21 : //
22 : // set MOZ_LOG=UDPSocket:5
23 : //
24 :
25 : namespace mozilla {
26 : namespace net {
27 : extern LazyLogModule gUDPSocketLog;
28 : #define UDPSOCKET_LOG(args) MOZ_LOG(gUDPSocketLog, LogLevel::Debug, args)
29 : #define UDPSOCKET_LOG_ENABLED() MOZ_LOG_TEST(gUDPSocketLog, LogLevel::Debug)
30 : } // namespace net
31 :
32 : namespace dom {
33 :
34 : struct UDPOptions;
35 : class StringOrBlobOrArrayBufferOrArrayBufferView;
36 :
37 : class UDPSocket final : public DOMEventTargetHelper
38 : , public nsIUDPSocketListener
39 : , public nsIUDPSocketInternal
40 : {
41 : public:
42 : NS_DECL_ISUPPORTS_INHERITED
43 0 : NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(UDPSocket, DOMEventTargetHelper)
44 : NS_DECL_NSIUDPSOCKETLISTENER
45 : NS_DECL_NSIUDPSOCKETINTERNAL
46 0 : NS_REALLY_FORWARD_NSIDOMEVENTTARGET(DOMEventTargetHelper)
47 :
48 : public:
49 : nsPIDOMWindowInner*
50 0 : GetParentObject() const
51 : {
52 0 : return GetOwner();
53 : }
54 :
55 : virtual JSObject*
56 : WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
57 :
58 : virtual void
59 : DisconnectFromOwner() override;
60 :
61 : static already_AddRefed<UDPSocket>
62 : Constructor(const GlobalObject& aGlobal, const UDPOptions& aOptions, ErrorResult& aRv);
63 :
64 : void
65 0 : GetLocalAddress(nsString& aRetVal) const
66 : {
67 0 : aRetVal = mLocalAddress;
68 0 : }
69 :
70 : Nullable<uint16_t>
71 0 : GetLocalPort() const
72 : {
73 0 : return mLocalPort;
74 : }
75 :
76 : void
77 0 : GetRemoteAddress(nsString& aRetVal) const
78 : {
79 0 : if (mRemoteAddress.IsVoid()) {
80 0 : SetDOMStringToNull(aRetVal);
81 0 : return;
82 : }
83 :
84 0 : aRetVal = NS_ConvertUTF8toUTF16(mRemoteAddress);
85 : }
86 :
87 : Nullable<uint16_t>
88 0 : GetRemotePort() const
89 : {
90 0 : return mRemotePort;
91 : }
92 :
93 : bool
94 0 : AddressReuse() const
95 : {
96 0 : return mAddressReuse;
97 : }
98 :
99 : bool
100 0 : Loopback() const
101 : {
102 0 : return mLoopback;
103 : }
104 :
105 : SocketReadyState
106 0 : ReadyState() const
107 : {
108 0 : return mReadyState;
109 : }
110 :
111 : Promise*
112 0 : Opened() const
113 : {
114 0 : return mOpened;
115 : }
116 :
117 : Promise*
118 0 : Closed() const
119 : {
120 0 : return mClosed;
121 : }
122 :
123 0 : IMPL_EVENT_HANDLER(message)
124 :
125 : already_AddRefed<Promise>
126 : Close();
127 :
128 : void
129 : JoinMulticastGroup(const nsAString& aMulticastGroupAddress, ErrorResult& aRv);
130 :
131 : void
132 : LeaveMulticastGroup(const nsAString& aMulticastGroupAddress, ErrorResult& aRv);
133 :
134 : bool
135 : Send(const StringOrBlobOrArrayBufferOrArrayBufferView& aData,
136 : const Optional<nsAString>& aRemoteAddress,
137 : const Optional<Nullable<uint16_t>>& aRemotePort,
138 : ErrorResult& aRv);
139 :
140 : private:
141 : class ListenerProxy : public nsIUDPSocketListener
142 : , public nsIUDPSocketInternal
143 : {
144 : public:
145 : NS_DECL_ISUPPORTS
146 0 : NS_FORWARD_SAFE_NSIUDPSOCKETLISTENER(mSocket)
147 0 : NS_FORWARD_SAFE_NSIUDPSOCKETINTERNAL(mSocket)
148 :
149 0 : explicit ListenerProxy(UDPSocket* aSocket)
150 0 : : mSocket(aSocket)
151 : {
152 0 : }
153 :
154 0 : void Disconnect()
155 : {
156 0 : mSocket = nullptr;
157 0 : }
158 :
159 : private:
160 0 : virtual ~ListenerProxy() {}
161 :
162 : UDPSocket* mSocket;
163 : };
164 :
165 : UDPSocket(nsPIDOMWindowInner* aOwner,
166 : const nsCString& aRemoteAddress,
167 : const Nullable<uint16_t>& aRemotePort);
168 :
169 : virtual ~UDPSocket();
170 :
171 : nsresult
172 : Init(const nsString& aLocalAddress,
173 : const Nullable<uint16_t>& aLocalPort,
174 : const bool& aAddressReuse,
175 : const bool& aLoopback);
176 :
177 : nsresult
178 : InitLocal(const nsAString& aLocalAddress, const uint16_t& aLocalPort);
179 :
180 : nsresult
181 : InitRemote(const nsAString& aLocalAddress, const uint16_t& aLocalPort);
182 :
183 : void
184 : HandleReceivedData(const nsACString& aRemoteAddress,
185 : const uint16_t& aRemotePort,
186 : const uint8_t* aData,
187 : const uint32_t& aDataLength);
188 :
189 : nsresult
190 : DispatchReceivedData(const nsACString& aRemoteAddress,
191 : const uint16_t& aRemotePort,
192 : const uint8_t* aData,
193 : const uint32_t& aDataLength);
194 :
195 : void
196 : CloseWithReason(nsresult aReason);
197 :
198 : nsresult
199 : DoPendingMcastCommand();
200 :
201 : nsString mLocalAddress;
202 : Nullable<uint16_t> mLocalPort;
203 : nsCString mRemoteAddress;
204 : Nullable<uint16_t> mRemotePort;
205 : bool mAddressReuse;
206 : bool mLoopback;
207 : SocketReadyState mReadyState;
208 : RefPtr<Promise> mOpened;
209 : RefPtr<Promise> mClosed;
210 :
211 : nsCOMPtr<nsIUDPSocket> mSocket;
212 : nsCOMPtr<nsIUDPSocketChild> mSocketChild;
213 : RefPtr<ListenerProxy> mListenerProxy;
214 :
215 0 : struct MulticastCommand {
216 : enum CommandType { Join, Leave };
217 :
218 0 : MulticastCommand(CommandType aCommand, const nsAString& aAddress)
219 0 : : mCommand(aCommand), mAddress(aAddress)
220 0 : { }
221 :
222 : CommandType mCommand;
223 : nsString mAddress;
224 : };
225 :
226 : nsTArray<MulticastCommand> mPendingMcastCommands;
227 : };
228 :
229 : } // namespace dom
230 : } // namespace mozilla
231 :
232 : #endif // mozilla_dom_UDPSocket_h__
|