|
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* vim: set ts=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 file, |
|
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
6 |
|
7 // Original author: ekr@rtfm.com |
|
8 |
|
9 /* Some source code here from nICEr. Copyright is: |
|
10 |
|
11 Copyright (c) 2007, Adobe Systems, Incorporated |
|
12 All rights reserved. |
|
13 |
|
14 Redistribution and use in source and binary forms, with or without |
|
15 modification, are permitted provided that the following conditions are |
|
16 met: |
|
17 |
|
18 * Redistributions of source code must retain the above copyright |
|
19 notice, this list of conditions and the following disclaimer. |
|
20 |
|
21 * Redistributions in binary form must reproduce the above copyright |
|
22 notice, this list of conditions and the following disclaimer in the |
|
23 documentation and/or other materials provided with the distribution. |
|
24 |
|
25 * Neither the name of Adobe Systems, Network Resonance nor the names of its |
|
26 contributors may be used to endorse or promote products derived from |
|
27 this software without specific prior written permission. |
|
28 |
|
29 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
30 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
31 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
32 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
33 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
34 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
35 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
36 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
37 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
38 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
39 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
40 */ |
|
41 |
|
42 |
|
43 // Implementation of nICEr/nr_socket that is tied to the Gecko |
|
44 // SocketTransportService. |
|
45 |
|
46 #ifndef nr_socket_prsock__ |
|
47 #define nr_socket_prsock__ |
|
48 |
|
49 #include <queue> |
|
50 |
|
51 #include "nspr.h" |
|
52 #include "prio.h" |
|
53 |
|
54 #include "nsAutoPtr.h" |
|
55 #include "nsCOMPtr.h" |
|
56 #include "nsASocketHandler.h" |
|
57 #include "nsISocketTransportService.h" |
|
58 #include "nsXPCOM.h" |
|
59 #include "nsIEventTarget.h" |
|
60 #include "nsIUDPSocketChild.h" |
|
61 |
|
62 #include "databuffer.h" |
|
63 #include "m_cpp_utils.h" |
|
64 #include "mozilla/ReentrantMonitor.h" |
|
65 #include "mozilla/RefPtr.h" |
|
66 |
|
67 // Stub declaration for nICEr type |
|
68 typedef struct nr_socket_vtbl_ nr_socket_vtbl; |
|
69 |
|
70 namespace mozilla { |
|
71 |
|
72 namespace net { |
|
73 union NetAddr; |
|
74 } |
|
75 |
|
76 class NrSocketBase { |
|
77 public: |
|
78 NrSocketBase() : connect_invoked_(false), poll_flags_(0) { |
|
79 memset(cbs_, 0, sizeof(cbs_)); |
|
80 memset(cb_args_, 0, sizeof(cb_args_)); |
|
81 memset(&my_addr_, 0, sizeof(my_addr_)); |
|
82 } |
|
83 virtual ~NrSocketBase() {} |
|
84 |
|
85 // the nr_socket APIs |
|
86 virtual int create(nr_transport_addr *addr) = 0; |
|
87 virtual int sendto(const void *msg, size_t len, |
|
88 int flags, nr_transport_addr *to) = 0; |
|
89 virtual int recvfrom(void * buf, size_t maxlen, |
|
90 size_t *len, int flags, |
|
91 nr_transport_addr *from) = 0; |
|
92 virtual int getaddr(nr_transport_addr *addrp) = 0; |
|
93 virtual void close() = 0; |
|
94 virtual int connect(nr_transport_addr *addr) = 0; |
|
95 virtual int write(const void *msg, size_t len, size_t *written) = 0; |
|
96 virtual int read(void* buf, size_t maxlen, size_t *len) = 0; |
|
97 |
|
98 // Implementations of the async_event APIs |
|
99 virtual int async_wait(int how, NR_async_cb cb, void *cb_arg, |
|
100 char *function, int line); |
|
101 virtual int cancel(int how); |
|
102 |
|
103 // nsISupport reference counted interface |
|
104 NS_IMETHOD_(MozExternalRefCountType) AddRef(void) = 0; |
|
105 NS_IMETHOD_(MozExternalRefCountType) Release(void) = 0; |
|
106 |
|
107 uint32_t poll_flags() { |
|
108 return poll_flags_; |
|
109 } |
|
110 |
|
111 virtual nr_socket_vtbl *vtbl(); // To access in test classes. |
|
112 |
|
113 protected: |
|
114 void fire_callback(int how); |
|
115 |
|
116 bool connect_invoked_; |
|
117 nr_transport_addr my_addr_; |
|
118 |
|
119 private: |
|
120 NR_async_cb cbs_[NR_ASYNC_WAIT_WRITE + 1]; |
|
121 void *cb_args_[NR_ASYNC_WAIT_WRITE + 1]; |
|
122 uint32_t poll_flags_; |
|
123 }; |
|
124 |
|
125 class NrSocket : public NrSocketBase, |
|
126 public nsASocketHandler { |
|
127 public: |
|
128 NrSocket() : fd_(nullptr) {} |
|
129 virtual ~NrSocket() { |
|
130 PR_Close(fd_); |
|
131 } |
|
132 |
|
133 // Implement nsASocket |
|
134 virtual void OnSocketReady(PRFileDesc *fd, int16_t outflags); |
|
135 virtual void OnSocketDetached(PRFileDesc *fd); |
|
136 virtual void IsLocal(bool *aIsLocal); |
|
137 virtual uint64_t ByteCountSent() { return 0; } |
|
138 virtual uint64_t ByteCountReceived() { return 0; } |
|
139 |
|
140 // nsISupports methods |
|
141 NS_DECL_THREADSAFE_ISUPPORTS |
|
142 |
|
143 // Implementations of the async_event APIs |
|
144 virtual int async_wait(int how, NR_async_cb cb, void *cb_arg, |
|
145 char *function, int line); |
|
146 virtual int cancel(int how); |
|
147 |
|
148 |
|
149 // Implementations of the nr_socket APIs |
|
150 virtual int create(nr_transport_addr *addr); // (really init, but it's called create) |
|
151 virtual int sendto(const void *msg, size_t len, |
|
152 int flags, nr_transport_addr *to); |
|
153 virtual int recvfrom(void * buf, size_t maxlen, |
|
154 size_t *len, int flags, |
|
155 nr_transport_addr *from); |
|
156 virtual int getaddr(nr_transport_addr *addrp); |
|
157 virtual void close(); |
|
158 virtual int connect(nr_transport_addr *addr); |
|
159 virtual int write(const void *msg, size_t len, size_t *written); |
|
160 virtual int read(void* buf, size_t maxlen, size_t *len); |
|
161 |
|
162 private: |
|
163 DISALLOW_COPY_ASSIGN(NrSocket); |
|
164 |
|
165 PRFileDesc *fd_; |
|
166 nsCOMPtr<nsIEventTarget> ststhread_; |
|
167 }; |
|
168 |
|
169 struct nr_udp_message { |
|
170 nr_udp_message(const PRNetAddr &from, nsAutoPtr<DataBuffer> &data) |
|
171 : from(from), data(data) { |
|
172 } |
|
173 |
|
174 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(nr_udp_message); |
|
175 |
|
176 PRNetAddr from; |
|
177 nsAutoPtr<DataBuffer> data; |
|
178 |
|
179 private: |
|
180 DISALLOW_COPY_ASSIGN(nr_udp_message); |
|
181 }; |
|
182 |
|
183 class NrSocketIpc : public NrSocketBase, |
|
184 public nsIUDPSocketInternal { |
|
185 public: |
|
186 |
|
187 enum NrSocketIpcState { |
|
188 NR_INIT, |
|
189 NR_CONNECTING, |
|
190 NR_CONNECTED, |
|
191 NR_CLOSING, |
|
192 NR_CLOSED, |
|
193 }; |
|
194 |
|
195 NS_DECL_THREADSAFE_ISUPPORTS |
|
196 NS_DECL_NSIUDPSOCKETINTERNAL |
|
197 |
|
198 NrSocketIpc(const nsCOMPtr<nsIEventTarget> &main_thread); |
|
199 virtual ~NrSocketIpc() {}; |
|
200 |
|
201 // Implementations of the NrSocketBase APIs |
|
202 virtual int create(nr_transport_addr *addr); |
|
203 virtual int sendto(const void *msg, size_t len, |
|
204 int flags, nr_transport_addr *to); |
|
205 virtual int recvfrom(void * buf, size_t maxlen, |
|
206 size_t *len, int flags, |
|
207 nr_transport_addr *from); |
|
208 virtual int getaddr(nr_transport_addr *addrp); |
|
209 virtual void close(); |
|
210 virtual int connect(nr_transport_addr *addr); |
|
211 virtual int write(const void *msg, size_t len, size_t *written); |
|
212 virtual int read(void* buf, size_t maxlen, size_t *len); |
|
213 |
|
214 private: |
|
215 DISALLOW_COPY_ASSIGN(NrSocketIpc); |
|
216 |
|
217 // Main thread executors of the NrSocketBase APIs |
|
218 void create_m(const nsACString &host, const uint16_t port); |
|
219 void sendto_m(const net::NetAddr &addr, nsAutoPtr<DataBuffer> buf); |
|
220 void close_m(); |
|
221 // STS thread executor |
|
222 void recv_callback_s(RefPtr<nr_udp_message> msg); |
|
223 |
|
224 bool err_; |
|
225 NrSocketIpcState state_; |
|
226 std::queue<RefPtr<nr_udp_message> > received_msgs_; |
|
227 |
|
228 nsCOMPtr<nsIUDPSocketChild> socket_child_; |
|
229 nsCOMPtr<nsIEventTarget> sts_thread_; |
|
230 const nsCOMPtr<nsIEventTarget> main_thread_; |
|
231 ReentrantMonitor monitor_; |
|
232 }; |
|
233 |
|
234 int nr_netaddr_to_transport_addr(const net::NetAddr *netaddr, |
|
235 nr_transport_addr *addr, |
|
236 int protocol); |
|
237 int nr_praddr_to_transport_addr(const PRNetAddr *praddr, |
|
238 nr_transport_addr *addr, |
|
239 int protocol, int keep); |
|
240 int nr_transport_addr_get_addrstring_and_port(nr_transport_addr *addr, |
|
241 nsACString *host, int32_t *port); |
|
242 } // close namespace |
|
243 #endif |