大家帮忙看下!

insulted 2009-04-17 12:48:44

void UdpAgent::sendmsg(int nbytes, AppData* data, const char* flags)
{
Packet *p;
int n;
if (size_)
n = nbytes / size_;
else
printf("Error: UDP size = 0\n");
if (nbytes == -1) {
printf("Error: sendmsg() for UDP should not be -1\n");
return;
}
// If they are sending data, then it must fit within a single packet.
if (data && nbytes > size_) {
printf("Error: data greater than maximum UDP packet size\n");
return; ////(0)
}
double local_time = Scheduler::instance().clock();
while (n-- > 0) { ////(1)
p = allocpkt();
hdr_cmn::access(p)->size() = size_;
hdr_rtp* rh = hdr_rtp::access(p);
rh->flags() = 0;
rh->seqno() = ++seqno_;
hdr_cmn::access(p)->timestamp() =
(u_int32_t)(SAMPLERATE*local_time);
// add "beginning of talkspurt" labels (tcl/ex/test-rcvr.tcl)
if (flags && (0 ==strcmp(flags, "NEW_BURST")))
rh->flags() |= RTP_M;
p->setdata(data);
target_->recv(p);
}
n = nbytes % size_;
if (n > 0) { ////(2)
p = allocpkt();
hdr_cmn::access(p)->size() = n;
hdr_rtp* rh = hdr_rtp::access(p);
rh->flags() = 0;
rh->seqno() = ++seqno_;
hdr_cmn::access(p)->timestamp() =
(u_int32_t)(SAMPLERATE*local_time);
// add "beginning of talkspurt" labels (tcl/ex/test-rcvr.tcl)
if (flags && (0 == strcmp(flags, "NEW_BURST")))
rh->flags() |= RTP_M;
p->setdata(data);
target_->recv(p);
}
idle();
}


void UdpAgent::sendmsg(int nbytes, AppData* data, const char* flags)
{
Packet *p;
int n;
if (size_)
n = nbytes / size_;
else
printf("Error: UDP size = 0\n");
if (nbytes == -1) {
printf("Error: sendmsg() for UDP should not be -1\n");
return;
}
// If they are sending data, then it must fit within a single packet.
if (data && nbytes > size_) {
printf("Error: data greater than maximum UDP packet size\n");
return; ////(0)
}
double local_time = Scheduler::instance().clock();
while (n-- > 0) { ////(1)
p = allocpkt();
hdr_cmn::access(p)->size() = size_;
hdr_rtp* rh = hdr_rtp::access(p);
rh->flags() = 0;
rh->seqno() = ++seqno_;
hdr_cmn::access(p)->timestamp() =
(u_int32_t)(SAMPLERATE*local_time);
// add "beginning of talkspurt" labels (tcl/ex/test-rcvr.tcl)
if (flags && (0 ==strcmp(flags, "NEW_BURST")))
rh->flags() |= RTP_M;
p->setdata(data);
target_->recv(p);
}
n = nbytes % size_;
if (n > 0) { ////(2)
p = allocpkt();
hdr_cmn::access(p)->size() = n;
hdr_rtp* rh = hdr_rtp::access(p);
rh->flags() = 0;
rh->seqno() = ++seqno_;
hdr_cmn::access(p)->timestamp() =
(u_int32_t)(SAMPLERATE*local_time);
// add "beginning of talkspurt" labels (tcl/ex/test-rcvr.tcl)
if (flags && (0 == strcmp(flags, "NEW_BURST")))
rh->flags() |= RTP_M;
p->setdata(data);
target_->recv(p);
}
idle();
}



以上是udpagent的源码,但是请大家注意了:
如果不从(0)处出错退出,则(1)处的while循环和(2)处的if,只能选择其中一个执行!
也就是说,没有从(0)处出错退出,则nbytes <= size_,则n = nbytes / size_ ,n值一定在[0,1]的范围内!
那么如果n等于1,则(1)处while循环会执行,并且只会执行一次,仅仅一次!
那么(2)处的 if 就绝对不会执行了,因为新的n值( n = nbytes % size_;)为零了!
另外的一种情况是,n等于0,则(1)处while循环就不会执行了!

所以,我觉得(1)处的while循环没有必要啊!
可以将(1)(2)处改为
if(1)
else(2)
这样的结构不是更简单?
...全文
97 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
jame2001 2009-04-17
  • 打赏
  • 举报
回复
不過, 其實它的目的也就是讓你執行一次 setdata 的動作...
setdata(data);
insulted 2009-04-17
  • 打赏
  • 举报
回复
楼上的代码不小心粘了两遍,呵呵!请大家看这个吧


void UdpAgent::sendmsg(int nbytes, AppData* data, const char* flags)
{
Packet *p;
int n;
if (size_)
n = nbytes / size_;
else
printf("Error: UDP size = 0\n");
if (nbytes == -1) {
printf("Error: sendmsg() for UDP should not be -1\n");
return;
}
// If they are sending data, then it must fit within a single packet.
if (data && nbytes > size_) {
printf("Error: data greater than maximum UDP packet size\n");
return; ////(0)
}
double local_time = Scheduler::instance().clock();
while (n-- > 0) { ////(1)
p = allocpkt();
hdr_cmn::access(p)->size() = size_;
hdr_rtp* rh = hdr_rtp::access(p);
rh->flags() = 0;
rh->seqno() = ++seqno_;
hdr_cmn::access(p)->timestamp() =
(u_int32_t)(SAMPLERATE*local_time);
// add "beginning of talkspurt" labels (tcl/ex/test-rcvr.tcl)
if (flags && (0 ==strcmp(flags, "NEW_BURST")))
rh->flags() |= RTP_M;
p->setdata(data);
target_->recv(p);
}
n = nbytes % size_;
if (n > 0) { ////(2)
p = allocpkt();
hdr_cmn::access(p)->size() = n;
hdr_rtp* rh = hdr_rtp::access(p);
rh->flags() = 0;
rh->seqno() = ++seqno_;
hdr_cmn::access(p)->timestamp() =
(u_int32_t)(SAMPLERATE*local_time);
// add "beginning of talkspurt" labels (tcl/ex/test-rcvr.tcl)
if (flags && (0 == strcmp(flags, "NEW_BURST")))
rh->flags() |= RTP_M;
p->setdata(data);
target_->recv(p);
}
idle();
}



以上是udpagent的源码,但是请大家注意了:
如果不从(0)处出错退出,则(1)处的while循环和(2)处的if,只能选择其中一个执行!
也就是说,没有从(0)处出错退出,则nbytes <= size_,则n = nbytes / size_ ,n值一定在[0,1]的范围内!
那么如果n等于1,则(1)处while循环会执行,并且只会执行一次,仅仅一次!
那么(2)处的 if 就绝对不会执行了,因为新的n值( n = nbytes % size_;)为零了!
另外的一种情况是,n等于0,则(1)处while循环就不会执行了!

所以,我觉得(1)处的while循环没有必要啊!
可以将(1)(2)处改为
if(1)
else(2)
这样的结构不是更简单?

顺便问一句:csdn上问ns2的问题,在哪里啊?谢谢!
jame2001 2009-04-17
  • 打赏
  • 举报
回复
我倒覺得這段的意義, 有點奇怪, 反正下面while()會去切成size_, 為什麼不能nbytes 不能 大於 size_

if (data && nbytes > size_)
{
printf("Error: data greater than maximum UDP packet size\n");
return;
}

65,210

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