8 #define SERVER_SEND 0x01
9 #define SERVER_RECV 0x02
12 float br_recv, br_send;
15 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
16 volatile static struct comm_stat br_counter[10];
18 //===================================================================
19 int sendData(struct comm_stat *pcs)
24 struct timeval tv1, tv2;
26 gettimeofday(&tv1, NULL);
27 memset(line, '1' + i%36, 3000);
30 for(i=0; 0<50000; i++)
32 struct timeval tv = { 0, 25000 };
33 int flag = RUDPSELECT_WRITABLE;
35 PA_IoVecSetPtr(&v[0], line);
36 PA_IoVecSetPtr(&v[1], line);
37 PA_IoVecSetPtr(&v[2], line);
38 PA_IoVecSetLen(&v[0], 230);
39 PA_IoVecSetLen(&v[1], 1100);
40 PA_IoVecSetLen(&v[2], 100);
42 n_byt = PA_IoVecGetLen(&v[0]) + PA_IoVecGetLen(&v[1]) + PA_IoVecGetLen(&v[2]);
43 if(RUDPSelectSock(pcs->sock, 1, flag, &tv) > 0)
45 rlt = RUDPSendV(pcs->sock, 1, v, 3, 0);
48 printf("RUDPSend error: %d\n", rlt);
57 gettimeofday(&tv2, NULL);
58 if(tv2.tv_sec - tv1.tv_sec >= 1)
60 float delta = (tv2.tv_sec - tv1.tv_sec) + ((int)(tv2.tv_usec/1000) - (int)(tv1.tv_usec/1000))/1000.0;
61 pcs->br_send = total/delta*8/1024/1024.0;
67 printf("***************************************\n");
71 RUDPSend(pcs->sock, 0, line, 1, 0);
72 printf("all data sent.\n");
77 void recvData(struct comm_stat *pcs)
83 struct timeval tv1, tv2;
85 gettimeofday(&tv1, NULL);
88 struct timeval tv = { 0, 500000 };
89 int flag = RUDPSELECT_READABLE, rlt;
90 if(1)//(rlt = RUDPSelectSock(pcs->sock, -1, flag, &tv)) > 0)
92 if((len = RUDPRecv(pcs->sock, &chno, line, 2000, 0)) > 0)
96 //fprintf(stderr, "\rPacket %d from chn 0", cnt); fflush(stderr);
100 if(line[0] == '\0') break;
101 //printf("%s\n", line);
106 fprintf(stderr, "RUDPRecv: %d\n", len);
109 gettimeofday(&tv2, NULL);
110 if(tv2.tv_sec - tv1.tv_sec >= 1)
112 float delta = (tv2.tv_sec - tv1.tv_sec) + ((int)(tv2.tv_usec/1000) - (int)(tv1.tv_usec/1000))/1000.0;
113 pcs->br_recv= total/delta*8/1024/1024.0;
119 printf("ERROR: RUDPSelectSock: %d\n", rlt);
123 printf("receiving finished.\n");
126 int sendRecvData(struct comm_stat *pcs)
131 struct timeval tv1, tv2;
134 gettimeofday(&tv1, NULL);
135 memset(line, '1', 3000);
140 struct timeval tv = { 0, 50000 };
143 v[0].iov_base = v[1].iov_base = v[2].iov_base = line;
147 n_byt = v[0].iov_len + v[1].iov_len + v[2].iov_len;
149 if(pcs->cmd & SERVER_SEND) flag |= RUDPSELECT_WRITABLE;
150 if(pcs->cmd & SERVER_RECV) flag |= RUDPSELECT_READABLE;
151 if(RUDPSelectSock(pcs->sock, 0, &flag, &tv) > 0) //Not supported
153 if(flag & RUDPSELECT_WRITABLE)
155 rlt = RUDPSendV(pcs->sock, 0, v, 3, 0);
158 printf("RUDPSend error: %d\n", rlt);
166 if(flag & RUDPSELECT_READABLE)
169 if((len = RUDPRecv(pcs->sock, &chno, line, 2000, 0)) > 0)
175 fprintf(stderr, "RUDPRecv: %d\n", len);
181 gettimeofday(&tv2, NULL);
182 if(tv2.tv_sec - tv1.tv_sec >= 1)
184 float delta = (tv2.tv_sec - tv1.tv_sec) + ((int)(tv2.tv_usec/1000) - (int)(tv1.tv_usec/1000))/1000.0;
185 pcs->br_send = totalw/delta*8/1024/1024.0;
186 pcs->br_recv = totalr/delta*8/1024/1024.0;
192 printf("***************************************\n");
196 RUDPSend(pcs->sock, 0, line, 1, 0);
197 printf("all data sent.\n");
202 void *threadSendRecv(void *p)
204 struct comm_stat *pcs = (struct comm_stat*)p;
205 pthread_detach(pthread_self());
209 RUDPClose(pcs->sock);
210 memset(pcs, 0, sizeof(*pcs));
217 void *threadSend(void *p)
219 struct comm_stat *pcs = (struct comm_stat*)p;
220 pthread_detach(pthread_self());
225 RUDPClose(pcs->sock);
226 memset(pcs, 0, sizeof(*pcs));
231 void* threadRecv(void *p)
233 struct comm_stat *pcs = (struct comm_stat*)p;
234 pthread_detach(pthread_self());
239 RUDPClose(pcs->sock);
240 memset(pcs, 0, sizeof(*pcs));
245 void *threadPrint(void *p)
250 pthread_detach(pthread_self());
254 len = sprintf(ss, "Mbps recv|send::: ");
257 len += sprintf(ss+len, "%d: %.2f|%.2f; ", i, br_counter[i].br_recv, br_counter[i].br_send);
259 fprintf(stderr, "\r%s", ss); fflush(stderr);
264 int main(int argc, char *argv[])
266 struct sockaddr_in sai;
273 memset(&sai, 0, sizeof(sai));
274 sai.sin_family = AF_INET;
275 sai.sin_addr.s_addr = argc==2?inet_addr(argv[1]):htonl(INADDR_ANY);
276 sai.sin_port = htons(5001);
277 if(RUDPBind(s, (struct sockaddr*)&sai, sizeof(sai)) < 0)
285 int sa_len = sizeof(sai);
289 pthread_create(&thd, NULL, threadPrint, NULL);
292 if(RUDPAccept(s, &a, (struct sockaddr*)&sai, &sa_len) < 0)
294 printf("accept error\n");
301 if(br_counter[i].sock == NULL)
305 br_counter[i].sock = a;
307 while((len = RUDPRecv(a, &chno, &cmd, 1, 0)) > 0)
312 printf("client cmd: %d\n", cmd);
313 br_counter[i].cmd = cmd;
315 pthread_create(&thd, NULL, threadSendRecv, (void*)&br_counter);
317 if(cmd & SERVER_SEND)
318 pthread_create(&thd, NULL, threadSend, (void*)&br_counter[i]);
319 if(cmd & SERVER_RECV)
320 pthread_create(&thd, NULL, threadRecv, (void*)&br_counter[i]);