Class: MysqlPR::Packet
- Inherits:
-
Object
- Object
- MysqlPR::Packet
- Defined in:
- lib/mysql-pr/packet.rb
Class Method Summary collapse
-
.lcb(num) ⇒ Object
convert Numeric to LengthCodedBinary.
-
.lcs(str) ⇒ Object
convert String to LengthCodedString.
Instance Method Summary collapse
- #eof? ⇒ Boolean
-
#initialize(data) ⇒ Packet
constructor
A new instance of Packet.
- #lcb ⇒ Object
- #lcs ⇒ Object
- #read(len) ⇒ Object
- #string ⇒ Object
- #to_s ⇒ Object
- #ulong ⇒ Object
- #ushort ⇒ Object
- #utiny ⇒ Object
Constructor Details
#initialize(data) ⇒ Packet
Returns a new instance of Packet.
18 19 20 |
# File 'lib/mysql-pr/packet.rb', line 18 def initialize(data) @data = data end |
Class Method Details
.lcb(num) ⇒ Object
convert Numeric to LengthCodedBinary
4 5 6 7 8 9 10 |
# File 'lib/mysql-pr/packet.rb', line 4 def self.lcb(num) return "\xfb" if num.nil? return [num].pack("C") if num < 251 return [252, num].pack("Cv") if num < 65536 return [253, num&0xffff, num>>16].pack("CvC") if num < 16777216 return [254, num&0xffffffff, num>>32].pack("CVV") end |
Instance Method Details
#eof? ⇒ Boolean
68 69 70 |
# File 'lib/mysql-pr/packet.rb', line 68 def eof? @data[0] == ?\xfe && @data.length == 5 end |
#lcb ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/mysql-pr/packet.rb', line 22 def lcb return nil if @data.empty? case v = utiny when 0xfb return nil when 0xfc return ushort when 0xfd c, v = utiny, ushort return (v << 8)+c when 0xfe v1, v2 = ulong, ulong return (v2 << 32)+v1 else return v end end |
#lcs ⇒ Object
40 41 42 43 44 |
# File 'lib/mysql-pr/packet.rb', line 40 def lcs len = self.lcb return nil unless len @data.slice!(0, len) end |
#read(len) ⇒ Object
46 47 48 |
# File 'lib/mysql-pr/packet.rb', line 46 def read(len) @data.slice!(0, len) end |
#string ⇒ Object
50 51 52 53 54 |
# File 'lib/mysql-pr/packet.rb', line 50 def string str = @data.unpack('Z*').first @data.slice!(0, str.length+1) str end |
#to_s ⇒ Object
72 73 74 |
# File 'lib/mysql-pr/packet.rb', line 72 def to_s @data end |
#ulong ⇒ Object
64 65 66 |
# File 'lib/mysql-pr/packet.rb', line 64 def ulong @data.slice!(0, 4).unpack('V').first end |
#ushort ⇒ Object
60 61 62 |
# File 'lib/mysql-pr/packet.rb', line 60 def ushort @data.slice!(0, 2).unpack('v').first end |
#utiny ⇒ Object
56 57 58 |
# File 'lib/mysql-pr/packet.rb', line 56 def utiny @data.slice!(0, 1).unpack('C').first end |