Skip to content

Commit

Permalink
修复服务器启动途中收到连接请求导致的bug
Browse files Browse the repository at this point in the history
  • Loading branch information
xia-chu committed Sep 28, 2023
1 parent b11582c commit a883b5e
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Network/TcpServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ void TcpServer::cloneFrom(const TcpServer &that) {
_main_server = false;
_on_create_socket = that._on_create_socket;
_session_alloc = that._session_alloc;
_socket->cloneSocket(*(that._socket));
weak_ptr<TcpServer> weak_self = std::static_pointer_cast<TcpServer>(shared_from_this());
_timer = std::make_shared<Timer>(2.0f, [weak_self]() -> bool {
auto strong_self = weak_self.lock();
Expand All @@ -105,6 +104,7 @@ void TcpServer::cloneFrom(const TcpServer &that) {
}, _poller);
this->mINI::operator=(that);
_parent = static_pointer_cast<TcpServer>(const_cast<TcpServer &>(that).shared_from_this());
_socket->cloneSocket(*(that._socket));
}

// 接收到客户端连接请求
Expand Down
6 changes: 3 additions & 3 deletions src/Network/UdpServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,21 +117,21 @@ void UdpServer::cloneFrom(const UdpServer &that) {
throw std::invalid_argument("UdpServer::cloneFrom other with null socket");
}
setupEvent();
_cloned = true;
// clone callbacks
_on_create_socket = that._on_create_socket;
_session_alloc = that._session_alloc;
_session_mutex = that._session_mutex;
_session_map = that._session_map;
// clone properties
this->mINI::operator=(that);
// clone udp socket
#if 0
_socket->cloneSocket(*(that._socket));
#else
// 实验发现cloneSocket方式虽然可以节省fd资源,但是在某些系统上线程漂移问题更严重
_socket->bindUdpSock(that._socket->get_local_port(), that._socket->get_local_ip());
#endif
// clone properties
this->mINI::operator=(that);
_cloned = true;
}

void UdpServer::onRead(const Buffer::Ptr &buf, sockaddr *addr, int addr_len) {
Expand Down

0 comments on commit a883b5e

Please sign in to comment.