1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 from Anomos.Protocol import CHOKE, UNCHOKE, INTERESTED, NOT_INTERESTED, \
19 HAVE, BITFIELD, REQUEST, PIECE, CANCEL, \
20 TCODE, CONFIRM, ENCRYPTED, RELAY, BREAK, PARTIAL, \
21 ACKBREAK
22 from Anomos.Protocol import tobinary, toint, AnomosProtocol
23 from Anomos.bitfield import Bitfield
24 from Anomos import log_on_call, LOG as log
25
27
29 AnomosProtocol.__init__(self)
30
31 self.msgmap.update({CHOKE: self.got_choke,\
32 UNCHOKE: self.got_unchoke,\
33 INTERESTED: self.got_interested,\
34 NOT_INTERESTED: self.got_not_interested,\
35 HAVE: self.got_have,\
36 BITFIELD: self.got_bitfield,\
37 REQUEST: self.got_request,\
38 PIECE: self.got_piece,\
39 CANCEL: self.got_cancel,\
40 CONFIRM: self.got_confirm, \
41 ENCRYPTED: self.got_encrypted, \
42 RELAY: self.got_relay, \
43 BREAK: self.got_break, \
44 PARTIAL: self.got_partial, \
45 ACKBREAK: self.got_ack_break})
47 log.warning("Invalid message of type %02x on %s. Closing stream."% \
48 (ord(t), self.uniq_id()))
49 self.close()
51 """ Send method for file transfer messages.
52 ie. CHOKE, INTERESTED, PIECE """
53 payload = ENCRYPTED + self.e2e_key.encrypt(type + message)
54 self.neighbor.queue_message(self.stream_id, RELAY + payload)
55
56
57
58
65 if self.e2e_key is not None:
66 m = self.e2e_key.decrypt(message[1:])
67 self.got_message(m)
68 else:
69 raise RuntimeError("Received encrypted data before we were ready")
77 if self.sent_break:
78 if not self.closed:
79 self.shutdown()
80 self.neighbor.end_stream(self.stream_id)
81 self.neighbor = None
83 p_remain = toint(message[1:5])
84 payload = message[5:]
85 self.partial_recv += payload
86 if len(self.partial_recv) > self.neighbor.config['max_message_length']:
87 log.error("Received message longer than max length, %d"%l)
88 return
89 if len(payload) == p_remain:
90 self.got_message(self.partial_recv)
91 self.partial_recv = ''
119 i = toint(message[1:5])
120 if i >= self.torrent.numpieces:
121 self.fatal_error("Piece index out of range")
122 return
123 if self.upload:
124 self.upload.got_request(i, toint(message[5:9]), toint(message[9:]))
126 i = toint(message[1:5])
127 if i >= self.torrent.numpieces:
128 self.fatal_error("Piece index out of range")
129 return
130 if self.upload:
131 self.upload.got_cancel(i, toint(message[5:9]), toint(message[9:]))
133 i = toint(message[1:5])
134 if i >= self.torrent.numpieces:
135 self.fatal_error("Piece index out of range")
136 return
137 if self.download.got_piece(i, toint(message[5:9]), message[9:]):
138 for ep in self.torrent.active_streams:
139 ep.send_have(i)
140
141
173
174
175
179