Class: Mysql::Net

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sock) ⇒ Net

Returns a new instance of Net.



1007
1008
1009
1010
# File 'lib/rspider/mysql.rb', line 1007

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

Class Method Details

.int2str(n) ⇒ Object



1055
1056
1057
# File 'lib/rspider/mysql.rb', line 1055

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

.int3str(n) ⇒ Object



1059
1060
1061
# File 'lib/rspider/mysql.rb', line 1059

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

.int4str(n) ⇒ Object



1063
1064
1065
# File 'lib/rspider/mysql.rb', line 1063

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

Instance Method Details

#clearObject



1012
1013
1014
# File 'lib/rspider/mysql.rb', line 1012

def clear()
  @pkt_nr = 0
end

#closeObject



1051
1052
1053
# File 'lib/rspider/mysql.rb', line 1051

def close()
  @sock.close
end

#readObject



1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
# File 'lib/rspider/mysql.rb', line 1016

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



1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
# File 'lib/rspider/mysql.rb', line 1034

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