我一直在开发基于 kivy 和 twisted 框架的P2P 聊天应用程序,我希望能有所了解,但我遇到了这个问题,如果客户端需要连接到另一个客户端(通过其服务器)它需要进行一种握手,
现在第一步是连接到客户端;
conn = reactor.connectTCP(host, port, CommCoreClientFactory(self))
然后写入连接;
conn.transport.write("data..\r\n")
这里连接成功但是线路不通,我按照自己的意图把上面的代码压缩了,请看comm/twisscomm.py中的add_peer_to_swarm(self, pid, host)方法
我的 clientProtocol/Factory 和 serverProtocol/Factory 代码可以在下面找到;(它们可以在 comm/commcoreclient.py 和 comm/commcoreserver.py 中找到。)
客户端协议
class CommCoreClientProtocol(LineReceiver):
"""
Communications core client protocol code.
"""
def __init__(self, factory):
self._peer_host = None
self._peer_port = None
self._peer_repr = None
self.factory = factory
def connectionMade(self):
"Run when connection is established with server."
self._peer_host = self.transport.getPeer().host
self._peer_port = self.transport.getPeer().port
self._peer_repr = self._peer_host + " on " + str(self._peer_port)
Logger.debug(
"Connection success! Connected to {}".format(self._peer_repr))
def connectionLost(self, reason):
"Run when connection is lost with server."
Logger.warn("Lost connection with peer {}".format(self._peer_repr))
def lineReceived(self, line):
"Run when response is recieved from server."
response = self.factory.app.handle_response(line)
if response:
print response
Logger.debug("Recieved : {}".format(base64.b64encode(line)))
客户工厂
class CommCoreClientFactory(protocol.ReconnectingClientFactory):
protocol = CommCoreClientProtocol
def __init__(self, app):
self.app = app
def startedConnecting(self, connector):
"Run when initiaition of connection takes place."
Logger.debug("Attempting connection...")
def buildProtocol(self, addr):
"Build protocol on successful connection."
Logger.debug("Connected.")
Logger.debug("Resetting reconnection delay.")
# Reset the delay on connection success
self.resetDelay()
# Overridden build protocol
#client_protocol = self.protocol()
#client_protocol.factory = self
#return client_protocol
return CommCoreClientProtocol(self)
def clientConnectionLost(self, connector, reason):
"Run when connection with server is lost."
#self.app.print_message("connection lost")
Logger.debug("Lost connection: {}".format(reason.getErrorMessage()))
return protocol.ReconnectingClientFactory.clientConnectionLost(
self, connector, reason
)
def clientConnectionFailed(self, connector, reason):
"Run when attempt to connect with server fails."
#self.app.print_message("connection failed")
Logger.debug("Connection failed. {}".format(reason.getErrorMessage()))
return protocol.ReconnectingClientFactory.clientConnectionFailed(
self, connector, reason
)
服务器协议
class CommCoreServerProtocol(LineReceiver):
"Server backend to pocess the commands"
def __init__(self):
self._peer_host = None
self._peer_port = None
self._peer_repr = None
def connectionMade(self):
"Run when connection is established with server."
self._peer_host = self.transport.getPeer().host
self._peer_port = self.transport.getPeer().port
self._peer_repr = self._peer_host + " on " + str(self._peer_port)
Logger.debug(
"Connection success! Connected to {}".format(self._peer_repr))
def connectionLost(self, reason):
"Run when connection is lost with server."
Logger.error("Lost connection with peer {}".format(self._peer_repr))
def lineReceived(self, line):
print "REVCD LINE!", line
response = self.factory.app.handle_recieved_data(line)
if response:
#self.transport.write(response)
self.sendLine(response)
服务器工厂
class CommCoreServerFactory(protocol.Factory):
protocol = CommCoreServerProtocol
def __init__(self, app):
self.app = app
(请原谅伪劣的缩进!)
我想知道我哪里可能出错了。另外,如果您有兴趣,我已经提交了此问题。如果您浏览我的代码 (comm/twiscomm.py),您会发现有些事情可能无法在服务器端完全正常工作,尤其是使用 handle_received_data() 时,但由于未收到数据,因此甚至不会调用它。