我正在使用 INET 框架在 Omnet++ 中模拟网络,我收到以下错误:
check_and_cast(): 无法将 (inet::Indication*)closed 转换为类型 'inet::Packet *' -- 在模块 > (inet::DFNodeUDP) UDPnetworksim.DF2.app[0] (id=232) 中,在 t=300s,事件#
这是我处理 check_and_cast() 的代码部分:
void DFNodeUDP::handleMessageWhenUp(cMessage *msg)
{
if (msg->getKind() == msg_kind::STOP_UDP) {
socket.destroy();
UdpControl::getInstance().decrementNumSockets();
if (selfMsg->isSelfMessage()) {
cancelEvent(selfMsg);
}
delete msg;
msg = nullptr;
return;
}
if (msg->getKind() == msg_kind::RESTART_UDP) {
socket.setOutputGate(gate("socketOut"));
int localPort = par("localPort");
const char *localAddress = par("localAddress");
socket.bind(*localAddress ? L3AddressResolver().resolve(localAddress) : L3Address(), localPort);
MulticastGroupList mgl = getModuleFromPar<IInterfaceTable>(par("interfaceTableModule"), this)->collectMulticastGroups();
socket.joinLocalMulticastGroups(mgl);
socket.setCallback(this);
UdpControl::getInstance().addSocket(getParentModule()->getName(), socket.getSocketId());
selfMsg = new cMessage("restart", START);
scheduleAt(simTime(), selfMsg);
delete msg;
msg = nullptr;
return;
}
if (ExperimentControlUDP::getInstance().getState() == 1) {
if (msg->getKind() == msg_kind::INIT_TIMER) {
// Schedule next INIT_TIMER
scheduleAt(simTime() + frequency, msg);
// Schedule message to be finally sent after propagation delay
EV_INFO << "delayedMsgSend " << getParentModule()->getName() << endl;
delayedMsgSend();
} else if (msg->getKind() == msg_kind::APP_SELF_MSG) {
delete msg;
msg = nullptr;
EV_INFO << "finalMsgSend " << getParentModule()->getName() << endl;
finalMsgSendRouter();
} else if (find(destAddressStr.begin(), destAddressStr.end(), msg->getSenderModule()->getParentModule()->getName()) != destAddressStr.end()) {
string destination = ExperimentControlUDP::getInstance().getMasterRoute(msg->getKind())[1];
destination = "UDPnetworksim." + destination + ".app[0]";
cModule *targetModule = getModuleByPath(destination.c_str());
sendDirect(msg, targetModule, "appIn");
} else if (msg->getKind() == msg_kind::APP_MSG_SENT) {
// Packet *packet = check_and_cast<Packet *>(msg);
// saveData(packet);
delete msg;
} else {
EV_INFO << "msg->getKind() == " << msg->getKind() << endl;
delete msg;
msg = nullptr;
}
} else if (msg->isSelfMessage()) {
if (msg->getKind() == msg_kind::INIT_TIMER) {
delete msg;
} else if (msg->getKind() == msg_kind::DELAY) {
delete msg;
Packet *packet = new Packet("to SN");
if(dontFragment)
packet->addTagIfAbsent<FragmentationReq>()->setDontFragment(true);
const auto& payload = makeShared<ApplicationPacket>();
payload->setChunkLength(B(par("messageLength")));;
payload->addTag<CreationTimeTag>()->setCreationTime(simTime());
packet->insertAtBack(payload);
L3Address destAddr = chooseDestAddr(-1, ExperimentControlUDP::getInstance().IDmap[id]);
socket.sendTo(packet, destAddr, 2000);
} else {
ASSERT(msg == selfMsg);
switch (selfMsg->getKind()) {
case START:
processStart();
break;
case SEND:
processSend();
break;
case STOP:
processStop();
break;
default:
throw cRuntimeError("Invalid kind %d in self message", (int)selfMsg->getKind());
}
}
} else {
ASSERT(msg->getKind() != msg_kind::DELAY);
if (msg->getKind() == L4_msg::UPSTREAM) {
Packet *pk = check_and_cast<Packet *>(msg);
saveData(pk);
} else {
socket.processMessage(msg);
}
}
}
同样,这几乎发生在模拟结束时,之前一切运行顺利。
看来你用的是INET。您的错误可能是由于不正确地使用 INET 中的方法而产生的。要查找导致您的
omnetpp.ini
集合出错的指令:
debug-on-errors = true
然后在调试模式下开始模拟。模拟的执行将在导致错误的指令之前停止。
参考:运行仿真 - 调试
请问这个问题是怎么解决的?