X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=p2pool%2Futil%2Fp2protocol.py;h=6e8634e6e4d1b43ccbb2eae5025ca93ce24f7aa7;hb=e487ae91601659d74915899930eba08371534d28;hp=e3f83a2656569a35221be3bf2c66f0706cef2d8e;hpb=0396dd8c218bc1696fd723c786fba2fa66f0a01a;p=p2pool.git diff --git a/p2pool/util/p2protocol.py b/p2pool/util/p2protocol.py index e3f83a2..6e8634e 100644 --- a/p2pool/util/p2protocol.py +++ b/p2pool/util/p2protocol.py @@ -15,24 +15,12 @@ class TooLong(Exception): pass class Protocol(protocol.Protocol): - def __init__(self, message_prefix, max_payload_length, traffic_happened=variable.Event()): + def __init__(self, message_prefix, max_payload_length, traffic_happened=variable.Event(), ignore_trailing_payload=False): self._message_prefix = message_prefix self._max_payload_length = max_payload_length self.dataReceived2 = datachunker.DataChunker(self.dataReceiver()) - self.paused_var = variable.Variable(False) self.traffic_happened = traffic_happened - - def connectionMade(self): - self.transport.registerProducer(self, True) - - def pauseProducing(self): - self.paused_var.set(True) - - def resumeProducing(self): - self.paused_var.set(False) - - def stopProducing(self): - pass + self.ignore_trailing_payload = ignore_trailing_payload def dataReceived(self, data): self.traffic_happened.happened('p2p/in', len(data)) @@ -53,7 +41,10 @@ class Protocol(protocol.Protocol): payload = yield length if hashlib.sha256(hashlib.sha256(payload).digest()).digest()[:4] != checksum: - print 'invalid hash for', self.transport.getPeer().host, repr(command), length, checksum.encode('hex'), hashlib.sha256(hashlib.sha256(payload).digest()).digest()[:4].encode('hex'), payload.encode('hex') + print 'invalid hash for', self.transport.getPeer().host, repr(command), length, checksum.encode('hex') + if p2pool.DEBUG: + print hashlib.sha256(hashlib.sha256(payload).digest()).digest()[:4].encode('hex'), payload.encode('hex') + self.badPeerHappened() continue type_ = getattr(self, 'message_' + command, None) @@ -63,11 +54,11 @@ class Protocol(protocol.Protocol): continue try: - self.packetReceived(command, type_.unpack(payload)) + self.packetReceived(command, type_.unpack(payload, self.ignore_trailing_payload)) except: print 'RECV', command, payload[:100].encode('hex') + ('...' if len(payload) > 100 else '') log.err(None, 'Error handling message: (see RECV line)') - self.transport.loseConnection() + self.disconnect() def packetReceived(self, command, payload2): handler = getattr(self, 'handle_' + command, None) @@ -79,8 +70,16 @@ class Protocol(protocol.Protocol): if getattr(self, 'connected', True) and not getattr(self, 'disconnecting', False): handler(**payload2) + def disconnect(self): + if hasattr(self.transport, 'abortConnection'): + # Available since Twisted 11.1 + self.transport.abortConnection() + else: + # This doesn't always close timed out connections! warned about in main + self.transport.loseConnection() + def badPeerHappened(self): - self.transport.loseConnection() + self.disconnect() def sendPacket(self, command, payload2): if len(command) >= 12: @@ -95,7 +94,6 @@ class Protocol(protocol.Protocol): data = self._message_prefix + struct.pack('<12sI', command, len(payload)) + hashlib.sha256(hashlib.sha256(payload).digest()).digest()[:4] + payload self.traffic_happened.happened('p2p/out', len(data)) self.transport.write(data) - return self.paused_var.get_when_satisfies(lambda paused: not paused) def __getattr__(self, attr): prefix = 'send_'