Class: Baykit::BayServer::Protocol::PacketPartAccessor
- Inherits:
-
Object
- Object
- Baykit::BayServer::Protocol::PacketPartAccessor
- Includes:
- Baykit::BayServer, Util
- Defined in:
- lib/baykit/bayserver/protocol/packet_part_accessor.rb
Instance Attribute Summary collapse
-
#max_len ⇒ Object
readonly
Returns the value of attribute max_len.
-
#packet ⇒ Object
readonly
Returns the value of attribute packet.
-
#pos ⇒ Object
readonly
Returns the value of attribute pos.
-
#start ⇒ Object
readonly
Returns the value of attribute start.
Instance Method Summary collapse
- #check_read(len) ⇒ Object
- #check_write(len) ⇒ Object
- #forward(len) ⇒ Object
- #get_byte ⇒ Object
- #get_bytes(buf, ofs = 0, len = buf.length) ⇒ Object
- #get_int ⇒ Object
- #get_short ⇒ Object
-
#initialize(pkt, start, max_len) ⇒ PacketPartAccessor
constructor
A new instance of PacketPartAccessor.
- #put_byte(b) ⇒ Object
- #put_bytes(buf, ofs = 0, len = buf.length) ⇒ Object
- #put_int(val) ⇒ Object
- #put_short(val) ⇒ Object
- #put_string(str) ⇒ Object
Constructor Details
#initialize(pkt, start, max_len) ⇒ PacketPartAccessor
Returns a new instance of PacketPartAccessor.
16 17 18 19 20 21 |
# File 'lib/baykit/bayserver/protocol/packet_part_accessor.rb', line 16 def initialize(pkt, start, max_len) @packet = pkt @start = start @max_len = max_len @pos = 0 end |
Instance Attribute Details
#max_len ⇒ Object (readonly)
Returns the value of attribute max_len.
13 14 15 |
# File 'lib/baykit/bayserver/protocol/packet_part_accessor.rb', line 13 def max_len @max_len end |
#packet ⇒ Object (readonly)
Returns the value of attribute packet.
11 12 13 |
# File 'lib/baykit/bayserver/protocol/packet_part_accessor.rb', line 11 def packet @packet end |
#pos ⇒ Object (readonly)
Returns the value of attribute pos.
14 15 16 |
# File 'lib/baykit/bayserver/protocol/packet_part_accessor.rb', line 14 def pos @pos end |
#start ⇒ Object (readonly)
Returns the value of attribute start.
12 13 14 |
# File 'lib/baykit/bayserver/protocol/packet_part_accessor.rb', line 12 def start @start end |
Instance Method Details
#check_read(len) ⇒ Object
100 101 102 103 104 105 |
# File 'lib/baykit/bayserver/protocol/packet_part_accessor.rb', line 100 def check_read(len) max_len = (@max_len >= 0) ? @max_len : (@packet.buf_len - @start) if @pos + len > max_len raise Sink.new("Invalid array index") end end |
#check_write(len) ⇒ Object
107 108 109 110 111 |
# File 'lib/baykit/bayserver/protocol/packet_part_accessor.rb', line 107 def check_write(len) if @max_len > 0 && @pos + len > @max_len raise Sink.new("Buffer overflow") end end |
#forward(len) ⇒ Object
113 114 115 116 117 118 |
# File 'lib/baykit/bayserver/protocol/packet_part_accessor.rb', line 113 def forward(len) @pos += len if @start + @pos > @packet.buf_len @packet.buf_len = @start + @pos end end |
#get_byte ⇒ Object
70 71 72 73 74 |
# File 'lib/baykit/bayserver/protocol/packet_part_accessor.rb', line 70 def get_byte buf = StringUtil.alloc(1) get_bytes(buf, 0, 1) buf[0].codepoints[0] end |
#get_bytes(buf, ofs = 0, len = buf.length) ⇒ Object
76 77 78 79 80 81 82 83 84 |
# File 'lib/baykit/bayserver/protocol/packet_part_accessor.rb', line 76 def get_bytes(buf, ofs=0, len=buf.length) if buf == nil raise Sink.new("nil") end check_read(len) buf[ofs, len] = @packet.buf[@start + @pos, len] @pos += len end |
#get_int ⇒ Object
92 93 94 95 96 97 98 |
# File 'lib/baykit/bayserver/protocol/packet_part_accessor.rb', line 92 def get_int b1 = get_byte b2 = get_byte b3 = get_byte b4 = get_byte b1 << 24 | b2 << 16 | b3 << 8 | b4 end |
#get_short ⇒ Object
86 87 88 89 90 |
# File 'lib/baykit/bayserver/protocol/packet_part_accessor.rb', line 86 def get_short h = get_byte l = get_byte h << 8 | l end |
#put_byte(b) ⇒ Object
23 24 25 26 27 |
# File 'lib/baykit/bayserver/protocol/packet_part_accessor.rb', line 23 def put_byte(b) buf = StringUtil.alloc(1) buf << b put_bytes(buf, 0, 1) end |
#put_bytes(buf, ofs = 0, len = buf.length) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/baykit/bayserver/protocol/packet_part_accessor.rb', line 29 def put_bytes(buf, ofs=0, len=buf.length) if len > 0 check_write(len) #while(@start + @pos + len > @packet.buf.length) # packet.expand() #end begin @packet.buf[@start + @pos, len] = buf[ofs, len] rescue IndexError => e raise IndexError.new("data exceeds packet size: len=#{len} pktlen=#{buf.length - @start}") end forward(len) end end |
#put_int(val) ⇒ Object
53 54 55 56 57 58 59 60 61 |
# File 'lib/baykit/bayserver/protocol/packet_part_accessor.rb', line 53 def put_int(val) b1 = val >> 24 & 0xFF b2 = val >> 16 & 0xFF b3 = val >> 8 & 0xFF b4 = val & 0xFF buf = StringUtil.alloc(4) buf << b1 << b2 << b3 << b4 put_bytes(buf) end |
#put_short(val) ⇒ Object
45 46 47 48 49 50 51 |
# File 'lib/baykit/bayserver/protocol/packet_part_accessor.rb', line 45 def put_short(val) h = val >> 8 & 0xFF l = val & 0xFF buf = StringUtil.alloc(2) buf << h << l put_bytes(buf) end |
#put_string(str) ⇒ Object
63 64 65 66 67 68 |
# File 'lib/baykit/bayserver/protocol/packet_part_accessor.rb', line 63 def put_string(str) if str == nil raise Sink.new("nil") end put_bytes(StringUtil.to_bytes(str)) end |