Class: Mysql::Net
- Inherits:
-
Object
- Object
- Mysql::Net
- Defined in:
- lib/og/vendor/mysql.rb
Class Method Summary collapse
Instance Method Summary collapse
- #clear ⇒ Object
- #close ⇒ Object
-
#initialize(sock) ⇒ Net
constructor
A new instance of Net.
- #read ⇒ Object
- #write(data) ⇒ Object
Constructor Details
#initialize(sock) ⇒ Net
Returns a new instance of Net.
1065 1066 1067 1068 |
# File 'lib/og/vendor/mysql.rb', line 1065 def initialize(sock) @sock = sock @pkt_nr = 0 end |
Class Method Details
.int2str(n) ⇒ Object
1119 1120 1121 |
# File 'lib/og/vendor/mysql.rb', line 1119 def Net::int2str(n) [n].pack("v") end |
.int3str(n) ⇒ Object
1123 1124 1125 |
# File 'lib/og/vendor/mysql.rb', line 1123 def Net::int3str(n) [n%256, n>>8].pack("cv") end |
.int4str(n) ⇒ Object
1127 1128 1129 |
# File 'lib/og/vendor/mysql.rb', line 1127 def Net::int4str(n) [n].pack("V") end |
Instance Method Details
#clear ⇒ Object
1070 1071 1072 |
# File 'lib/og/vendor/mysql.rb', line 1070 def clear() @pkt_nr = 0 end |
#close ⇒ Object
1115 1116 1117 |
# File 'lib/og/vendor/mysql.rb', line 1115 def close() @sock.close end |
#read ⇒ Object
1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 |
# File 'lib/og/vendor/mysql.rb', line 1074 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
1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 |
# File 'lib/og/vendor/mysql.rb', line 1095 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 |