File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / miniupnpc / receivedata.c
Revision 1.1: download - view: text, annotated - select for diffs - revision graph
Tue Feb 21 23:16:22 2012 UTC (12 years, 8 months ago) by misho
CVS tags: MAIN, HEAD
Initial revision

    1: /* $Id: receivedata.c,v 1.1 2012/02/21 23:16:22 misho Exp $ */
    2: /* Project : miniupnp
    3:  * Author : Thomas Bernard
    4:  * Copyright (c) 2011 Thomas Bernard
    5:  * This software is subject to the conditions detailed in the
    6:  * LICENCE file provided in this distribution. */
    7: 
    8: #include <stdio.h>
    9: #ifdef WIN32
   10: #include <winsock2.h>
   11: #include <ws2tcpip.h>
   12: #else
   13: #include <unistd.h>
   14: #if defined(__amigaos__) && !defined(__amigaos4__)
   15: #define socklen_t int
   16: #else /* #if defined(__amigaos__) && !defined(__amigaos4__) */
   17: #include <sys/select.h>
   18: #endif /* #else defined(__amigaos__) && !defined(__amigaos4__) */
   19: #include <sys/socket.h>
   20: #if !defined(__amigaos__) && !defined(__amigaos4__)
   21: #include <poll.h>
   22: #endif
   23: #include <errno.h>
   24: #define MINIUPNPC_IGNORE_EINTR
   25: #endif
   26: 
   27: #ifdef WIN32
   28: #define PRINT_SOCKET_ERROR(x)    printf("Socket error: %s, %d\n", x, WSAGetLastError());
   29: #else
   30: #define PRINT_SOCKET_ERROR(x) perror(x)
   31: #endif
   32: 
   33: #include "receivedata.h"
   34: 
   35: int
   36: receivedata(int socket, char * data, int length, int timeout)
   37: {
   38:     int n;
   39: #if !defined(WIN32) && !defined(__amigaos__) && !defined(__amigaos4__)
   40: 	/* using poll */
   41:     struct pollfd fds[1]; /* for the poll */
   42: #ifdef MINIUPNPC_IGNORE_EINTR
   43:     do {
   44: #endif
   45:         fds[0].fd = socket;
   46:         fds[0].events = POLLIN;
   47:         n = poll(fds, 1, timeout);
   48: #ifdef MINIUPNPC_IGNORE_EINTR
   49:     } while(n < 0 && errno == EINTR);
   50: #endif
   51:     if(n < 0) {
   52:         PRINT_SOCKET_ERROR("poll");
   53:         return -1;
   54:     } else if(n == 0) {
   55: 		/* timeout */
   56:         return 0;
   57:     }
   58: #else /* !defined(WIN32) && !defined(__amigaos__) && !defined(__amigaos4__) */
   59: 	/* using select under WIN32 and amigaos */
   60:     fd_set socketSet;
   61:     TIMEVAL timeval;
   62:     FD_ZERO(&socketSet);
   63:     FD_SET(socket, &socketSet);
   64:     timeval.tv_sec = timeout / 1000;
   65:     timeval.tv_usec = (timeout % 1000) * 1000;
   66:     n = select(FD_SETSIZE, &socketSet, NULL, NULL, &timeval);
   67:     if(n < 0) {
   68:         PRINT_SOCKET_ERROR("select");
   69:         return -1;
   70:     } else if(n == 0) {
   71:         return 0;
   72:     }    
   73: #endif
   74: 	n = recv(socket, data, length, 0);
   75: 	if(n<0) {
   76: 		PRINT_SOCKET_ERROR("recv");
   77: 	}
   78: 	return n;
   79: }
   80: 
   81: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>