transportlayer.h (3160B)
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 "m_cpp_utils.h" 13 #include "mediapacket.h" 14 #include "nsCOMPtr.h" 15 #include "nsIEventTarget.h" 16 #include "sigslot.h" 17 18 namespace mozilla { 19 20 class TransportFlow; 21 22 typedef int TransportResult; 23 24 enum { TE_WOULDBLOCK = -1, TE_ERROR = -2, TE_INTERNAL = -3 }; 25 26 #define TRANSPORT_LAYER_ID(name) \ 27 const std::string id() const override { return name; } \ 28 static std::string ID() { return name; } 29 30 // Abstract base class for network transport layers. 31 class TransportLayer : public sigslot::has_slots<> { 32 public: 33 // The state of the transport flow 34 // We can't use "ERROR" because Windows has a macro named "ERROR" 35 enum State { TS_NONE, TS_INIT, TS_CONNECTING, TS_OPEN, TS_CLOSED, TS_ERROR }; 36 37 // Is this a stream or datagram flow 38 TransportLayer() : state_(TS_NONE), downward_(nullptr) {} 39 40 virtual ~TransportLayer() = default; 41 42 // Called to initialize 43 nsresult Init(); // Called by Insert() to set up -- do not override 44 virtual nsresult InitInternal() { return NS_OK; } // Called by Init 45 46 void SetFlowId(const std::string& flow_id) { flow_id_ = flow_id; } 47 48 virtual void Chain(TransportLayer* downward); 49 50 // Downward interface 51 TransportLayer* downward() { return downward_; } 52 53 // Get the state 54 State state() const { return state_; } 55 // Must be implemented by derived classes 56 virtual TransportResult SendPacket(MediaPacket& packet) = 0; 57 58 // Get the thread. 59 const nsCOMPtr<nsIEventTarget> GetThread() const { return target_; } 60 61 // Event definitions that one can register for 62 // State has changed 63 sigslot::signal2<TransportLayer*, State> SignalStateChange; 64 // Data received on the flow 65 sigslot::signal2<TransportLayer*, MediaPacket&> SignalPacketReceived; 66 67 // Return the layer id for this layer 68 virtual const std::string id() const = 0; 69 70 // The id of the flow 71 const std::string& flow_id() const { return flow_id_; } 72 73 protected: 74 virtual void WasInserted() {} 75 virtual void SetState(State state, const char* file, unsigned line); 76 // Check if we are on the right thread 77 void CheckThread() const { MOZ_ASSERT(CheckThreadInt(), "Wrong thread"); } 78 79 State state_; 80 std::string flow_id_; 81 TransportLayer* downward_; // The next layer in the stack 82 nsCOMPtr<nsIEventTarget> target_; 83 84 private: 85 DISALLOW_COPY_ASSIGN(TransportLayer); 86 87 bool CheckThreadInt() const { 88 bool on; 89 90 if (!target_) // OK if no thread set. 91 return true; 92 93 NS_ENSURE_SUCCESS(target_->IsOnCurrentThread(&on), false); 94 NS_ENSURE_TRUE(on, false); 95 96 return true; 97 } 98 }; 99 100 #define LAYER_INFO \ 101 "Flow[" << flow_id() << "(none)" \ 102 << "]; Layer[" << id() << "]: " 103 #define TL_SET_STATE(x) SetState((x), __FILE__, __LINE__) 104 105 } // namespace mozilla 106 #endif