|
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 class nsUDPSocket : public nsASocketHandler |
|
18 , public nsIUDPSocket |
|
19 { |
|
20 public: |
|
21 NS_DECL_THREADSAFE_ISUPPORTS |
|
22 NS_DECL_NSIUDPSOCKET |
|
23 |
|
24 // nsASocketHandler methods: |
|
25 virtual void OnSocketReady(PRFileDesc* fd, int16_t outFlags); |
|
26 virtual void OnSocketDetached(PRFileDesc* fd); |
|
27 virtual void IsLocal(bool* aIsLocal); |
|
28 |
|
29 uint64_t ByteCountSent() { return mByteWriteCount; } |
|
30 uint64_t ByteCountReceived() { return mByteReadCount; } |
|
31 |
|
32 void AddOutputBytes(uint64_t aBytes); |
|
33 |
|
34 nsUDPSocket(); |
|
35 |
|
36 // This must be public to support older compilers (xlC_r on AIX) |
|
37 virtual ~nsUDPSocket(); |
|
38 |
|
39 private: |
|
40 void OnMsgClose(); |
|
41 void OnMsgAttach(); |
|
42 |
|
43 // try attaching our socket (mFD) to the STS's poll list. |
|
44 nsresult TryAttach(); |
|
45 |
|
46 // lock protects access to mListener; |
|
47 // so mListener is not cleared while being used/locked. |
|
48 mozilla::Mutex mLock; |
|
49 PRFileDesc *mFD; |
|
50 mozilla::net::NetAddr mAddr; |
|
51 nsCOMPtr<nsIUDPSocketListener> mListener; |
|
52 nsCOMPtr<nsIEventTarget> mListenerTarget; |
|
53 bool mAttached; |
|
54 nsRefPtr<nsSocketTransportService> mSts; |
|
55 |
|
56 uint64_t mByteReadCount; |
|
57 uint64_t mByteWriteCount; |
|
58 }; |
|
59 |
|
60 //----------------------------------------------------------------------------- |
|
61 |
|
62 class nsUDPMessage : public nsIUDPMessage |
|
63 { |
|
64 public: |
|
65 NS_DECL_CYCLE_COLLECTING_ISUPPORTS |
|
66 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(nsUDPMessage) |
|
67 NS_DECL_NSIUDPMESSAGE |
|
68 |
|
69 nsUDPMessage(mozilla::net::NetAddr* aAddr, |
|
70 nsIOutputStream* aOutputStream, |
|
71 FallibleTArray<uint8_t>& aData); |
|
72 |
|
73 private: |
|
74 virtual ~nsUDPMessage(); |
|
75 |
|
76 mozilla::net::NetAddr mAddr; |
|
77 nsCOMPtr<nsIOutputStream> mOutputStream; |
|
78 FallibleTArray<uint8_t> mData; |
|
79 JS::Heap<JSObject*> mJsobj; |
|
80 }; |
|
81 |
|
82 |
|
83 //----------------------------------------------------------------------------- |
|
84 |
|
85 class nsUDPOutputStream : public nsIOutputStream |
|
86 { |
|
87 public: |
|
88 NS_DECL_THREADSAFE_ISUPPORTS |
|
89 NS_DECL_NSIOUTPUTSTREAM |
|
90 |
|
91 nsUDPOutputStream(nsUDPSocket* aSocket, |
|
92 PRFileDesc* aFD, |
|
93 PRNetAddr& aPrClientAddr); |
|
94 virtual ~nsUDPOutputStream(); |
|
95 |
|
96 private: |
|
97 nsRefPtr<nsUDPSocket> mSocket; |
|
98 PRFileDesc *mFD; |
|
99 PRNetAddr mPrClientAddr; |
|
100 bool mIsClosed; |
|
101 }; |
|
102 |
|
103 #endif // nsUDPSocket_h__ |