-
Notifications
You must be signed in to change notification settings - Fork 1
/
leanXip.c
270 lines (215 loc) · 5.04 KB
/
leanXip.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/* The Toeggeli-Application
Copyright (C) 2008 Reto Bättig
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or (at
your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this library; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/*!@file leanXip.c
* @The ip server for the leanXtogg application
*/
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <stdio.h>
#include <sys/select.h>
#include "oscar.h"
#include "leanXtools.h"
#include "leanXip.h"
struct client {
int sock;
char *r_ptr; /* This client got all the data from wbuf up to this ptr */
};
struct client clients[MAX_CLI];
struct sockaddr_in addr;
int srv_sock;
char data[DATABUF];
struct ringbuf wbuf;
int ip_start_server()
{
int err;
int i;
srv_sock=socket(PF_INET, SOCK_STREAM, 0);
if (srv_sock==SOCK_ERROR)
fatalerror("Could not start IP server\n");
i=1;
setsockopt(srv_sock, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(int));
i=1024*512;
setsockopt(srv_sock, SOL_SOCKET, SO_SNDBUF, &i, sizeof(int));
bzero(&addr, sizeof(addr));
addr.sin_port = htons(PORT);
addr.sin_family = AF_INET;
err = bind(srv_sock, (struct sockaddr*)&addr, sizeof(addr));
if (srv_sock==SOCK_ERROR)
fatalerror("Could bind socket\n");
err = listen(srv_sock, MAX_CLI);
ring_init(&wbuf, SENDBUF);
for (i=0; i<MAX_CLI; i++)
clients[i].sock = -1;
return 0;
} /* ip_start_server */
int ip_stop_server()
{
int i;
for (i=0; i<MAX_CLI; i++)
if (clients[i].sock >0)
close(clients[i].sock);
close(srv_sock);
return 0;
} /* ip_stop_server */
/*
* select_readable
*
* Checks whether the filedescriptor is readable
*
* Return value: true, if readable
*/
bool select_readable(int fd)
{
int retval;
fd_set r;
struct timeval timeout;
bzero(&timeout, sizeof(timeout));
FD_ZERO(&r);
FD_SET(fd, &r);
retval = select(fd+1, &r, NULL, NULL, &timeout);
if (retval == -1)
OscLog(ERROR, "Select Error\n");
if ((retval>0) && FD_ISSET(fd, &r))
return TRUE;
return FALSE;
}
/*
* select_writeable
*
* Checks whether the filedescriptor is writeable
*
* Return value: true, if readable
*/
bool select_writeable(int fd)
{
int retval;
fd_set w;
struct timeval timeout;
bzero(&timeout, sizeof(timeout));
FD_ZERO(&w);
FD_SET(fd, &w);
retval = select(fd+1, NULL, &w, NULL, &timeout);
if (retval == -1)
OscLog(ERROR, "Select Error\n");
if ((retval>0) && FD_ISSET(fd, &w))
return TRUE;
return FALSE;
}
void ip_cli_connect()
{
int i;
for (i=0; i<MAX_CLI; i++) {
if (clients[i].sock == -1)
break;
}
if (i== MAX_CLI) {
OscLog(INFO, "To many clients\n");
return;
}
OscLog(DEBUG, "New client connects to IP server\n");
clients[i].sock=accept(srv_sock, NULL, 0);
if (clients[i].sock==SOCK_ERROR)
fatalerror("accept failed\n");
clients[i].r_ptr = wbuf.r_ptr;
}
void ip_cli_disconnect(int client)
{
clients[client].sock = -1;
}
void ip_read(int client)
{
int err;
char dummy[100];
err=read(clients[client].sock, &dummy, sizeof(dummy)-1);
if (err==0) {
ip_cli_disconnect(client);
OscLog(DEBUG, "Client disconnected\n");
}
if (err>0) {
dummy[err]=0;
printf(dummy);
}
}
int ip_send_all(char *buf, int len)
{
if (ring_free(&wbuf) > len)
return ring_write(&wbuf, buf, len);
else
return 0;
}
void fix_readpointer() {
char *minptr;
char *rptr;
bool hasclient = FALSE;
int i;
minptr = wbuf.w_ptr;
for (i=0; i<MAX_CLI; i++) if (clients[i].sock>0 ) {
hasclient = TRUE;
rptr = clients[i].r_ptr;
if (rptr > wbuf.w_ptr)
rptr -= wbuf.size;
minptr = min(minptr, rptr);
}
if (minptr < wbuf.data)
minptr+=wbuf.size;
if (hasclient) wbuf.r_ptr = minptr;
}
void ip_do_work()
{
int i, len;
if (select_readable(srv_sock)) {
ip_cli_connect();
}
for (i=0; i<MAX_CLI; i++) if (clients[i].sock>0 ) {
while ((clients[i].r_ptr != wbuf.w_ptr) && select_writeable(clients[i].sock)){
len = ring_peekfrom(&wbuf, clients[i].r_ptr, data, DATABUF);
len = send(clients[i].sock, data, len, 0);
if (len>0)
ring_addtoptr(&wbuf, &(clients[i].r_ptr), len);
}
while ((clients[i].sock>0) && select_readable(clients[i].sock)) {
ip_read(i);
}
}
fix_readpointer();
}
uint32 ip_sendtest()
{
uint32 len=0;
int retval;
int i;
for (i=0; i<MAX_CLI; i++) {
if (clients[i].sock>0) {
retval=send(clients[i].sock, data, DATABUF, 0);
if (retval>0)
len+=retval;
}
}
return len;
}
void ip_test() {
int i=0;
char s[100];
while (1) {
ip_do_work();
sleep(1);
sprintf(s, "i=%i\n", i);
i=i+1;
ip_send_all(s, strlen(s)+1);
}
}