Changeset View
Changeset View
Standalone View
Standalone View
test/functional/test_framework/mininode.py
Show First 20 Lines • Show All 206 Lines • ▼ Show 20 Lines | def handle_write(self): | ||||
try: | try: | ||||
sent = self.send(self.sendbuf) | sent = self.send(self.sendbuf) | ||||
except: | except: | ||||
self.handle_close() | self.handle_close() | ||||
return | return | ||||
self.sendbuf = self.sendbuf[sent:] | self.sendbuf = self.sendbuf[sent:] | ||||
def send_message(self, message, pushbuf=False): | def format_message(self, message): | ||||
"""Send a P2P message over the socket. | |||||
This method takes a P2P payload, builds the P2P header and adds | |||||
the message to the send buffer to be sent over the socket.""" | |||||
if self.state != "connected" and not pushbuf: | |||||
raise IOError('Not connected, no pushbuf') | |||||
self._log_message("send", message) | |||||
command = message.command | command = message.command | ||||
data = message.serialize() | data = message.serialize() | ||||
tmsg = MAGIC_BYTES[self.network] | tmsg = MAGIC_BYTES[self.network] | ||||
tmsg += command | tmsg += command | ||||
tmsg += b"\x00" * (12 - len(command)) | tmsg += b"\x00" * (12 - len(command)) | ||||
tmsg += struct.pack("<I", len(data)) | tmsg += struct.pack("<I", len(data)) | ||||
th = sha256(data) | th = sha256(data) | ||||
h = sha256(th) | h = sha256(th) | ||||
tmsg += h[:4] | tmsg += h[:4] | ||||
tmsg += data | tmsg += data | ||||
return tmsg | |||||
def send_message(self, message, pushbuf=False): | |||||
"""Send a P2P message over the socket. | |||||
This method takes a P2P payload, builds the P2P header and adds | |||||
the message to the send buffer to be sent over the socket.""" | |||||
if self.state != "connected" and not pushbuf: | |||||
raise IOError('Not connected, no pushbuf') | |||||
self._log_message("send", message) | |||||
tmsg = self.format_message(message) | |||||
self.send_raw_message(tmsg, pushbuf) | |||||
def send_raw_message(self, tmsg, pushbuf=False): | |||||
"""Send any raw message over the socket. | |||||
This method adds a raw message to the send buffer to be sent over the | |||||
socket.""" | |||||
if self.state != "connected" and not pushbuf: | |||||
raise IOError('Not connected, no pushbuf') | |||||
with mininode_lock: | with mininode_lock: | ||||
if (len(self.sendbuf) == 0 and not pushbuf): | if (len(self.sendbuf) == 0 and not pushbuf): | ||||
try: | try: | ||||
sent = self.send(tmsg) | sent = self.send(tmsg) | ||||
self.sendbuf = tmsg[sent:] | self.sendbuf = tmsg[sent:] | ||||
except BlockingIOError: | except BlockingIOError: | ||||
self.sendbuf = tmsg | self.sendbuf = tmsg | ||||
else: | else: | ||||
▲ Show 20 Lines • Show All 244 Lines • Show Last 20 Lines |