1 | |
|
|
2 | /* |
|
|
3 | * static char *rcsid_sockets_c = |
|
|
4 | * "$Id: lowlevel.C,v 1.5 2006/09/10 13:43:33 root Exp $"; |
|
|
5 | */ |
|
|
6 | |
|
|
7 | /* |
1 | /* |
8 | CrossFire, A Multiplayer game for X-windows |
2 | CrossFire, A Multiplayer game for X-windows |
9 | |
3 | |
10 | Copyright (C) 1992 Frank Tore Johansen |
4 | Copyright (C) 1992 Frank Tore Johansen |
11 | |
5 | |
… | |
… | |
163 | abort (); |
157 | abort (); |
164 | } |
158 | } |
165 | /* We already have a partial packet */ |
159 | /* We already have a partial packet */ |
166 | if (sl->len < 2) |
160 | if (sl->len < 2) |
167 | { |
161 | { |
168 | #ifdef WIN32 /* ***WIN32 SockList_ReadPacket: change read() to recv() */ |
|
|
169 | |
|
|
170 | stat = recv (fd, sl->buf + sl->len, 2 - sl->len, 0); |
|
|
171 | |
|
|
172 | #else |
|
|
173 | do |
162 | do |
174 | { |
163 | { |
175 | stat = read (fd, sl->buf + sl->len, 2 - sl->len); |
164 | stat = read (fd, sl->buf + sl->len, 2 - sl->len); |
176 | } |
165 | } |
177 | while ((stat == -1) && (errno == EINTR)); |
166 | while ((stat == -1) && (errno == EINTR)); |
178 | #endif |
167 | |
179 | if (stat < 0) |
168 | if (stat < 0) |
180 | { |
169 | { |
181 | /* In non blocking mode, EAGAIN is set when there is no |
170 | /* In non blocking mode, EAGAIN is set when there is no |
182 | * data available. |
171 | * data available. |
183 | */ |
172 | */ |
184 | #ifdef WIN32 /* ***WIN32 SockList_ReadPacket: error handling for win32 */ |
|
|
185 | if ((stat == -1) && WSAGetLastError () != WSAEWOULDBLOCK) |
|
|
186 | { |
|
|
187 | if (WSAGetLastError () == WSAECONNRESET) |
|
|
188 | LOG (llevDebug, "Connection closed by client\n"); |
|
|
189 | else |
|
|
190 | { |
|
|
191 | LOG (llevDebug, "ReadPacket got error %d, returning 0\n", WSAGetLastError ()); |
|
|
192 | } |
|
|
193 | return -1; /* kick this user! */ |
|
|
194 | } |
|
|
195 | #else |
|
|
196 | if (errno != EAGAIN && errno != EWOULDBLOCK) |
173 | if (errno != EAGAIN && errno != EWOULDBLOCK) |
197 | { |
174 | { |
198 | LOG (llevDebug, "ReadPacket got error %s, returning 0\n", strerror (errno)); |
175 | LOG (llevDebug, "ReadPacket got error %s, returning 0\n", strerror (errno)); |
199 | } |
176 | } |
200 | #endif |
|
|
201 | return 0; /*Error */ |
177 | return 0; /*Error */ |
202 | } |
178 | } |
203 | if (stat == 0) |
179 | if (stat == 0) |
204 | return -1; |
180 | return -1; |
205 | sl->len += stat; |
181 | sl->len += stat; |
… | |
… | |
219 | LOG (llevError, "SockList_ReadPacket: Want to read more bytes than will fit in buffer (%d>=%d).\n", toread + sl->len, len); |
195 | LOG (llevError, "SockList_ReadPacket: Want to read more bytes than will fit in buffer (%d>=%d).\n", toread + sl->len, len); |
220 | /* Quick hack in case for 'oldsocketmode' input. If we are |
196 | /* Quick hack in case for 'oldsocketmode' input. If we are |
221 | * closing the socket anyways, then reading this extra 100 bytes |
197 | * closing the socket anyways, then reading this extra 100 bytes |
222 | * shouldn't hurt. |
198 | * shouldn't hurt. |
223 | */ |
199 | */ |
224 | #ifdef WIN32 /* ***win32 SockList_ReadPacket: change read() to recv() */ |
|
|
225 | recv (fd, sl->buf + 2, 100, 0); |
|
|
226 | #else |
|
|
227 | read (fd, sl->buf + 2, 100); |
200 | read (fd, sl->buf + 2, 100); |
228 | #endif /* end win32 */ |
|
|
229 | |
201 | |
230 | /* return error so the socket is closed */ |
202 | /* return error so the socket is closed */ |
231 | return -1; |
203 | return -1; |
232 | } |
204 | } |
233 | do |
205 | do |
234 | { |
206 | { |
235 | #ifdef WIN32 /* ***win32 SockList_ReadPacket: change read() to recv() */ |
|
|
236 | stat = recv (fd, sl->buf + sl->len, toread, 0); |
|
|
237 | #else |
|
|
238 | do |
207 | do |
239 | { |
208 | { |
240 | stat = read (fd, sl->buf + sl->len, toread); |
209 | stat = read (fd, sl->buf + sl->len, toread); |
241 | } |
210 | } |
242 | while ((stat < 0) && (errno == EINTR)); |
211 | while ((stat < 0) && (errno == EINTR)); |
243 | #endif |
|
|
244 | if (stat < 0) |
212 | if (stat < 0) |
245 | { |
213 | { |
246 | |
214 | |
247 | #ifdef WIN32 /* ***win32 SockList_ReadPacket: change error handling for win32 */ |
|
|
248 | if ((stat == -1) && WSAGetLastError () != WSAEWOULDBLOCK) |
|
|
249 | { |
|
|
250 | if (WSAGetLastError () == WSAECONNRESET) |
|
|
251 | LOG (llevDebug, "Connection closed by client\n"); |
|
|
252 | else |
|
|
253 | { |
|
|
254 | LOG (llevDebug, "ReadPacket got error %d, returning 0\n", WSAGetLastError ()); |
|
|
255 | } |
|
|
256 | return -1; /* kick this user! */ |
|
|
257 | } |
|
|
258 | #else |
|
|
259 | if (errno != EAGAIN && errno != EWOULDBLOCK) |
215 | if (errno != EAGAIN && errno != EWOULDBLOCK) |
260 | { |
216 | { |
261 | LOG (llevDebug, "ReadPacket got error %s, returning 0\n", strerror (errno)); |
217 | LOG (llevDebug, "ReadPacket got error %s, returning 0\n", strerror (errno)); |
262 | } |
218 | } |
263 | #endif |
|
|
264 | return 0; /*Error */ |
219 | return 0; /*Error */ |
265 | } |
220 | } |
266 | if (stat == 0) |
221 | if (stat == 0) |
267 | return -1; |
222 | return -1; |
268 | sl->len += stat; |
223 | sl->len += stat; |
… | |
… | |
314 | |
269 | |
315 | end = ns->outputbuffer.start + ns->outputbuffer.len; |
270 | end = ns->outputbuffer.start + ns->outputbuffer.len; |
316 | /* The buffer is already in a wrapped state, so adjust end */ |
271 | /* The buffer is already in a wrapped state, so adjust end */ |
317 | if (end >= SOCKETBUFSIZE) |
272 | if (end >= SOCKETBUFSIZE) |
318 | end -= SOCKETBUFSIZE; |
273 | end -= SOCKETBUFSIZE; |
|
|
274 | |
319 | avail = SOCKETBUFSIZE - end; |
275 | avail = SOCKETBUFSIZE - end; |
320 | |
276 | |
321 | /* We can all fit it behind the current data without wrapping */ |
277 | /* We can all fit it behind the current data without wrapping */ |
322 | if (avail >= len) |
278 | if (avail >= len) |
323 | { |
|
|
324 | memcpy (ns->outputbuffer.data + end, buf, len); |
279 | memcpy (ns->outputbuffer.data + end, buf, len); |
325 | } |
|
|
326 | else |
280 | else |
327 | { |
281 | { |
328 | memcpy (ns->outputbuffer.data + end, buf, avail); |
282 | memcpy (ns->outputbuffer.data + end, buf, avail); |
329 | memcpy (ns->outputbuffer.data, buf + avail, len - avail); |
283 | memcpy (ns->outputbuffer.data, buf + avail, len - avail); |
330 | } |
284 | } |
|
|
285 | |
331 | ns->outputbuffer.len += len; |
286 | ns->outputbuffer.len += len; |
332 | #if 0 |
287 | #if 0 |
333 | LOG (llevDebug, "Added %d to output buffer, total length now %d, start=%d\n", len, ns->outputbuffer.len, ns->outputbuffer.start); |
288 | LOG (llevDebug, "Added %d to output buffer, total length now %d, start=%d\n", len, ns->outputbuffer.len, ns->outputbuffer.start); |
334 | #endif |
289 | #endif |
335 | } |
290 | } |
… | |
… | |
355 | { |
310 | { |
356 | max = SOCKETBUFSIZE - ns->outputbuffer.start; |
311 | max = SOCKETBUFSIZE - ns->outputbuffer.start; |
357 | if (ns->outputbuffer.len < max) |
312 | if (ns->outputbuffer.len < max) |
358 | max = ns->outputbuffer.len; |
313 | max = ns->outputbuffer.len; |
359 | |
314 | |
360 | #ifdef WIN32 /* ***win32 write_socket_buffer: change write() to send() */ |
|
|
361 | amt = send (ns->fd, ns->outputbuffer.data + ns->outputbuffer.start, max, 0); |
|
|
362 | #else |
|
|
363 | do |
315 | do |
364 | { |
316 | { |
365 | amt = write (ns->fd, ns->outputbuffer.data + ns->outputbuffer.start, max); |
317 | amt = write (ns->fd, ns->outputbuffer.data + ns->outputbuffer.start, max); |
366 | } |
318 | } |
367 | while ((amt < 0) && (errno == EINTR)); |
319 | while ((amt < 0) && (errno == EINTR)); |
368 | #endif |
|
|
369 | |
320 | |
370 | if (amt < 0) |
321 | if (amt < 0) |
371 | { /* We got an error */ |
322 | { /* We got an error */ |
372 | |
323 | |
373 | #ifdef WIN32 /* ***win32 write_socket_buffer: change error handling */ |
|
|
374 | if (amt == -1 && WSAGetLastError () != WSAEWOULDBLOCK) |
|
|
375 | { |
|
|
376 | LOG (llevError, "New socket write failed (wsb) (%d).\n", WSAGetLastError ()); |
|
|
377 | #else |
|
|
378 | if (errno != EWOULDBLOCK) |
324 | if (errno != EWOULDBLOCK) |
379 | { |
325 | { |
380 | LOG (llevError, "New socket write failed (wsb) (%d: %s).\n", errno, strerror (errno)); |
326 | LOG (llevError, "New socket write failed (wsb) (%d: %s).\n", errno, strerror (errno)); |
381 | #endif |
|
|
382 | ns->status = Ns_Dead; |
327 | ns->status = Ns_Dead; |
383 | return; |
328 | return; |
384 | } |
329 | } |
385 | else |
330 | else |
386 | { /* EWOULDBLOCK */ |
331 | { /* EWOULDBLOCK */ |
… | |
… | |
430 | #endif |
375 | #endif |
431 | /* If we manage to write more than we wanted, take it as a bonus */ |
376 | /* If we manage to write more than we wanted, take it as a bonus */ |
432 | while (len > 0) |
377 | while (len > 0) |
433 | { |
378 | { |
434 | |
379 | |
435 | #ifdef WIN32 /* ***win32 Write_To_Socket: change write() to send() */ |
|
|
436 | amt = send (ns->fd, pos, len, 0); |
|
|
437 | #else |
|
|
438 | do |
380 | do |
439 | { |
381 | { |
440 | amt = write (ns->fd, pos, len); |
382 | amt = write (ns->fd, pos, len); |
441 | } |
383 | } |
442 | while ((amt < 0) && (errno == EINTR)); |
384 | while ((amt < 0) && (errno == EINTR)); |
443 | #endif |
|
|
444 | |
385 | |
445 | if (amt < 0) |
386 | if (amt < 0) |
446 | { /* We got an error */ |
387 | { /* We got an error */ |
447 | #ifdef WIN32 /* ***win32 Write_To_Socket: change error handling */ |
|
|
448 | if (amt == -1 && WSAGetLastError () != WSAEWOULDBLOCK) |
|
|
449 | { |
|
|
450 | LOG (llevError, "New socket write failed WTS (%d).\n", WSAGetLastError ()); |
|
|
451 | #else |
|
|
452 | if (errno != EWOULDBLOCK) |
388 | if (errno != EWOULDBLOCK) |
453 | { |
389 | { |
454 | LOG (llevError, "New socket write failed WTS (%d: %s).\n", /* ---WIN32 */ |
390 | LOG (llevError, "New socket write failed WTS (%d: %s).\n", /* ---WIN32 */ |
455 | errno, strerror (errno)); |
391 | errno, strerror (errno)); |
456 | #endif |
|
|
457 | ns->status = Ns_Dead; |
392 | ns->status = Ns_Dead; |
458 | return; |
393 | return; |
459 | } |
394 | } |
460 | else |
395 | else |
461 | { /* EWOULDBLOCK */ |
396 | { /* EWOULDBLOCK */ |