endpoint_proxy.py 文件源码

python
阅读 28 收藏 0 点赞 0 评论 0

项目:lc_cloud 作者: refractionPOINT 项目源码 文件源码
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()
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号