在 rtmp.py 中处理死套接字

Dealing with dead sockets in rtmp.py

我目前正在使用 rtmp.py 作为 RTMP 直播服务器。它运行良好,但是一天有几次,一个可能连接不良的人会离开并留下他们的插座。更糟糕的是,除了杀死整个服务器之外,没有什么可以关闭它。 Killcx 甚至不会触及连接。这显然不理想。当套接字仍然绑定到例如 /live1 时,其他用户无法使用此安装点。

项目可在 Github 上查看源代码。我对 Python 非常陌生,在我的研究中,我相信客户端连接严重中断导致无法关闭()。我曾试图寻求开发人员的帮助,但没有成功。如果有人能帮我实现一个补丁,我很乐意将其提交到项目页面,希望它也能使其他人受益。

rtmp.py source

如果您需要我提供更多信息,请告诉我。提前致谢!

创作者联系了我。对于可能遇到此问题的未来用户,我提供了一个对我有用的答案。对 运行(self) 进行了以下更改:rtmp.py 中的定义。根据您的个人喜好调整值或使用 as-is。评论为 'Issue 106'.

def run(self):
try:
    while True:
        sock, remote = (yield multitask.accept(self.sock))  # receive client TCP
        if sock == None:
            if _debug: print 'rtmp.Server accept(sock) returned None.'
            break
        if _debug: print 'connection received from', remote
        sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) # make it non-block
        sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1) # Issue #106
        sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPIDLE, 10) # Issue #106
        sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPINTVL, 10) # Issue #106
        sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPCNT, 2) # Issue #106
        client = Client(sock, self)
except GeneratorExit: pass # terminate
except:
    if _debug: print 'rtmp.Server exception ', (sys and sys.exc_info() or None)

if (self.sock):
    try: self.sock.close(); self.sock = None
    except: pass
if (self.queue):
    yield self.queue.put((None, None))
    self.queue = None