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