class QuartzTorrent::PeerHandshake
Represents a bittorrent peer protocol handshake message.
Constants
- InfoHashLen
- PeerIdLen
- ProtocolName
Attributes
infoHash[RW]
peerId[RW]
reserved[RW]
Public Class Methods
new()
click to toggle source
# File lib/quartz_torrent/peermsg.rb, line 15 def initialize @infoHash = nil @peerId = nil @reserved = nil end
unserializeExceptPeerIdFrom(io)
click to toggle source
Unserialize the first part of a PeerHandshake
message from the passed io object up to but not including the peer id. This is needed when handling a handshake from the tracker which doesn’t have a peer id.
# File lib/quartz_torrent/peermsg.rb, line 64 def self.unserializeExceptPeerIdFrom(io) result = PeerHandshake.new len = io.read(1).unpack("C")[0] proto = io.read(len) raise "Unrecognized peer protocol name '#{proto}'" if proto != ProtocolName result.reserved = io.read(8) # reserved result.infoHash = io.read(InfoHashLen) result end
unserializeFrom(io)
click to toggle source
Unserialize a PeerHandshake
message from the passed io object and return it. Throws exceptions on failure.
# File lib/quartz_torrent/peermsg.rb, line 50 def self.unserializeFrom(io) result = PeerHandshake.new len = io.read(1).unpack("C")[0] proto = io.read(len) raise "Unrecognized peer protocol name '#{proto}'" if proto != ProtocolName result.reserved = io.read(8) # reserved result.infoHash = io.read(InfoHashLen) result.peerId = io.read(PeerIdLen) result end
Public Instance Methods
infoHash=(v)
click to toggle source
# File lib/quartz_torrent/peermsg.rb, line 26 def infoHash=(v) raise "InfoHash is not #{InfoHashLen} bytes long" if v.length != InfoHashLen @infoHash = v end
peerId=(v)
click to toggle source
# File lib/quartz_torrent/peermsg.rb, line 21 def peerId=(v) raise "PeerId is not #{PeerIdLen} bytes long" if v.length != PeerIdLen @peerId = v end
serializeTo(io)
click to toggle source
Serialize this PeerHandshake
message to the passed io object. Throws exceptions on failure.
# File lib/quartz_torrent/peermsg.rb, line 36 def serializeTo(io) raise "PeerId is not set" if ! @peerId raise "InfoHash is not set" if ! @infoHash result = [ProtocolName.length].pack("C") result << ProtocolName result << [0,0,0,0,0,0x10,0,0].pack("C8") # Reserved. 0x10 means we support extensions (BEP 10). result << @infoHash result << @peerId io.write result end