def handle( self, source, address ):
global currentEndpoints
try:
if 0 == len( currentEndpoints ): return
print( "Connection from %s" % str( address ) )
try:
source.setsockopt( socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1 )
source.setsockopt( socket.IPPROTO_TCP, socket.TCP_KEEPIDLE, 5 )
source.setsockopt( socket.IPPROTO_TCP, socket.TCP_KEEPINTVL, 10 )
source.setsockopt( socket.IPPROTO_TCP, socket.TCP_KEEPCNT, 2 )
except:
print( "Failed to set keepalive on source connection" )
try:
dest = create_connection( random.sample( currentEndpoints, 1 )[ 0 ] )
except:
print( "Failed to connect to EndpointProcessor" )
else:
try:
try:
dest.setsockopt( socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1 )
dest.setsockopt( socket.IPPROTO_TCP, socket.TCP_KEEPIDLE, 5 )
dest.setsockopt( socket.IPPROTO_TCP, socket.TCP_KEEPINTVL, 10 )
dest.setsockopt( socket.IPPROTO_TCP, socket.TCP_KEEPCNT, 2 )
except:
print( "Failed to set keepalive on dest connection" )
# Send a small connection header that contains the original
# source of the connection.
connectionHeaders = msgpack.packb( address )
dest.sendall( struct.pack( '!I', len( connectionHeaders ) ) )
dest.sendall( connectionHeaders )
gevent.joinall( ( gevent.spawn( forward, source, dest, address, self ),
gevent.spawn( forward, dest, source, address, self ) ) )
finally:
dest.close()
finally:
source.close()
评论列表
文章目录