[Twisted-Python] non-blocking .read

Bob Ippolito bob at redivi.com
Wed Apr 2 14:22:21 MST 2003


Just to bring generators back up..  Note that of course, the wrappers 
for read and the actual flow.BufferedProtocol class don't exist.. but 
their implementation is possible with the given syntax.
I'm imagining that a "flow" version of Int32Receiver could be coded 
like this:

class NetstringProtocol(flow.BufferedProtocol):
	def connectionMade(self):
		pass

	def handleConnection(self):
		self.connectionMade()
		reason = None

		# the string receive loop
		while 1:
			# read int32, size of int32 is 4
			result = self.read(4)
			yield result
			if result.isFailure():
				reason = result.get()
				break
			strlen, = struct.unpack('!I', result.get())
			if strlen <= 0:
				endFailure = Failure(ValueError('Zero length strings are not 
valid'))
				break
			# read string
			result = self.read(strlen)
			yield result
			if result.isFailure():
				reason = result.get()
				break
			self.stringReceived(result.get())

		# exception caused connection to drop, or connection dropped on its 
own
		self.connectionLost(reason)

	def connectionLost(self, reason):
		pass	





More information about the Twisted-Python mailing list