@@ -52,7 +52,6 @@ bool Server::Impl::initialize(const network::SockAddr &bind_addr, int listen_bac
5252 return false ;
5353
5454 tcp_server_.setConnectedCallback (bind (&Impl::onTcpConnected, this , _1));
55- tcp_server_.setDisconnectedCallback (bind (&Impl::onTcpDisconnected, this , _1));
5655 tcp_server_.setReceiveCallback (bind (&Impl::onTcpReceived, this , _1, _2), 0 );
5756 tcp_server_.setSendCompleteCallback (bind (&Impl::onTcpSendCompleted, this , _1));
5857
@@ -85,10 +84,6 @@ void Server::Impl::cleanup()
8584 req_handler_.clear ();
8685 tcp_server_.cleanup ();
8786
88- for (auto conn : conns_)
89- delete conn;
90- conns_.clear ();
91-
9287 state_ = State::kNone ;
9388 }
9489}
@@ -107,18 +102,12 @@ void Server::Impl::onTcpConnected(const TcpServer::ConnToken &ct)
107102{
108103 RECORD_SCOPE ();
109104 auto conn = new Connection;
110- tcp_server_.setContext (ct, conn);
111- conns_.insert (conn);
112- }
113-
114- void Server::Impl::onTcpDisconnected (const TcpServer::ConnToken &ct)
115- {
116- RECORD_SCOPE ();
117- Connection *conn = static_cast <Connection*>(tcp_server_.getContext (ct));
118- TBOX_ASSERT (conn != nullptr );
119-
120- conns_.erase (conn);
121- delete conn;
105+ tcp_server_.setContext (ct, conn,
106+ [](void * ptr) {
107+ auto conn = static_cast <Connection*>(ptr);
108+ CHECK_DELETE_OBJ (conn);
109+ }
110+ );
122111}
123112
124113namespace {
@@ -180,8 +169,6 @@ void Server::Impl::onTcpReceived(const TcpServer::ConnToken &ct, Buffer &buff)
180169 } else if (conn->req_parser .state () == RequestParser::State::kFail ) {
181170 LogNotice (" parse http from %s fail" , tcp_server_.getClientAddress (ct).toString ().c_str ());
182171 tcp_server_.disconnect (ct);
183- conns_.erase (conn);
184- delete conn;
185172 break ;
186173
187174 } else {
@@ -200,11 +187,8 @@ void Server::Impl::onTcpSendCompleted(const TcpServer::ConnToken &ct)
200187 TBOX_ASSERT (conn != nullptr );
201188
202189 // ! 如果最后一个已完成发送,则断开连接
203- if (conn->res_index > conn->close_index ) {
190+ if (conn->res_index > conn->close_index )
204191 tcp_server_.disconnect (ct);
205- conns_.erase (conn);
206- delete conn;
207- }
208192}
209193
210194/* *
0 commit comments