media/mtransport/transportlayer.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial