Class: Mysql::Net

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record/vendor/mysql.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sock) ⇒ Net

Returns a new instance of Net.



1000
1001
1002
1003
# File 'lib/active_record/vendor/mysql.rb', line 1000

def initialize(sock)
  @sock = sock
  @pkt_nr = 0
end

Class Method Details

.int2str(n) ⇒ Object



1048
1049
1050
# File 'lib/active_record/vendor/mysql.rb', line 1048

def Net::int2str(n)
  [n].pack("v")
end

.int3str(n) ⇒ Object



1052
1053
1054
# File 'lib/active_record/vendor/mysql.rb', line 1052

def Net::int3str(n)
  [n%256, n>>8].pack("cv")
end

.int4str(n) ⇒ Object



1056
1057
1058
# File 'lib/active_record/vendor/mysql.rb', line 1056

def Net::int4str(n)
  [n].pack("V")
end

Instance Method Details

#clearObject



1005
1006
1007
# File 'lib/active_record/vendor/mysql.rb', line 1005

def clear()
  @pkt_nr = 0
end

#closeObject



1044
1045
1046
# File 'lib/active_record/vendor/mysql.rb', line 1044

def close()
  @sock.close
end

#readObject



1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
# File 'lib/active_record/vendor/mysql.rb', line 1009

def read()
  buf = []
  len = nil
  @sock.sync = false
  while len == nil or len == MAX_PACKET_LENGTH do
	a = @sock.read(4)
	len = a[0]+a[1]*256+a[2]*256*256
	pkt_nr = a[3]
	if @pkt_nr != pkt_nr then
	  raise "Packets out of order: #{@pkt_nr}<>#{pkt_nr}"
	end
	@pkt_nr = @pkt_nr + 1 & 0xff
	buf << @sock.read(len)
  end
  @sock.sync = true
  buf.join
end

#write(data) ⇒ Object



1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
# File 'lib/active_record/vendor/mysql.rb', line 1027

def write(data)
  if data.is_a? Array then
	data = data.join
  end
  @sock.sync = false
  ptr = 0
  while data.length >= MAX_PACKET_LENGTH do
	@sock.write Net::int3str(MAX_PACKET_LENGTH)+@pkt_nr.chr+data[ptr, MAX_PACKET_LENGTH]
	@pkt_nr = @pkt_nr + 1 & 0xff
	ptr += MAX_PACKET_LENGTH
  end
  @sock.write Net::int3str(data.length-ptr)+@pkt_nr.chr+data[ptr .. -1]
  @pkt_nr = @pkt_nr + 1 & 0xff
  @sock.sync = true
  @sock.flush
end