Class: Mysql::Net

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

Overview

:nodoc:

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sock) ⇒ Net

Returns a new instance of Net.



1080
1081
1082
1083
# File 'lib/mysql.rb', line 1080

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

Class Method Details

.int2str(n) ⇒ Object



1134
1135
1136
# File 'lib/mysql.rb', line 1134

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

.int3str(n) ⇒ Object



1138
1139
1140
# File 'lib/mysql.rb', line 1138

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

.int4str(n) ⇒ Object



1142
1143
1144
# File 'lib/mysql.rb', line 1142

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

Instance Method Details

#clearObject



1085
1086
1087
# File 'lib/mysql.rb', line 1085

def clear()
  @pkt_nr = 0
end

#closeObject



1130
1131
1132
# File 'lib/mysql.rb', line 1130

def close()
  @sock.close
end

#readObject



1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
# File 'lib/mysql.rb', line 1089

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
rescue
  errno = Error::CR_SERVER_LOST 
  raise Error::new(errno, Error::err(errno)) 
end

#write(data) ⇒ Object



1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
# File 'lib/mysql.rb', line 1110

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
rescue
  errno = Error::CR_SERVER_LOST 
  raise Error::new(errno, Error::err(errno)) 
end