Protocol#snd: StringIO eliminated, more functional.

master v0.2.0
Denis Knauf 2022-09-29 11:38:11 +02:00
parent 18c0b6434a
commit 7e02cbbe84
1 changed files with 17 additions and 12 deletions

View File

@ -159,19 +159,24 @@ class Knot::Protocol
def snd sock: nil, **data
rsock = sock || @sock
s = ''.b
sock = StringIO.new s
sock.write [1].pack( 'c')
#s = ''.b
#sock = StringIO.new s
#sock.write [1].pack( 'c')
data[:flags] ||= ''
Idx::Idx.each do |n|
v = data[n.to_sym]&.to_s&.b
sock.write [n.to_i, v.size, v].pack( 'c na*') if v
end
sock.write [3].pack( 'c')
sock.flush
ds =
Idx::Idx.
select {|n| data[n.to_sym] }.
map {|n| v = data[n.to_sym].to_s.b; [n.to_i, v.size, v ] }
s = (ds.flatten+[3]).pack( ('c na*'*ds.length)+'c').b
#Idx::Idx.each do |n|
# v = data[n.to_sym]&.to_s&.b
# sock.write [n.to_i, v.size, v].pack( 'c na*') if v
#end
#sock.write [3].pack( 'c')
#sock.flush
if 0 >= @logger.sev_threshold
@logger.debug "send data #{data.inspect}"
@logger.debug "send raw #{data.inspect}"
@logger.debug "send raw #{s.inspect}"
end
rsock.write s
rsock.flush
@ -194,8 +199,8 @@ class Knot::Protocol
def read n
s = @sock.read n
@str.insert -1, s
s
@str.insert -1, s unless s.nil?
s || ''
end
end