]> git.lizzy.rs Git - rudp.git/blob - rudpsels.c
Switch to CMake
[rudp.git] / rudpsels.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
11 //===================================================================
12 int sendData(RUDPSOCKET sock)
13 {
14         char line[3000];
15         int i=0, rlt = 0;
16
17         memset(line, '1' + i%36, 3000);
18
19         unsigned long n_byt;
20         for(i=0; 0<50000; i++)
21         {
22                 struct timeval tv = { 0, 25000 };
23                 int flag = RUDPSELECT_WRITABLE;
24                 PA_IOVEC v[3];
25                 v[0].iov_base = v[1].iov_base = v[2].iov_base = line;
26                 v[0].iov_len = 230;
27                 v[1].iov_len = 1100;
28                 v[2].iov_len = 100;
29
30                 n_byt = v[0].iov_len + v[1].iov_len + v[2].iov_len;
31                 if(RUDPSelectSock(sock, 1, flag, &tv) > 0)
32                 {
33                         rlt = RUDPSendV(sock, 1, v, 3, 0);
34                         if(rlt < 0)
35                         {
36                                 printf("RUDPSend error: %d\n", rlt);
37                                 break;
38                         }
39                         else
40                         {
41                         }
42                 }
43         }
44
45         printf("***************************************\n");
46         if(rlt > 0)
47         {
48                 line[0] = '\0';
49                 RUDPSend(sock, 0, line, 1, 0);
50                 printf("all data sent.\n");
51         }
52         return 0;
53 }
54
55 void recvData(RUDPSOCKET sock)
56 {
57         int cnt = 0;
58         char line[2100];
59         int len, chno;
60         int ic = 0;
61         char cBusy[] = "-\\|/";
62
63         while( 1 )
64         {
65                 struct timeval tv = { 0, 500000 };
66                 int flag = RUDPSELECT_READABLE, rlt;
67                 if((rlt = RUDPSelectSock(sock, -1, flag, &tv)) > 0)
68                 {
69                         if((len = RUDPRecv(sock, &chno, line, 2000, 0)) > 0)
70                         {
71                                 printf(line);
72                         }
73                         else if(len < 0)
74                         {
75                                 fprintf(stderr, "RUDPRecv: %d\n", len);
76                                 exit(-1);
77                         }
78                 } else if(rlt < 0)
79                 {
80                         printf("ERROR: RUDPSelectSock: %d\n", rlt);
81                         exit(-1);
82                 }
83                 else {
84                         printf("\r%c", cBusy[ic]); fflush(stdout);
85                         ic = (ic+1)%4;
86                 }
87         }
88         printf("receiving finished.\n");
89 }
90
91 void *threadSend(void *p)
92 {
93         RUDPSOCKET sock = (RUDPSOCKET)p;
94         pthread_detach(pthread_self());
95
96         //recvData(pcs);
97         sendData(sock);
98
99         RUDPClose(sock);
100         sleep(1);
101         return NULL;
102 }
103
104 void* threadRecv(void *p)
105 {
106         RUDPSOCKET sock = (RUDPSOCKET)p;
107         pthread_detach(pthread_self());
108
109         recvData(sock);
110
111         RUDPClose(sock);
112         sleep(1);
113         return NULL;
114 }
115
116 int main(int argc, char *argv[])
117 {
118         struct sockaddr_in sai;
119         RUDPSOCKET s;
120
121         RUDPStart();
122        
123         s = RUDPSocket();
124
125         memset(&sai, 0, sizeof(sai));
126         sai.sin_family = AF_INET;
127         sai.sin_addr.s_addr = argc==2?inet_addr(argv[1]):htonl(INADDR_ANY);
128         sai.sin_port = htons(5001);
129         if(RUDPBind(s, (struct sockaddr*)&sai, sizeof(sai)) < 0)
130         {
131                 perror("bind");
132                 return -1;
133         }
134
135         RUDPListen(s, 5);
136
137         int sa_len = sizeof(sai);
138         RUDPSOCKET a;
139         pthread_t thd;
140
141         while(1)
142         {
143                 if(RUDPAccept(s, &a, (struct sockaddr*)&sai, &sa_len) < 0)
144                 {
145                         printf("accept error\n");
146                 }
147                 else
148                 {
149 #if 0
150                         char cmd;
151                         int chno, len;
152
153                         while((len = RUDPRecv(a, &chno, &cmd, 1, 0)) > 0)
154                         {
155                                 if( chno == 0 )
156                                         break;
157                         }
158                         if(cmd & SERVER_SEND)
159                                 pthread_create(&thd, NULL, threadSend, a);
160                         if(cmd & SERVER_RECV)
161                                 pthread_create(&thd, NULL, threadRecv, a);
162 #else
163                         pthread_create(&thd, NULL, threadRecv, a);
164 #endif
165                 }
166         }
167
168         RUDPClose(s);
169
170         RUDPCleanup();
171
172         return 0;
173 }