//------------------------------------------------------------------------------------- void Proxy::onGetWitness() { if(cellMailbox()) { // 通知cell获得客户端 Network::Bundle* pBundle = Network::Bundle::ObjPool().createObject(); (*pBundle).newMessage(CellappInterface::onGetWitnessFromBase); (*pBundle) << this->id(); sendToCellapp(pBundle); } }
//------------------------------------------------------------------------------------- void Proxy::giveClientTo(Proxy* proxy) { if(isDestroyed()) { char err[255]; kbe_snprintf(err, 255, "Proxy[%s]::giveClientTo: %d is destroyed.", scriptName(), id()); PyErr_SetString(PyExc_TypeError, err); PyErr_PrintEx(0); onGiveClientToFailure(); return; } if(clientMailbox_ == NULL || clientMailbox_->getChannel() == NULL) { char err[255]; kbe_snprintf(err, 255, "Proxy[%s]::giveClientTo: no has client.", scriptName()); PyErr_SetString(PyExc_TypeError, err); PyErr_PrintEx(0); onGiveClientToFailure(); return; } Network::Channel* lpChannel = clientMailbox_->getChannel(); if(proxy) { if(proxy->isDestroyed()) { char err[255]; kbe_snprintf(err, 255, "Proxy[%s]::giveClientTo: target(%d) is destroyed.", scriptName(), proxy->id()); PyErr_SetString(PyExc_TypeError, err); PyErr_PrintEx(0); onGiveClientToFailure(); return; } if(proxy->id() == this->id()) { char err[255]; kbe_snprintf(err, 255, "Proxy[%s]::giveClientTo: target(%d) is self.", scriptName(), proxy->id()); PyErr_SetString(PyExc_TypeError, err); PyErr_PrintEx(0); onGiveClientToFailure(); return; } EntityMailbox* mb = proxy->clientMailbox(); if(mb != NULL) { ERROR_MSG(fmt::format("Proxy::giveClientTo: {}[{}] give client to {}[{}], {} has clientMailbox.\n", scriptName(), id(), proxy->scriptName(), proxy->id(), proxy->scriptName())); onGiveClientToFailure(); return; } if(cellMailbox()) { // 当前这个entity如果有cell,说明已经绑定了witness, 那么既然我们将控制权 // 交换给了另一个entity, 这个entity需要解绑定witness。 // 通知cell丢失witness Network::Bundle* pBundle = Network::Bundle::ObjPool().createObject(); (*pBundle).newMessage(CellappInterface::onLoseWitness); (*pBundle) << this->id(); sendToCellapp(pBundle); } // 既然客户端失去对其的控制, 那么通知client销毁这个entity Network::Bundle* pBundle = Network::Bundle::ObjPool().createObject(); (*pBundle).newMessage(ClientInterface::onEntityDestroyed); (*pBundle) << this->id(); sendToClient(ClientInterface::onEntityDestroyed, pBundle); // 将控制权交换 entitiesEnabled_ = false; clientMailbox()->addr(Network::Address::NONE); Py_DECREF(clientMailbox()); proxy->setClientType(this->getClientType()); proxy->setClientDatas(this->getClientDatas()); this->setClientType(UNKNOWN_CLIENT_COMPONENT_TYPE); this->setClientDatas(""); clientMailbox(NULL); proxy->onGiveClientTo(lpChannel); addr(Network::Address::NONE); } }
//------------------------------------------------------------------------------------- void Proxy::giveClientTo(Proxy* proxy) { if(isDestroyed()) { char err[255]; kbe_snprintf(err, 255, "Proxy[%s]::giveClientTo: %d is destroyed.", scriptName(), id()); PyErr_SetString(PyExc_TypeError, err); PyErr_PrintEx(0); onGiveClientToFailure(); return; } if(clientMailbox_ == NULL || clientMailbox_->getChannel() == NULL) { char err[255]; kbe_snprintf(err, 255, "Proxy[%s]::giveClientTo: no has client.", scriptName()); PyErr_SetString(PyExc_TypeError, err); PyErr_PrintEx(0); onGiveClientToFailure(); return; } Mercury::Channel* lpChannel = clientMailbox_->getChannel(); if(proxy) { if(proxy->isDestroyed()) { char err[255]; kbe_snprintf(err, 255, "Proxy[%s]::giveClientTo: target(%d) is destroyed.", scriptName(), proxy->id()); PyErr_SetString(PyExc_TypeError, err); PyErr_PrintEx(0); onGiveClientToFailure(); return; } if(proxy->id() == this->id()) { char err[255]; kbe_snprintf(err, 255, "Proxy[%s]::giveClientTo: target(%d) is self.", scriptName(), proxy->id()); PyErr_SetString(PyExc_TypeError, err); PyErr_PrintEx(0); onGiveClientToFailure(); return; } EntityMailbox* mb = proxy->clientMailbox(); if(mb != NULL) { ERROR_MSG(boost::format("Proxy::giveClientTo: %1%[%2%] give client to %3%[%4%], %5% has clientMailbox.\n") % scriptName() % id() % proxy->scriptName() % proxy->id() % proxy->scriptName()); onGiveClientToFailure(); return; } if(cellMailbox()) { // 通知cell丢失客户端 Mercury::Bundle* pBundle = Mercury::Bundle::ObjPool().createObject(); (*pBundle).newMessage(CellappInterface::onResetWitness); (*pBundle) << this->id(); sendToCellapp(pBundle); } entitiesEnabled_ = false; clientMailbox()->addr(Mercury::Address::NONE); Py_DECREF(clientMailbox()); proxy->onGiveClientTo(lpChannel); clientMailbox(NULL); addr(Mercury::Address::NONE); if(proxy->clientMailbox() != NULL) { // 通知client销毁当前entity Mercury::Bundle* pBundle = Mercury::Bundle::ObjPool().createObject(); (*pBundle).newMessage(ClientInterface::onEntityDestroyed); (*pBundle) << this->id(); proxy->sendToClient(ClientInterface::onEntityDestroyed, pBundle); //Mercury::Bundle::ObjPool().reclaimObject(pBundle); } } }