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/. */ 5 6 #ifndef nsUDPSocket_h__ 7 #define nsUDPSocket_h__ 8 9 #include "nsIUDPSocket.h" 10 #include "mozilla/Mutex.h" 11 #include "mozilla/net/DNS.h" 12 #include "nsIOutputStream.h" 13 #include "nsCycleCollectionParticipant.h" 14 15 //----------------------------------------------------------------------------- 16 17 namespace mozilla { 18 namespace net { 19 20 class nsUDPSocket final : public nsASocketHandler, public nsIUDPSocket { 21 public: 22 NS_DECL_THREADSAFE_ISUPPORTS 23 NS_DECL_NSIUDPSOCKET 24 25 // nsASocketHandler methods: 26 virtual void OnSocketReady(PRFileDesc* fd, int16_t outFlags) override; 27 virtual void OnSocketDetached(PRFileDesc* fd) override; 28 virtual void IsLocal(bool* aIsLocal) override; 29 ByteCountSent()30 uint64_t ByteCountSent() override { return mByteWriteCount; } ByteCountReceived()31 uint64_t ByteCountReceived() override { return mByteReadCount; } 32 33 void AddOutputBytes(uint64_t aBytes); 34 35 nsUDPSocket(); 36 37 private: 38 virtual ~nsUDPSocket(); 39 40 void OnMsgClose(); 41 void OnMsgAttach(); 42 43 // try attaching our socket (mFD) to the STS's poll list. 44 nsresult TryAttach(); 45 46 friend class SetSocketOptionRunnable; 47 nsresult SetSocketOption(const PRSocketOptionData& aOpt); 48 nsresult JoinMulticastInternal(const PRNetAddr& aAddr, 49 const PRNetAddr& aIface); 50 nsresult LeaveMulticastInternal(const PRNetAddr& aAddr, 51 const PRNetAddr& aIface); 52 nsresult SetMulticastInterfaceInternal(const PRNetAddr& aIface); 53 54 void CloseSocket(); 55 56 // lock protects access to mListener; 57 // so mListener is not cleared while being used/locked. 58 Mutex mLock; 59 PRFileDesc* mFD; 60 NetAddr mAddr; 61 OriginAttributes mOriginAttributes; 62 nsCOMPtr<nsIUDPSocketListener> mListener; 63 nsCOMPtr<nsIEventTarget> mListenerTarget; 64 bool mAttached; 65 RefPtr<nsSocketTransportService> mSts; 66 67 uint64_t mByteReadCount; 68 uint64_t mByteWriteCount; 69 }; 70 71 //----------------------------------------------------------------------------- 72 73 class nsUDPMessage : public nsIUDPMessage { 74 public: 75 NS_DECL_CYCLE_COLLECTING_ISUPPORTS 76 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(nsUDPMessage) 77 NS_DECL_NSIUDPMESSAGE 78 79 nsUDPMessage(NetAddr* aAddr, nsIOutputStream* aOutputStream, 80 FallibleTArray<uint8_t>& aData); 81 82 private: 83 virtual ~nsUDPMessage(); 84 85 NetAddr mAddr; 86 nsCOMPtr<nsIOutputStream> mOutputStream; 87 FallibleTArray<uint8_t> mData; 88 JS::Heap<JSObject*> mJsobj; 89 }; 90 91 //----------------------------------------------------------------------------- 92 93 class nsUDPOutputStream : public nsIOutputStream { 94 public: 95 NS_DECL_THREADSAFE_ISUPPORTS 96 NS_DECL_NSIOUTPUTSTREAM 97 98 nsUDPOutputStream(nsUDPSocket* aSocket, PRFileDesc* aFD, 99 PRNetAddr& aPrClientAddr); 100 101 private: 102 virtual ~nsUDPOutputStream() = default; 103 104 RefPtr<nsUDPSocket> mSocket; 105 PRFileDesc* mFD; 106 PRNetAddr mPrClientAddr; 107 bool mIsClosed; 108 }; 109 110 } // namespace net 111 } // namespace mozilla 112 113 #endif // nsUDPSocket_h__ 114