Ticket #2315 (closed enhancement: fixed)
Request to add int8 string support to basic protocols
| Reported by: | metcalfc | Owned by: | therve |
|---|---|---|---|
| Priority: | highest | Milestone: | |
| Component: | core | Keywords: | |
| Cc: | therve, exarkun | Branch: | |
| Author: | Launchpad Bug: |
Description
It would be helpful if support for an int8 string receiver were added to the basic protocol suite. There already is support for int16 and int32. It would make sense to cover the three basic int types.
I'm including the source for an int8 implementation.
class Int8StringReceiver(protocol.Protocol, _PauseableMixin):
"""A receiver for int8-prefixed strings.
An int8 string is a string prefixed by 1 bytes, the 8-bit length of
the string encoded in network byte order.
This class publishes the same interface as NetstringReceiver.
"""
recvd = ""
def stringReceived(self, msg):
"""Override this.
"""
raise NotImplementedError
def dataReceived(self, recd):
"""Convert int8 prefixed strings into calls to stringReceived.
"""
self.recvd = self.recvd + recd
while len(self.recvd) > 0 and not self.paused:
length = ord(self.recvd[0])
if len(self.recvd) < length+1:
break
packet = self.recvd[1:length+1]
self.recvd = self.recvd[length+1:]
self.stringReceived(packet)
def sendString(self, data):
"""Send an int8-prefixed string to the other end of the connection.
"""
assert len(data) < 256, "message too long"
self.transport.write(struct.pack("!h",len(data)) + data)
Attachments
Change History
Note: See
TracTickets for help on using
tickets.

