4,356
社区成员
发帖
与我相关
我的任务
分享
#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;
}