Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import weakref
- from twisted.internet import reactor
- from twisted.internet.endpoints import TCP4ServerEndpoint
- from twisted.python.sendmsg import getsockfam
- from twisted.internet.protocol import Factory, Protocol
- import twisted.internet.abstract
- class EchoProtocol(Protocol):
- def dataReceived(self, data):
- self.transport.write(data)
- class EchoFactory(Factory):
- protocol = EchoProtocol
- class TransferProtocol(Protocol):
- def dataReceived(self, data):
- self.transport.write('main process still listening!: %s' % (data))
- def connectionMade(self):
- self.transport.write('this message should make it to the subprocess\n')
- # attempt 1: do nothing
- # everything works fine in the adopt (including receiving the written message), but old protocol still exists (though isn't doing anything)
- # attempt 1: try calling loseConnection
- # we lose connection before the adopt opens the socket (presumably TCP disconnect message was sent)
- #
- # self.transport.loseConnection()
- # attempt 2: try calling abortConnection
- # result is same as loseConnection
- #
- # self.transport.abortConnection()
- # attempt 3: try monkey patching the socket close out and calling loseConnection
- # result: same as doing nothing-- adopt works (including receiving the written message), old protocol still exists
- #
- # def ignored(*args, **kwargs):
- # print 'ignored :D'
- #
- # self.transport._closeSocket = ignored
- # self.transport.loseConnection()
- reactor.callLater(0, adopt, self.transport.fileno())
- class ServerFactory(Factory):
- def buildProtocol(self, addr):
- p = TransferProtocol()
- self.ref = weakref.ref(p)
- return p
- f = ServerFactory()
- def adopt(fileno):
- print "does old protocol still exist?: %r" % (f.ref())
- reactor.adoptStreamConnection(fileno, getsockfam(fileno), EchoFactory())
- port = 1337
- endpoint = TCP4ServerEndpoint(reactor, port)
- d = endpoint.listen(f)
- reactor.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement