UDPSocket.h 5.1 KB

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