media/mtransport/transportflow.h
author Brian Smith <bsmith@mozilla.com>
Wed, 05 Dec 2012 15:19:18 -0800
changeset 115146 5d514385c585f71d77e0bcd279e958bfc2d986d6
parent 111621 a39913319ce6520429ea3aa32fe1eac429cb660a
child 126181 da67bb9e16912bdf5c5c6ba965e4679c791ecfd9
permissions -rw-r--r--
Bug 816392: Update to NSS 3.14.1 beta 2 (NSS_3_14_1_BETA2), r=me, a=rrelyea

/* -*- 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>
#include <queue>
#include <string>

#include "nscore.h"
#include "nsISupportsImpl.h"
#include "transportlayer.h"
#include "m_cpp_utils.h"

// A stack of transport layers acts as a flow.
// Generally, one reads and writes to the top layer.
namespace mozilla {

class TransportFlow : public sigslot::has_slots<> {
 public:
  TransportFlow() : id_("(anonymous)") {}
  TransportFlow(const std::string id) : id_(id) {}
  ~TransportFlow();

  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
  //
  // The flow takes ownership of the layers after a successful
  // push.
  nsresult PushLayer(TransportLayer *layer);

  // Convenience function to push multiple layers on. Layers
  // are pushed on in the order that they are in the queue.
  // Any layers which cannot be pushed on are just deleted
  // and an error is returned.
  // TODO(ekr@rtfm.com): Change layers to be ref-counted.
  nsresult PushLayers(std::queue<TransportLayer *> layers);

  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;

  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(TransportFlow)

 private:
  DISALLOW_COPY_ASSIGN(TransportFlow);

  void StateChange(TransportLayer *layer, TransportLayer::State state);
  void PacketReceived(TransportLayer* layer, const unsigned char *data,
      size_t len);

  std::string id_;
  std::deque<TransportLayer *> layers_;
};

}  // close namespace
#endif