我一直在基于kivy和扭曲的框架P2P chat application工作,我希望得到土地的平躺,但我遇到了这个问题,如果客户端需要连接到另一个客户端(通过其服务器)需要执行一种握手,扭曲的蟒蛇self.transport连接到服务器后不工作
现在初始步骤是连接到客户端;
conn = reactor.connectTCP(host, port, CommCoreClientFactory(self))
然后写入连接;
conn.transport.write("data..\r\n")
这里连接成功,但线路不经过去,我都凝结着上面的代码按我的意图,请参阅通信/ twisscomm方法add_peer_to_swarm(个体经营,PID,主机)。 py
我的客户端协议/工厂和服务器协议/工厂代码可以在下面找到; (它们可以在通信/ commcoreclient.py和通讯/ commcoreserver.py RESP被发现。)
客户端协议
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
(原谅伪劣缩进!)
我想知道我可能会错误。 另外如果您有兴趣,我已经提交了这个issue。如果你通过我的代码(comm/twiscomm.py),你会发现有些东西可能无法在服务器端完全工作,尤其是handle_received_data(),但是由于没有收到数据,所以甚至不会调用它。
对不起,我打算说conn.transport.write(“data \ r \ n”)。进行更正。感谢您及时的回复。 – vaizguy
我已更新答案以反映您更改的问题:-)。 – Glyph
+1看来我的基本理解是有缺陷的,只能通过Protocol类访问传输。谢谢! – vaizguy