Ignore:
Timestamp:
10/18/17 13:54:41 (7 years ago)
Author:
tbretz
Message:
Updated to v20r20 - This also includes some minor fixes, I requested.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/FACT++/dim/src/tcpip.c

    r18097 r18920  
    1616#ifdef WIN32
    1717#define FD_SETSIZE      16384
    18 #define poll(pfd,nfds,timeout)  WSAPoll(pfd,nfds,timeout)
     18#endif
     19
     20#include <stdio.h>
     21#include <time.h>
     22#define DIMLIB
     23#include <dim.h>
     24
     25#ifdef WIN32
     26//#define poll(pfd,nfds,timeout)        WSAPoll(pfd,nfds,timeout)
    1927#define ioctl ioctlsocket
    2028
     
    2634#define EADDRNOTAVAIL WSAEADDRNOTAVAIL
    2735#define EWOULDBLOCK WSAEWOULDBLOCK
     36#define EINPROGRESS WSAEINPROGRESS
    2837#define ECONNREFUSED WSAECONNREFUSED
     38#define ETIMEDOUT WSAETIMEDOUT
    2939#define HOST_NOT_FOUND  WSAHOST_NOT_FOUND
    3040#define NO_DATA WSANO_DATA
     
    8090#define MY_FD_ISSET(fd, set)    FD_ISSET(fd, set)
    8191#endif
    82 
    83 #include <stdio.h>
    84 #include <time.h>
    85 #define DIMLIB
    86 #include <dim.h>
    8792
    8893#define ushort unsigned short
     
    813818    fd_set      rfds;
    814819    int conn_id, ret, selret, count;
     820#ifndef __linux__
    815821        struct timeval  timeout;
     822#endif
    816823
    817824        if(num){}
    818825        do
    819826        {
    820                 timeout.tv_sec = 0;             /* Don't wait, just poll */
    821                 timeout.tv_usec = 0;
    822827                list_to_fds( &rfds );
    823828#ifdef __linux__
    824829                selret = poll(Pollfds, Pollfd_size, 0);
    825830#else
     831                timeout.tv_sec = 0;             /* Don't wait, just poll */
     832                timeout.tv_usec = 0;
    826833                selret = select(FD_SETSIZE, &rfds, NULL, NULL, &timeout);
    827834#endif
     
    890897#endif
    891898                if(ret <= 0)
    892                   {
    893                     printf("poll returned %d, errno %d\n", ret, errno);
    894                   }
     899                {
     900                    printf("poll/select returned %d, errno %d\n", ret, errno);
     901                }
    895902                if(ret > 0)
    896903                {
     
    10231030        int host_addr;
    10241031#endif
    1025         int path, val, ret_code, ret;
     1032        int path, val, ret_code, ret, retcon, selret;
    10261033        int a,b,c,d;
    10271034/* Fix for gcc 4.6 "dereferencing type-punned pointer will break strict-aliasing rules"?!*/
     
    10291036        unsigned char *ipaddr = ipaddr_buff;
    10301037        int host_number = 0;
     1038        int tcpip_would_block();
     1039#ifdef __linux__
     1040        struct pollfd pollitem;
     1041#else
     1042        struct timeval  timeout;
     1043        fd_set rfds, wfds, efds;
     1044#endif
     1045        int so_error = ETIMEDOUT;
     1046        int so_error_len = sizeof(so_error);
    10311047
    10321048    dim_tcpip_init(0);
     
    11611177#endif
    11621178        sockname.sin_port = htons((ushort) port); /* port number to send to */
    1163         while((ret = connect(path, (struct sockaddr*)&sockname, sizeof(sockname))) == -1 )
    1164         {
    1165                 if(errno != EINTR)
     1179        /*
     1180        while ((ret = connect(path, (struct sockaddr*)&sockname, sizeof(sockname))) == -1)
     1181        {
     1182                if (errno != EINTR)
    11661183                {
    11671184                        closesock(path);
     
    11691186                }
    11701187        }
    1171         strcpy( Net_conns[conn_id].node, node );
     1188        */
     1189        set_non_blocking(path);
     1190        retcon = connect(path, (struct sockaddr*)&sockname, sizeof(sockname));
     1191#ifndef WIN32
     1192        ret = errno;
     1193#else
     1194        ret = WSAGetLastError();
     1195#endif
     1196        set_blocking(path);
     1197        if (retcon == -1)
     1198        {
     1199                if (tcpip_would_block(ret))
     1200                {
     1201#ifdef __linux__
     1202                        pollitem.fd = path;
     1203                        pollitem.events = POLLIN | POLLOUT | POLLERR;
     1204                        pollitem.revents = 0;
     1205                        selret = poll(&pollitem, 1, CONNECT_TMOUT * 1000);
     1206#else
     1207                        timeout.tv_sec = CONNECT_TMOUT;
     1208                        timeout.tv_usec = 0;
     1209                        FD_ZERO(&wfds);
     1210                        FD_SET(path, &wfds);
     1211                        FD_ZERO(&rfds);
     1212                        FD_SET(path, &rfds);
     1213                        FD_ZERO(&efds);
     1214                        FD_SET(path, &efds);
     1215                        selret = select(FD_SETSIZE, &rfds, &wfds, &efds, &timeout);
     1216#endif
     1217                        if (selret > 0)
     1218                        {
     1219                                getsockopt(path, SOL_SOCKET, SO_ERROR, (void *)&so_error, &so_error_len);
     1220                        }
     1221                }
     1222                if (so_error != 0)
     1223                {
     1224#ifndef WIN32
     1225                        errno = so_error;
     1226#else
     1227                        WSASetLastError(so_error);
     1228#endif
     1229                        closesock(path);
     1230                        return(0);
     1231                }
     1232        }
     1233        strcpy(Net_conns[conn_id].node, node);
    11721234        strcpy( Net_conns[conn_id].task, task );
    11731235        Net_conns[conn_id].channel = path;
     
    15471609   if(code == EWOULDBLOCK)
    15481610                return(1);
    1549     return(0);
     1611   else if (code == EINPROGRESS)
     1612           return(1);
     1613   return(0);
    15501614}
    15511615
Note: See TracChangeset for help on using the changeset viewer.