protocol-zmtp 0.8.0 → 0.8.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5372fe6734b5faeded3069e3b510a7ff648458508c82e3a6c2b9d353ebd57179
4
- data.tar.gz: b6e5cb324afac1deece4df0a0c8cc3aadbfa2ca3a31d29b38ece87dcea777c82
3
+ metadata.gz: 81270e34092271f0e0aaceed0ce882885751def59b75716872d074952e4d6b41
4
+ data.tar.gz: ccbf823376d70d8654384b99c1bb30b270dc9493cd432c524a3caab175eaa364
5
5
  SHA512:
6
- metadata.gz: 21ec084b6bf898ed867c9c80700a3d9fad53146798d906a1cadeaed25f28745fa56d47f350e35e6af55e9ba5f7c8c7919342d2bba8e21d6068f585fb54105cc8
7
- data.tar.gz: 90812f6950f8f558a9f33a7f57c2142c63b7c21bab6d4a5d2e788bdb5c9b26154a41b50dde097852e7bfab46e901d706c34d81b6acca0bfa5fd920bfcc655fe7
6
+ metadata.gz: f148b0ac01768320f35f2edf7ac67f3d51ad3e5daa4a9c9dafccb57168e8e9f616f6f27425c00b82edcd7e0af898188ae37ea3a78f404a786bdd9a94c01cb675
7
+ data.tar.gz: bdab4d4b763394ea1f11aec4503a74d68f499671c93851adc96c2e34bcd7134de837b71941119a1aa029a0966c4ed4cfea8def3f5140244e50f9c2a9b3b0355d
@@ -130,9 +130,9 @@ module Protocol
130
130
  # @param ttl [Numeric] time-to-live in seconds (sent as deciseconds)
131
131
  # @param context [String] optional context bytes (up to 16 bytes)
132
132
  # @return [Command]
133
- def self.ping(ttl: 0, context: "".b)
133
+ def self.ping(ttl: 0, context: EMPTY_BINARY)
134
134
  ttl_ds = (ttl * 10).to_i
135
- new("PING", [ttl_ds].pack("n") + context.b)
135
+ new("PING", [ttl_ds].pack("n") + (context.encoding == Encoding::BINARY ? context : context.b))
136
136
  end
137
137
 
138
138
 
@@ -140,8 +140,8 @@ module Protocol
140
140
  #
141
141
  # @param context [String] context bytes echoed from the PING
142
142
  # @return [Command]
143
- def self.pong(context: "".b)
144
- new("PONG", context.b)
143
+ def self.pong(context: EMPTY_BINARY)
144
+ new("PONG", context.encoding == Encoding::BINARY ? context : context.b)
145
145
  end
146
146
 
147
147
 
@@ -150,7 +150,7 @@ module Protocol
150
150
  # @return [Array(Numeric, String)] [ttl_seconds, context_bytes]
151
151
  def ping_ttl_and_context
152
152
  ttl_ds = @data.unpack1("n")
153
- context = @data.bytesize > 2 ? @data.byteslice(2..) : "".b
153
+ context = @data.bytesize > 2 ? @data.byteslice(2..) : EMPTY_BINARY
154
154
  [ttl_ds / 10.0, context]
155
155
  end
156
156
 
@@ -31,21 +31,28 @@ module Protocol
31
31
  # @return [String] frame body (binary)
32
32
  attr_reader :body
33
33
 
34
+
34
35
  # @param body [String] frame body
35
36
  # @param more [Boolean] more frames follow
36
37
  # @param command [Boolean] this is a command frame
37
38
  def initialize(body, more: false, command: false)
38
- @body = body.b
39
+ @body = body.encoding == Encoding::BINARY ? body : body.b
39
40
  @more = more
40
41
  @command = command
41
42
  end
42
43
 
43
44
 
44
45
  # @return [Boolean] true if more frames follow in this message
45
- def more? = @more
46
+ def more?
47
+ @more
48
+ end
49
+
46
50
 
