multiclient.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #pragma once
  2. #include "neh.h"
  3. namespace NNeh {
  4. /// thread-safe dispacher for processing multiple neh requests
  5. /// (method Wait() MUST be called from single thread, methods Request and Interrupt are thread-safe)
  6. class IMultiClient {
  7. public:
  8. virtual ~IMultiClient() {
  9. }
  10. struct TRequest {
  11. TRequest()
  12. : Deadline(TInstant::Max())
  13. , UserData(nullptr)
  14. {
  15. }
  16. TRequest(const TMessage& msg, TInstant deadline = TInstant::Max(), void* userData = nullptr)
  17. : Msg(msg)
  18. , Deadline(deadline)
  19. , UserData(userData)
  20. {
  21. }
  22. TMessage Msg;
  23. TInstant Deadline;
  24. void* UserData;
  25. };
  26. /// WARNING:
  27. /// Wait(event) called from another thread can return Event
  28. /// for this request before this call return control
  29. virtual THandleRef Request(const TRequest& req) = 0;
  30. virtual size_t QueueSize() = 0;
  31. struct TEvent {
  32. enum TType {
  33. Timeout,
  34. Response,
  35. SizeEventType
  36. };
  37. TEvent()
  38. : Type(SizeEventType)
  39. , UserData(nullptr)
  40. {
  41. }
  42. TEvent(TType t, void* userData)
  43. : Type(t)
  44. , UserData(userData)
  45. {
  46. }
  47. TType Type;
  48. THandleRef Hndl;
  49. void* UserData;
  50. };
  51. /// return false if interrupted
  52. virtual bool Wait(TEvent&, TInstant = TInstant::Max()) = 0;
  53. /// interrupt guaranteed breaking execution Wait(), but few interrupts can be handled as one
  54. virtual void Interrupt() = 0;
  55. };
  56. typedef TAutoPtr<IMultiClient> TMultiClientPtr;
  57. TMultiClientPtr CreateMultiClient();
  58. }