#pragma once #include #include #include #include namespace NEventLoop { struct IEventHandler : public TAtomicRefCount { virtual void HandleEvent(SOCKET socket, void* cookie) = 0; virtual ~IEventHandler() { } }; typedef TIntrusivePtr TEventHandlerPtr; class TEventLoop; // TODO: make TChannel itself a pointer // to avoid confusion with Drop and Unregister class TChannel : public TAtomicRefCount { public: ~TChannel(); void EnableRead(); void DisableRead(); void EnableWrite(); void DisableWrite(); void Unregister(); SOCKET GetSocket() const; TSocket GetSocketPtr() const; private: class TImpl; friend class TEventLoop; TObjectCounter ObjectCounter; TChannel(TImpl*); private: THolder Impl; }; typedef TIntrusivePtr TChannelPtr; class TEventLoop { public: TEventLoop(const char* name = nullptr); ~TEventLoop(); void Run(); void Stop(); bool IsRunning(); TChannelPtr Register(TSocket socket, TEventHandlerPtr, void* cookie = nullptr); private: class TImpl; friend class TChannel; TObjectCounter ObjectCounter; private: THolder Impl; }; }