Protocol#snd: StringIO eliminated, more functional.
This commit is contained in:
parent
18c0b6434a
commit
7e02cbbe84
1 changed files with 17 additions and 12 deletions
|
@ -159,19 +159,24 @@ class Knot::Protocol
|
||||||
|
|
||||||
def snd sock: nil, **data
|
def snd sock: nil, **data
|
||||||
rsock = sock || @sock
|
rsock = sock || @sock
|
||||||
s = ''.b
|
#s = ''.b
|
||||||
sock = StringIO.new s
|
#sock = StringIO.new s
|
||||||
sock.write [1].pack( 'c')
|
#sock.write [1].pack( 'c')
|
||||||
data[:flags] ||= ''
|
data[:flags] ||= ''
|
||||||
Idx::Idx.each do |n|
|
ds =
|
||||||
v = data[n.to_sym]&.to_s&.b
|
Idx::Idx.
|
||||||
sock.write [n.to_i, v.size, v].pack( 'c na*') if v
|
select {|n| data[n.to_sym] }.
|
||||||
end
|
map {|n| v = data[n.to_sym].to_s.b; [n.to_i, v.size, v ] }
|
||||||
sock.write [3].pack( 'c')
|
s = (ds.flatten+[3]).pack( ('c na*'*ds.length)+'c').b
|
||||||
sock.flush
|
#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
|
if 0 >= @logger.sev_threshold
|
||||||
@logger.debug "send data #{data.inspect}"
|
@logger.debug "send data #{data.inspect}"
|
||||||
@logger.debug "send raw #{data.inspect}"
|
@logger.debug "send raw #{s.inspect}"
|
||||||
end
|
end
|
||||||
rsock.write s
|
rsock.write s
|
||||||
rsock.flush
|
rsock.flush
|
||||||
|
@ -194,8 +199,8 @@ class Knot::Protocol
|
||||||
|
|
||||||
def read n
|
def read n
|
||||||
s = @sock.read n
|
s = @sock.read n
|
||||||
@str.insert -1, s
|
@str.insert -1, s unless s.nil?
|
||||||
s
|
s || ''
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue