diff --git a/src/serverchan.cpp b/src/serverchan.cpp index 65a3677..78d86db 100644 --- a/src/serverchan.cpp +++ b/src/serverchan.cpp @@ -30,7 +30,9 @@ ServerChan::ServerChan(const std::shared_ptr &conn, ,state(Creating) {} -ServerChan::~ServerChan() {} +ServerChan::~ServerChan() { + assert(state==Destroy); +} ServerChannelControl::ServerChannelControl(const std::shared_ptr &conn, const std::shared_ptr& channel) :server(conn->iface->server->internal_self) @@ -110,28 +112,27 @@ void ServerChannel_shutdown(const std::shared_ptr& chan) if(chan->state==ServerChan::Destroy) return; - auto conn = chan->conn.lock(); - if(!conn) - return; - chan->state = ServerChan::Destroy; - conn->chanBySID.erase(chan->sid); + if(auto conn = chan->conn.lock()) { - for(auto& pair : chan->opByIOID) { - auto op = pair.second; - if(op->state==ServerOp::Dead) - continue; + conn->chanBySID.erase(chan->sid); - if(op->state==ServerOp::Executing && op->onCancel) - op->onCancel(); + for(auto& pair : chan->opByIOID) { + auto op = pair.second; + if(op->state==ServerOp::Dead) + continue; - op->state = ServerOp::Dead; + if(op->state==ServerOp::Executing && op->onCancel) + op->onCancel(); - if(op->onClose) - op->onClose(""); + op->state = ServerOp::Dead; - conn->opByIOID.erase(op->ioid); + if(op->onClose) + op->onClose(""); + + conn->opByIOID.erase(op->ioid); + } } chan->opByIOID.clear(); @@ -152,20 +153,16 @@ void ServerChannelControl::close() if(!ch) return; auto conn = ch->conn.lock(); - if(conn) { - if(ch->state==ServerChan::Active) { - // Send unsolicited Channel Destroy + if(conn && ch->state==ServerChan::Active) { + // Send unsolicited Channel Destroy - auto tx = bufferevent_get_output(conn->bev.get()); - EvOutBuf R(hostBE, tx); - to_wire(R, Header{CMD_DESTROY_CHANNEL, pva_flags::Server, 8}); - to_wire(R, ch->sid); - to_wire(R, ch->cid); - - ServerChannel_shutdown(ch); - } - ch->state = ServerChan::Destroy; + auto tx = bufferevent_get_output(conn->bev.get()); + EvOutBuf R(hostBE, tx); + to_wire(R, Header{CMD_DESTROY_CHANNEL, pva_flags::Server, 8}); + to_wire(R, ch->sid); + to_wire(R, ch->cid); } + ServerChannel_shutdown(ch); }); } diff --git a/src/serverconn.cpp b/src/serverconn.cpp index e8465b6..e675d26 100644 --- a/src/serverconn.cpp +++ b/src/serverconn.cpp @@ -279,21 +279,16 @@ void ServerConn::cleanup() { log_debug_printf(connsetup, "Client %s Cleanup TCP Connection\n", peerName.c_str()); - auto it = iface->server->connections.find(this); - if(it!=iface->server->connections.end()) { - auto self = std::move(it->second); - iface->server->connections.erase(it); + iface->server->connections.erase(this); - for(auto& pair : self->opByIOID) { - if(pair.second->onClose) - pair.second->onClose(""); - } - for(auto& pair : self->chanBySID) { - if(pair.second->onClose) - pair.second->onClose(""); - } - - // delete this + for(auto& pair : opByIOID) { + if(pair.second->onClose) + pair.second->onClose(""); + } + for(auto& pair : chanBySID) { + pair.second->state = ServerChan::Destroy; + if(pair.second->onClose) + pair.second->onClose(""); } }