@@ -76,7 +76,7 @@ def __init__(self):
76
76
77
77
# Message receiving methods
78
78
79
- def deliver (self , conn , message ):
79
+ def on_message (self , conn , message ):
80
80
"""Receive message and dispatch message to appropriate callback.
81
81
82
82
We keep a count of how many of each message type has been received
@@ -233,12 +233,14 @@ def __init__(self, dstaddr, dstport, callback, net="regtest", services=NODE_NETW
233
233
# Connection and disconnection methods
234
234
235
235
def handle_connect (self ):
236
+ """asyncore callback when a connection is opened."""
236
237
if self .state != "connected" :
237
238
logger .debug ("Connected & Listening: %s:%d" % (self .dstaddr , self .dstport ))
238
239
self .state = "connected"
239
240
self .cb .on_open (self )
240
241
241
242
def handle_close (self ):
243
+ """asyncore callback when a connection is closed."""
242
244
logger .debug ("Closing connection to: %s:%d" % (self .dstaddr , self .dstport ))
243
245
self .state = "closed"
244
246
self .recvbuf = b""
@@ -250,24 +252,27 @@ def handle_close(self):
250
252
self .cb .on_close (self )
251
253
252
254
def disconnect_node (self ):
253
- """ Disconnect the p2p connection.
255
+ """Disconnect the p2p connection.
254
256
255
257
Called by the test logic thread. Causes the p2p connection
256
258
to be disconnected on the next iteration of the asyncore loop."""
257
259
self .disconnect = True
258
260
259
261
# Socket read methods
260
262
261
- def readable (self ):
262
- return True
263
-
264
263
def handle_read (self ):
264
+ """asyncore callback when data is read from the socket."""
265
265
t = self .recv (8192 )
266
266
if len (t ) > 0 :
267
267
self .recvbuf += t
268
- self .got_data ()
268
+ self ._on_data ()
269
+
270
+ def _on_data (self ):
271
+ """Try to read P2P messages from the recv buffer.
269
272
270
- def got_data (self ):
273
+ This method reads data from the buffer in a loop. It deserializes,
274
+ parses and verifies the P2P header, then passes the P2P payload to
275
+ the on_message callback for processing."""
271
276
try :
272
277
while True :
273
278
if len (self .recvbuf ) < 4 :
@@ -292,24 +297,27 @@ def got_data(self):
292
297
f = BytesIO (msg )
293
298
t = MESSAGEMAP [command ]()
294
299
t .deserialize (f )
295
- self .got_message (t )
300
+ self ._log_message ("receive" , t )
301
+ self .on_message (t )
296
302
except Exception as e :
297
303
logger .exception ('Error reading message:' , repr (e ))
298
304
raise
299
305
300
- def got_message (self , message ):
301
- self . _log_message ( "receive" , message )
302
- self .cb .deliver (self , message )
306
+ def on_message (self , message ):
307
+ """Callback for processing a P2P payload. Calls into NodeConnCB."""
308
+ self .cb .on_message (self , message )
303
309
304
310
# Socket write methods
305
311
306
312
def writable (self ):
313
+ """asyncore method to determine whether the handle_write() callback should be called on the next loop."""
307
314
with mininode_lock :
308
315
pre_connection = self .state == "connecting"
309
316
length = len (self .sendbuf )
310
317
return (length > 0 or pre_connection )
311
318
312
319
def handle_write (self ):
320
+ """asyncore callback when data should be written to the socket."""
313
321
with mininode_lock :
314
322
# asyncore does not expose socket connection, only the first read/write
315
323
# event, thus we must check connection manually here to know when we
@@ -327,6 +335,10 @@ def handle_write(self):
327
335
self .sendbuf = self .sendbuf [sent :]
328
336
329
337
def send_message (self , message , pushbuf = False ):
338
+ """Send a P2P message over the socket.
339
+
340
+ This method takes a P2P payload, builds the P2P header and adds
341
+ the message to the send buffer to be sent over the socket."""
330
342
if self .state != "connected" and not pushbuf :
331
343
raise IOError ('Not connected, no pushbuf' )
332
344
self ._log_message ("send" , message )
@@ -353,6 +365,7 @@ def send_message(self, message, pushbuf=False):
353
365
# Class utility methods
354
366
355
367
def _log_message (self , direction , msg ):
368
+ """Logs a message being sent or received over the connection."""
356
369
if direction == "send" :
357
370
log_message = "Send message to "
358
371
elif direction == "receive" :
0 commit comments