event_loop.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #pragma once
  2. #include <util/generic/object_counter.h>
  3. #include <util/generic/ptr.h>
  4. #include <util/network/init.h>
  5. #include <util/network/socket.h>
  6. namespace NEventLoop {
  7. struct IEventHandler
  8. : public TAtomicRefCount<IEventHandler> {
  9. virtual void HandleEvent(SOCKET socket, void* cookie) = 0;
  10. virtual ~IEventHandler() {
  11. }
  12. };
  13. typedef TIntrusivePtr<IEventHandler> TEventHandlerPtr;
  14. class TEventLoop;
  15. // TODO: make TChannel itself a pointer
  16. // to avoid confusion with Drop and Unregister
  17. class TChannel
  18. : public TAtomicRefCount<TChannel> {
  19. public:
  20. ~TChannel();
  21. void EnableRead();
  22. void DisableRead();
  23. void EnableWrite();
  24. void DisableWrite();
  25. void Unregister();
  26. SOCKET GetSocket() const;
  27. TSocket GetSocketPtr() const;
  28. private:
  29. class TImpl;
  30. friend class TEventLoop;
  31. TObjectCounter<TChannel> ObjectCounter;
  32. TChannel(TImpl*);
  33. private:
  34. THolder<TImpl> Impl;
  35. };
  36. typedef TIntrusivePtr<TChannel> TChannelPtr;
  37. class TEventLoop {
  38. public:
  39. TEventLoop(const char* name = nullptr);
  40. ~TEventLoop();
  41. void Run();
  42. void Stop();
  43. bool IsRunning();
  44. TChannelPtr Register(TSocket socket, TEventHandlerPtr, void* cookie = nullptr);
  45. private:
  46. class TImpl;
  47. friend class TChannel;
  48. TObjectCounter<TEventLoop> ObjectCounter;
  49. private:
  50. THolder<TImpl> Impl;
  51. };
  52. }