media/mtransport/transportflow.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

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 #include <deque>
michael@0 9
michael@0 10 #include "logging.h"
michael@0 11 #include "runnable_utils.h"
michael@0 12 #include "transportflow.h"
michael@0 13 #include "transportlayer.h"
michael@0 14
michael@0 15 namespace mozilla {
michael@0 16
michael@0 17 MOZ_MTLOG_MODULE("mtransport")
michael@0 18
michael@0 19 NS_IMPL_ISUPPORTS0(TransportFlow)
michael@0 20
michael@0 21 // There are some hacks here to allow destruction off of
michael@0 22 // the main thread.
michael@0 23 TransportFlow::~TransportFlow() {
michael@0 24 // Make sure that if we are off the right thread, we have
michael@0 25 // no more attached signals.
michael@0 26 if (!CheckThreadInt()) {
michael@0 27 MOZ_ASSERT(SignalStateChange.is_empty());
michael@0 28 MOZ_ASSERT(SignalPacketReceived.is_empty());
michael@0 29 }
michael@0 30
michael@0 31 // Push the destruction onto the STS thread. Note that there
michael@0 32 // is still some possibility that someone is accessing this
michael@0 33 // object simultaneously, but as long as smart pointer discipline
michael@0 34 // is maintained, it shouldn't be possible to access and
michael@0 35 // destroy it simultaneously. The conversion to an nsAutoPtr
michael@0 36 // ensures automatic destruction of the queue at exit of
michael@0 37 // DestroyFinal.
michael@0 38 nsAutoPtr<std::deque<TransportLayer*> > layers_tmp(layers_.forget());
michael@0 39 RUN_ON_THREAD(target_,
michael@0 40 WrapRunnableNM(&TransportFlow::DestroyFinal, layers_tmp),
michael@0 41 NS_DISPATCH_NORMAL);
michael@0 42 }
michael@0 43
michael@0 44 void TransportFlow::DestroyFinal(nsAutoPtr<std::deque<TransportLayer *> > layers) {
michael@0 45 ClearLayers(layers);
michael@0 46 }
michael@0 47
michael@0 48 void TransportFlow::ClearLayers(std::queue<TransportLayer *>* layers) {
michael@0 49 while (!layers->empty()) {
michael@0 50 delete layers->front();
michael@0 51 layers->pop();
michael@0 52 }
michael@0 53 }
michael@0 54
michael@0 55 void TransportFlow::ClearLayers(std::deque<TransportLayer *>* layers) {
michael@0 56 while (!layers->empty()) {
michael@0 57 delete layers->front();
michael@0 58 layers->pop_front();
michael@0 59 }
michael@0 60 }
michael@0 61
michael@0 62 nsresult TransportFlow::PushLayer(TransportLayer *layer) {
michael@0 63 CheckThread();
michael@0 64 ScopedDeletePtr<TransportLayer> layer_tmp(layer); // Destroy on failure.
michael@0 65
michael@0 66 // Don't allow pushes once we are in error state.
michael@0 67 if (state_ == TransportLayer::TS_ERROR) {
michael@0 68 MOZ_MTLOG(ML_ERROR, id_ + ": Can't call PushLayer in error state for flow");
michael@0 69 return NS_ERROR_FAILURE;
michael@0 70 }
michael@0 71
michael@0 72 nsresult rv = layer->Init();
michael@0 73 if (!NS_SUCCEEDED(rv)) {
michael@0 74 // Destroy the rest of the flow, because it's no longer in an acceptable
michael@0 75 // state.
michael@0 76 ClearLayers(layers_.get());
michael@0 77
michael@0 78 // Set ourselves to have failed.
michael@0 79 MOZ_MTLOG(ML_ERROR, id_ << ": Layer initialization failed; invalidating");
michael@0 80 StateChangeInt(TransportLayer::TS_ERROR);
michael@0 81
michael@0 82 return rv;
michael@0 83 }
michael@0 84 EnsureSameThread(layer);
michael@0 85
michael@0 86 TransportLayer *old_layer = layers_->empty() ? nullptr : layers_->front();
michael@0 87
michael@0 88 // Re-target my signals to the new layer
michael@0 89 if (old_layer) {
michael@0 90 old_layer->SignalStateChange.disconnect(this);
michael@0 91 old_layer->SignalPacketReceived.disconnect(this);
michael@0 92 }
michael@0 93 layers_->push_front(layer_tmp.forget());
michael@0 94 layer->Inserted(this, old_layer);
michael@0 95
michael@0 96 layer->SignalStateChange.connect(this, &TransportFlow::StateChange);
michael@0 97 layer->SignalPacketReceived.connect(this, &TransportFlow::PacketReceived);
michael@0 98 StateChangeInt(layer->state());
michael@0 99
michael@0 100 return NS_OK;
michael@0 101 }
michael@0 102
michael@0 103 // This is all-or-nothing.
michael@0 104 nsresult TransportFlow::PushLayers(nsAutoPtr<std::queue<TransportLayer *> > layers) {
michael@0 105 CheckThread();
michael@0 106
michael@0 107 MOZ_ASSERT(!layers->empty());
michael@0 108 if (layers->empty()) {
michael@0 109 MOZ_MTLOG(ML_ERROR, id_ << ": Can't call PushLayers with empty layers");
michael@0 110 return NS_ERROR_INVALID_ARG;
michael@0 111 }
michael@0 112
michael@0 113 // Don't allow pushes once we are in error state.
michael@0 114 if (state_ == TransportLayer::TS_ERROR) {
michael@0 115 MOZ_MTLOG(ML_ERROR,
michael@0 116 id_ << ": Can't call PushLayers in error state for flow ");
michael@0 117 ClearLayers(layers.get());
michael@0 118 return NS_ERROR_FAILURE;
michael@0 119 }
michael@0 120
michael@0 121 nsresult rv = NS_OK;
michael@0 122
michael@0 123 // Disconnect all the old signals.
michael@0 124 disconnect_all();
michael@0 125
michael@0 126 TransportLayer *layer;
michael@0 127
michael@0 128 while (!layers->empty()) {
michael@0 129 TransportLayer *old_layer = layers_->empty() ? nullptr : layers_->front();
michael@0 130 layer = layers->front();
michael@0 131
michael@0 132 rv = layer->Init();
michael@0 133 if (NS_FAILED(rv)) {
michael@0 134 MOZ_MTLOG(ML_ERROR,
michael@0 135 id_ << ": Layer initialization failed; invalidating flow ");
michael@0 136 break;
michael@0 137 }
michael@0 138
michael@0 139 EnsureSameThread(layer);
michael@0 140
michael@0 141 // Push the layer onto the queue.
michael@0 142 layers_->push_front(layer);
michael@0 143 layers->pop();
michael@0 144 layer->Inserted(this, old_layer);
michael@0 145 }
michael@0 146
michael@0 147 if (NS_FAILED(rv)) {
michael@0 148 // Destroy any layers we could not push.
michael@0 149 ClearLayers(layers);
michael@0 150
michael@0 151 // Now destroy the rest of the flow, because it's no longer
michael@0 152 // in an acceptable state.
michael@0 153 ClearLayers(layers_);
michael@0 154
michael@0 155 // Set ourselves to have failed.
michael@0 156 StateChangeInt(TransportLayer::TS_ERROR);
michael@0 157
michael@0 158 // Return failure.
michael@0 159 return rv;
michael@0 160 }
michael@0 161
michael@0 162 // Finally, attach ourselves to the top layer.
michael@0 163 layer->SignalStateChange.connect(this, &TransportFlow::StateChange);
michael@0 164 layer->SignalPacketReceived.connect(this, &TransportFlow::PacketReceived);
michael@0 165 StateChangeInt(layer->state()); // Signals if the state changes.
michael@0 166
michael@0 167 return NS_OK;
michael@0 168 }
michael@0 169
michael@0 170 TransportLayer *TransportFlow::top() const {
michael@0 171 CheckThread();
michael@0 172
michael@0 173 return layers_->empty() ? nullptr : layers_->front();
michael@0 174 }
michael@0 175
michael@0 176 TransportLayer *TransportFlow::GetLayer(const std::string& id) const {
michael@0 177 CheckThread();
michael@0 178
michael@0 179 for (std::deque<TransportLayer *>::const_iterator it = layers_->begin();
michael@0 180 it != layers_->end(); ++it) {
michael@0 181 if ((*it)->id() == id)
michael@0 182 return *it;
michael@0 183 }
michael@0 184
michael@0 185 return nullptr;
michael@0 186 }
michael@0 187
michael@0 188 TransportLayer::State TransportFlow::state() {
michael@0 189 CheckThread();
michael@0 190
michael@0 191 return state_;
michael@0 192 }
michael@0 193
michael@0 194 TransportResult TransportFlow::SendPacket(const unsigned char *data,
michael@0 195 size_t len) {
michael@0 196 CheckThread();
michael@0 197
michael@0 198 if (state_ != TransportLayer::TS_OPEN) {
michael@0 199 return TE_ERROR;
michael@0 200 }
michael@0 201 return top() ? top()->SendPacket(data, len) : TE_ERROR;
michael@0 202 }
michael@0 203
michael@0 204 bool TransportFlow::Contains(TransportLayer *layer) const {
michael@0 205 if (layers_) {
michael@0 206 for (auto l = layers_->begin(); l != layers_->end(); ++l) {
michael@0 207 if (*l == layer) {
michael@0 208 return true;
michael@0 209 }
michael@0 210 }
michael@0 211 }
michael@0 212 return false;
michael@0 213 }
michael@0 214
michael@0 215 void TransportFlow::EnsureSameThread(TransportLayer *layer) {
michael@0 216 // Enforce that if any of the layers have a thread binding,
michael@0 217 // they all have the same binding.
michael@0 218 if (target_) {
michael@0 219 const nsCOMPtr<nsIEventTarget>& lthread = layer->GetThread();
michael@0 220
michael@0 221 if (lthread && (lthread != target_))
michael@0 222 MOZ_CRASH();
michael@0 223 }
michael@0 224 else {
michael@0 225 target_ = layer->GetThread();
michael@0 226 }
michael@0 227 }
michael@0 228
michael@0 229 void TransportFlow::StateChangeInt(TransportLayer::State state) {
michael@0 230 CheckThread();
michael@0 231
michael@0 232 if (state == state_) {
michael@0 233 return;
michael@0 234 }
michael@0 235
michael@0 236 state_ = state;
michael@0 237 SignalStateChange(this, state_);
michael@0 238 }
michael@0 239
michael@0 240 void TransportFlow::StateChange(TransportLayer *layer,
michael@0 241 TransportLayer::State state) {
michael@0 242 CheckThread();
michael@0 243
michael@0 244 StateChangeInt(state);
michael@0 245 }
michael@0 246
michael@0 247 void TransportFlow::PacketReceived(TransportLayer* layer,
michael@0 248 const unsigned char *data,
michael@0 249 size_t len) {
michael@0 250 CheckThread();
michael@0 251
michael@0 252 SignalPacketReceived(this, data, len);
michael@0 253 }
michael@0 254
michael@0 255 } // close namespace

mercurial