【发布时间】:2020-03-02 12:05:54
【问题描述】:
我有两个具有相似工作流程的函数。一个是来自 NS3 的更改示例,另一个是对生产者节点更改的旋转。
我的制作人
void
ProactiveProducer::SendData(Name dataName)
{
// dataName.append(m_postfix);
// dataName.appendVersion();
if (!m_active)
return;
NS_LOG_FUNCTION_NOARGS();
auto data = make_shared<Data>();
data->setName(dataName);
data->setFreshnessPeriod(::ndn::time::milliseconds(m_freshness.GetMilliSeconds()));
data->setContent(make_shared< ::ndn::Buffer>(m_virtualPayloadSize));
Signature signature;
SignatureInfo signatureInfo(static_cast< ::ndn::tlv::SignatureTypeValue>(255));
if (m_keyLocator.size() > 0) {
signatureInfo.setKeyLocator(m_keyLocator);
}
signature.setInfo(signatureInfo);
signature.setValue(::ndn::makeNonNegativeIntegerBlock(::ndn::tlv::SignatureValue, m_signature));
data->setSignature(signature);
NS_LOG_INFO("node(" << GetNode()->GetId() << ") responding with Data: " << data->getName());
// to create real wire encoding
data->wireEncode();
m_transmittedDatas(data, this, m_face);
m_appLink->onReceiveData(*data);
ScheduleNextPacket();
}
void
ProactiveProducer::ScheduleNextPacket()
{
NS_LOG_DEBUG ("m_sendEvent: " << m_sendEvent.IsRunning());
if (m_firstTime) {
m_sendEvent = Simulator::Schedule(Seconds(0.0), &ProactiveProducer::SendData(m_prefix), this);
m_firstTime = false;
} else if (!m_sendEvent.IsRunning()) {
m_sendEvent = Simulator::Schedule(Seconds(1.0 / m_frequency), &ProactiveProducer::SendData(m_prefix), this);
}
}
NS3 消费者
void
ModConsumer::SendPacket()
{
// if the application isn't running don't do anything
if (!m_active)
return;
NS_LOG_FUNCTION_NOARGS();
// Will be an invalid packet
uint32_t seq = std::numeric_limits<uint32_t>::max(); // invalid
/*
if the integer is positice the loop runs infinitely --> poor code
- when the size of the list goes to 0 it will exit and therefore seq will be max int
- the consumer seems to work based off of packets to be retransmitted.
- removes the first entry in list of packets to be retransmitted
- removes packet from list of retransmissions
- then transmits that?
*/
while (m_retxSeqs.size()) {
seq = *m_retxSeqs.begin();
m_retxSeqs.erase(m_retxSeqs.begin());
break;
}
// will check fail conditions or increment sequence
if (seq == std::numeric_limits<uint32_t>::max()) {
// NS_LOG_DEBUG ("Reached max Sequence: " << seq << " max_seq: " << m_seq);
if (m_seqMax != std::numeric_limits<uint32_t>::max()) {
if (m_seq >= m_seqMax) {
NS_LOG_DEBUG ("maximum sequence number has been requested, m_seq: " << m_seq << " m_seqMax: " << m_seqMax);
return; // we are totally done
}
}
seq = m_seq++;
}
shared_ptr<Name> nameWithSequence = make_shared<Name>(m_interestName);
nameWithSequence->appendSequenceNumber(seq);
shared_ptr<Interest> interest = make_shared<Interest>();
interest->setNonce(m_rand->GetValue(0, std::numeric_limits<uint32_t>::max()));
interest->setName(*nameWithSequence);
interest->setCanBePrefix(false);
time::milliseconds interestLifeTime(m_interestLifeTime.GetMilliSeconds());
interest->setInterestLifetime(interestLifeTime);
interest->setMustBeFresh(true);
// NS_LOG_DEBUG ("Requesting Interest: \n" << *interest);
// NS_LOG_INFO("> Interest for " << seq);
WillSendOutInterest(seq);
m_transmittedInterests(interest, this, m_face);
m_appLink->onReceiveInterest(*interest);
ScheduleNextPacket();
}
// Yes this is a different class, pasting for convenience
void
ModConsumerCbr::ScheduleNextPacket()
{
NS_LOG_DEBUG ("m_sendEvent: " << m_sendEvent.IsRunning());
if (m_firstTime) {
m_sendEvent = Simulator::Schedule(Seconds(0.0), &ModConsumer::SendPacket, this);
m_firstTime = false;
} else if (!m_sendEvent.IsRunning()) {
m_sendEvent = Simulator::Schedule(Seconds(1.0 / m_frequency), &ModConsumer::SendPacket, this);
}
}
当我运行代码时,编译器会为生产者类抛出以下错误
error: cannot take the address of an rvalue of type 'void'
m_sendEvent = Simulator::Schedule(Seconds(0.0), &ProactiveProducer::SendData(m_prefix), this);
我想我明白为什么会抛出错误。我正在传递一个 void 类型的函数。它不应该是可寻址的。所以我无法理解消费者如何正确编译和运行。任何关于我所缺少的建议将不胜感激。我是 C++ 新手。
【问题讨论】:
-
此问题中显示的代码无法满足 stackoverflow.com 对minimal reproducible example 的要求,因此 stackoverflow.com 上的任何人都不太可能确定问题所在。这个问题必须是edited 以显示一个最小示例,不超过一两页代码(“最小”部分),任何人都可以剪切/粘贴、编译、运行和重现所描述的问题(“ reproducible”部分)完全如图所示(这包括任何辅助信息,例如程序的输入)。请参阅How to Ask 了解更多信息。
-
你真的想要返回值的地址(这通常是个坏主意)还是想要传递一个函数指针?
-
如果您查看工作中的消费者代码,您会注意到它没有调用函数。
-
你为什么将
ProactiveProducer::m_prefix传递给ProactiveProducer::SendData?它已经是一个类成员,它可以访问m_prefix。如果您想保留原始签名以供其他用途,可以添加重载void SendData() { return SendData(prefix); }。
标签: c++ networking memory-address void-pointers ns-3