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
|
|
|
|
|
|
|
|
#ifndef transportflow_h__
|
|
|
|
#define transportflow_h__
|
|
|
|
|
|
|
|
#include <deque>
|
2012-10-19 00:01:52 +04:00
|
|
|
#include <queue>
|
2012-10-03 00:04:58 +04:00
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include "nscore.h"
|
|
|
|
#include "nsISupportsImpl.h"
|
2013-02-19 15:43:34 +04:00
|
|
|
#include "mozilla/Scoped.h"
|
2012-10-03 00:04:58 +04:00
|
|
|
#include "transportlayer.h"
|
|
|
|
#include "m_cpp_utils.h"
|
2013-09-19 17:54:39 +04:00
|
|
|
#include "nsAutoPtr.h"
|
2012-10-03 00:04:58 +04:00
|
|
|
|
|
|
|
// A stack of transport layers acts as a flow.
|
|
|
|
// Generally, one reads and writes to the top layer.
|
2013-02-19 15:43:34 +04:00
|
|
|
|
|
|
|
// This code has a confusing hybrid threading model which
|
|
|
|
// probably needs some eventual refactoring.
|
|
|
|
// TODO(ekr@rtfm.com): Bug 844891
|
|
|
|
//
|
|
|
|
// TransportFlows are not inherently bound to a thread *but*
|
|
|
|
// TransportLayers can be. If any layer in a flow is bound
|
|
|
|
// to a given thread, then all layers in the flow MUST be
|
|
|
|
// bound to that thread and you can only manipulate the
|
|
|
|
// flow (push layers, write, etc.) on that thread.
|
|
|
|
//
|
|
|
|
// The sole official exception to this is that you are
|
|
|
|
// allowed to *destroy* a flow off the bound thread provided
|
|
|
|
// that there are no listeners on its signals. This exception
|
|
|
|
// is designed to allow idioms where you create the flow
|
|
|
|
// and then something goes wrong and you destroy it and
|
|
|
|
// you don't want to bother with a thread dispatch.
|
|
|
|
//
|
|
|
|
// Eventually we hope to relax the "no listeners"
|
|
|
|
// restriction by thread-locking the signals, but previous
|
|
|
|
// attempts have caused deadlocks.
|
|
|
|
//
|
|
|
|
// Most of these invariants are enforced by hard asserts
|
|
|
|
// (i.e., those which fire even in production builds).
|
|
|
|
|
2012-10-03 00:04:58 +04:00
|
|
|
namespace mozilla {
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class TransportFlow final : public nsISupports,
|
2015-03-27 21:52:19 +03:00
|
|
|
public sigslot::has_slots<> {
|
2012-10-03 00:04:58 +04:00
|
|
|
public:
|
2013-03-04 11:10:30 +04:00
|
|
|
TransportFlow()
|
|
|
|
: id_("(anonymous)"),
|
2013-02-19 15:43:34 +04:00
|
|
|
state_(TransportLayer::TS_NONE),
|
|
|
|
layers_(new std::deque<TransportLayer *>) {}
|
2014-08-16 01:41:29 +04:00
|
|
|
explicit TransportFlow(const std::string id)
|
2013-03-04 11:10:30 +04:00
|
|
|
: id_(id),
|
2013-02-19 15:43:34 +04:00
|
|
|
state_(TransportLayer::TS_NONE),
|
|
|
|
layers_(new std::deque<TransportLayer *>) {}
|
2013-03-04 11:10:30 +04:00
|
|
|
|
2012-10-03 00:04:58 +04:00
|
|
|
const std::string& id() const { return id_; }
|
|
|
|
|
|
|
|
// Layer management. Note PushLayer() is not thread protected, so
|
|
|
|
// either:
|
|
|
|
// (a) Do it in the thread handling the I/O
|
|
|
|
// (b) Do it before you activate the I/O system
|
2012-10-19 00:01:52 +04:00
|
|
|
//
|
|
|
|
// The flow takes ownership of the layers after a successful
|
|
|
|
// push.
|
2012-10-03 00:04:58 +04:00
|
|
|
nsresult PushLayer(TransportLayer *layer);
|
2012-10-19 00:01:52 +04:00
|
|
|
|
|
|
|
// Convenience function to push multiple layers on. Layers
|
|
|
|
// are pushed on in the order that they are in the queue.
|
2013-03-04 11:10:30 +04:00
|
|
|
// Any failures cause the flow to become inoperable and
|
|
|
|
// destroys all the layers including those already pushed.
|
2012-10-19 00:01:52 +04:00
|
|
|
// TODO(ekr@rtfm.com): Change layers to be ref-counted.
|
2013-03-04 11:10:30 +04:00
|
|
|
nsresult PushLayers(nsAutoPtr<std::queue<TransportLayer *> > layers);
|
2012-10-19 00:01:52 +04:00
|
|
|
|
2012-10-03 00:04:58 +04:00
|
|
|
TransportLayer *top() const;
|
|
|
|
TransportLayer *GetLayer(const std::string& id) const;
|
|
|
|
|
|
|
|
// Wrappers for whatever TLayer happens to be the top layer
|
|
|
|
// at the time. This way you don't need to do top()->Foo().
|
|
|
|
TransportLayer::State state(); // Current state
|
|
|
|
TransportResult SendPacket(const unsigned char *data, size_t len);
|
|
|
|
|
|
|
|
// State has changed. Reflects the top flow.
|
|
|
|
sigslot::signal2<TransportFlow *, TransportLayer::State>
|
|
|
|
SignalStateChange;
|
|
|
|
|
|
|
|
// Data received on the flow
|
|
|
|
sigslot::signal3<TransportFlow*, const unsigned char *, size_t>
|
|
|
|
SignalPacketReceived;
|
|
|
|
|
2014-01-15 04:29:42 +04:00
|
|
|
bool Contains(TransportLayer *layer) const;
|
|
|
|
|
2014-03-15 23:00:17 +04:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2012-10-03 00:04:58 +04:00
|
|
|
|
|
|
|
private:
|
2014-07-31 21:05:58 +04:00
|
|
|
~TransportFlow();
|
|
|
|
|
2012-10-03 00:04:58 +04:00
|
|
|
DISALLOW_COPY_ASSIGN(TransportFlow);
|
|
|
|
|
2013-02-19 15:43:34 +04:00
|
|
|
// Check if we are on the right thread
|
|
|
|
void CheckThread() const {
|
|
|
|
if (!CheckThreadInt())
|
|
|
|
MOZ_CRASH();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CheckThreadInt() const {
|
|
|
|
bool on;
|
|
|
|
|
|
|
|
if (!target_) // OK if no thread set.
|
|
|
|
return true;
|
|
|
|
if (NS_FAILED(target_->IsOnCurrentThread(&on)))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return on;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EnsureSameThread(TransportLayer *layer);
|
|
|
|
|
2012-10-03 00:04:58 +04:00
|
|
|
void StateChange(TransportLayer *layer, TransportLayer::State state);
|
2013-03-04 11:10:30 +04:00
|
|
|
void StateChangeInt(TransportLayer::State state);
|
2012-10-03 00:04:58 +04:00
|
|
|
void PacketReceived(TransportLayer* layer, const unsigned char *data,
|
|
|
|
size_t len);
|
2013-02-19 15:43:34 +04:00
|
|
|
static void DestroyFinal(nsAutoPtr<std::deque<TransportLayer *> > layers);
|
|
|
|
|
|
|
|
// Overload needed because we use deque internally and queue externally.
|
|
|
|
static void ClearLayers(std::deque<TransportLayer *>* layers);
|
|
|
|
static void ClearLayers(std::queue<TransportLayer *>* layers);
|
2012-10-03 00:04:58 +04:00
|
|
|
|
|
|
|
std::string id_;
|
2013-03-04 11:10:30 +04:00
|
|
|
TransportLayer::State state_;
|
2013-02-19 15:43:34 +04:00
|
|
|
ScopedDeletePtr<std::deque<TransportLayer *> > layers_;
|
|
|
|
nsCOMPtr<nsIEventTarget> target_;
|
2012-10-03 00:04:58 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
} // close namespace
|
|
|
|
#endif
|