net-protocol 0.1.3 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/lib/net/protocol.rb +65 -19
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ca28f0356ecc7ee77cb5d0ead1ea91d9a70446e9de9a8e4927bdd4caf8d8e910
|
4
|
+
data.tar.gz: 394be407dd3ab468f376be2032a2c6c6d78d05863bd21a9353e206ea473bbc37
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 52dcdc5b3c200e3f29dea711c392d6127e39cf5cdf12787bb585a41ca874ccb3b93c734145d688c4c83acf98f28f0c7e89ebc49aeeb64caa0cb172a452f9c73c
|
7
|
+
data.tar.gz: d57a73c0d8a1260012f26826ef257b4b6df616aa07e0fd46fc63710b2cd5cf3dafd322db057db1202ae1158c77d729c9e65ec3e12cc2d1705a4a2d2221cf0962
|
data/README.md
CHANGED
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.
|
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 +
|
158
|
-
s =
|
159
|
-
|
160
|
-
|
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.
|
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 =
|
179
|
-
|
180
|
-
|
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.
|
200
|
+
return rbuf_consume(idx + terminator.bytesize - @rbuf_offset)
|
196
201
|
rescue EOFError
|
197
202
|
raise unless ignore_eof
|
198
|
-
return rbuf_consume
|
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 = @
|
216
|
+
tmp = @rbuf_empty ? @rbuf : nil
|
212
217
|
case rv = @io.read_nonblock(BUFSIZE, tmp, exception: false)
|
213
218
|
when String
|
214
|
-
|
215
|
-
|
216
|
-
|
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
|
232
|
-
if
|
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.
|
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.
|
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-
|
11
|
+
date: 2022-12-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: timeout
|