1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
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/. */
7 #ifndef mozilla_dom_UDPSocket_h__
8 #define mozilla_dom_UDPSocket_h__
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"
25 class StringOrBlobOrArrayBufferOrArrayBufferView
;
27 class UDPSocket MOZ_FINAL
: public DOMEventTargetHelper
28 , public nsIUDPSocketListener
29 , public nsIUDPSocketInternal
32 NS_DECL_ISUPPORTS_INHERITED
33 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(UDPSocket
, DOMEventTargetHelper
)
34 NS_DECL_NSIUDPSOCKETLISTENER
35 NS_DECL_NSIUDPSOCKETINTERNAL
36 NS_REALLY_FORWARD_NSIDOMEVENTTARGET(DOMEventTargetHelper
)
40 GetParentObject() const
46 WrapObject(JSContext
* aCx
) MOZ_OVERRIDE
;
49 DisconnectFromOwner() MOZ_OVERRIDE
;
51 static already_AddRefed
<UDPSocket
>
52 Constructor(const GlobalObject
& aGlobal
, const UDPOptions
& aOptions
, ErrorResult
& aRv
);
55 GetLocalAddress(nsString
& aRetVal
) const
57 aRetVal
= mLocalAddress
;
67 GetRemoteAddress(nsString
& aRetVal
) const
69 if (mRemoteAddress
.IsVoid()) {
70 SetDOMStringToNull(aRetVal
);
74 aRetVal
= NS_ConvertUTF8toUTF16(mRemoteAddress
);
113 IMPL_EVENT_HANDLER(message
)
115 already_AddRefed
<Promise
>
119 JoinMulticastGroup(const nsAString
& aMulticastGroupAddress
, ErrorResult
& aRv
);
122 LeaveMulticastGroup(const nsAString
& aMulticastGroupAddress
, ErrorResult
& aRv
);
125 Send(const StringOrBlobOrArrayBufferOrArrayBufferView
& aData
,
126 const Optional
<nsAString
>& aRemoteAddress
,
127 const Optional
<Nullable
<uint16_t>>& aRemotePort
,
131 class ListenerProxy
: public nsIUDPSocketListener
132 , public nsIUDPSocketInternal
136 NS_FORWARD_SAFE_NSIUDPSOCKETLISTENER(mSocket
)
137 NS_FORWARD_SAFE_NSIUDPSOCKETINTERNAL(mSocket
)
139 explicit ListenerProxy(UDPSocket
* aSocket
)
150 virtual ~ListenerProxy() {}
155 UDPSocket(nsPIDOMWindow
* aOwner
,
156 const nsCString
& aRemoteAddress
,
157 const Nullable
<uint16_t>& aRemotePort
);
159 virtual ~UDPSocket();
162 Init(const nsString
& aLocalAddress
,
163 const Nullable
<uint16_t>& aLocalPort
,
164 const bool& aAddressReuse
,
165 const bool& aLoopback
);
168 InitLocal(const nsAString
& aLocalAddress
, const uint16_t& aLocalPort
);
171 InitRemote(const nsAString
& aLocalAddress
, const uint16_t& aLocalPort
);
174 HandleReceivedData(const nsACString
& aRemoteAddress
,
175 const uint16_t& aRemotePort
,
176 const uint8_t* aData
,
177 const uint32_t& aDataLength
);
180 DispatchReceivedData(const nsACString
& aRemoteAddress
,
181 const uint16_t& aRemotePort
,
182 const uint8_t* aData
,
183 const uint32_t& aDataLength
);
186 CloseWithReason(nsresult aReason
);
189 DoPendingMcastCommand();
191 nsString mLocalAddress
;
192 Nullable
<uint16_t> mLocalPort
;
193 nsCString mRemoteAddress
;
194 Nullable
<uint16_t> mRemotePort
;
197 SocketReadyState mReadyState
;
198 nsRefPtr
<Promise
> mOpened
;
199 nsRefPtr
<Promise
> mClosed
;
201 nsCOMPtr
<nsIUDPSocket
> mSocket
;
202 nsCOMPtr
<nsIUDPSocketChild
> mSocketChild
;
203 nsRefPtr
<ListenerProxy
> mListenerProxy
;
205 struct MulticastCommand
{
206 enum CommandType
{ Join
, Leave
};
208 MulticastCommand(CommandType aCommand
, const nsAString
& aAddress
)
209 : mCommand(aCommand
), mAddress(aAddress
)
212 CommandType mCommand
;
216 nsTArray
<MulticastCommand
> mPendingMcastCommands
;
220 } // namespace mozilla
222 #endif // mozilla_dom_UDPSocket_h__