| 1 | /* |
| 2 | Copyright (C) 2013 by Ronnie Sahlberg <ronniesahlberg@gmail.com> |
| 3 | |
| 4 | This program is free software; you can redistribute it and/or modify |
| 5 | it under the terms of the GNU Lesser General Public License as published by |
| 6 | the Free Software Foundation; either version 2.1 of the License, or |
| 7 | (at your option) any later version. |
| 8 | |
| 9 | This program is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | GNU Lesser General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU Lesser General Public License |
| 15 | along with this program; if not, see <http://www.gnu.org/licenses/>. |
| 16 | */ |
| 17 | |
| 18 | #ifdef AROS |
| 19 | |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <stdring.h> |
| 23 | #include <sys/types.h> |
| 24 | #include <sys/time.h> |
| 25 | #include "aros_compat.h" |
| 26 | |
| 27 | #undef poll |
| 28 | |
| 29 | /* unix device major/minor numbers dont make much sense on amiga */ |
| 30 | int major(int i) |
| 31 | { |
| 32 | return 1; |
| 33 | } |
| 34 | int minor(int i) |
| 35 | { |
| 36 | return 2; |
| 37 | } |
| 38 | |
| 39 | struct Library * SocketBase = NULL; |
| 40 | |
| 41 | void aros_init_socket(void) |
| 42 | { |
| 43 | if (!(SocketBase = OpenLibrary("bsdsocket.library", 4))) { |
| 44 | printf("No TCP/IP stack available.\n"); |
| 45 | exit(10); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | int aros_poll(struct pollfd *fds, unsigned int nfds, int timo) |
| 50 | { |
| 51 | struct timeval timeout, *toptr; |
| 52 | fd_set ifds, ofds, efds, *ip, *op; |
| 53 | unsigned int i; |
| 54 | int rc; |
| 55 | |
| 56 | // Set up the file-descriptor sets in ifds, ofds and efds. |
| 57 | FD_ZERO(&ifds); |
| 58 | FD_ZERO(&ofds); |
| 59 | FD_ZERO(&efds); |
| 60 | for (i = 0, op = ip = 0; i < nfds; ++i) |
| 61 | { |
| 62 | fds[i].revents = 0; |
| 63 | if(fds[i].events & (POLLIN|POLLPRI)) |
| 64 | { |
| 65 | ip = &ifds; |
| 66 | FD_SET(fds[i].fd, ip); |
| 67 | } |
| 68 | if(fds[i].events & POLLOUT) |
| 69 | { |
| 70 | op = &ofds; |
| 71 | FD_SET(fds[i].fd, op); |
| 72 | } |
| 73 | FD_SET(fds[i].fd, &efds); |
| 74 | } |
| 75 | |
| 76 | // Set up the timeval structure for the timeout parameter |
| 77 | if(timo < 0) |
| 78 | { |
| 79 | toptr = 0; |
| 80 | } |
| 81 | else |
| 82 | { |
| 83 | toptr = &timeout; |
| 84 | timeout.tv_sec = timo / 1000; |
| 85 | timeout.tv_usec = (timo - timeout.tv_sec * 1000) * 1000; |
| 86 | } |
| 87 | |
| 88 | rc = WaitSelect(0, ip, op, &efds, toptr, NULL); |
| 89 | |
| 90 | if(rc <= 0) |
| 91 | return rc; |
| 92 | |
| 93 | if(rc > 0) |
| 94 | { |
| 95 | for (i = 0; i < nfds; ++i) |
| 96 | { |
| 97 | int fd = fds[i].fd; |
| 98 | if(fds[i].events & (POLLIN|POLLPRI) && FD_ISSET(fd, &ifds)) |
| 99 | fds[i].revents |= POLLIN; |
| 100 | if(fds[i].events & POLLOUT && FD_ISSET(fd, &ofds)) |
| 101 | fds[i].revents |= POLLOUT; |
| 102 | if(FD_ISSET(fd, &efds)) // Some error was detected ... should be some way to know. |
| 103 | fds[i].revents |= POLLHUP; |
| 104 | } |
| 105 | } |
| 106 | return rc; |
| 107 | } |
| 108 | |
| 109 | #endif |