2012-10-03 00:04:58 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
// Original author: ekr@rtfm.com
|
|
|
|
#include <deque>
|
|
|
|
|
2013-03-04 11:10:30 +04:00
|
|
|
#include "logging.h"
|
2013-02-19 15:43:34 +04:00
|
|
|
#include "runnable_utils.h"
|
2012-10-03 00:04:58 +04:00
|
|
|
#include "transportflow.h"
|
|
|
|
#include "transportlayer.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2013-03-04 11:10:30 +04:00
|
|
|
MOZ_MTLOG_MODULE("mtransport")
|
|
|
|
|
2014-03-15 23:00:17 +04:00
|
|
|
NS_IMPL_ISUPPORTS0(TransportFlow)
|
|
|
|
|
2013-02-19 15:43:34 +04:00
|
|
|
// There are some hacks here to allow destruction off of
|
|
|
|
// the main thread.
|
2012-10-03 00:04:58 +04:00
|
|
|
TransportFlow::~TransportFlow() {
|
2013-02-19 15:43:34 +04:00
|
|
|
// Make sure that if we are off the right thread, we have
|
|
|
|
// no more attached signals.
|
|
|
|
if (!CheckThreadInt()) {
|
|
|
|
MOZ_ASSERT(SignalStateChange.is_empty());
|
|
|
|
MOZ_ASSERT(SignalPacketReceived.is_empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Push the destruction onto the STS thread. Note that there
|
|
|
|
// is still some possibility that someone is accessing this
|
|
|
|
// object simultaneously, but as long as smart pointer discipline
|
|
|
|
// is maintained, it shouldn't be possible to access and
|
|
|
|
// destroy it simultaneously. The conversion to an nsAutoPtr
|
|
|
|
// ensures automatic destruction of the queue at exit of
|
|
|
|
// DestroyFinal.
|
|
|
|
nsAutoPtr<std::deque<TransportLayer*> > layers_tmp(layers_.forget());
|
|
|
|
RUN_ON_THREAD(target_,
|
|
|
|
WrapRunnableNM(&TransportFlow::DestroyFinal, layers_tmp),
|
|
|
|
NS_DISPATCH_NORMAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TransportFlow::DestroyFinal(nsAutoPtr<std::deque<TransportLayer *> > layers) {
|
|
|
|
ClearLayers(layers);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TransportFlow::ClearLayers(std::queue<TransportLayer *>* layers) {
|
|
|
|
while (!layers->empty()) {
|
|
|
|
delete layers->front();
|
|
|
|
layers->pop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TransportFlow::ClearLayers(std::deque<TransportLayer *>* layers) {
|
|
|
|
while (!layers->empty()) {
|
|
|
|
delete layers->front();
|
|
|
|
layers->pop_front();
|
2012-10-03 00:04:58 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult TransportFlow::PushLayer(TransportLayer *layer) {
|
2013-02-19 15:43:34 +04:00
|
|
|
CheckThread();
|
|
|
|
ScopedDeletePtr<TransportLayer> layer_tmp(layer); // Destroy on failure.
|
|
|
|
|
2013-03-04 11:10:30 +04:00
|
|
|
// Don't allow pushes once we are in error state.
|
|
|
|
if (state_ == TransportLayer::TS_ERROR) {
|
2013-07-03 22:40:36 +04:00
|
|
|
MOZ_MTLOG(ML_ERROR, id_ + ": Can't call PushLayer in error state for flow");
|
2013-03-04 11:10:30 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2012-10-03 00:04:58 +04:00
|
|
|
nsresult rv = layer->Init();
|
2013-03-04 11:10:30 +04:00
|
|
|
if (!NS_SUCCEEDED(rv)) {
|
2013-02-19 15:43:34 +04:00
|
|
|
// Destroy the rest of the flow, because it's no longer in an acceptable
|
|
|
|
// state.
|
|
|
|
ClearLayers(layers_.get());
|
|
|
|
|
2013-03-04 11:10:30 +04:00
|
|
|
// Set ourselves to have failed.
|
2013-07-03 22:40:36 +04:00
|
|
|
MOZ_MTLOG(ML_ERROR, id_ << ": Layer initialization failed; invalidating");
|
2013-03-04 11:10:30 +04:00
|
|
|
StateChangeInt(TransportLayer::TS_ERROR);
|
2013-02-19 15:43:34 +04:00
|
|
|
|
2012-10-03 00:04:58 +04:00
|
|
|
return rv;
|
2013-03-04 11:10:30 +04:00
|
|
|
}
|
2013-02-19 15:43:34 +04:00
|
|
|
EnsureSameThread(layer);
|
2012-10-03 00:04:58 +04:00
|
|
|
|
2013-02-19 15:43:34 +04:00
|
|
|
TransportLayer *old_layer = layers_->empty() ? nullptr : layers_->front();
|
2012-10-03 00:04:58 +04:00
|
|
|
|
|
|
|
// Re-target my signals to the new layer
|
|
|
|
if (old_layer) {
|
|
|
|
old_layer->SignalStateChange.disconnect(this);
|
|
|
|
old_layer->SignalPacketReceived.disconnect(this);
|
|
|
|
}
|
2013-02-19 15:43:34 +04:00
|
|
|
layers_->push_front(layer_tmp.forget());
|
2013-03-04 11:10:30 +04:00
|
|
|
layer->Inserted(this, old_layer);
|
|
|
|
|
2012-10-03 00:04:58 +04:00
|
|
|
layer->SignalStateChange.connect(this, &TransportFlow::StateChange);
|
|
|
|
layer->SignalPacketReceived.connect(this, &TransportFlow::PacketReceived);
|
2013-03-04 11:10:30 +04:00
|
|
|
StateChangeInt(layer->state());
|
2012-10-03 00:04:58 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-03-04 11:10:30 +04:00
|
|
|
// This is all-or-nothing.
|
|
|
|
nsresult TransportFlow::PushLayers(nsAutoPtr<std::queue<TransportLayer *> > layers) {
|
2013-02-19 15:43:34 +04:00
|
|
|
CheckThread();
|
|
|
|
|
2013-03-04 11:10:30 +04:00
|
|
|
MOZ_ASSERT(!layers->empty());
|
|
|
|
if (layers->empty()) {
|
2013-07-03 22:40:36 +04:00
|
|
|
MOZ_MTLOG(ML_ERROR, id_ << ": Can't call PushLayers with empty layers");
|
2013-03-04 11:10:30 +04:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't allow pushes once we are in error state.
|
|
|
|
if (state_ == TransportLayer::TS_ERROR) {
|
2013-07-03 22:40:36 +04:00
|
|
|
MOZ_MTLOG(ML_ERROR,
|
|
|
|
id_ << ": Can't call PushLayers in error state for flow ");
|
2013-02-19 15:43:34 +04:00
|
|
|
ClearLayers(layers.get());
|
2013-03-04 11:10:30 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
|
|
|
// Disconnect all the old signals.
|
|
|
|
disconnect_all();
|
2012-10-19 00:01:52 +04:00
|
|
|
|
2013-03-04 11:10:30 +04:00
|
|
|
TransportLayer *layer;
|
2012-10-19 00:01:52 +04:00
|
|
|
|
2013-03-04 11:10:30 +04:00
|
|
|
while (!layers->empty()) {
|
2013-02-19 15:43:34 +04:00
|
|
|
TransportLayer *old_layer = layers_->empty() ? nullptr : layers_->front();
|
2013-03-04 11:10:30 +04:00
|
|
|
layer = layers->front();
|
|
|
|
|
|
|
|
rv = layer->Init();
|
2012-10-19 00:01:52 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
2013-07-03 22:40:36 +04:00
|
|
|
MOZ_MTLOG(ML_ERROR,
|
|
|
|
id_ << ": Layer initialization failed; invalidating flow ");
|
2013-03-04 11:10:30 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-02-19 15:43:34 +04:00
|
|
|
EnsureSameThread(layer);
|
|
|
|
|
2013-03-04 11:10:30 +04:00
|
|
|
// Push the layer onto the queue.
|
2013-02-19 15:43:34 +04:00
|
|
|
layers_->push_front(layer);
|
2013-03-04 11:10:30 +04:00
|
|
|
layers->pop();
|
|
|
|
layer->Inserted(this, old_layer);
|
|
|
|
}
|
2012-10-19 00:01:52 +04:00
|
|
|
|
2013-03-04 11:10:30 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// Destroy any layers we could not push.
|
2013-02-19 15:43:34 +04:00
|
|
|
ClearLayers(layers);
|
2013-03-04 11:10:30 +04:00
|
|
|
|
|
|
|
// Now destroy the rest of the flow, because it's no longer
|
|
|
|
// in an acceptable state.
|
2013-02-19 15:43:34 +04:00
|
|
|
ClearLayers(layers_);
|
2013-03-04 11:10:30 +04:00
|
|
|
|
|
|
|
// Set ourselves to have failed.
|
|
|
|
StateChangeInt(TransportLayer::TS_ERROR);
|
|
|
|
|
|
|
|
// Return failure.
|
|
|
|
return rv;
|
2012-10-19 00:01:52 +04:00
|
|
|
}
|
|
|
|
|
2013-03-04 11:10:30 +04:00
|
|
|
// Finally, attach ourselves to the top layer.
|
|
|
|
layer->SignalStateChange.connect(this, &TransportFlow::StateChange);
|
|
|
|
layer->SignalPacketReceived.connect(this, &TransportFlow::PacketReceived);
|
|
|
|
StateChangeInt(layer->state()); // Signals if the state changes.
|
|
|
|
|
2012-10-19 00:01:52 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-10-03 00:04:58 +04:00
|
|
|
TransportLayer *TransportFlow::top() const {
|
2013-02-19 15:43:34 +04:00
|
|
|
CheckThread();
|
|
|
|
|
|
|
|
return layers_->empty() ? nullptr : layers_->front();
|
2012-10-03 00:04:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
TransportLayer *TransportFlow::GetLayer(const std::string& id) const {
|
2013-02-19 15:43:34 +04:00
|
|
|
CheckThread();
|
|
|
|
|
|
|
|
for (std::deque<TransportLayer *>::const_iterator it = layers_->begin();
|
|
|
|
it != layers_->end(); ++it) {
|
2012-10-03 00:04:58 +04:00
|
|
|
if ((*it)->id() == id)
|
|
|
|
return *it;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
TransportLayer::State TransportFlow::state() {
|
2013-02-19 15:43:34 +04:00
|
|
|
CheckThread();
|
|
|
|
|
2013-03-04 11:10:30 +04:00
|
|
|
return state_;
|
2012-10-03 00:04:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
TransportResult TransportFlow::SendPacket(const unsigned char *data,
|
|
|
|
size_t len) {
|
2013-02-19 15:43:34 +04:00
|
|
|
CheckThread();
|
|
|
|
|
2013-03-04 11:10:30 +04:00
|
|
|
if (state_ != TransportLayer::TS_OPEN) {
|
|
|
|
return TE_ERROR;
|
|
|
|
}
|
2012-10-03 00:04:58 +04:00
|
|
|
return top() ? top()->SendPacket(data, len) : TE_ERROR;
|
|
|
|
}
|
|
|
|
|
2014-01-15 04:29:42 +04:00
|
|
|
bool TransportFlow::Contains(TransportLayer *layer) const {
|
|
|
|
if (layers_) {
|
|
|
|
for (auto l = layers_->begin(); l != layers_->end(); ++l) {
|
|
|
|
if (*l == layer) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-02-19 15:43:34 +04:00
|
|
|
void TransportFlow::EnsureSameThread(TransportLayer *layer) {
|
|
|
|
// Enforce that if any of the layers have a thread binding,
|
|
|
|
// they all have the same binding.
|
|
|
|
if (target_) {
|
|
|
|
const nsCOMPtr<nsIEventTarget>& lthread = layer->GetThread();
|
|
|
|
|
|
|
|
if (lthread && (lthread != target_))
|
|
|
|
MOZ_CRASH();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
target_ = layer->GetThread();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-04 11:10:30 +04:00
|
|
|
void TransportFlow::StateChangeInt(TransportLayer::State state) {
|
2013-02-19 15:43:34 +04:00
|
|
|
CheckThread();
|
|
|
|
|
2013-03-04 11:10:30 +04:00
|
|
|
if (state == state_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
state_ = state;
|
|
|
|
SignalStateChange(this, state_);
|
|
|
|
}
|
|
|
|
|
2012-10-03 00:04:58 +04:00
|
|
|
void TransportFlow::StateChange(TransportLayer *layer,
|
|
|
|
TransportLayer::State state) {
|
2013-02-19 15:43:34 +04:00
|
|
|
CheckThread();
|
|
|
|
|
2013-03-04 11:10:30 +04:00
|
|
|
StateChangeInt(state);
|
2012-10-03 00:04:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void TransportFlow::PacketReceived(TransportLayer* layer,
|
|
|
|
const unsigned char *data,
|
|
|
|
size_t len) {
|
2013-02-19 15:43:34 +04:00
|
|
|
CheckThread();
|
|
|
|
|
2012-10-03 00:04:58 +04:00
|
|
|
SignalPacketReceived(this, data, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // close namespace
|