Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* vim:set ts=2 sw=2 et cindent: */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #ifndef nsUDPSocket_h__ |
michael@0 | 7 | #define nsUDPSocket_h__ |
michael@0 | 8 | |
michael@0 | 9 | #include "nsIUDPSocket.h" |
michael@0 | 10 | #include "mozilla/Mutex.h" |
michael@0 | 11 | #include "nsIOutputStream.h" |
michael@0 | 12 | #include "nsAutoPtr.h" |
michael@0 | 13 | #include "nsCycleCollectionParticipant.h" |
michael@0 | 14 | |
michael@0 | 15 | //----------------------------------------------------------------------------- |
michael@0 | 16 | |
michael@0 | 17 | class nsUDPSocket : public nsASocketHandler |
michael@0 | 18 | , public nsIUDPSocket |
michael@0 | 19 | { |
michael@0 | 20 | public: |
michael@0 | 21 | NS_DECL_THREADSAFE_ISUPPORTS |
michael@0 | 22 | NS_DECL_NSIUDPSOCKET |
michael@0 | 23 | |
michael@0 | 24 | // nsASocketHandler methods: |
michael@0 | 25 | virtual void OnSocketReady(PRFileDesc* fd, int16_t outFlags); |
michael@0 | 26 | virtual void OnSocketDetached(PRFileDesc* fd); |
michael@0 | 27 | virtual void IsLocal(bool* aIsLocal); |
michael@0 | 28 | |
michael@0 | 29 | uint64_t ByteCountSent() { return mByteWriteCount; } |
michael@0 | 30 | uint64_t ByteCountReceived() { return mByteReadCount; } |
michael@0 | 31 | |
michael@0 | 32 | void AddOutputBytes(uint64_t aBytes); |
michael@0 | 33 | |
michael@0 | 34 | nsUDPSocket(); |
michael@0 | 35 | |
michael@0 | 36 | // This must be public to support older compilers (xlC_r on AIX) |
michael@0 | 37 | virtual ~nsUDPSocket(); |
michael@0 | 38 | |
michael@0 | 39 | private: |
michael@0 | 40 | void OnMsgClose(); |
michael@0 | 41 | void OnMsgAttach(); |
michael@0 | 42 | |
michael@0 | 43 | // try attaching our socket (mFD) to the STS's poll list. |
michael@0 | 44 | nsresult TryAttach(); |
michael@0 | 45 | |
michael@0 | 46 | // lock protects access to mListener; |
michael@0 | 47 | // so mListener is not cleared while being used/locked. |
michael@0 | 48 | mozilla::Mutex mLock; |
michael@0 | 49 | PRFileDesc *mFD; |
michael@0 | 50 | mozilla::net::NetAddr mAddr; |
michael@0 | 51 | nsCOMPtr<nsIUDPSocketListener> mListener; |
michael@0 | 52 | nsCOMPtr<nsIEventTarget> mListenerTarget; |
michael@0 | 53 | bool mAttached; |
michael@0 | 54 | nsRefPtr<nsSocketTransportService> mSts; |
michael@0 | 55 | |
michael@0 | 56 | uint64_t mByteReadCount; |
michael@0 | 57 | uint64_t mByteWriteCount; |
michael@0 | 58 | }; |
michael@0 | 59 | |
michael@0 | 60 | //----------------------------------------------------------------------------- |
michael@0 | 61 | |
michael@0 | 62 | class nsUDPMessage : public nsIUDPMessage |
michael@0 | 63 | { |
michael@0 | 64 | public: |
michael@0 | 65 | NS_DECL_CYCLE_COLLECTING_ISUPPORTS |
michael@0 | 66 | NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(nsUDPMessage) |
michael@0 | 67 | NS_DECL_NSIUDPMESSAGE |
michael@0 | 68 | |
michael@0 | 69 | nsUDPMessage(mozilla::net::NetAddr* aAddr, |
michael@0 | 70 | nsIOutputStream* aOutputStream, |
michael@0 | 71 | FallibleTArray<uint8_t>& aData); |
michael@0 | 72 | |
michael@0 | 73 | private: |
michael@0 | 74 | virtual ~nsUDPMessage(); |
michael@0 | 75 | |
michael@0 | 76 | mozilla::net::NetAddr mAddr; |
michael@0 | 77 | nsCOMPtr<nsIOutputStream> mOutputStream; |
michael@0 | 78 | FallibleTArray<uint8_t> mData; |
michael@0 | 79 | JS::Heap<JSObject*> mJsobj; |
michael@0 | 80 | }; |
michael@0 | 81 | |
michael@0 | 82 | |
michael@0 | 83 | //----------------------------------------------------------------------------- |
michael@0 | 84 | |
michael@0 | 85 | class nsUDPOutputStream : public nsIOutputStream |
michael@0 | 86 | { |
michael@0 | 87 | public: |
michael@0 | 88 | NS_DECL_THREADSAFE_ISUPPORTS |
michael@0 | 89 | NS_DECL_NSIOUTPUTSTREAM |
michael@0 | 90 | |
michael@0 | 91 | nsUDPOutputStream(nsUDPSocket* aSocket, |
michael@0 | 92 | PRFileDesc* aFD, |
michael@0 | 93 | PRNetAddr& aPrClientAddr); |
michael@0 | 94 | virtual ~nsUDPOutputStream(); |
michael@0 | 95 | |
michael@0 | 96 | private: |
michael@0 | 97 | nsRefPtr<nsUDPSocket> mSocket; |
michael@0 | 98 | PRFileDesc *mFD; |
michael@0 | 99 | PRNetAddr mPrClientAddr; |
michael@0 | 100 | bool mIsClosed; |
michael@0 | 101 | }; |
michael@0 | 102 | |
michael@0 | 103 | #endif // nsUDPSocket_h__ |