Skip to content

Commit 34e8973

Browse files
committed
!44 opt(http):优化了Http的Session管理;在TcpServer中添加了detachConnection()方法
* opt(http):优化了Http的Session管理;在TcpServer中添加了detachConnection()方法
1 parent 8d700d0 commit 34e8973

File tree

4 files changed

+21
-25
lines changed

4 files changed

+21
-25
lines changed

modules/http/server/server_imp.cpp

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -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

124113
namespace {
@@ -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
/**

modules/http/server/server_imp.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ class Server::Impl {
6363
private:
6464

6565
void onTcpConnected(const TcpServer::ConnToken &ct);
66-
void onTcpDisconnected(const TcpServer::ConnToken &ct);
6766
void onTcpReceived(const TcpServer::ConnToken &ct, Buffer &buff);
6867
void onTcpSendCompleted(const TcpServer::ConnToken &ct);
6968

@@ -85,7 +84,6 @@ class Server::Impl {
8584

8685
TcpServer tcp_server_;
8786
vector<RequestHandler> req_handler_;
88-
set<Connection*> conns_; //! 仅用于保存Connection指针,用于释放
8987
State state_ = State::kNone;
9088
bool context_log_enable_ = false;
9189

modules/network/tcp_server.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,17 @@ TcpServer::State TcpServer::state() const
225225
return d_->state;
226226
}
227227

228+
TcpConnection* TcpServer::detachConnection(const ConnToken &client)
229+
{
230+
auto conn = d_->conns.free(client);
231+
if (conn != nullptr) {
232+
conn->setReceiveCallback(nullptr, 0);
233+
conn->setDisconnectedCallback(nullptr);
234+
conn->setSendCompleteCallback(nullptr);
235+
}
236+
return conn;
237+
}
238+
228239
void TcpServer::onTcpConnected(TcpConnection *new_conn)
229240
{
230241
RECORD_SCOPE();

modules/network/tcp_server.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ class TcpServer {
9494

9595
State state() const;
9696

97+
//! 分离指定的连接,用于实现WebSocket
98+
TcpConnection* detachConnection(const ConnToken &client);
99+
97100
protected:
98101
void onTcpConnected(TcpConnection *new_conn);
99102
void onTcpDisconnected(const ConnToken &client);

0 commit comments

Comments
 (0)