poller.cpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #include "poller.h"
  2. #include "pollerimpl.h"
  3. #include <util/memory/tempbuf.h>
  4. namespace {
  5. struct TMutexLocking {
  6. using TMyMutex = TMutex;
  7. };
  8. } // namespace
  9. class TSocketPoller::TImpl: public TPollerImpl<TMutexLocking> {
  10. public:
  11. inline size_t DoWaitReal(void** ev, TEvent* events, size_t len, const TInstant& deadLine) {
  12. const size_t ret = WaitD(events, len, deadLine);
  13. for (size_t i = 0; i < ret; ++i) {
  14. ev[i] = ExtractEvent(&events[i]);
  15. }
  16. return ret;
  17. }
  18. inline size_t DoWait(void** ev, size_t len, const TInstant& deadLine) {
  19. if (len == 1) {
  20. TEvent tmp;
  21. return DoWaitReal(ev, &tmp, 1, deadLine);
  22. } else {
  23. TTempArray<TEvent> tmpEvents(len);
  24. return DoWaitReal(ev, tmpEvents.Data(), len, deadLine);
  25. }
  26. }
  27. };
  28. TSocketPoller::TSocketPoller()
  29. : Impl_(new TImpl())
  30. {
  31. }
  32. TSocketPoller::~TSocketPoller() = default;
  33. void TSocketPoller::WaitRead(SOCKET sock, void* cookie) {
  34. Impl_->Set(cookie, sock, CONT_POLL_READ);
  35. }
  36. void TSocketPoller::WaitWrite(SOCKET sock, void* cookie) {
  37. Impl_->Set(cookie, sock, CONT_POLL_WRITE);
  38. }
  39. void TSocketPoller::WaitReadWrite(SOCKET sock, void* cookie) {
  40. Impl_->Set(cookie, sock, CONT_POLL_READ | CONT_POLL_WRITE);
  41. }
  42. void TSocketPoller::WaitRdhup(SOCKET sock, void* cookie) {
  43. Impl_->Set(cookie, sock, CONT_POLL_RDHUP);
  44. }
  45. void TSocketPoller::WaitReadOneShot(SOCKET sock, void* cookie) {
  46. Impl_->Set(cookie, sock, CONT_POLL_READ | CONT_POLL_ONE_SHOT);
  47. }
  48. void TSocketPoller::WaitWriteOneShot(SOCKET sock, void* cookie) {
  49. Impl_->Set(cookie, sock, CONT_POLL_WRITE | CONT_POLL_ONE_SHOT);
  50. }
  51. void TSocketPoller::WaitReadWriteOneShot(SOCKET sock, void* cookie) {
  52. Impl_->Set(cookie, sock, CONT_POLL_READ | CONT_POLL_WRITE | CONT_POLL_ONE_SHOT);
  53. }
  54. void TSocketPoller::WaitReadWriteEdgeTriggered(SOCKET sock, void* cookie) {
  55. Impl_->Set(cookie, sock, CONT_POLL_READ | CONT_POLL_WRITE | CONT_POLL_EDGE_TRIGGERED);
  56. }
  57. void TSocketPoller::RestartReadWriteEdgeTriggered(SOCKET sock, void* cookie, bool empty) {
  58. Impl_->Set(cookie, sock, CONT_POLL_READ | CONT_POLL_WRITE | CONT_POLL_MODIFY | CONT_POLL_EDGE_TRIGGERED | (empty ? CONT_POLL_BACKLOG_EMPTY : 0));
  59. }
  60. void TSocketPoller::Unwait(SOCKET sock) {
  61. Impl_->Remove(sock);
  62. }
  63. size_t TSocketPoller::WaitD(void** ev, size_t len, const TInstant& deadLine) {
  64. return Impl_->DoWait(ev, len, deadLine);
  65. }