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