使用扭曲控制传入字节的馈送
Controlling the feed of incoming bytes using twisted
我需要解决以下问题
作为连接到服务器的客户端,服务器以以下形式发送数据块:
[4 bytes][msg - block of bytes the size of int(previous 4 bytes)]
使用 twisted
时,我需要使用 msg
部分调用 dataReceived(self, data)
,我不介意接收 4 字节前缀,但我需要以确保我一次获得整个消息块,而不是碎片化。
请指教。
我最终编写了以下自定义接收器
HEADER_LENGTH = 4
class CustomReceiver(Protocol):
_buffer = b''
def dataReceived(self, data):
logger.info(f'DATA RECEIVED: {data}')
data = (self._buffer + data)
header = data[:HEADER_LENGTH]
logger.info(f'header: {header} len: {len(header)}')
while len(header) == HEADER_LENGTH:
response_length = int.from_bytes(header, byteorder='big')
response = data[HEADER_LENGTH:][:response_length]
self.responseReceived(response)
data = data[HEADER_LENGTH + response_length:]
header = data[:HEADER_LENGTH]
self._buffer = header
我不确定是否应该为 dataReceived()
添加锁定机制,同时调用会破坏 _buffer
数据。
StatefulProtocol
对这样的协议很有帮助。
from twisted.protocols.stateful import StatefulProtocol
HEADER_LENGTH = 4
class YourProtocol(StatefulProtocol):
# Define the first handler and what data it expects.
def getInitialState(self):
return (
# The first handler is self._header
self._header,
# And it expects HEADER_LENGTH (4) bytes
HEADER_LENGTH,
)
# When HEADER_LENGTH bytes have been received, this is called.
def _header(self, data):
# It returns a tuple representing the next state handler.
return (
# The next thing we can handle is a response
self._response,
# And the response is made up of this many bytes.
int.from_bytes(header, byteorder='big'),
)
# When the number of bytes from the header has been received,
# this is called.
def _response(self, data):
# Application dispatch of the data
self.responseReceived(data)
# Return to the initial state to process the next received data.
return self.getInitialState()
我需要解决以下问题
作为连接到服务器的客户端,服务器以以下形式发送数据块:
[4 bytes][msg - block of bytes the size of int(previous 4 bytes)]
使用 twisted
时,我需要使用 msg
部分调用 dataReceived(self, data)
,我不介意接收 4 字节前缀,但我需要以确保我一次获得整个消息块,而不是碎片化。
请指教。
我最终编写了以下自定义接收器
HEADER_LENGTH = 4
class CustomReceiver(Protocol):
_buffer = b''
def dataReceived(self, data):
logger.info(f'DATA RECEIVED: {data}')
data = (self._buffer + data)
header = data[:HEADER_LENGTH]
logger.info(f'header: {header} len: {len(header)}')
while len(header) == HEADER_LENGTH:
response_length = int.from_bytes(header, byteorder='big')
response = data[HEADER_LENGTH:][:response_length]
self.responseReceived(response)
data = data[HEADER_LENGTH + response_length:]
header = data[:HEADER_LENGTH]
self._buffer = header
我不确定是否应该为 dataReceived()
添加锁定机制,同时调用会破坏 _buffer
数据。
StatefulProtocol
对这样的协议很有帮助。
from twisted.protocols.stateful import StatefulProtocol
HEADER_LENGTH = 4
class YourProtocol(StatefulProtocol):
# Define the first handler and what data it expects.
def getInitialState(self):
return (
# The first handler is self._header
self._header,
# And it expects HEADER_LENGTH (4) bytes
HEADER_LENGTH,
)
# When HEADER_LENGTH bytes have been received, this is called.
def _header(self, data):
# It returns a tuple representing the next state handler.
return (
# The next thing we can handle is a response
self._response,
# And the response is made up of this many bytes.
int.from_bytes(header, byteorder='big'),
)
# When the number of bytes from the header has been received,
# this is called.
def _response(self, data):
# Application dispatch of the data
self.responseReceived(data)
# Return to the initial state to process the next received data.
return self.getInitialState()