Class: QuartzTorrent::PeerWireMessage
- Inherits:
-
Object
- Object
- QuartzTorrent::PeerWireMessage
- Defined in:
- lib/quartz_torrent/peermsg.rb
Overview
Represents a bittorrent peer protocol wire message (a non-handshake message). All messages other than handshake have a 4-byte length, 1-byte message id, and payload.
Direct Known Subclasses
BitfieldMessage, Cancel, Choke, Extended, Have, Interested, KeepAlive, Piece, Request, Unchoke, Uninterested
Constant Summary collapse
- MessageKeepAlive =
-1
- MessageChoke =
0
- MessageUnchoke =
1
- MessageInterested =
2
- MessageUninterested =
3
- MessageHave =
4
- MessageBitfield =
5
- MessageRequest =
6
- MessagePiece =
7
- MessageCancel =
8
- MessageExtended =
20
Instance Attribute Summary collapse
-
#messageId ⇒ Object
readonly
Get the message id of this message (the message type from the peer wire protocol).
Instance Method Summary collapse
-
#initialize(messageId) ⇒ PeerWireMessage
constructor
Create a new PeerWireMessage with the specified message id.
-
#length ⇒ Object
Total message length.
-
#payloadLength ⇒ Object
Subclasses must implement this method.
-
#serializeTo(io) ⇒ Object
Serialize this message to the passed io.
- #to_s ⇒ Object
-
#unserialize(payload) ⇒ Object
Unserialize the message from the passed string.
Constructor Details
#initialize(messageId) ⇒ PeerWireMessage
Create a new PeerWireMessage with the specified message id.
91 92 93 |
# File 'lib/quartz_torrent/peermsg.rb', line 91 def initialize() @messageId = end |
Instance Attribute Details
#messageId ⇒ Object (readonly)
Get the message id of this message (the message type from the peer wire protocol).
96 97 98 |
# File 'lib/quartz_torrent/peermsg.rb', line 96 def @messageId end |
Instance Method Details
#length ⇒ Object
Total message length
110 111 112 |
# File 'lib/quartz_torrent/peermsg.rb', line 110 def length payloadLength + 5 end |
#payloadLength ⇒ Object
Subclasses must implement this method. It should return an integer.
105 106 107 |
# File 'lib/quartz_torrent/peermsg.rb', line 105 def payloadLength raise "Subclasses of PeerWireMessage must implement payloadLength but #{self.class} didn't" end |
#serializeTo(io) ⇒ Object
Serialize this message to the passed io
99 100 101 102 |
# File 'lib/quartz_torrent/peermsg.rb', line 99 def serializeTo(io) io.write [payloadLength+1].pack("N") io.write [@messageId].pack("C") end |
#to_s ⇒ Object
119 120 121 |
# File 'lib/quartz_torrent/peermsg.rb', line 119 def to_s "#{self.class} message" end |
#unserialize(payload) ⇒ Object
Unserialize the message from the passed string.
115 116 117 |
# File 'lib/quartz_torrent/peermsg.rb', line 115 def unserialize(payload) raise "Subclasses of PeerWireMessage must implement unserialize but #{self.class} didn't" end |