asio.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #include "io_service_impl.h"
  2. #include "deadline_timer_impl.h"
  3. #include "tcp_socket_impl.h"
  4. #include "tcp_acceptor_impl.h"
  5. using namespace NDns;
  6. using namespace NAsio;
  7. namespace NAsio {
  8. TIOService::TWork::TWork(TWork& w)
  9. : Srv_(w.Srv_)
  10. {
  11. Srv_.GetImpl().WorkStarted();
  12. }
  13. TIOService::TWork::TWork(TIOService& srv)
  14. : Srv_(srv)
  15. {
  16. Srv_.GetImpl().WorkStarted();
  17. }
  18. TIOService::TWork::~TWork() {
  19. Srv_.GetImpl().WorkFinished();
  20. }
  21. TIOService::TIOService()
  22. : Impl_(new TImpl())
  23. {
  24. }
  25. TIOService::~TIOService() {
  26. }
  27. void TIOService::Run() {
  28. Impl_->Run();
  29. }
  30. size_t TIOService::GetOpQueueSize() noexcept {
  31. return Impl_->GetOpQueueSize();
  32. }
  33. void TIOService::Post(TCompletionHandler h) {
  34. Impl_->Post(std::move(h));
  35. }
  36. void TIOService::Abort() {
  37. Impl_->Abort();
  38. }
  39. TDeadlineTimer::TDeadlineTimer(TIOService& srv) noexcept
  40. : Srv_(srv)
  41. , Impl_(nullptr)
  42. {
  43. }
  44. TDeadlineTimer::~TDeadlineTimer() {
  45. if (Impl_) {
  46. Srv_.GetImpl().ScheduleOp(new TUnregisterTimerOperation(Impl_));
  47. }
  48. }
  49. void TDeadlineTimer::AsyncWaitExpireAt(TDeadline deadline, THandler h) {
  50. if (!Impl_) {
  51. Impl_ = new TDeadlineTimer::TImpl(Srv_.GetImpl());
  52. Srv_.GetImpl().ScheduleOp(new TRegisterTimerOperation(Impl_));
  53. }
  54. Impl_->AsyncWaitExpireAt(deadline, h);
  55. }
  56. void TDeadlineTimer::Cancel() {
  57. Impl_->Cancel();
  58. }
  59. TTcpSocket::TTcpSocket(TIOService& srv) noexcept
  60. : Srv_(srv)
  61. , Impl_(new TImpl(srv.GetImpl()))
  62. {
  63. }
  64. TTcpSocket::~TTcpSocket() {
  65. }
  66. void TTcpSocket::AsyncConnect(const TEndpoint& ep, TTcpSocket::TConnectHandler h, TDeadline deadline) {
  67. Impl_->AsyncConnect(ep, h, deadline);
  68. }
  69. void TTcpSocket::AsyncWrite(TSendedData& d, TTcpSocket::TWriteHandler h, TDeadline deadline) {
  70. Impl_->AsyncWrite(d, h, deadline);
  71. }
  72. void TTcpSocket::AsyncWrite(TContIOVector* vec, TWriteHandler h, TDeadline deadline) {
  73. Impl_->AsyncWrite(vec, h, deadline);
  74. }
  75. void TTcpSocket::AsyncWrite(const void* data, size_t size, TWriteHandler h, TDeadline deadline) {
  76. class TBuffers: public IBuffers {
  77. public:
  78. TBuffers(const void* theData, size_t theSize)
  79. : Part(theData, theSize)
  80. , IOVec(&Part, 1)
  81. {
  82. }
  83. TContIOVector* GetIOvec() override {
  84. return &IOVec;
  85. }
  86. IOutputStream::TPart Part;
  87. TContIOVector IOVec;
  88. };
  89. TSendedData d(new TBuffers(data, size));
  90. Impl_->AsyncWrite(d, h, deadline);
  91. }
  92. void TTcpSocket::AsyncRead(void* buff, size_t size, TTcpSocket::TReadHandler h, TDeadline deadline) {
  93. Impl_->AsyncRead(buff, size, h, deadline);
  94. }
  95. void TTcpSocket::AsyncReadSome(void* buff, size_t size, TTcpSocket::TReadHandler h, TDeadline deadline) {
  96. Impl_->AsyncReadSome(buff, size, h, deadline);
  97. }
  98. void TTcpSocket::AsyncPollRead(TTcpSocket::TPollHandler h, TDeadline deadline) {
  99. Impl_->AsyncPollRead(h, deadline);
  100. }
  101. void TTcpSocket::AsyncPollWrite(TTcpSocket::TPollHandler h, TDeadline deadline) {
  102. Impl_->AsyncPollWrite(h, deadline);
  103. }
  104. void TTcpSocket::AsyncCancel() {
  105. return Impl_->AsyncCancel();
  106. }
  107. size_t TTcpSocket::WriteSome(TContIOVector& d, TErrorCode& ec) noexcept {
  108. return Impl_->WriteSome(d, ec);
  109. }
  110. size_t TTcpSocket::WriteSome(const void* buff, size_t size, TErrorCode& ec) noexcept {
  111. return Impl_->WriteSome(buff, size, ec);
  112. }
  113. size_t TTcpSocket::ReadSome(void* buff, size_t size, TErrorCode& ec) noexcept {
  114. return Impl_->ReadSome(buff, size, ec);
  115. }
  116. bool TTcpSocket::IsOpen() const noexcept {
  117. return Native() != INVALID_SOCKET;
  118. }
  119. void TTcpSocket::Shutdown(TShutdownMode what, TErrorCode& ec) {
  120. return Impl_->Shutdown(what, ec);
  121. }
  122. SOCKET TTcpSocket::Native() const noexcept {
  123. return Impl_->Fd();
  124. }
  125. TEndpoint TTcpSocket::RemoteEndpoint() const {
  126. return Impl_->RemoteEndpoint();
  127. }
  128. //////////////////////////////////
  129. TTcpAcceptor::TTcpAcceptor(TIOService& srv) noexcept
  130. : Srv_(srv)
  131. , Impl_(new TImpl(srv.GetImpl()))
  132. {
  133. }
  134. TTcpAcceptor::~TTcpAcceptor() {
  135. }
  136. void TTcpAcceptor::Bind(TEndpoint& ep, TErrorCode& ec) noexcept {
  137. return Impl_->Bind(ep, ec);
  138. }
  139. void TTcpAcceptor::Listen(int backlog, TErrorCode& ec) noexcept {
  140. return Impl_->Listen(backlog, ec);
  141. }
  142. void TTcpAcceptor::AsyncAccept(TTcpSocket& s, TTcpAcceptor::TAcceptHandler h, TDeadline deadline) {
  143. return Impl_->AsyncAccept(s, h, deadline);
  144. }
  145. void TTcpAcceptor::AsyncCancel() {
  146. Impl_->AsyncCancel();
  147. }
  148. }