protocol-zmtp 0.5.0 → 0.5.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: 0f82192f566af0256273b9dd794ad7032c5b1f3d1b2f8a58c37235847715f6fc
4
- data.tar.gz: b74e78954d38be3f239d96cbcac676dc564b9ab56dff16f1beff8fbcedb77b8b
3
+ metadata.gz: a29e580def88791398cee267152d16bd7e187a518bd409f2e8a2acb1e364cab1
4
+ data.tar.gz: a27e2d1335fe8e0376abf7d17e3b9e4ed2d82e3c98d9dd70733327d789144576
5
5
  SHA512:
6
- metadata.gz: 985cc52f11cc1ad7d24bdbec3916ee5485bda72a396413b3735254ae97430117b40ae3fd6331107e953c0b99bc3c05a997c368eeae642e64b023dd3d744381eb
7
- data.tar.gz: 100df0f00b447bbc6799c4468b040f15e8fe633bf25d736b44931683772d5f52a50fa2646e8ce3f4768f6076d47482a9a004944f55f42a3d35720ca3f6ede27e
6
+ metadata.gz: 92318c07232337b3b108e5a98b4735c293e15810a8b41b7d1abe046f7f0ec7ed602072a91baa2ba6a6394ab05a4d260768e01a4302652ac96ca9ed61c9ad621d
7
+ data.tar.gz: 5e5fe073b34866af67c8b0e820afb95d37b2404cab2a986b26da24d70bb30fa809afef47a27c303b396f83a3f94c3f29da9e9a95f54f4413f83605b9d5fef8b1
@@ -23,11 +23,13 @@ module Protocol
23
23
  # @return [String] command data (binary)
24
24
  attr_reader :data
25
25
 
26
+ EMPTY_DATA = "".b.freeze
27
+
26
28
  # @param name [String] command name
27
29
  # @param data [String] command data
28
- def initialize(name, data = "".b)
30
+ def initialize(name, data = EMPTY_DATA)
29
31
  @name = name
30
- @data = data.b
32
+ @data = data.encoding == Encoding::BINARY ? data : data.b
31
33
  end
32
34
 
33
35
 
@@ -35,8 +37,9 @@ module Protocol
35
37
  #
36
38
  # @return [String] binary body (name-length + name + data)
37
39
  def to_body
38
- name_bytes = @name.b
39
- name_bytes.bytesize.chr.b + name_bytes + @data
40
+ name_bytes = @name.encoding == Encoding::BINARY ? @name : @name.b
41
+ buf = String.new(capacity: 1 + name_bytes.bytesize + @data.bytesize, encoding: Encoding::BINARY)
42
+ buf << Frame::FLAG_BYTES[name_bytes.bytesize] << name_bytes << @data
40
43
  end
41
44
 
42
45
 
@@ -19,6 +19,12 @@ module Protocol
19
19
  # Short frame: 1-byte size, max body 255 bytes.
20
20
  SHORT_MAX = 255
21
21
 
22
+ # Pre-computed single-byte flag strings (avoids Integer#chr + String#b per frame).
23
+ FLAG_BYTES = Array.new(256) { |i| i.chr.b.freeze }.freeze
24
+
25
+ # Frozen empty binary string for zero-length frame bodies.
26
+ EMPTY_BODY = "".b.freeze
27
+
22
28
 
23
29
  # @return [String] frame body (binary)
24
30
  attr_reader :body
@@ -49,9 +55,9 @@ module Protocol
49
55
  flags |= FLAGS_COMMAND if @command
50
56
 
51
57
  if size > SHORT_MAX
52
- (flags | FLAGS_LONG).chr.b + [size].pack("Q>") + @body
58
+ FLAG_BYTES[flags | FLAGS_LONG] + [size].pack("Q>") + @body
53
59
  else
54
- flags.chr.b + size.chr.b + @body
60
+ FLAG_BYTES[flags] + FLAG_BYTES[size] + @body
55
61
  end
56
62
  end
57
63
 
@@ -64,9 +70,20 @@ module Protocol
64
70
  # @return [String] frozen binary wire representation
65
71
  #
66
72
  def self.encode_message(parts)
67
- buf = +""
68
- parts.each_with_index do |part, i|
69
- buf << new(part, more: i < parts.size - 1).to_wire
73
+ buf = String.new(encoding: Encoding::BINARY)
74
+ last = parts.size - 1
75
+ i = 0
76
+ while i < parts.size
77
+ body = parts[i]
78
+ body = body.b unless body.encoding == Encoding::BINARY
79
+ size = body.bytesize
80
+ flags = i < last ? FLAGS_MORE : 0
81
+ if size > SHORT_MAX
82
+ buf << FLAG_BYTES[flags | FLAGS_LONG] << [size].pack("Q>") << body
83
+ else
84
+ buf << FLAG_BYTES[flags] << FLAG_BYTES[size] << body
85
+ end
86
+ i += 1
70
87
  end
71
88
  buf.freeze
72
89
  end
@@ -95,7 +112,7 @@ module Protocol
95
112
  raise Error, "frame size #{size} exceeds max_message_size #{max_message_size}"
96
113
  end
97
114
 
98
- body = size > 0 ? io.read_exactly(size) : "".b
115
+ body = size > 0 ? io.read_exactly(size) : EMPTY_BODY
99
116
 
100
117
  new(body, more: more, command: command)
101
118
  end
@@ -306,13 +306,17 @@ module Protocol
306
306
  def write_frames(parts)
307
307
  encrypted = @mechanism.encrypted?
308
308
  buf = @header_buf
309
+ last = parts.size - 1
309
310
 
310
- parts.each_with_index do |part, i|
311
- more = i < parts.size - 1
311
+ i = 0
312
+ while i < parts.size
313
+ part = parts[i]
314
+ more = i < last
312
315
  if encrypted
313
- @io.write(@mechanism.encrypt(part.b, more: more))
316
+ body = part.encoding == Encoding::BINARY ? part : part.b
317
+ @io.write(@mechanism.encrypt(body, more: more))
314
318
  else
315
- body = part.b
319
+ body = part.encoding == Encoding::BINARY ? part : part.b
316
320
  size = body.bytesize
317
321
  flags = more ? Codec::Frame::FLAGS_MORE : 0
318
322
  buf.clear
@@ -324,6 +328,7 @@ module Protocol
324
328
  @io.write(buf)
325
329
  @io.write(body)
326
330
  end
331
+ i += 1
327
332
  end
328
333
  end
329
334
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Protocol
4
4
  module ZMTP
5
- VERSION = "0.5.0"
5
+ VERSION = "0.5.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.5.0
4
+ version: 0.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Patrik Wenger