]> git.lizzy.rs Git - rudp.git/blob - rudpsvr.c
Switch to CMake
[rudp.git] / rudpsvr.c
1 #include "rudp.h"
2 #include <string.h>
3 #include <stdio.h>
4 #include <unistd.h>
5 #include <stdlib.h>
6 #include <pthread.h>
7
8 #define SERVER_SEND     0x01
9 #define SERVER_RECV     0x02
10 struct comm_stat {
11         RUDPSOCKET sock;
12         float br_recv, br_send;
13         int cmd;
14 };
15 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
16 volatile static struct comm_stat br_counter[10];
17
18 //===================================================================
19 int sendData(struct comm_stat *pcs)
20 {
21         char line[3000];
22         int i=0, rlt = 0;
23         int total = 0;
24         struct timeval tv1, tv2;
25
26         gettimeofday(&tv1, NULL);
27         memset(line, '1' + i%36, 3000);
28
29         unsigned long n_byt;
30         for(i=0; 0<50000; i++)
31         {
32                 struct timeval tv = { 0, 25000 };
33                 int flag = RUDPSELECT_WRITABLE;
34                 PA_IOVEC v[3];
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);
41
42                 n_byt = PA_IoVecGetLen(&v[0]) + PA_IoVecGetLen(&v[1]) + PA_IoVecGetLen(&v[2]);
43                 if(RUDPSelectSock(pcs->sock, 1, flag, &tv) > 0)
44                 {
45                         rlt = RUDPSendV(pcs->sock, 1, v, 3, 0);
46                         if(rlt < 0)
47                         {
48                                 printf("RUDPSend error: %d\n", rlt);
49                                 break;
50                         }
51                         else
52                         {
53                                 total += n_byt;
54                         }
55                 }
56
57                 gettimeofday(&tv2, NULL);
58                 if(tv2.tv_sec - tv1.tv_sec >= 1)
59                 {
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;
62                         total = 0;
63                         tv1 = tv2;
64                 }
65         }
66
67         printf("***************************************\n");
68         if(rlt > 0)
69         {
70                 line[0] = '\0';
71                 RUDPSend(pcs->sock, 0, line, 1, 0);
72                 printf("all data sent.\n");
73         }
74         return 0;
75 }
76
77 void recvData(struct comm_stat *pcs)
78 {
79         int cnt = 0;
80         char line[2100];
81         int len, chno;
82         int total = 0;
83         struct timeval tv1, tv2;
84
85         gettimeofday(&tv1, NULL);
86         while( 1 )
87         {
88                 struct timeval tv = { 0, 500000 };
89                 int flag = RUDPSELECT_READABLE, rlt;
90                 if(1)//(rlt = RUDPSelectSock(pcs->sock, -1, flag, &tv)) > 0)
91                 {
92                         if((len = RUDPRecv(pcs->sock, &chno, line, 2000, 0)) > 0)
93                         {
94                                 if(chno == 0)
95                                 {
96                                         //fprintf(stderr, "\rPacket %d from chn 0", cnt); fflush(stderr);
97                                 }
98                                 cnt++;
99                                 line[len] = '\0';
100                                 if(line[0] == '\0') break;
101                                 //printf("%s\n", line);
102                                 total += len;
103                         }
104                         else if(len < 0)
105                         {
106                                 fprintf(stderr, "RUDPRecv: %d\n", len);
107                                 exit(-1);
108                         }
109                         gettimeofday(&tv2, NULL);
110                         if(tv2.tv_sec - tv1.tv_sec >= 1)
111                         {
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;
114                                 total = 0;
115                                 tv1 = tv2;
116                         }
117                 } else if(rlt < 0)
118                 {
119                         printf("ERROR: RUDPSelectSock: %d\n", rlt);
120                         exit(-1);
121                 }
122         }
123         printf("receiving finished.\n");
124 }
125 #if 0
126 int sendRecvData(struct comm_stat *pcs)
127 {
128         char line[3000];
129         int rlt = 0;
130         int totalr, totalw;
131         struct timeval tv1, tv2;
132
133         totalr = totalw = 0;
134         gettimeofday(&tv1, NULL);
135         memset(line, '1', 3000);
136
137         unsigned long n_byt;
138         while(1)
139         {
140                 struct timeval tv = { 0, 50000 };
141                 int flag = 0;
142                 PA_IOVEC v[3];
143                 v[0].iov_base = v[1].iov_base = v[2].iov_base = line;
144                 v[0].iov_len = 230;
145                 v[1].iov_len = 1100;
146                 v[2].iov_len = 100;
147                 n_byt = v[0].iov_len + v[1].iov_len + v[2].iov_len;
148
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
152                 {
153                         if(flag & RUDPSELECT_WRITABLE)
154                         {
155                                 rlt = RUDPSendV(pcs->sock, 0, v, 3, 0);
156                                 if(rlt < 0)
157                                 {
158                                         printf("RUDPSend error: %d\n", rlt);
159                                         break;
160                                 }
161                                 else
162                                 {
163                                         totalw += n_byt;
164                                 }
165                         }
166                         if(flag & RUDPSELECT_READABLE)
167                         {
168                                 int chno, len;
169                                 if((len = RUDPRecv(pcs->sock, &chno, line, 2000, 0)) > 0)
170                                 {
171                                         totalr += len;
172                                 }
173                                 else if(len < 0)
174                                 {
175                                         fprintf(stderr, "RUDPRecv: %d\n", len);
176                                         exit(-1);
177                                 }
178                         }
179                 }
180
181                 gettimeofday(&tv2, NULL);
182                 if(tv2.tv_sec - tv1.tv_sec >= 1)
183                 {
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;
187                         totalr = totalw = 0;
188                         tv1 = tv2;
189                 }
190         }
191
192         printf("***************************************\n");
193         if(rlt > 0)
194         {
195                 line[0] = '\0';
196                 RUDPSend(pcs->sock, 0, line, 1, 0);
197                 printf("all data sent.\n");
198         }
199         return 0;
200 }
201
202 void *threadSendRecv(void *p)
203 {
204         struct comm_stat *pcs = (struct comm_stat*)p;
205         pthread_detach(pthread_self());
206
207         sendRecvData(pcs);
208
209         RUDPClose(pcs->sock);
210         memset(pcs, 0, sizeof(*pcs));
211         sleep(5);
212         return NULL;
213 }
214
215 #endif
216
217 void *threadSend(void *p)
218 {
219         struct comm_stat *pcs = (struct comm_stat*)p;
220         pthread_detach(pthread_self());
221
222         //recvData(pcs);
223         sendData(pcs);
224
225         RUDPClose(pcs->sock);
226         memset(pcs, 0, sizeof(*pcs));
227         sleep(5);
228         return NULL;
229 }
230
231 void* threadRecv(void *p)
232 {
233         struct comm_stat *pcs = (struct comm_stat*)p;
234         pthread_detach(pthread_self());
235
236         recvData(pcs);
237         //sendData(pcs);
238
239         RUDPClose(pcs->sock);
240         memset(pcs, 0, sizeof(*pcs));
241         sleep(5);
242         return NULL;
243 }
244
245 void *threadPrint(void *p)
246 {
247         int i, len;
248         char ss[128];
249
250         pthread_detach(pthread_self());
251         while(1)
252         {
253                 sleep(1);
254                 len = sprintf(ss, "Mbps recv|send::: ");
255                 for(i=0; i<5; i++)
256                 {
257                         len += sprintf(ss+len, "%d: %.2f|%.2f; ", i, br_counter[i].br_recv, br_counter[i].br_send);
258                 }
259                 fprintf(stderr, "\r%s", ss); fflush(stderr);
260         }
261         return NULL;
262 }
263
264 int main(int argc, char *argv[])
265 {
266         struct sockaddr_in sai;
267         RUDPSOCKET s;
268
269         RUDPStart();
270        
271         s = RUDPSocket();
272
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)
278         {
279                 perror("bind");
280                 return -1;
281         }
282
283         RUDPListen(s, 5);
284
285         int sa_len = sizeof(sai);
286         RUDPSOCKET a;
287         pthread_t thd;
288
289         pthread_create(&thd, NULL, threadPrint, NULL);
290         while(1)
291         {
292                 if(RUDPAccept(s, &a, (struct sockaddr*)&sai, &sa_len) < 0)
293                 {
294                         printf("accept error\n");
295                 }
296                 else
297                 {
298                         int i;
299                         for(i=0; i<10; i++)
300                         {
301                                 if(br_counter[i].sock == NULL)
302                                 {
303                                         char cmd;
304                                         int chno, len;
305                                         br_counter[i].sock = a;
306
307                                         while((len = RUDPRecv(a, &chno, &cmd, 1, 0)) > 0)
308                                         {
309                                                 if( chno == 0 )
310                                                         break;
311                                         }
312                                         printf("client cmd: %d\n", cmd);
313                                         br_counter[i].cmd = cmd;
314 #if 0
315                                         pthread_create(&thd, NULL, threadSendRecv, (void*)&br_counter);
316 #else
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]);
321 #endif
322                                         break;
323                                 }
324                         }
325                 }
326         }
327
328         RUDPClose(s);
329
330         RUDPCleanup();
331
332         return 0;
333 }