用libuv写的广播发送端

Mr_guoc 2017-03-01 04:39:58

#include "include/uv.h"
#include <stdio.h>
#include <stdlib.h>
#include "SpeedCounter.hpp"
#include <string.h>
#include <iostream>
#include <time.h>
#include <unistd.h>
using namespace std;

static uv_udp_t client;
static uv_loop_t *loop;
static int a = 0;
static int b = 0;
SpeedCounter speed1;
SpeedCounter speed2;
int len = 0;

struct tagSend
{
uv_udp_send_t req;
uv_buf_t *buf;
char *msg;
};

static void close_cb(uv_handle_t* handle)
{
uv_is_closing(handle);
}

static void cl_send_cb(uv_udp_send_t* req, int status)
{
tagSend *pstSend = (tagSend *)req->data;
printf("in the send_cb\n");
b++;
if(b % 10000 ==0)
{
printf("b = %d, b tps : %d\n", b, speed2.GetSpeedInSecond(b));
}
delete []pstSend->msg;
delete pstSend->buf;
delete pstSend;
}

void thread_run(void *arg)
{
printf("front \n");
//下面的设置没有用,不起作用
//int r = uv_loop_configure((uv_loop_t *)arg, UV_LOOP_BLOCK_SIGNAL);
//if(r != 0)
// printf("uv_loop_configure failed\n");
while(1)
{
uv_run((uv_loop_t *)arg, UV_RUN_DEFAULT);
}
printf("behind \n");
}

int main(void)
{
struct sockaddr_in addr;
struct sockaddr_in send_addr;
int r;
uv_thread_t tid;

loop = uv_default_loop();
r = uv_ip4_addr("192.168.80.224", 5566, &addr);
if(r != 0 )
printf("init addr failed\n");
r = uv_udp_init(loop, &client);
if(r != 0)
printf("init client failed\n");
r = uv_udp_bind(&client, (const struct sockaddr*) &addr, 4);
if(r != 0)
printf("uv_udp_bind failed\n");
r = uv_udp_set_broadcast(&client, 1);
if(r != 0 )
printf("set broadcast failed\n");
r = uv_ip4_addr("192.168.80.255", 9050, &send_addr);
if(r != 0)
printf("init send_addr failed\n");
r = uv_thread_create(&tid, thread_run, loop);
if(r != 0)
printf("create thread failed\n");

printf("please cin the length\n");
cin>>len;
for(int i = 0;i<20;i++)
//while(1)
{

//if(client.send_queue_count < 20)
{
tagSend *pstSend = new tagSend;
pstSend->buf = new uv_buf_t;
pstSend->msg = new char[len+1];
memset(pstSend->msg, 'Q', len);
*pstSend->buf = uv_buf_init(pstSend->msg,len);
pstSend->req.data = pstSend;
printf("before send\n");
r = uv_udp_send(&pstSend->req, &client, pstSend->buf, 1, (const struct sockaddr*) &send_addr, cl_send_cb);
if(r != 0)
{
printf("send failed\n");
return -1;
}
printf("after send, message :%s\n", pstSend->msg);
a++;
if(a %10000 == 0)
{
printf("a = %d, a tps :%d\n",a, speed1.GetSpeedInSecond(a));
}
}
//else
{
// sleep(1);
cout<<"deep "<<client.send_queue_count<<endl;
}
}
return 0;
}

...全文
248 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
JoeBlackzqq 2018-12-19
  • 打赏
  • 举报
回复
引用 4 楼 JoeBlackzqq 的回复:
然后在主进程中调用uv_close(&server, closecb),


是主线程!
JoeBlackzqq 2018-12-19
  • 打赏
  • 举报
回复
然后在主进程中调用uv_close(&server, closecb),
JoeBlackzqq 2018-12-19
  • 打赏
  • 举报
回复
引用 2 楼 Mr_guoc 的回复:
找到原因了,uv_run放到线程里面,要用异步通知的方式去发送数据,uv_async_send,这样uv_run循环才有效,再深一步的原因只能去看源码了


我将uv_run放在一个子线程中去执行,在子线程中启动了一个tcpserver,希望主进程中调用uv_close(&server, closecb), 然后我发现子线程并没有进入closecb,导致uv_run这个事件循环无法结束了!

请问你知道 原因吗?
Mr_guoc 2017-03-01
  • 打赏
  • 举报
回复
找到原因了,uv_run放到线程里面,要用异步通知的方式去发送数据,uv_async_send,这样uv_run循环才有效,再深一步的原因只能去看源码了
Mr_guoc 2017-03-01
  • 打赏
  • 举报
回复
用libuv库写了一个广播发送端,现在问题是只发送两次消息,然后就不发送了,不知道哪里有问题,用过libuv的大神麻烦来看一下吧,感激不尽

4,356

社区成员

发帖
与我相关
我的任务
社区描述
通信技术相关讨论
社区管理员
  • 网络通信
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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