47
51
  # @return [Boolean] true if this is a command frame
48
- def command? = @command
52
+ def command?
53
+ @command
54
+ end
55
+
49
56
 
50
57
  # Encodes to wire bytes.
51
58
  #
@@ -57,9 +64,11 @@ module Protocol
57
64
  flags |= FLAGS_COMMAND if @command
58
65
 
59
66
  if size > SHORT_MAX
60
- FLAG_BYTES[flags | FLAGS_LONG] + [size].pack("Q>") + @body
67
+ buf = String.new(capacity: 9 + size, encoding: Encoding::BINARY)
68
+ buf << FLAG_BYTES[flags | FLAGS_LONG] << [size].pack("Q>") << @body
61
69
  else
62
- FLAG_BYTES[flags] + FLAG_BYTES[size] + @body
70
+ buf = String.new(capacity: 2 + size, encoding: Encoding::BINARY)
71
+ buf << FLAG_BYTES[flags] << FLAG_BYTES[size] << @body
63
72
  end
64
73
  end
65
74
 
@@ -72,14 +81,29 @@ module Protocol
72
81
  # @return [String] frozen binary wire representation
73
82
  #
74
83
  def self.encode_message(parts)
75
- buf = String.new(encoding: Encoding::BINARY)
84
+ if parts.size == 1
85
+ s = parts[0].bytesize
86
+ wire_size = s > SHORT_MAX ? 9 + s : 2 + s
87
+ else
88
+ wire_size = 0
89
+ j = 0
90
+ while j < parts.size
91
+ s = parts[j].bytesize
92
+ wire_size += s > SHORT_MAX ? 9 + s : 2 + s
93
+ j += 1
94
+ end
95
+ end
96
+
97
+ buf = String.new(capacity: wire_size, encoding: Encoding::BINARY)
76
98
  last = parts.size - 1
77
99
  i = 0
100
+
78
101
  while i < parts.size
79
102
  body = parts[i]
80
103
  body = body.b unless body.encoding == Encoding::BINARY
81
104
  size = body.bytesize
82
105
  flags = i < last ? FLAGS_MORE : 0
106
+
83
107
  if size > SHORT_MAX
84
108
  buf << FLAG_BYTES[flags | FLAGS_LONG] << [size].pack("Q>") << body
85
109
  else
@@ -87,6 +111,7 @@ module Protocol
87
111
  end
88
112
  i += 1
89
113
  end
114
+
90
115
  buf.freeze
91
116
  end
92
117
 
@@ -137,6 +162,7 @@ module Protocol
137
162
  (rest.getbyte(4) << 16) | (rest.getbyte(5) << 8) |
138
163
  rest.getbyte(6)
139
164
  end
165
+
140
166
  end
141
167
  end
142
168
  end
@@ -34,7 +34,7 @@ module Protocol
34
34
  # @return [String] binary frame body
35
35
  def body(prefix, cancel: false)
36
36
  flag = cancel ? FLAG_CANCEL : FLAG_SUBSCRIBE
37
- (flag + prefix.b).b
37
+ flag + (prefix.encoding == Encoding::BINARY ? prefix : prefix.b)
38
38
  end
39
39
 
40
40
 
@@ -15,32 +15,41 @@ module Protocol
15
15
  # @return [String] peer's socket type (from READY handshake)
16
16
  attr_reader :peer_socket_type
17
17
 
18
+
18
19
  # @return [String] peer's identity (from READY handshake)
19
20
  attr_reader :peer_identity
20
21
 
22
+
21
23
  # @return [Integer] peer's QoS level (from READY handshake, 0 if absent)
22
24
  attr_reader :peer_qos
23
25
 
26
+
24
27
  # @return [String] peer's supported hash algorithms in preference order
25
28
  attr_reader :peer_qos_hash
26
29
 
30
+
27
31
  # @return [Hash{String => String}, nil] full peer READY property hash
28
32
  # (set after a successful handshake; nil before)
29
33
  attr_reader :peer_properties
