Class: Warchat::Network::BinaryWriter
- Inherits:
-
Object
- Object
- Warchat::Network::BinaryWriter
- Defined in:
- lib/warchat/network/binary_writer.rb
Instance Attribute Summary collapse
-
#stream ⇒ Object
readonly
Returns the value of attribute stream.
Instance Method Summary collapse
- #byte(obj) ⇒ Object
- #bytes(obj) ⇒ Object
-
#initialize(stream) ⇒ BinaryWriter
constructor
A new instance of BinaryWriter.
- #string(obj) ⇒ Object
- #write(obj) ⇒ Object
- #write_array(obj) ⇒ Object
- #write_bool(obj) ⇒ Object (also: #write_true_class, #write_false_class)
- #write_byte_string(obj) ⇒ Object
- #write_hash(obj) ⇒ Object
- #write_integer(obj) ⇒ Object
- #write_string(obj) ⇒ Object
Constructor Details
#initialize(stream) ⇒ BinaryWriter
Returns a new instance of BinaryWriter.
8 9 10 |
# File 'lib/warchat/network/binary_writer.rb', line 8 def initialize stream @stream = stream end |
Instance Attribute Details
#stream ⇒ Object (readonly)
Returns the value of attribute stream.
6 7 8 |
# File 'lib/warchat/network/binary_writer.rb', line 6 def stream @stream end |
Instance Method Details
#byte(obj) ⇒ Object
77 78 79 80 |
# File 'lib/warchat/network/binary_writer.rb', line 77 def byte obj obj = (obj.is_a? Integer and [obj].pack('C') or obj.to_s[0..0]) @stream.print obj end |
#bytes(obj) ⇒ Object
82 83 84 |
# File 'lib/warchat/network/binary_writer.rb', line 82 def bytes obj @stream.print(obj.to_s) end |
#string(obj) ⇒ Object
71 72 73 74 75 |
# File 'lib/warchat/network/binary_writer.rb', line 71 def string obj obj = obj.to_s int_32 obj.length @stream.print obj end |
#write(obj) ⇒ Object
12 13 14 15 16 17 |
# File 'lib/warchat/network/binary_writer.rb', line 12 def write obj m = (obj.class.ancestors.map do |c| "write_#{File.basename c.name.underscore}".to_sym end.find(&method(:respond_to?)) or :write_string) send(m,obj) end |
#write_array(obj) ⇒ Object
43 44 45 46 47 48 49 |
# File 'lib/warchat/network/binary_writer.rb', line 43 def write_array obj byte 2 int_32 obj.size obj.each do |v| write(v) end end |
#write_bool(obj) ⇒ Object Also known as: write_true_class, write_false_class
57 58 59 60 |
# File 'lib/warchat/network/binary_writer.rb', line 57 def write_bool obj byte 6 byte(obj ? 1 : 0) end |
#write_byte_string(obj) ⇒ Object
51 52 53 54 55 |
# File 'lib/warchat/network/binary_writer.rb', line 51 def write_byte_string obj byte 4 int_32 obj.length bytes obj end |
#write_hash(obj) ⇒ Object
34 35 36 37 38 39 40 41 |
# File 'lib/warchat/network/binary_writer.rb', line 34 def write_hash obj byte 1 int_32 obj.size obj.each do |k,v| string(k.to_s) write(v) end end |
#write_integer(obj) ⇒ Object
24 25 26 27 28 29 30 31 32 |
# File 'lib/warchat/network/binary_writer.rb', line 24 def write_integer obj if obj < 2**32 byte 3 int_32 obj else byte 7 int_64 obj end end |
#write_string(obj) ⇒ Object
19 20 21 22 |
# File 'lib/warchat/network/binary_writer.rb', line 19 def write_string obj byte 5 string obj.to_s end |