net-protocol 0.1.3 → 0.2.0

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 +65 -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: ca28f0356ecc7ee77cb5d0ead1ea91d9a70446e9de9a8e4927bdd4caf8d8e910
4
+ data.tar.gz: 394be407dd3ab468f376be2032a2c6c6d78d05863bd21a9353e206ea473bbc37
5
5
  SHA512:
6
- metadata.gz: 4be52fa68351db428b2e0fadfb65965bae7c5e4b7dffda269cf9d9086d7f5ac91cb9c4a399788f6c7ed515be85ecb9c01e0feee94737b60a758c44438372d85b
7
- data.tar.gz: e282e7f623600edbe1590a390cec89f6a93f0a26226d9567decad7c0f0a890d9971fc6223b8f5698894edf493c586b2b055f67af0ea14471f7c0f5a894e04b46
6
+ metadata.gz: 52dcdc5b3c200e3f29dea711c392d6127e39cf5cdf12787bb585a41ca874ccb3b93c734145d688c4c83acf98f28f0c7e89ebc49aeeb64caa0cb172a452f9c73c
7
+ data.tar.gz: d57a73c0d8a1260012f26826ef257b4b6df616aa07e0fd46fc63710b2cd5cf3dafd322db057db1202ae1158c77d729c9e65ec3e12cc2d1705a4a2d2221cf0962
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.0"
30
30
 
31
31
  private
32
32
  def Protocol.protocol_param(name, val)
@@ -120,6 +120,7 @@ module Net # :nodoc:
120
120
  @continue_timeout = continue_timeout
121
121
  @debug_output = debug_output
122
122
  @rbuf = ''.b
123
+ @rbuf_offset = 0
123
124
  end
124
125
 
125
126
  attr_reader :io
@@ -154,14 +155,15 @@ module Net # :nodoc:
154
155
  LOG "reading #{len} bytes..."
155
156
  read_bytes = 0
156
157
  begin
157
- while read_bytes + @rbuf.size < len
158
- s = rbuf_consume(@rbuf.size)
159
- read_bytes += s.size
160
- dest << s
158
+ while read_bytes + rbuf_size < len
159
+ if s = rbuf_consume_all_shareable!
160
+ read_bytes += s.bytesize
161
+ dest << s
162
+ end
161
163
  rbuf_fill
162
164
  end
163
165
  s = rbuf_consume(len - read_bytes)
164
- read_bytes += s.size
166
+ read_bytes += s.bytesize
165
167
  dest << s
166
168
  rescue EOFError
167
169
  raise unless ignore_eof
@@ -175,9 +177,10 @@ module Net # :nodoc:
175
177
  read_bytes = 0
176
178
  begin
177
179
  while true
178
- s = rbuf_consume(@rbuf.size)
179
- read_bytes += s.size
180
- dest << s
180
+ if s = rbuf_consume_all_shareable!
181
+ read_bytes += s.bytesize
182
+ dest << s
183
+ end
181
184
  rbuf_fill
182
185
  end
183
186
  rescue EOFError
@@ -188,14 +191,16 @@ module Net # :nodoc:
188
191
  end
189
192
 
190
193
  def readuntil(terminator, ignore_eof = false)
194
+ offset = @rbuf_offset
191
195
  begin
192
- until idx = @rbuf.index(terminator)
196
+ until idx = @rbuf.index(terminator, offset)
197
+ offset = @rbuf.bytesize
193
198
  rbuf_fill
194
199
  end
195
- return rbuf_consume(idx + terminator.size)
200
+ return rbuf_consume(idx + terminator.bytesize - @rbuf_offset)
196
201
  rescue EOFError
197
202
  raise unless ignore_eof
198
- return rbuf_consume(@rbuf.size)
203
+ return rbuf_consume
199
204
  end
200
205
  end
201
206
 
@@ -208,12 +213,16 @@ module Net # :nodoc:
208
213
  BUFSIZE = 1024 * 16
209
214
 
210
215
  def rbuf_fill
211
- tmp = @rbuf.empty? ? @rbuf : nil
216
+ tmp = @rbuf_empty ? @rbuf : nil
212
217
  case rv = @io.read_nonblock(BUFSIZE, tmp, exception: false)
213
218
  when String
214
- return if rv.equal?(tmp)
215
- @rbuf << rv
216
- rv.clear
219
+ @rbuf_empty = false
220
+ if rv.equal?(tmp)
221
+ @rbuf_offset = 0
222
+ else
223
+ @rbuf << rv
224
+ rv.clear
225
+ end
217
226
  return
218
227
  when :wait_readable
219
228
  (io = @io.to_io).wait_readable(@read_timeout) or raise Net::ReadTimeout.new(io)
@@ -228,13 +237,50 @@ module Net # :nodoc:
228
237
  end while true
229
238
  end
230
239
 
231
- def rbuf_consume(len)
232
- if len == @rbuf.size
240
+ def rbuf_flush
241
+ if @rbuf_empty
242
+ @rbuf.clear
243
+ @rbuf_offset = 0
244
+ end
245
+ nil
246
+ end
247
+
248
+ def rbuf_size
249
+ @rbuf.bytesize - @rbuf_offset
250
+ end
251
+
252
+ # Warning: this method may share the buffer to avoid
253
+ # copying. The caller must no longer use the returned
254
+ # string once rbuf_fill has been called again
255
+ def rbuf_consume_all_shareable!
256
+ @rbuf_empty = true
257
+ buf = if @rbuf_offset == 0
258
+ @rbuf
259
+ else
260
+ @rbuf.byteslice(@rbuf_offset..-1)
261
+ end
262
+ @rbuf_offset = @rbuf.bytesize
263
+ buf
264
+ end
265
+
266
+ def rbuf_consume(len = nil)
267
+ if @rbuf_offset == 0 && (len.nil? || len == @rbuf.bytesize)
233
268
  s = @rbuf
234
269
  @rbuf = ''.b
270
+ @rbuf_offset = 0
271
+ @rbuf_empty = true
272
+ elsif len.nil?
273
+ s = @rbuf.byteslice(@rbuf_offset..-1)
274
+ @rbuf = ''.b
275
+ @rbuf_offset = 0
276
+ @rbuf_empty = true
235
277
  else
236
- s = @rbuf.slice!(0, len)
278
+ s = @rbuf.byteslice(@rbuf_offset, len)
279
+ @rbuf_offset += len
280
+ @rbuf_empty = @rbuf_offset == @rbuf.bytesize
281
+ rbuf_flush
237
282
  end
283
+
238
284
  @debug_output << %Q[-> #{s.dump}\n] if @debug_output
239
285
  s
240
286
  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.0
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-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: timeout