30
34
 
35
+
31
36
  # @return [Integer, nil] peer ZMTP major version (from greeting)
32
37
  attr_reader :peer_major
33
38
 
39
+
34
40
  # @return [Integer, nil] peer ZMTP minor version (from greeting);
35
41
  # 0 for ZMTP 3.0 peers, 1 for ZMTP 3.1+
36
42
  attr_reader :peer_minor
37
43
 
44
+
38
45
  # @return [Object] transport IO (#read_exactly, #write, #flush, #close)
39
46
  attr_reader :io
40
47
 
48
+
41
49
  # @return [Float, nil] monotonic timestamp of last received frame
42
50
  attr_reader :last_received_at
43
51
 
52
+
44
53
  # @param io [#read_exactly, #write, #flush, #close] transport IO
45
54
  # @param socket_type [String] our socket type name (e.g. "REQ")
46
55
  # @param identity [String] our identity
@@ -79,14 +88,12 @@ module Protocol
79
88
  # @return [void]
80
89
  # @raise [Error] on handshake failure
81
90
  def handshake!
82
- result = @mechanism.handshake!(
83
- @io,
91
+ result = @mechanism.handshake! @io,
84
92
  as_server: @as_server,
85
93
  socket_type: @socket_type,
86
94
  identity: @identity,
87
95
  qos: @qos,
88
- qos_hash: @qos_hash,
89
- )
96
+ qos_hash: @qos_hash
90
97
 
91
98
  @peer_socket_type = result[:peer_socket_type]
92
99
  @peer_identity = result[:peer_identity]
@@ -198,15 +205,19 @@ module Protocol
198
205
  # @raise [EOFError] if connection is closed
199
206
  def receive_message
200
207
  frames = []
208
+
201
209
  loop do
202
210
  frame = read_frame
211
+
203
212
  if frame.command?
204
213
  yield frame if block_given?
205
214
  next
206
215
  end
216
+
207
217
  frames << frame.body.freeze
208
218
  break unless frame.more?
209
219
  end
220
+
210
221
  frames.freeze
211
222
  end
212
223
 
@@ -244,6 +255,7 @@ module Protocol
244
255
  close
245
256
  raise
246
257
  end
258
+
247
259
  touch_heartbeat
248
260
 
249
261
  frame = @mechanism.decrypt(frame) if @mechanism.encrypted?
@@ -259,6 +271,7 @@ module Protocol
259
271
  next
260
272
  end
261
273
  end
274
+
262
275
  return frame
263
276
  end
264
277
  end
@@ -326,9 +339,11 @@ module Protocol
326
339
  last = parts.size - 1
327
340
 
328
341
  i = 0
342
+
329
343
  while i < parts.size
330
344
  part = parts[i]
331
345
  more = i < last
346
+
332
347
  if encrypted
333
348
  body = part.encoding == Encoding::BINARY ? part : part.b
334
349
  @io.write(@mechanism.encrypt(body, more: more))
@@ -336,18 +351,23 @@ module Protocol
336
351
  body = part.encoding == Encoding::BINARY ? part : part.b
337
352
  size = body.bytesize
338
353
  flags = more ? Codec::Frame::FLAGS_MORE : 0
354
+
339
355
  buf.clear
356
+
340
357
  if size > Codec::Frame::SHORT_MAX
341
358
  [flags | Codec::Frame::FLAGS_LONG, size].pack("CQ>", buffer: buf)
342
359
  else
343
360
  [flags, size].pack("CC", buffer: buf)
344
361
  end
362
+
345
363
  @io.write(buf)
346
364
  @io.write(body)
347
365
  end
366
+
348
367
  i += 1
349
368
  end
350
369
  end
370
+
351
371
  end
352
372
  end
353
373
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Protocol
4
4
  module ZMTP
5
- VERSION = "0.8.0"
5
+ VERSION = "0.8.1"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: protocol-zmtp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.0
4
+ version: 0.8.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Patrik Wenger