handler.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #pragma once
  2. #include "defs.h"
  3. #include "message.h"
  4. #include "message_status.h"
  5. #include "use_after_free_checker.h"
  6. #include "use_count_checker.h"
  7. #include <util/generic/noncopyable.h>
  8. namespace NBus {
  9. /////////////////////////////////////////////////////////////////
  10. /// \brief Interface to message bus handler
  11. struct IBusErrorHandler {
  12. friend struct ::NBus::NPrivate::TBusSessionImpl;
  13. private:
  14. TUseAfterFreeChecker UseAfterFreeChecker;
  15. TUseCountChecker UseCountChecker;
  16. public:
  17. /// called when message or reply can't be delivered
  18. virtual void OnError(TAutoPtr<TBusMessage> pMessage, EMessageStatus status);
  19. virtual ~IBusErrorHandler() {
  20. }
  21. };
  22. class TClientConnectionEvent {
  23. public:
  24. enum EType {
  25. CONNECTED,
  26. DISCONNECTED,
  27. };
  28. private:
  29. EType Type;
  30. ui64 Id;
  31. TNetAddr Addr;
  32. public:
  33. TClientConnectionEvent(EType type, ui64 id, TNetAddr addr)
  34. : Type(type)
  35. , Id(id)
  36. , Addr(addr)
  37. {
  38. }
  39. EType GetType() const {
  40. return Type;
  41. }
  42. ui64 GetId() const {
  43. return Id;
  44. }
  45. TNetAddr GetAddr() const {
  46. return Addr;
  47. }
  48. };
  49. class TOnMessageContext : TNonCopyable {
  50. private:
  51. THolder<TBusMessage> Message;
  52. TBusIdentity Ident;
  53. // TODO: we don't need to store session, we have connection in ident
  54. TBusServerSession* Session;
  55. public:
  56. TOnMessageContext()
  57. : Session()
  58. {
  59. }
  60. TOnMessageContext(TAutoPtr<TBusMessage> message, TBusIdentity& ident, TBusServerSession* session)
  61. : Message(message)
  62. , Session(session)
  63. {
  64. Ident.Swap(ident);
  65. }
  66. bool IsInWork() const {
  67. return Ident.IsInWork();
  68. }
  69. bool operator!() const {
  70. return !IsInWork();
  71. }
  72. TBusMessage* GetMessage() {
  73. return Message.Get();
  74. }
  75. TBusMessage* ReleaseMessage() {
  76. return Message.Release();
  77. }
  78. TBusServerSession* GetSession() {
  79. return Session;
  80. }
  81. template <typename U /* <: TBusMessage */>
  82. EMessageStatus SendReplyAutoPtr(TAutoPtr<U>& rep);
  83. EMessageStatus SendReplyMove(TBusMessageAutoPtr response);
  84. void AckMessage(TBusIdentity& ident);
  85. void ForgetRequest();
  86. void Swap(TOnMessageContext& that) {
  87. DoSwap(Message, that.Message);
  88. Ident.Swap(that.Ident);
  89. DoSwap(Session, that.Session);
  90. }
  91. TNetAddr GetPeerAddrNetAddr() const;
  92. bool IsConnectionAlive() const;
  93. };
  94. struct IBusServerHandler: public IBusErrorHandler {
  95. virtual void OnMessage(TOnMessageContext& onMessage) = 0;
  96. /// called when reply has been sent from destination
  97. virtual void OnSent(TAutoPtr<TBusMessage> pMessage);
  98. };
  99. struct IBusClientHandler: public IBusErrorHandler {
  100. /// called on source when reply arrives from destination
  101. virtual void OnReply(TAutoPtr<TBusMessage> pMessage, TAutoPtr<TBusMessage> pReply) = 0;
  102. /// called when client side message has gone into wire, place to call AckMessage()
  103. virtual void OnMessageSent(TBusMessage* pMessage);
  104. virtual void OnMessageSentOneWay(TAutoPtr<TBusMessage> pMessage);
  105. virtual void OnClientConnectionEvent(const TClientConnectionEvent&);
  106. };
  107. }