123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- /***********************************************
- File Name: acceptor.h
- Author: Abby Cin
- Mail: abbytsing@gmail.com
- Created Time: 10/22/20 8:05 PM
- ***********************************************/
-
- #ifndef _CONET_ACCEPTOR_H_
- #define _CONET_ACCEPTOR_H_
-
- class acceptor
- {
- public:
- acceptor(context& ctx, resolver r) : ctx_{ctx}, fd_{r.fd_} {}
-
- [[nodiscard]] int listen() const { return ::listen(fd_, SOMAXCONN); }
-
- auto accept()
- {
- struct awaiter
- {
- context& ctx_;
- int fd_;
- awaiter(context& ctx, int fd) : ctx_{ctx}, fd_{fd} {}
-
- bool await_ready() { return false; }
- void await_suspend(handle_t co) { ctx_.push(fd_, co); }
- peer await_resume()
- {
- if(!ctx_.running())
- {
- return {ctx_, -1};
- }
- int sock = ::accept4(fd_, nullptr, nullptr, SOCK_NONBLOCK);
- return {ctx_, sock};
- }
- };
- return awaiter{ctx_, fd_};
- }
-
- private:
- context& ctx_;
- int fd_;
- };
- #endif //_CONET_ACCEPTOR_H_
|