1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/media/mtransport/transportlayer.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,125 @@ 1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* vim: set ts=2 et sw=2 tw=80: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.8 + * You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +// Original author: ekr@rtfm.com 1.11 + 1.12 +#ifndef transportlayer_h__ 1.13 +#define transportlayer_h__ 1.14 + 1.15 +#include "sigslot.h" 1.16 + 1.17 +#include "mozilla/DebugOnly.h" 1.18 +#include "mozilla/RefPtr.h" 1.19 +#include "nsCOMPtr.h" 1.20 +#include "nsIEventTarget.h" 1.21 + 1.22 +#include "m_cpp_utils.h" 1.23 + 1.24 +namespace mozilla { 1.25 + 1.26 +class TransportFlow; 1.27 + 1.28 +typedef int TransportResult; 1.29 + 1.30 +enum { 1.31 + TE_WOULDBLOCK = -1, TE_ERROR = -2, TE_INTERNAL = -3 1.32 +}; 1.33 + 1.34 +#define TRANSPORT_LAYER_ID(name) \ 1.35 + virtual const std::string id() { return name; } \ 1.36 + static std::string ID() { return name; } 1.37 + 1.38 +// Abstract base class for network transport layers. 1.39 +class TransportLayer : public sigslot::has_slots<> { 1.40 + public: 1.41 + // The state of the transport flow 1.42 + // We can't use "ERROR" because Windows has a macro named "ERROR" 1.43 + enum State { TS_NONE, TS_INIT, TS_CONNECTING, TS_OPEN, TS_CLOSED, TS_ERROR }; 1.44 + enum Mode { STREAM, DGRAM }; 1.45 + 1.46 + // Is this a stream or datagram flow 1.47 + TransportLayer(Mode mode = STREAM) : 1.48 + mode_(mode), 1.49 + state_(TS_NONE), 1.50 + flow_id_(), 1.51 + downward_(nullptr) {} 1.52 + 1.53 + virtual ~TransportLayer() {} 1.54 + 1.55 + // Called to initialize 1.56 + nsresult Init(); // Called by Insert() to set up -- do not override 1.57 + virtual nsresult InitInternal() { return NS_OK; } // Called by Init 1.58 + 1.59 + // Called when inserted into a flow 1.60 + virtual void Inserted(TransportFlow *flow, TransportLayer *downward); 1.61 + 1.62 + // Downward interface 1.63 + TransportLayer *downward() { return downward_; } 1.64 + 1.65 + // Dispatch a call onto our thread (or run on the same thread if 1.66 + // thread is not set). This is always synchronous. 1.67 + nsresult RunOnThread(nsIRunnable *event); 1.68 + 1.69 + // Get the state 1.70 + State state() const { return state_; } 1.71 + // Must be implemented by derived classes 1.72 + virtual TransportResult SendPacket(const unsigned char *data, size_t len) = 0; 1.73 + 1.74 + // Get the thread. 1.75 + const nsCOMPtr<nsIEventTarget> GetThread() const { 1.76 + return target_; 1.77 + } 1.78 + 1.79 + // Event definitions that one can register for 1.80 + // State has changed 1.81 + sigslot::signal2<TransportLayer*, State> SignalStateChange; 1.82 + // Data received on the flow 1.83 + sigslot::signal3<TransportLayer*, const unsigned char *, size_t> 1.84 + SignalPacketReceived; 1.85 + 1.86 + // Return the layer id for this layer 1.87 + virtual const std::string id() = 0; 1.88 + 1.89 + // The id of the flow 1.90 + const std::string& flow_id() { 1.91 + return flow_id_; 1.92 + } 1.93 + 1.94 + protected: 1.95 + virtual void WasInserted() {} 1.96 + virtual void SetState(State state, const char *file, unsigned line); 1.97 + // Check if we are on the right thread 1.98 + void CheckThread() { 1.99 + NS_ABORT_IF_FALSE(CheckThreadInt(), "Wrong thread"); 1.100 + } 1.101 + 1.102 + Mode mode_; 1.103 + State state_; 1.104 + std::string flow_id_; 1.105 + TransportLayer *downward_; // The next layer in the stack 1.106 + nsCOMPtr<nsIEventTarget> target_; 1.107 + 1.108 + private: 1.109 + DISALLOW_COPY_ASSIGN(TransportLayer); 1.110 + 1.111 + bool CheckThreadInt() { 1.112 + bool on; 1.113 + 1.114 + if (!target_) // OK if no thread set. 1.115 + return true; 1.116 + 1.117 + NS_ENSURE_SUCCESS(target_->IsOnCurrentThread(&on), false); 1.118 + NS_ENSURE_TRUE(on, false); 1.119 + 1.120 + return true; 1.121 + } 1.122 +}; 1.123 + 1.124 +#define LAYER_INFO "Flow[" << flow_id() << "(none)" << "]; Layer[" << id() << "]: " 1.125 +#define TL_SET_STATE(x) SetState((x), __FILE__, __LINE__) 1.126 + 1.127 +} // close namespace 1.128 +#endif