Bumping manifests a=b2g-bump
[gecko.git] / media / mtransport / nr_socket_prsock.h
blob476a36587ada5dca2e439d062bae168e4db34768
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/. */
7 // Original author: ekr@rtfm.com
9 /* Some source code here from nICEr. Copyright is:
11 Copyright (c) 2007, Adobe Systems, Incorporated
12 All rights reserved.
14 Redistribution and use in source and binary forms, with or without
15 modification, are permitted provided that the following conditions are
16 met:
18 * Redistributions of source code must retain the above copyright
19 notice, this list of conditions and the following disclaimer.
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.
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.
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.
43 // Implementation of nICEr/nr_socket that is tied to the Gecko
44 // SocketTransportService.
46 #ifndef nr_socket_prsock__
47 #define nr_socket_prsock__
49 #include <queue>
51 #include "nspr.h"
52 #include "prio.h"
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 #include "nsProxyRelease.h"
63 #include "databuffer.h"
64 #include "m_cpp_utils.h"
65 #include "mozilla/ReentrantMonitor.h"
66 #include "mozilla/RefPtr.h"
67 #include "mozilla/TimeStamp.h"
69 // Stub declaration for nICEr type
70 typedef struct nr_socket_vtbl_ nr_socket_vtbl;
72 namespace mozilla {
74 namespace net {
75 union NetAddr;
78 class NrSocketBase {
79 public:
80 NrSocketBase() : connect_invoked_(false), poll_flags_(0) {
81 memset(cbs_, 0, sizeof(cbs_));
82 memset(cb_args_, 0, sizeof(cb_args_));
83 memset(&my_addr_, 0, sizeof(my_addr_));
85 virtual ~NrSocketBase() {}
87 // the nr_socket APIs
88 virtual int create(nr_transport_addr *addr) = 0;
89 virtual int sendto(const void *msg, size_t len,
90 int flags, nr_transport_addr *to) = 0;
91 virtual int recvfrom(void * buf, size_t maxlen,
92 size_t *len, int flags,
93 nr_transport_addr *from) = 0;
94 virtual int getaddr(nr_transport_addr *addrp) = 0;
95 virtual void close() = 0;
96 virtual int connect(nr_transport_addr *addr) = 0;
97 virtual int write(const void *msg, size_t len, size_t *written) = 0;
98 virtual int read(void* buf, size_t maxlen, size_t *len) = 0;
100 // Implementations of the async_event APIs
101 virtual int async_wait(int how, NR_async_cb cb, void *cb_arg,
102 char *function, int line);
103 virtual int cancel(int how);
105 // nsISupport reference counted interface
106 NS_IMETHOD_(MozExternalRefCountType) AddRef(void) = 0;
107 NS_IMETHOD_(MozExternalRefCountType) Release(void) = 0;
109 uint32_t poll_flags() {
110 return poll_flags_;
113 virtual nr_socket_vtbl *vtbl(); // To access in test classes.
115 static TimeStamp short_term_violation_time();
116 static TimeStamp long_term_violation_time();
118 protected:
119 void fire_callback(int how);
121 bool connect_invoked_;
122 nr_transport_addr my_addr_;
124 private:
125 NR_async_cb cbs_[NR_ASYNC_WAIT_WRITE + 1];
126 void *cb_args_[NR_ASYNC_WAIT_WRITE + 1];
127 uint32_t poll_flags_;
130 class NrSocket : public NrSocketBase,
131 public nsASocketHandler {
132 public:
133 NrSocket() : fd_(nullptr) {}
135 // Implement nsASocket
136 virtual void OnSocketReady(PRFileDesc *fd, int16_t outflags) MOZ_OVERRIDE;
137 virtual void OnSocketDetached(PRFileDesc *fd) MOZ_OVERRIDE;
138 virtual void IsLocal(bool *aIsLocal) MOZ_OVERRIDE;
139 virtual uint64_t ByteCountSent() MOZ_OVERRIDE { return 0; }
140 virtual uint64_t ByteCountReceived() MOZ_OVERRIDE { return 0; }
142 // nsISupports methods
143 NS_DECL_THREADSAFE_ISUPPORTS
145 // Implementations of the async_event APIs
146 virtual int async_wait(int how, NR_async_cb cb, void *cb_arg,
147 char *function, int line) MOZ_OVERRIDE;
148 virtual int cancel(int how) MOZ_OVERRIDE;
151 // Implementations of the nr_socket APIs
152 virtual int create(nr_transport_addr *addr) MOZ_OVERRIDE; // (really init, but it's called create)
153 virtual int sendto(const void *msg, size_t len,
154 int flags, nr_transport_addr *to) MOZ_OVERRIDE;
155 virtual int recvfrom(void * buf, size_t maxlen,
156 size_t *len, int flags,
157 nr_transport_addr *from) MOZ_OVERRIDE;
158 virtual int getaddr(nr_transport_addr *addrp) MOZ_OVERRIDE;
159 virtual void close() MOZ_OVERRIDE;
160 virtual int connect(nr_transport_addr *addr) MOZ_OVERRIDE;
161 virtual int write(const void *msg, size_t len, size_t *written) MOZ_OVERRIDE;
162 virtual int read(void* buf, size_t maxlen, size_t *len) MOZ_OVERRIDE;
164 private:
165 virtual ~NrSocket() {
166 if (fd_)
167 PR_Close(fd_);
170 DISALLOW_COPY_ASSIGN(NrSocket);
172 PRFileDesc *fd_;
173 nsCOMPtr<nsIEventTarget> ststhread_;
176 struct nr_udp_message {
177 nr_udp_message(const PRNetAddr &from, nsAutoPtr<DataBuffer> &data)
178 : from(from), data(data) {
181 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(nr_udp_message);
183 PRNetAddr from;
184 nsAutoPtr<DataBuffer> data;
186 private:
187 ~nr_udp_message() {}
188 DISALLOW_COPY_ASSIGN(nr_udp_message);
191 class NrSocketIpc : public NrSocketBase {
192 public:
194 enum NrSocketIpcState {
195 NR_INIT,
196 NR_CONNECTING,
197 NR_CONNECTED,
198 NR_CLOSING,
199 NR_CLOSED,
202 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(NrSocketIpc)
204 NS_IMETHODIMP CallListenerError(const nsACString &message,
205 const nsACString &filename,
206 uint32_t line_number);
207 NS_IMETHODIMP CallListenerReceivedData(const nsACString &host,
208 uint16_t port,
209 const uint8_t *data,
210 uint32_t data_length);
211 NS_IMETHODIMP CallListenerOpened();
212 NS_IMETHODIMP CallListenerClosed();
214 explicit NrSocketIpc(const nsCOMPtr<nsIEventTarget> &main_thread);
216 // Implementations of the NrSocketBase APIs
217 virtual int create(nr_transport_addr *addr) MOZ_OVERRIDE;
218 virtual int sendto(const void *msg, size_t len,
219 int flags, nr_transport_addr *to) MOZ_OVERRIDE;
220 virtual int recvfrom(void * buf, size_t maxlen,
221 size_t *len, int flags,
222 nr_transport_addr *from) MOZ_OVERRIDE;
223 virtual int getaddr(nr_transport_addr *addrp) MOZ_OVERRIDE;
224 virtual void close() MOZ_OVERRIDE;
225 virtual int connect(nr_transport_addr *addr) MOZ_OVERRIDE;
226 virtual int write(const void *msg, size_t len, size_t *written) MOZ_OVERRIDE;
227 virtual int read(void* buf, size_t maxlen, size_t *len) MOZ_OVERRIDE;
229 private:
230 virtual ~NrSocketIpc() {};
232 DISALLOW_COPY_ASSIGN(NrSocketIpc);
234 // Main thread executors of the NrSocketBase APIs
235 void create_m(const nsACString &host, const uint16_t port);
236 void sendto_m(const net::NetAddr &addr, nsAutoPtr<DataBuffer> buf);
237 void close_m();
238 // STS thread executor
239 void recv_callback_s(RefPtr<nr_udp_message> msg);
241 bool err_;
242 NrSocketIpcState state_;
243 std::queue<RefPtr<nr_udp_message> > received_msgs_;
245 nsMainThreadPtrHandle<nsIUDPSocketChild> socket_child_;
246 nsCOMPtr<nsIEventTarget> sts_thread_;
247 const nsCOMPtr<nsIEventTarget> main_thread_;
248 ReentrantMonitor monitor_;
251 // The socket child holds onto one of these, which just passes callbacks
252 // through and makes sure the ref to the NrSocketIpc is released on STS.
253 class NrSocketIpcProxy : public nsIUDPSocketInternal {
254 public:
255 NS_DECL_THREADSAFE_ISUPPORTS
256 NS_DECL_NSIUDPSOCKETINTERNAL
258 nsresult Init(const nsRefPtr<NrSocketIpc>& socket);
260 private:
261 virtual ~NrSocketIpcProxy();
263 nsRefPtr<NrSocketIpc> socket_;
264 nsCOMPtr<nsIEventTarget> sts_thread_;
267 int nr_netaddr_to_transport_addr(const net::NetAddr *netaddr,
268 nr_transport_addr *addr,
269 int protocol);
270 int nr_praddr_to_transport_addr(const PRNetAddr *praddr,
271 nr_transport_addr *addr,
272 int protocol, int keep);
273 int nr_transport_addr_get_addrstring_and_port(nr_transport_addr *addr,
274 nsACString *host, int32_t *port);
275 } // close namespace
276 #endif