Ignore:
Timestamp:
Sep 25, 2007, 11:10:34 PM (14 years ago)
Author:
charles
Message:
  • record the ports of incoming sockets. we might need them later if we want to disconnect and reconnect.
  • for portability, use libevent API for some socket upkeep
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/net.c

    r3105 r3171  
    3535#endif
    3636
     37#include <evutil.h>
     38
    3739#include "transmission.h"
    3840#include "fdlimit.h"
     
    7476 **********************************************************************/
    7577
    76 static int makeSocketNonBlocking( int s )
    77 {
    78 #ifdef WIN32
    79     unsigned long flags = 1;
    80     if( ioctlsocket( s, FIONBIO, &flags) == SOCKET_ERROR )
    81 #elif defined(__BEOS__)
    82     int flags = 1;
    83     if( setsockopt( s, SOL_SOCKET, SO_NONBLOCK,
    84                     &flags, sizeof( int ) ) < 0 )
     78static int
     79makeSocketNonBlocking( int fd )
     80{
     81    if( fd >= 0 )
     82    {
     83#if defined(__BEOS__)
     84        int flags = 1;
     85        if( setsockopt( fd, SOL_SOCKET, SO_NONBLOCK,
     86                        &flags, sizeof( int ) ) < 0 )
    8587#else
    86     int flags = 1;
    87     if( ( flags = fcntl( s, F_GETFL, 0 ) ) < 0 ||
    88         fcntl( s, F_SETFL, flags | O_NONBLOCK ) < 0 )
    89 #endif
    90     {
    91         tr_err( "Couldn't set socket to non-blocking mode (%s)",
    92                 strerror( sockerrno ) );
    93         tr_netClose( s );
    94         return -1;
    95     }
    96 
    97     return s;
    98 }
    99 
    100 static int createSocket( int type, int priority )
    101 {
    102     int s;
    103     if( ( s = tr_fdSocketCreate( type, priority ) ) < 0 )
    104     {
    105         return -1;
    106     }
    107     return makeSocketNonBlocking( s );
     88        if( evutil_make_socket_nonblocking( fd ) )
     89#endif
     90        {
     91            tr_err( "Couldn't set socket to non-blocking mode (%s)",
     92                    strerror( sockerrno ) );
     93            tr_netClose( fd );
     94            fd = -1;
     95        }
     96    }
     97
     98    return fd;
     99}
     100
     101static int
     102createSocket( int type, int priority )
     103{
     104    return makeSocketNonBlocking( tr_fdSocketCreate( type, priority ) );
    108105}
    109106
     
    239236
    240237
    241 int tr_netAccept( int b, struct in_addr * addr, tr_port_t * port )
    242 {
    243     int s;
    244     if( ( s = tr_fdSocketAccept( b, addr, port ) ) < 0 )
    245     {
    246         return -1;
    247     }
    248     return makeSocketNonBlocking( s );
     238int
     239tr_netAccept( int b, struct in_addr * addr, tr_port_t * port )
     240{
     241    return makeSocketNonBlocking( tr_fdSocketAccept( b, addr, port ) );
    249242}
    250243
Note: See TracChangeset for help on using the changeset viewer.