1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/media/mtransport/nr_socket_prsock.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,243 @@ 1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* vim: set ts=2 et sw=2 tw=80: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.8 + * You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +// Original author: ekr@rtfm.com 1.11 + 1.12 +/* Some source code here from nICEr. Copyright is: 1.13 + 1.14 +Copyright (c) 2007, Adobe Systems, Incorporated 1.15 +All rights reserved. 1.16 + 1.17 +Redistribution and use in source and binary forms, with or without 1.18 +modification, are permitted provided that the following conditions are 1.19 +met: 1.20 + 1.21 +* Redistributions of source code must retain the above copyright 1.22 + notice, this list of conditions and the following disclaimer. 1.23 + 1.24 +* Redistributions in binary form must reproduce the above copyright 1.25 + notice, this list of conditions and the following disclaimer in the 1.26 + documentation and/or other materials provided with the distribution. 1.27 + 1.28 +* Neither the name of Adobe Systems, Network Resonance nor the names of its 1.29 + contributors may be used to endorse or promote products derived from 1.30 + this software without specific prior written permission. 1.31 + 1.32 +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.33 +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.34 +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1.35 +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 1.36 +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 1.37 +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 1.38 +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 1.39 +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 1.40 +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 1.41 +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 1.42 +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.43 +*/ 1.44 + 1.45 + 1.46 +// Implementation of nICEr/nr_socket that is tied to the Gecko 1.47 +// SocketTransportService. 1.48 + 1.49 +#ifndef nr_socket_prsock__ 1.50 +#define nr_socket_prsock__ 1.51 + 1.52 +#include <queue> 1.53 + 1.54 +#include "nspr.h" 1.55 +#include "prio.h" 1.56 + 1.57 +#include "nsAutoPtr.h" 1.58 +#include "nsCOMPtr.h" 1.59 +#include "nsASocketHandler.h" 1.60 +#include "nsISocketTransportService.h" 1.61 +#include "nsXPCOM.h" 1.62 +#include "nsIEventTarget.h" 1.63 +#include "nsIUDPSocketChild.h" 1.64 + 1.65 +#include "databuffer.h" 1.66 +#include "m_cpp_utils.h" 1.67 +#include "mozilla/ReentrantMonitor.h" 1.68 +#include "mozilla/RefPtr.h" 1.69 + 1.70 +// Stub declaration for nICEr type 1.71 +typedef struct nr_socket_vtbl_ nr_socket_vtbl; 1.72 + 1.73 +namespace mozilla { 1.74 + 1.75 +namespace net { 1.76 + union NetAddr; 1.77 +} 1.78 + 1.79 +class NrSocketBase { 1.80 +public: 1.81 + NrSocketBase() : connect_invoked_(false), poll_flags_(0) { 1.82 + memset(cbs_, 0, sizeof(cbs_)); 1.83 + memset(cb_args_, 0, sizeof(cb_args_)); 1.84 + memset(&my_addr_, 0, sizeof(my_addr_)); 1.85 + } 1.86 + virtual ~NrSocketBase() {} 1.87 + 1.88 + // the nr_socket APIs 1.89 + virtual int create(nr_transport_addr *addr) = 0; 1.90 + virtual int sendto(const void *msg, size_t len, 1.91 + int flags, nr_transport_addr *to) = 0; 1.92 + virtual int recvfrom(void * buf, size_t maxlen, 1.93 + size_t *len, int flags, 1.94 + nr_transport_addr *from) = 0; 1.95 + virtual int getaddr(nr_transport_addr *addrp) = 0; 1.96 + virtual void close() = 0; 1.97 + virtual int connect(nr_transport_addr *addr) = 0; 1.98 + virtual int write(const void *msg, size_t len, size_t *written) = 0; 1.99 + virtual int read(void* buf, size_t maxlen, size_t *len) = 0; 1.100 + 1.101 + // Implementations of the async_event APIs 1.102 + virtual int async_wait(int how, NR_async_cb cb, void *cb_arg, 1.103 + char *function, int line); 1.104 + virtual int cancel(int how); 1.105 + 1.106 + // nsISupport reference counted interface 1.107 + NS_IMETHOD_(MozExternalRefCountType) AddRef(void) = 0; 1.108 + NS_IMETHOD_(MozExternalRefCountType) Release(void) = 0; 1.109 + 1.110 + uint32_t poll_flags() { 1.111 + return poll_flags_; 1.112 + } 1.113 + 1.114 + virtual nr_socket_vtbl *vtbl(); // To access in test classes. 1.115 + 1.116 +protected: 1.117 + void fire_callback(int how); 1.118 + 1.119 + bool connect_invoked_; 1.120 + nr_transport_addr my_addr_; 1.121 + 1.122 +private: 1.123 + NR_async_cb cbs_[NR_ASYNC_WAIT_WRITE + 1]; 1.124 + void *cb_args_[NR_ASYNC_WAIT_WRITE + 1]; 1.125 + uint32_t poll_flags_; 1.126 +}; 1.127 + 1.128 +class NrSocket : public NrSocketBase, 1.129 + public nsASocketHandler { 1.130 +public: 1.131 + NrSocket() : fd_(nullptr) {} 1.132 + virtual ~NrSocket() { 1.133 + PR_Close(fd_); 1.134 + } 1.135 + 1.136 + // Implement nsASocket 1.137 + virtual void OnSocketReady(PRFileDesc *fd, int16_t outflags); 1.138 + virtual void OnSocketDetached(PRFileDesc *fd); 1.139 + virtual void IsLocal(bool *aIsLocal); 1.140 + virtual uint64_t ByteCountSent() { return 0; } 1.141 + virtual uint64_t ByteCountReceived() { return 0; } 1.142 + 1.143 + // nsISupports methods 1.144 + NS_DECL_THREADSAFE_ISUPPORTS 1.145 + 1.146 + // Implementations of the async_event APIs 1.147 + virtual int async_wait(int how, NR_async_cb cb, void *cb_arg, 1.148 + char *function, int line); 1.149 + virtual int cancel(int how); 1.150 + 1.151 + 1.152 + // Implementations of the nr_socket APIs 1.153 + virtual int create(nr_transport_addr *addr); // (really init, but it's called create) 1.154 + virtual int sendto(const void *msg, size_t len, 1.155 + int flags, nr_transport_addr *to); 1.156 + virtual int recvfrom(void * buf, size_t maxlen, 1.157 + size_t *len, int flags, 1.158 + nr_transport_addr *from); 1.159 + virtual int getaddr(nr_transport_addr *addrp); 1.160 + virtual void close(); 1.161 + virtual int connect(nr_transport_addr *addr); 1.162 + virtual int write(const void *msg, size_t len, size_t *written); 1.163 + virtual int read(void* buf, size_t maxlen, size_t *len); 1.164 + 1.165 +private: 1.166 + DISALLOW_COPY_ASSIGN(NrSocket); 1.167 + 1.168 + PRFileDesc *fd_; 1.169 + nsCOMPtr<nsIEventTarget> ststhread_; 1.170 +}; 1.171 + 1.172 +struct nr_udp_message { 1.173 + nr_udp_message(const PRNetAddr &from, nsAutoPtr<DataBuffer> &data) 1.174 + : from(from), data(data) { 1.175 + } 1.176 + 1.177 + NS_INLINE_DECL_THREADSAFE_REFCOUNTING(nr_udp_message); 1.178 + 1.179 + PRNetAddr from; 1.180 + nsAutoPtr<DataBuffer> data; 1.181 + 1.182 +private: 1.183 + DISALLOW_COPY_ASSIGN(nr_udp_message); 1.184 +}; 1.185 + 1.186 +class NrSocketIpc : public NrSocketBase, 1.187 + public nsIUDPSocketInternal { 1.188 +public: 1.189 + 1.190 + enum NrSocketIpcState { 1.191 + NR_INIT, 1.192 + NR_CONNECTING, 1.193 + NR_CONNECTED, 1.194 + NR_CLOSING, 1.195 + NR_CLOSED, 1.196 + }; 1.197 + 1.198 + NS_DECL_THREADSAFE_ISUPPORTS 1.199 + NS_DECL_NSIUDPSOCKETINTERNAL 1.200 + 1.201 + NrSocketIpc(const nsCOMPtr<nsIEventTarget> &main_thread); 1.202 + virtual ~NrSocketIpc() {}; 1.203 + 1.204 + // Implementations of the NrSocketBase APIs 1.205 + virtual int create(nr_transport_addr *addr); 1.206 + virtual int sendto(const void *msg, size_t len, 1.207 + int flags, nr_transport_addr *to); 1.208 + virtual int recvfrom(void * buf, size_t maxlen, 1.209 + size_t *len, int flags, 1.210 + nr_transport_addr *from); 1.211 + virtual int getaddr(nr_transport_addr *addrp); 1.212 + virtual void close(); 1.213 + virtual int connect(nr_transport_addr *addr); 1.214 + virtual int write(const void *msg, size_t len, size_t *written); 1.215 + virtual int read(void* buf, size_t maxlen, size_t *len); 1.216 + 1.217 +private: 1.218 + DISALLOW_COPY_ASSIGN(NrSocketIpc); 1.219 + 1.220 + // Main thread executors of the NrSocketBase APIs 1.221 + void create_m(const nsACString &host, const uint16_t port); 1.222 + void sendto_m(const net::NetAddr &addr, nsAutoPtr<DataBuffer> buf); 1.223 + void close_m(); 1.224 + // STS thread executor 1.225 + void recv_callback_s(RefPtr<nr_udp_message> msg); 1.226 + 1.227 + bool err_; 1.228 + NrSocketIpcState state_; 1.229 + std::queue<RefPtr<nr_udp_message> > received_msgs_; 1.230 + 1.231 + nsCOMPtr<nsIUDPSocketChild> socket_child_; 1.232 + nsCOMPtr<nsIEventTarget> sts_thread_; 1.233 + const nsCOMPtr<nsIEventTarget> main_thread_; 1.234 + ReentrantMonitor monitor_; 1.235 +}; 1.236 + 1.237 +int nr_netaddr_to_transport_addr(const net::NetAddr *netaddr, 1.238 + nr_transport_addr *addr, 1.239 + int protocol); 1.240 +int nr_praddr_to_transport_addr(const PRNetAddr *praddr, 1.241 + nr_transport_addr *addr, 1.242 + int protocol, int keep); 1.243 +int nr_transport_addr_get_addrstring_and_port(nr_transport_addr *addr, 1.244 + nsACString *host, int32_t *port); 1.245 +} // close namespace 1.246 +#endif