net-protocol 0.1.3 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/lib/net/protocol.rb +56 -19
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 426f967c28318d12339c67a1c40db52f25167a546e5760947c2f4653d946aa08
4
- data.tar.gz: 1a536d8d45be996ec1bbf72443adb8aa9aaf9951c7f70366539c836af7836dd6
3
+ metadata.gz: dddd9155f88e3e664a77ae33aed748ce833c114871dfab60e2be632214a2e063
4
+ data.tar.gz: 45ea621c80a7cef086d1094502c47aa60c0320ab585a8dd0e9c4684de2be5608
5
5
  SHA512:
6
- metadata.gz: 4be52fa68351db428b2e0fadfb65965bae7c5e4b7dffda269cf9d9086d7f5ac91cb9c4a399788f6c7ed515be85ecb9c01e0feee94737b60a758c44438372d85b
7
- data.tar.gz: e282e7f623600edbe1590a390cec89f6a93f0a26226d9567decad7c0f0a890d9971fc6223b8f5698894edf493c586b2b055f67af0ea14471f7c0f5a894e04b46
6
+ metadata.gz: fa55cb8a2eb4d6a14815dc9367c5787aea13ce9f6127298aa41614ffdf1ac4b421a4d6e6da2ad40740c99ec6a18b7541fd9e194f2bfd3e093e1223683fa8e34d
7
+ data.tar.gz: e77247d5a85f3adcf618f47b8cf3ef9b0be28f6b988ae48de43c17796d9d6c2bff6fdb083784c636f6a122c4bfaddde722fefa28f46837bc4f8358f2a63c41f2
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Net::Protocol
2
2
 
3
- The abstruct interface for net-* client.
3
+ The abstract interface for net-* client.
4
4
 
5
5
  ## Installation
6
6
 
data/lib/net/protocol.rb CHANGED
@@ -26,7 +26,7 @@ require 'io/wait'
26
26
  module Net # :nodoc:
27
27
 
28
28
  class Protocol #:nodoc: internal use only
29
- VERSION = "0.1.3"
29
+ VERSION = "0.2.1"
30
30
 
31
31
  private
32
32
  def Protocol.protocol_param(name, val)
@@ -120,6 +120,8 @@ module Net # :nodoc:
120
120
  @continue_timeout = continue_timeout
121
121
  @debug_output = debug_output
122
122
  @rbuf = ''.b
123
+ @rbuf_empty = true
124
+ @rbuf_offset = 0
123
125
  end
124
126
 
125
127
  attr_reader :io
@@ -154,14 +156,15 @@ module Net # :nodoc:
154
156
  LOG "reading #{len} bytes..."
155
157
  read_bytes = 0
156
158
  begin
157
- while read_bytes + @rbuf.size < len
158
- s = rbuf_consume(@rbuf.size)
159
- read_bytes += s.size
160
- dest << s
159
+ while read_bytes + rbuf_size < len
160
+ if s = rbuf_consume_all
161
+ read_bytes += s.bytesize
162
+ dest << s
163
+ end
161
164
  rbuf_fill
162
165
  end
163
166
  s = rbuf_consume(len - read_bytes)
164
- read_bytes += s.size
167
+ read_bytes += s.bytesize
165
168
  dest << s
166
169
  rescue EOFError
167
170
  raise unless ignore_eof
@@ -175,9 +178,10 @@ module Net # :nodoc:
175
178
  read_bytes = 0
176
179
  begin
177
180
  while true
178
- s = rbuf_consume(@rbuf.size)
179
- read_bytes += s.size
180
- dest << s
181
+ if s = rbuf_consume_all
182
+ read_bytes += s.bytesize
183
+ dest << s
184
+ end
181
185
  rbuf_fill
182
186
  end
183
187
  rescue EOFError
@@ -188,14 +192,16 @@ module Net # :nodoc:
188
192
  end
189
193
 
190
194
  def readuntil(terminator, ignore_eof = false)
195
+ offset = @rbuf_offset
191
196
  begin
192
- until idx = @rbuf.index(terminator)
197
+ until idx = @rbuf.index(terminator, offset)
198
+ offset = @rbuf.bytesize
193
199
  rbuf_fill
194
200
  end
195
- return rbuf_consume(idx + terminator.size)
201
+ return rbuf_consume(idx + terminator.bytesize - @rbuf_offset)
196
202
  rescue EOFError
197
203
  raise unless ignore_eof
198
- return rbuf_consume(@rbuf.size)
204
+ return rbuf_consume
199
205
  end
200
206
  end
201
207
 
@@ -208,12 +214,16 @@ module Net # :nodoc:
208
214
  BUFSIZE = 1024 * 16
209
215
 
210
216
  def rbuf_fill
211
- tmp = @rbuf.empty? ? @rbuf : nil
217
+ tmp = @rbuf_empty ? @rbuf : nil
212
218
  case rv = @io.read_nonblock(BUFSIZE, tmp, exception: false)
213
219
  when String
214
- return if rv.equal?(tmp)
215
- @rbuf << rv
216
- rv.clear
220
+ @rbuf_empty = false
221
+ if rv.equal?(tmp)
222
+ @rbuf_offset = 0
223
+ else
224
+ @rbuf << rv
225
+ rv.clear
226
+ end
217
227
  return
218
228
  when :wait_readable
219
229
  (io = @io.to_io).wait_readable(@read_timeout) or raise Net::ReadTimeout.new(io)
@@ -228,13 +238,40 @@ module Net # :nodoc:
228
238
  end while true
229
239
  end
230
240
 
231
- def rbuf_consume(len)
232
- if len == @rbuf.size
241
+ def rbuf_flush
242
+ if @rbuf_empty
243
+ @rbuf.clear
244
+ @rbuf_offset = 0
245
+ end
246
+ nil
247
+ end
248
+
249
+ def rbuf_size
250
+ @rbuf.bytesize - @rbuf_offset
251
+ end
252
+
253
+ def rbuf_consume_all
254
+ rbuf_consume if rbuf_size > 0
255
+ end
256
+
257
+ def rbuf_consume(len = nil)
258
+ if @rbuf_offset == 0 && (len.nil? || len == @rbuf.bytesize)
233
259
  s = @rbuf
234
260
  @rbuf = ''.b
261
+ @rbuf_offset = 0
262
+ @rbuf_empty = true
263
+ elsif len.nil?
264
+ s = @rbuf.byteslice(@rbuf_offset..-1)
265
+ @rbuf = ''.b
266
+ @rbuf_offset = 0
267
+ @rbuf_empty = true
235
268
  else
236
- s = @rbuf.slice!(0, len)
269
+ s = @rbuf.byteslice(@rbuf_offset, len)
270
+ @rbuf_offset += len
271
+ @rbuf_empty = @rbuf_offset == @rbuf.bytesize
272
+ rbuf_flush
237
273
  end
274
+
238
275
  @debug_output << %Q[-> #{s.dump}\n] if @debug_output
239
276
  s
240
277
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: net-protocol
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yukihiro Matsumoto
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-04-01 00:00:00.000000000 Z
11
+ date: 2022-12-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: timeout