transportlayer.h 3.1 KB

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