Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* vim: set ts=2 et sw=2 tw=80: */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
michael@0 | 5 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | // Original author: ekr@rtfm.com |
michael@0 | 8 | |
michael@0 | 9 | #ifndef transportlayerprsock_h__ |
michael@0 | 10 | #define transportlayerprsock_h__ |
michael@0 | 11 | |
michael@0 | 12 | #include "nspr.h" |
michael@0 | 13 | #include "prio.h" |
michael@0 | 14 | |
michael@0 | 15 | #include "nsASocketHandler.h" |
michael@0 | 16 | #include "nsCOMPtr.h" |
michael@0 | 17 | #include "nsISocketTransportService.h" |
michael@0 | 18 | #include "nsXPCOM.h" |
michael@0 | 19 | |
michael@0 | 20 | #include "m_cpp_utils.h" |
michael@0 | 21 | #include "transportflow.h" |
michael@0 | 22 | #include "transportlayer.h" |
michael@0 | 23 | |
michael@0 | 24 | namespace mozilla { |
michael@0 | 25 | |
michael@0 | 26 | class TransportLayerPrsock : public TransportLayer { |
michael@0 | 27 | public: |
michael@0 | 28 | TransportLayerPrsock() : fd_(nullptr), handler_() {} |
michael@0 | 29 | |
michael@0 | 30 | virtual ~TransportLayerPrsock() { |
michael@0 | 31 | Detach(); |
michael@0 | 32 | } |
michael@0 | 33 | |
michael@0 | 34 | |
michael@0 | 35 | // Internal initializer |
michael@0 | 36 | virtual nsresult InitInternal(); |
michael@0 | 37 | |
michael@0 | 38 | void Import(PRFileDesc *fd, nsresult *result); |
michael@0 | 39 | |
michael@0 | 40 | void Detach() { |
michael@0 | 41 | handler_->Detach(); |
michael@0 | 42 | } |
michael@0 | 43 | |
michael@0 | 44 | // Implement TransportLayer |
michael@0 | 45 | virtual TransportResult SendPacket(const unsigned char *data, size_t len); |
michael@0 | 46 | |
michael@0 | 47 | TRANSPORT_LAYER_ID("prsock") |
michael@0 | 48 | |
michael@0 | 49 | private: |
michael@0 | 50 | DISALLOW_COPY_ASSIGN(TransportLayerPrsock); |
michael@0 | 51 | |
michael@0 | 52 | // Inner class |
michael@0 | 53 | class SocketHandler : public nsASocketHandler { |
michael@0 | 54 | public: |
michael@0 | 55 | SocketHandler(TransportLayerPrsock *prsock, PRFileDesc *fd) : |
michael@0 | 56 | prsock_(prsock), fd_(fd) { |
michael@0 | 57 | mPollFlags = PR_POLL_READ; |
michael@0 | 58 | } |
michael@0 | 59 | virtual ~SocketHandler() {} |
michael@0 | 60 | |
michael@0 | 61 | void Detach() { |
michael@0 | 62 | mCondition = NS_BASE_STREAM_CLOSED; |
michael@0 | 63 | prsock_ = nullptr; |
michael@0 | 64 | } |
michael@0 | 65 | |
michael@0 | 66 | // Implement nsASocket |
michael@0 | 67 | virtual void OnSocketReady(PRFileDesc *fd, int16_t outflags) { |
michael@0 | 68 | if (prsock_) { |
michael@0 | 69 | prsock_->OnSocketReady(fd, outflags); |
michael@0 | 70 | } |
michael@0 | 71 | } |
michael@0 | 72 | |
michael@0 | 73 | virtual void OnSocketDetached(PRFileDesc *fd) { |
michael@0 | 74 | if (prsock_) { |
michael@0 | 75 | prsock_->OnSocketDetached(fd); |
michael@0 | 76 | } |
michael@0 | 77 | PR_Close(fd_); |
michael@0 | 78 | } |
michael@0 | 79 | |
michael@0 | 80 | virtual void IsLocal(bool *aIsLocal) { |
michael@0 | 81 | // TODO(jesup): better check? Does it matter? (likely no) |
michael@0 | 82 | *aIsLocal = false; |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | virtual uint64_t ByteCountSent() { return 0; } |
michael@0 | 86 | virtual uint64_t ByteCountReceived() { return 0; } |
michael@0 | 87 | |
michael@0 | 88 | // nsISupports methods |
michael@0 | 89 | NS_DECL_THREADSAFE_ISUPPORTS |
michael@0 | 90 | |
michael@0 | 91 | private: |
michael@0 | 92 | TransportLayerPrsock *prsock_; |
michael@0 | 93 | PRFileDesc *fd_; |
michael@0 | 94 | private: |
michael@0 | 95 | DISALLOW_COPY_ASSIGN(SocketHandler); |
michael@0 | 96 | }; |
michael@0 | 97 | |
michael@0 | 98 | // Allow SocketHandler to talk to our APIs |
michael@0 | 99 | friend class SocketHandler; |
michael@0 | 100 | |
michael@0 | 101 | // Functions to be called by SocketHandler |
michael@0 | 102 | void OnSocketReady(PRFileDesc *fd, int16_t outflags); |
michael@0 | 103 | void OnSocketDetached(PRFileDesc *fd) { |
michael@0 | 104 | TL_SET_STATE(TS_CLOSED); |
michael@0 | 105 | } |
michael@0 | 106 | void IsLocal(bool *aIsLocal) { |
michael@0 | 107 | // TODO(jesup): better check? Does it matter? (likely no) |
michael@0 | 108 | *aIsLocal = false; |
michael@0 | 109 | } |
michael@0 | 110 | |
michael@0 | 111 | PRFileDesc *fd_; |
michael@0 | 112 | nsRefPtr<SocketHandler> handler_; |
michael@0 | 113 | nsCOMPtr<nsISocketTransportService> stservice_; |
michael@0 | 114 | |
michael@0 | 115 | }; |
michael@0 | 116 | |
michael@0 | 117 | |
michael@0 | 118 | |
michael@0 | 119 | } // close namespace |
michael@0 | 120 | #endif |