| 1 | /* |
| 2 | * This file is part of DisOrder |
| 3 | * Copyright (C) 2013 Richard Kettlewell |
| 4 | * |
| 5 | * This program is free software: you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation, either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | /** @file lib/socketio.c |
| 19 | * @brief Buffered socket IO |
| 20 | */ |
| 21 | #include "common.h" |
| 22 | #include "socketio.h" |
| 23 | #include "mem.h" |
| 24 | |
| 25 | #include <errno.h> |
| 26 | #include <sys/types.h> |
| 27 | #if HAVE_SYS_SOCKET_H |
| 28 | # include <sys/socket.h> |
| 29 | #endif |
| 30 | #if HAVE_UNISTD_H |
| 31 | # include <unistd.h> |
| 32 | #endif |
| 33 | |
| 34 | void socketio_init(struct socketio *sio, SOCKET sd) { |
| 35 | sio->sd = sd; |
| 36 | sio->inputptr = sio->inputlimit = sio->input; |
| 37 | sio->outputused = 0; |
| 38 | sio->error = 0; |
| 39 | } |
| 40 | |
| 41 | int socketio_write(struct socketio *sio, const void *buffer, size_t n) { |
| 42 | size_t chunk; |
| 43 | while(n > 0) { |
| 44 | chunk = n > sizeof sio->output ? sizeof sio->output : n; |
| 45 | if(chunk) { |
| 46 | memcpy(sio->output + sio->outputused, buffer, chunk); |
| 47 | sio->outputused += chunk; |
| 48 | buffer = (char *)buffer + chunk; |
| 49 | n -= chunk; |
| 50 | } |
| 51 | if(sio->outputused == sizeof sio->output) |
| 52 | if(socketio_flush(sio)) |
| 53 | return -1; |
| 54 | } |
| 55 | return 0; |
| 56 | } |
| 57 | |
| 58 | static int socketio_fill(struct socketio *sio) { |
| 59 | int n = recv(sio->sd, sio->input, sizeof sio->input, 0); |
| 60 | if(n <= 0) { |
| 61 | sio->error = n < 0 ? socket_error() : -1; |
| 62 | return -1; |
| 63 | } |
| 64 | sio->inputptr = sio->input; |
| 65 | sio->inputlimit = sio->input + n; |
| 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | int socketio_getc(struct socketio *sio) { |
| 70 | if(sio->inputptr >= sio->inputlimit) { |
| 71 | if(socketio_fill(sio)) |
| 72 | return EOF; |
| 73 | } |
| 74 | return *sio->inputptr++; |
| 75 | } |
| 76 | |
| 77 | int socketio_flush(struct socketio *sio) { |
| 78 | size_t written = 0; |
| 79 | while(written < sio->outputused) { |
| 80 | int n = send(sio->sd, sio->output + written, sio->outputused - written, 0); |
| 81 | if(n < 0) { |
| 82 | sio->error = socket_error(); |
| 83 | return -1; |
| 84 | } |
| 85 | written += n; |
| 86 | } |
| 87 | sio->outputused = 0; |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | void socketio_close(struct socketio *sio) { |
| 92 | socketio_flush(sio); |
| 93 | closesocket(sio->sd); |
| 94 | } |