rcpp_framework/core/loops/acceptor.h

59 lines
1.3 KiB
C++

/**
*
* Acceptor.h
* An Tao
*
* Public header file in trantor lib.
*
* Copyright 2018, An Tao. All rights reserved.
* Use of this source code is governed by a BSD-style license
* that can be found in the License file.
*
*
*/
#pragma once
#include "channel.h"
#include "core/net/inet_address.h"
#include "core/net/socket.h"
#include "event_loop.h"
#include <functional>
namespace trantor {
using NewConnectionCallback = std::function<void(int fd, const InetAddress &)>;
class Acceptor {
protected:
Acceptor(const Acceptor &) = delete;
Acceptor &operator=(const Acceptor &) = delete;
// some uncopyable classes maybe support move constructor....
Acceptor(Acceptor &&) noexcept(true) = default;
Acceptor &operator=(Acceptor &&) noexcept(true) = default;
public:
Acceptor(EventLoop *loop,
const InetAddress &addr,
bool reUseAddr = true,
bool reUsePort = true);
~Acceptor();
const InetAddress &addr() const {
return addr_;
}
void setNewConnectionCallback(const NewConnectionCallback &cb) {
newConnectionCallback_ = cb;
};
void listen();
protected:
#ifndef _WIN32
int idleFd_;
#endif
Socket sock_;
InetAddress addr_;
EventLoop *loop_;
NewConnectionCallback newConnectionCallback_;
Channel acceptChannel_;
void readCallback();
};
} // namespace trantor