Add parameters specify tcp server bind ip address (#1287)

This commit is contained in:
Kunoi Sayami 2020-11-10 04:46:07 +08:00 committed by GitHub
parent ea8c57fea3
commit a49c8676d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 3 deletions

View File

@ -11,7 +11,10 @@
namespace td {
TcpListener::TcpListener(int port, ActorShared<Callback> callback) : port_(port), callback_(std::move(callback)) {
TcpListener::TcpListener(int port, ActorShared<Callback> callback, Slice server_address) :
port_(port), callback_(std::move(callback)), server_address_(server_address.str()) {
}
void TcpListener::hangup() {
@ -19,7 +22,7 @@ void TcpListener::hangup() {
}
void TcpListener::start_up() {
auto r_socket = ServerSocketFd::open(port_);
auto r_socket = ServerSocketFd::open(port_, server_address_);
if (r_socket.is_error()) {
LOG(ERROR) << "Can't open server socket: " << r_socket.error();
set_timeout_in(5);

View File

@ -20,13 +20,14 @@ class TcpListener final : public Actor {
virtual void accept(SocketFd fd) = 0;
};
TcpListener(int port, ActorShared<Callback> callback);
TcpListener(int port, ActorShared<Callback> callback, Slice server_address = Slice("0.0.0.0"));
void hangup() override;
private:
int port_;
ServerSocketFd server_fd_;
ActorShared<Callback> callback_;
const string server_address_;
void start_up() override;
void tear_down() override;
void loop() override;