nr_socket_tcp.h (3832B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set sw=2 ts=8 et tw=80 ft=cpp : */ 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 /* 8 Copyright (c) 2007, Adobe Systems, Incorporated 9 Copyright (c) 2013, Mozilla 10 11 All rights reserved. 12 13 Redistribution and use in source and binary forms, with or without 14 modification, are permitted provided that the following conditions are 15 met: 16 17 * Redistributions of source code must retain the above copyright 18 notice, this list of conditions and the following disclaimer. 19 20 * Redistributions in binary form must reproduce the above copyright 21 notice, this list of conditions and the following disclaimer in the 22 documentation and/or other materials provided with the distribution. 23 24 * Neither the name of Adobe Systems, Network Resonance, Mozilla nor 25 the names of its contributors may be used to endorse or promote 26 products derived from this software without specific prior written 27 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 #ifndef nr_socket_tcp_h__ 43 #define nr_socket_tcp_h__ 44 45 #include <list> 46 47 #include "mozilla/net/WebrtcTCPSocketCallback.h" 48 #include "nsTArray.h" 49 50 extern "C" { 51 #include "nr_api.h" 52 #include "nr_socket.h" 53 #include "transport_addr.h" 54 } 55 56 #include "nr_socket_prsock.h" 57 58 namespace mozilla { 59 using namespace net; 60 61 namespace net { 62 class WebrtcTCPSocketWrapper; 63 } // namespace net 64 65 class NrTcpSocketData; 66 class NrSocketProxyConfig; 67 68 class NrTcpSocket : public NrSocketBase, public WebrtcTCPSocketCallback { 69 public: 70 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(NrTcpSocket, override) 71 72 explicit NrTcpSocket(const std::shared_ptr<NrSocketProxyConfig>& aConfig); 73 74 // NrSocketBase 75 int create(nr_transport_addr* aAddr) override; 76 int connect(const nr_transport_addr* aAddr) override; 77 void close() override; 78 int write(const void* aBuffer, size_t aCount, size_t* aWrote) override; 79 int read(void* aBuffer, size_t aCount, size_t* aRead) override; 80 int getaddr(nr_transport_addr* aAddr) override; 81 int sendto(const void* aBuffer, size_t aCount, int aFlags, 82 const nr_transport_addr* aAddr) override; 83 int recvfrom(void* aBuffer, size_t aCount, size_t* aRead, int aFlags, 84 nr_transport_addr* aAddr) override; 85 int listen(int aBacklog) override; 86 int accept(nr_transport_addr* aAddr, nr_socket** aSocket) override; 87 88 // WebrtcTCPSocketCallback 89 void OnClose(nsresult aReason) override; 90 void OnConnected(const nsACString& aProxyType) override; 91 void OnRead(nsTArray<uint8_t>&& aReadData) override; 92 93 size_t CountUnreadBytes() const; 94 95 // for gtests 96 void AssignChannel_DoNotUse(WebrtcTCPSocketWrapper* aWrapper); 97 98 protected: 99 virtual ~NrTcpSocket(); 100 101 private: 102 void DoCallbacks(); 103 104 bool mClosed; 105 106 size_t mReadOffset; 107 std::list<NrTcpSocketData> mReadQueue; 108 109 std::shared_ptr<NrSocketProxyConfig> mConfig; 110 111 RefPtr<WebrtcTCPSocketWrapper> mWebrtcTCPSocket; 112 }; 113 114 } // namespace mozilla 115 116 #endif // nr_socket_tcp_h__