You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

acceptor.h 1019B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /***********************************************
  2. File Name: acceptor.h
  3. Author: Abby Cin
  4. Mail: abbytsing@gmail.com
  5. Created Time: 10/22/20 8:05 PM
  6. ***********************************************/
  7. #ifndef _CONET_ACCEPTOR_H_
  8. #define _CONET_ACCEPTOR_H_
  9. class acceptor
  10. {
  11. public:
  12. acceptor(context& ctx, resolver r) : ctx_{ctx}, fd_{r.fd_} {}
  13. [[nodiscard]] int listen() const { return ::listen(fd_, SOMAXCONN); }
  14. auto accept()
  15. {
  16. struct awaiter
  17. {
  18. context& ctx_;
  19. int fd_;
  20. awaiter(context& ctx, int fd) : ctx_{ctx}, fd_{fd} {}
  21. bool await_ready() { return false; }
  22. void await_suspend(handle_t co) { ctx_.push(fd_, co); }
  23. peer await_resume()
  24. {
  25. if(!ctx_.running())
  26. {
  27. return {ctx_, -1};
  28. }
  29. int sock = ::accept4(fd_, nullptr, nullptr, SOCK_NONBLOCK);
  30. return {ctx_, sock};
  31. }
  32. };
  33. return awaiter{ctx_, fd_};
  34. }
  35. private:
  36. context& ctx_;
  37. int fd_;
  38. };
  39. #endif //_CONET_ACCEPTOR_H_