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