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 transportlayer_h__
|
|
|
|
#define transportlayer_h__
|
|
|
|
|
|
|
|
#include "sigslot.h"
|
|
|
|
|
2013-01-29 21:01:11 +04:00
|
|
|
#include "mozilla/DebugOnly.h"
|
2015-10-18 08:24:48 +03:00
|
|
|
#include "mozilla/RefPtr.h"
|
2012-10-19 00:01:52 +04:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsIEventTarget.h"
|
2012-10-03 00:04:58 +04:00
|
|
|
|
|
|
|
#include "m_cpp_utils.h"
|
2018-05-10 01:13:35 +03:00
|
|
|
#include "mediapacket.h"
|
2012-10-03 00:04:58 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
class TransportFlow;
|
|
|
|
|
|
|
|
typedef int TransportResult;
|
|
|
|
|
|
|
|
enum { TE_WOULDBLOCK = -1, TE_ERROR = -2, TE_INTERNAL = -3 };
|
|
|
|
|
|
|
|
#define TRANSPORT_LAYER_ID(name) \
|
2017-02-07 01:44:14 +03:00
|
|
|
const std::string id() const override { return name; } \
|
2012-10-03 00:04:58 +04:00
|
|
|
static std::string ID() { return name; }
|
|
|
|
|
|
|
|
// Abstract base class for network transport layers.
|
|
|
|
class TransportLayer : public sigslot::has_slots<> {
|
|
|
|
public:
|
|
|
|
// The state of the transport flow
|
|
|
|
// We can't use "ERROR" because Windows has a macro named "ERROR"
|
|
|
|
enum State { TS_NONE, TS_INIT, TS_CONNECTING, TS_OPEN, TS_CLOSED, TS_ERROR };
|
|
|
|
|
|
|
|
// Is this a stream or datagram flow
|
|
|
|
TransportLayer() : state_(TS_NONE), flow_id_(), downward_(nullptr) {}
|
|
|
|
|
|
|
|
virtual ~TransportLayer() {}
|
|
|
|
|
|
|
|
// Called to initialize
|
|
|
|
nsresult Init(); // Called by Insert() to set up -- do not override
|
|
|
|
virtual nsresult InitInternal() { return NS_OK; } // Called by Init
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
void SetFlowId(const std::string& flow_id) { flow_id_ = flow_id; }
|
2018-04-28 00:57:28 +03:00
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
virtual void Chain(TransportLayer* downward);
|
2012-10-03 00:04:58 +04:00
|
|
|
|
|
|
|
// Downward interface
|
2019-05-01 11:47:10 +03:00
|
|
|
TransportLayer* downward() { return downward_; }
|
2012-10-03 00:04:58 +04:00
|
|
|
|
|
|
|
// Get the state
|
|
|
|
State state() const { return state_; }
|
|
|
|
// Must be implemented by derived classes
|
2019-05-01 11:47:10 +03:00
|
|
|
virtual TransportResult SendPacket(MediaPacket& packet) = 0;
|
2012-10-03 00:04:58 +04:00
|
|
|
|
2013-02-19 15:43:34 +04:00
|
|
|
// Get the thread.
|
|
|
|
const nsCOMPtr<nsIEventTarget> GetThread() const { return target_; }
|
|
|
|
|
2012-10-03 00:04:58 +04:00
|
|
|
// Event definitions that one can register for
|
|
|
|
// State has changed
|
2019-05-01 11:47:10 +03:00
|
|
|
sigslot::signal2<TransportLayer*, State> SignalStateChange;
|
2012-10-03 00:04:58 +04:00
|
|
|
// Data received on the flow
|
2019-05-01 11:47:10 +03:00
|
|
|
sigslot::signal2<TransportLayer*, MediaPacket&> SignalPacketReceived;
|
2012-10-03 00:04:58 +04:00
|
|
|
|
|
|
|
// Return the layer id for this layer
|
2014-08-04 19:49:00 +04:00
|
|
|
virtual const std::string id() const = 0;
|
2012-10-03 00:04:58 +04:00
|
|
|
|
|
|
|
// The id of the flow
|
2019-05-01 11:47:10 +03:00
|
|
|
const std::string& flow_id() const { return flow_id_; }
|
2012-10-03 00:04:58 +04:00
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual void WasInserted() {}
|
2019-05-01 11:47:10 +03:00
|
|
|
virtual void SetState(State state, const char* file, unsigned line);
|
2013-02-19 15:43:34 +04:00
|
|
|
// Check if we are on the right thread
|
2015-02-10 01:34:50 +03:00
|
|
|
void CheckThread() const { MOZ_ASSERT(CheckThreadInt(), "Wrong thread"); }
|
2012-10-19 00:01:52 +04:00
|
|
|
|
2012-10-03 00:04:58 +04:00
|
|
|
State state_;
|
2013-02-19 15:43:34 +04:00
|
|
|
std::string flow_id_;
|
2019-05-01 11:47:10 +03:00
|
|
|
TransportLayer* downward_; // The next layer in the stack
|
2012-10-19 00:01:52 +04:00
|
|
|
nsCOMPtr<nsIEventTarget> target_;
|
2012-10-03 00:04:58 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
DISALLOW_COPY_ASSIGN(TransportLayer);
|
2012-10-19 00:01:52 +04:00
|
|
|
|
2014-08-04 19:49:00 +04:00
|
|
|
bool CheckThreadInt() const {
|
2012-10-19 00:01:52 +04:00
|
|
|
bool on;
|
2013-02-19 15:43:34 +04:00
|
|
|
|
|
|
|
if (!target_) // OK if no thread set.
|
|
|
|
return true;
|
|
|
|
|
2012-10-19 00:01:52 +04:00
|
|
|
NS_ENSURE_SUCCESS(target_->IsOnCurrentThread(&on), false);
|
|
|
|
NS_ENSURE_TRUE(on, false);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2012-10-03 00:04:58 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
#define LAYER_INFO \
|
|
|
|
"Flow[" << flow_id() << "(none)" \
|
|
|
|
<< "]; Layer[" << id() << "]: "
|
2014-01-24 00:21:24 +04:00
|
|
|
#define TL_SET_STATE(x) SetState((x), __FILE__, __LINE__)
|
2012-10-03 00:04:58 +04:00
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
#endif
|