protocol-zmtp 0.1.0 → 0.1.2

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: 6eac99ed3115e12e24bfc7545fd235cbeddecf9acbf6e80363247f6d85c1ed37
4
- data.tar.gz: 58f81f079d7b5785baf7fa8b15f00224ac93c9505ed2f0b00d9f6bcc12147f55
3
+ metadata.gz: dafca992ff161d0cdb81664bfc85fd7f9194517222dcb5d11426a35c6a5f1c38
4
+ data.tar.gz: 9176b6c3090ef6ccaab8c37bb95d00f781a91c38a57606f08215ccaaf835726c
5
5
  SHA512:
6
- metadata.gz: ff813236409d769b319b712c120995ed1bbf5e0c51dcb772a9a3e86f3130a379a1aac64843425e67a76277f91e4c74b44450b4fbe60e5a8f1173bdefa051d50c
7
- data.tar.gz: 208791933fd0006915a7155ba0a18c32ec81f84854df0d77f39314e598fcc4fad3b1eb69cc341365685a3eae55928c662d767bce4331d5958c2e8d8039e84fc5
6
+ metadata.gz: 0aeecc4dd2454e4953ca64a5211aae337cc05846a348434f90cecd258495aefcac9489dc264df65dcbe9e7d0c266436ecdca355c80a8e4423a4b6c052d904f91
7
+ data.tar.gz: f725b19d48b976315b64e2c1f44663f0454db080a737de074f84a77122e623e8673afb7d662627972d680b6b9af7c209aab90c4c95a1a2654706b5daefe89dd9
@@ -52,13 +52,28 @@ module Protocol
52
52
  end
53
53
  end
54
54
 
55
+ # Encodes a multi-part message into a single wire-format string.
56
+ # The result can be written to multiple connections without
57
+ # re-encoding each time (useful for fan-out patterns like PUB).
58
+ #
59
+ # @param parts [Array<String>] message frames
60
+ # @return [String] frozen binary wire representation
61
+ #
62
+ def self.encode_message(parts)
63
+ buf = +""
64
+ parts.each_with_index do |part, i|
65
+ buf << new(part, more: i < parts.size - 1).to_wire
66
+ end
67
+ buf.freeze
68
+ end
69
+
55
70
  # Reads one frame from an IO-like object.
56
71
  #
57
72
  # @param io [#read_exactly] must support read_exactly(n)
58
73
  # @return [Frame]
59
74
  # @raise [Error] on invalid frame
60
75
  # @raise [EOFError] if the connection is closed
61
- def self.read_from(io)
76
+ def self.read_from(io, max_message_size: nil)
62
77
  flags = io.read_exactly(1).getbyte(0)
63
78
 
64
79
  more = (flags & FLAGS_MORE) != 0
@@ -71,6 +86,10 @@ module Protocol
71
86
  io.read_exactly(1).getbyte(0)
72
87
  end
73
88
 
89
+ if max_message_size && size > max_message_size
90
+ raise Error, "frame size #{size} exceeds max_message_size #{max_message_size}"
91
+ end
92
+
74
93
  body = size > 0 ? io.read_exactly(size) : "".b
75
94
 
76
95
  new(body, more: more, command: command)
@@ -91,6 +91,26 @@ module Protocol
91
91
  end
92
92
  end
93
93
 
94
+ # Writes pre-encoded wire bytes to the buffer without flushing.
95
+ # Used for fan-out: encode once, write to many connections.
96
+ #
97
+ # @param wire_bytes [String] ZMTP wire-format bytes
98
+ # @return [void]
99
+ def write_wire(wire_bytes)
100
+ @mutex.synchronize do
101
+ @io.write(wire_bytes)
102
+ end
103
+ end
104
+
105
+ # Returns true if the ZMTP mechanism encrypts at the frame level
106
+ # (e.g. CURVE). TLS encryption is below ZMTP and does not affect
107
+ # wire-format bytes.
108
+ #
109
+ # @return [Boolean]
110
+ def curve?
111
+ @mechanism.encrypted?
112
+ end
113
+
94
114
  # Flushes the write buffer to the underlying IO.
95
115
  def flush
96
116
  @mutex.synchronize do
@@ -140,18 +160,18 @@ module Protocol
140
160
  # @raise [EOFError] if connection is closed
141
161
  def read_frame
142
162
  loop do
143
- frame = Codec::Frame.read_from(@io)
163
+ begin
164
+ frame = Codec::Frame.read_from(@io, max_message_size: @max_message_size)
165
+ rescue Error
166
+ close
167
+ raise
168
+ end
144
169
  touch_heartbeat
145
170
 
146
171
  if @mechanism.encrypted? && frame.body.bytesize > 8 && frame.body.byteslice(0, 8) == "\x07MESSAGE".b
147
172
  frame = @mechanism.decrypt(frame)
148
173
  end
149
174
 
150
- if @max_message_size && !frame.command? && frame.body.bytesize > @max_message_size
151
- close
152
- raise Error, "frame size #{frame.body.bytesize} exceeds max_message_size #{@max_message_size}"
153
- end
154
-
155
175
  if frame.command?
156
176
  cmd = Codec::Command.from_body(frame.body)
157
177
  case cmd.name
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Protocol
4
4
  module ZMTP
5
- VERSION = "0.1.0"
5
+ VERSION = "0.1.2"
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.1.0
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Patrik Wenger