/src/mozilla-central/media/mtransport/transportlayer.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 | | /* vim: set ts=2 et sw=2 tw=80: */ |
3 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
4 | | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
5 | | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
6 | | |
7 | | // Original author: ekr@rtfm.com |
8 | | |
9 | | #ifndef transportlayer_h__ |
10 | | #define transportlayer_h__ |
11 | | |
12 | | #include "sigslot.h" |
13 | | |
14 | | #include "mozilla/DebugOnly.h" |
15 | | #include "mozilla/RefPtr.h" |
16 | | #include "nsCOMPtr.h" |
17 | | #include "nsIEventTarget.h" |
18 | | |
19 | | #include "m_cpp_utils.h" |
20 | | #include "mediapacket.h" |
21 | | |
22 | | namespace mozilla { |
23 | | |
24 | | class TransportFlow; |
25 | | |
26 | | typedef int TransportResult; |
27 | | |
28 | | enum { |
29 | | TE_WOULDBLOCK = -1, TE_ERROR = -2, TE_INTERNAL = -3 |
30 | | }; |
31 | | |
32 | | #define TRANSPORT_LAYER_ID(name) \ |
33 | 0 | const std::string id() const override { return name; } \ Unexecuted instantiation: mozilla::TransportLayerDtls::id() const Unexecuted instantiation: mozilla::TransportLayerPacketDumper::id() const Unexecuted instantiation: mozilla::TransportLayerIce::id() const Unexecuted instantiation: mozilla::TransportLayerSrtp::id() const Unexecuted instantiation: mozilla::TransportLayerLogging::id() const Unexecuted instantiation: mozilla::TransportLayerLoopback::id() const Unexecuted instantiation: TransportLayerLossy::id() const Unexecuted instantiation: TransportLayerDummy::id() const |
34 | 0 | static std::string ID() { return name; } Unexecuted instantiation: mozilla::TransportLayerDtls::ID() Unexecuted instantiation: mozilla::TransportLayerIce::ID() Unexecuted instantiation: mozilla::TransportLayerPacketDumper::ID() Unexecuted instantiation: mozilla::TransportLayerSrtp::ID() Unexecuted instantiation: mozilla::TransportLayerLogging::ID() Unexecuted instantiation: mozilla::TransportLayerLoopback::ID() Unexecuted instantiation: TransportLayerDummy::ID() Unexecuted instantiation: TransportLayerLossy::ID() |
35 | | |
36 | | // Abstract base class for network transport layers. |
37 | | class TransportLayer : public sigslot::has_slots<> { |
38 | | public: |
39 | | // The state of the transport flow |
40 | | // We can't use "ERROR" because Windows has a macro named "ERROR" |
41 | | enum State { TS_NONE, TS_INIT, TS_CONNECTING, TS_OPEN, TS_CLOSED, TS_ERROR }; |
42 | | |
43 | | // Is this a stream or datagram flow |
44 | | TransportLayer() : |
45 | | state_(TS_NONE), |
46 | | flow_id_(), |
47 | 0 | downward_(nullptr) {} |
48 | | |
49 | 0 | virtual ~TransportLayer() {} |
50 | | |
51 | | // Called to initialize |
52 | | nsresult Init(); // Called by Insert() to set up -- do not override |
53 | 0 | virtual nsresult InitInternal() { return NS_OK; } // Called by Init |
54 | | |
55 | 0 | void SetFlowId(const std::string& flow_id) {flow_id_ = flow_id;} |
56 | | |
57 | | virtual void Chain(TransportLayer *downward); |
58 | | |
59 | | // Downward interface |
60 | | TransportLayer *downward() { return downward_; } |
61 | | |
62 | | // Get the state |
63 | | State state() const { return state_; } |
64 | | // Must be implemented by derived classes |
65 | | virtual TransportResult SendPacket(MediaPacket& packet) = 0; |
66 | | |
67 | | // Get the thread. |
68 | 0 | const nsCOMPtr<nsIEventTarget> GetThread() const { |
69 | 0 | return target_; |
70 | 0 | } |
71 | | |
72 | | // Event definitions that one can register for |
73 | | // State has changed |
74 | | sigslot::signal2<TransportLayer*, State> SignalStateChange; |
75 | | // Data received on the flow |
76 | | sigslot::signal2<TransportLayer*, MediaPacket&> SignalPacketReceived; |
77 | | |
78 | | // Return the layer id for this layer |
79 | | virtual const std::string id() const = 0; |
80 | | |
81 | | // The id of the flow |
82 | 0 | const std::string& flow_id() const { |
83 | 0 | return flow_id_; |
84 | 0 | } |
85 | | |
86 | | protected: |
87 | 0 | virtual void WasInserted() {} |
88 | | virtual void SetState(State state, const char *file, unsigned line); |
89 | | // Check if we are on the right thread |
90 | 0 | void CheckThread() const { |
91 | 0 | MOZ_ASSERT(CheckThreadInt(), "Wrong thread"); |
92 | 0 | } |
93 | | |
94 | | State state_; |
95 | | std::string flow_id_; |
96 | | TransportLayer *downward_; // The next layer in the stack |
97 | | nsCOMPtr<nsIEventTarget> target_; |
98 | | |
99 | | private: |
100 | | DISALLOW_COPY_ASSIGN(TransportLayer); |
101 | | |
102 | | bool CheckThreadInt() const { |
103 | | bool on; |
104 | | |
105 | | if (!target_) // OK if no thread set. |
106 | | return true; |
107 | | |
108 | | NS_ENSURE_SUCCESS(target_->IsOnCurrentThread(&on), false); |
109 | | NS_ENSURE_TRUE(on, false); |
110 | | |
111 | | return true; |
112 | | } |
113 | | }; |
114 | | |
115 | | #define LAYER_INFO "Flow[" << flow_id() << "(none)" << "]; Layer[" << id() << "]: " |
116 | 0 | #define TL_SET_STATE(x) SetState((x), __FILE__, __LINE__) |
117 | | |
118 | | } // close namespace |
119 | | #endif |