Release s3d 0.2.2
[s3d.git] / server / network.c
blobeabbe0aedb13697c6c8cf1bbde481c18d6292569
1 /*
2 * network.c
4 * Copyright (C) 2004-2011 Simon Wunderlich <dotslash@packetmixer.de>
6 * This file is part of s3d, a 3d network display server.
7 * See http://s3d.berlios.de/ for more updates.
9 * s3d is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * s3d is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with s3d; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 #include "global.h"
25 #include <stdio.h>
26 #include <stdlib.h> /* free() */
27 #include <errno.h> /* errno() */
28 #include <unistd.h> /* close(), read(),write() */
29 #include <signal.h> /* SIGPIPE,SIG_ERR,SIGIO */
30 #ifdef G_SDL
31 #include <SDL.h> /* SDL_SetTimer() */
32 #endif
33 #ifdef SIGS
34 #include <signal.h> /* sighandler_t SIG_PIPE */
35 #endif
36 /* here go all the network functions */
37 /* */
38 /* right now, there is only a basic implementation for tcp-scokets. */
39 /* upcoming are unix-sockets and ipv6-support */
41 /* defines: */
43 uint8_t ibuf[MAXPLEN]; /* input buffer for a packet */
44 uint8_t obuf[MAXPLEN]; /* output buffer */
45 #ifdef SIGS
46 static int sigio = 0;
47 #endif
49 #ifdef SIGS
50 void sigpipe_handler(int S3DUNUSED(unused))
52 errs("sigpip_handler()", "there is a broken pipe somewhere");
55 void sigio_handler(int S3DUNUSED(unused))
57 sigio = 1;
59 #endif
61 /* maybe change the errors to fatal errors ... */
62 int network_init(void)
64 #ifdef SIGS
65 /* struct sigaction act; */
66 #endif
67 #ifdef TCP
68 tcp_init();
69 #endif
70 #ifdef SHM
71 shm_init();
72 #endif
73 #ifdef SIGS
74 if (signal(SIGPIPE, sigpipe_handler) == SIG_ERR)
75 errn("network_init():signal()", errno);
76 if (signal(SIGIO, sigio_handler) == SIG_ERR)
77 errn("s3d_init():signal()", errno);
78 #endif
79 return 0;
82 volatile int turn;
84 int net_turn_off(int S3DUNUSED(interval))
86 s3dprintf(VLOW, "Warning: High traffic on Network, interrupting read.");
87 turn = 0;
88 return 0;
91 /* this basicly polls for new connection */
92 int network_main(void)
94 turn = 1;
96 #ifdef TCP
97 #ifdef SIGS
98 if (sigio == 1) { /* as long as there is no locking/threadsafety, do like this ... */
99 #endif
100 tcp_pollport(); /* this polls for new processes */
101 #ifdef G_SDL
102 SDL_SetTimer(50, (SDL_TimerCallback) net_turn_off);
103 #endif
104 while (turn && tcp_pollproc()) {
105 } /* if there is new data, loop please. this is for testing now, and should be combined with timing later .. */
106 #ifdef G_SDL
107 SDL_SetTimer(0, NULL);
108 #endif
110 #ifdef SIGS
111 sigio = 0;
113 #endif
114 #endif
115 #ifdef SHM
116 shm_main();
117 #endif
118 return 0;
121 int n_remove(struct t_process *p)
123 switch (p->con_type) {
124 #ifdef SHM
125 case CON_SHM:
126 shm_remove(p);
127 break;
128 #endif
129 #ifdef TCP
130 case CON_TCP:
131 tcp_remove(p->sockid);
132 break;
133 #endif
135 p->con_type = CON_NULL;
136 return -1;
139 int n_readn(struct t_process *p, uint8_t * str, int s)
141 switch (p->con_type) {
142 #ifdef TCP
143 case CON_TCP:
144 return tcp_readn(p->sockid, str, s);
145 #endif
146 #ifdef SHM
147 case CON_SHM:
148 return shm_readn((struct buf_t *)p->shmsock.data_ctos, str, s);
149 #endif
151 return -1;
154 int n_writen(struct t_process *p, uint8_t * str, int s)
156 switch (p->con_type) {
157 #ifdef TCP
158 case CON_TCP:
159 return tcp_writen(p->sockid, str, s);
160 #endif
161 #ifdef SHM
162 case CON_SHM:
163 return shm_writen((struct buf_t *)p->shmsock.data_stoc, str, s);
164 #endif
166 return -1;
169 int network_quit(void)
171 #ifdef TCP
172 tcp_quit();
173 #endif
174 #ifdef SHM
175 shm_quit();
176 #endif
177 return 0;