io_request 2.2.0 → 2.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/io_request.rb +4 -1
- data/lib/io_request/client.rb +32 -22
- data/lib/io_request/connection/ssl_sockets.rb +5 -3
- data/lib/io_request/message.rb +2 -2
- data/lib/io_request/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 39b0e0eb362ea37ce9f2b7359ec439bfc422ca12b2f89ba4483a8cba20c99a2f
|
4
|
+
data.tar.gz: 7c7fc713e6de0386789cabbde9a68183d52c8cba268bce30ca9a76c503cb8f3b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5473de710bff683d5dc610a8eb43207353853280c5a4d5a81eccefb1cf40ea924811b5cc7ad16b9b155ebc43186a8691a32c0648cd6dc80c1bb44ba697b38507
|
7
|
+
data.tar.gz: 8121c16e2f3e3d1b8776aff30d34415a1d29699d9725208621c5af62b17f2664011cea4a03f7b2de370c676dfd0c3f3ec274de9700ba1980ad4e749864cb5bb6
|
data/lib/io_request.rb
CHANGED
@@ -1,7 +1,10 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
# Main module.
|
4
|
-
module IORequest
|
4
|
+
module IORequest
|
5
|
+
# Client received message of zero size.
|
6
|
+
class ZeroSizeMessageError < RuntimeError; end
|
7
|
+
end
|
5
8
|
|
6
9
|
require_relative 'io_request/version'
|
7
10
|
require_relative 'io_request/logging'
|
data/lib/io_request/client.rb
CHANGED
@@ -63,15 +63,21 @@ module IORequest
|
|
63
63
|
# Close connection.
|
64
64
|
def close
|
65
65
|
close_internal
|
66
|
+
|
66
67
|
join_threads
|
67
|
-
@open = false
|
68
68
|
end
|
69
69
|
|
70
70
|
# @yieldparam [Hash]
|
71
71
|
# @yieldreturn [Hash]
|
72
|
-
def
|
73
|
-
IORequest.logger.debug(prog_name) { 'Saved
|
74
|
-
@
|
72
|
+
def on_request(&block)
|
73
|
+
IORequest.logger.debug(prog_name) { 'Saved on_request block' }
|
74
|
+
@on_request = block
|
75
|
+
end
|
76
|
+
alias respond on_request
|
77
|
+
|
78
|
+
def on_close(&block)
|
79
|
+
IORequest.logger.debug(prog_name) { 'Saved on_close block' }
|
80
|
+
@on_close = block
|
75
81
|
end
|
76
82
|
|
77
83
|
# If callback block is provided, request will be sent asynchroniously.
|
@@ -96,27 +102,25 @@ module IORequest
|
|
96
102
|
|
97
103
|
def close_internal
|
98
104
|
IORequest.logger.debug(prog_name) { 'Closing connection' }
|
99
|
-
|
100
|
-
send_zero_size_request
|
101
|
-
rescue StandardError
|
102
|
-
IORequest.logger.debug(prog_name) { 'Failed to send zero-sized message. Closing anyway' }
|
103
|
-
end
|
104
|
-
stop_data_transition
|
105
|
+
send_zero_size_request
|
105
106
|
close_io
|
106
|
-
end
|
107
|
-
|
108
|
-
def stop_data_transition
|
109
|
-
return unless defined?(@data_transition_thread) && !@data_transition_thread.nil?
|
110
|
-
|
111
|
-
IORequest.logger.debug(prog_name) { 'Killing data transition thread' }
|
112
|
-
@data_transition_thread.kill
|
113
107
|
@data_transition_thread = nil
|
108
|
+
@open = false
|
109
|
+
@on_close&.call if defined?(@on_close)
|
114
110
|
end
|
115
111
|
|
116
112
|
def close_io
|
117
|
-
|
118
|
-
|
119
|
-
|
113
|
+
begin
|
114
|
+
@io_r&.close
|
115
|
+
rescue StandardError => e
|
116
|
+
IORequest.logger.debug "Failed to close read IO: #{e}"
|
117
|
+
end
|
118
|
+
begin
|
119
|
+
@io_w&.close
|
120
|
+
rescue StandardError => e
|
121
|
+
IORequest.logger.debug "Failed to close write IO: #{e}"
|
122
|
+
end
|
123
|
+
IORequest.logger.debug(prog_name) { 'Closed IO streams' }
|
120
124
|
end
|
121
125
|
|
122
126
|
def authorization
|
@@ -138,8 +142,11 @@ module IORequest
|
|
138
142
|
IORequest.logger.debug(prog_name) { 'Starting data transition loop' }
|
139
143
|
loop do
|
140
144
|
data_transition_iteration
|
145
|
+
rescue ZeroSizeMessageError
|
146
|
+
IORequest.logger.debug(prog_name) { 'Connection was closed from the other side' }
|
147
|
+
break
|
141
148
|
rescue StandardError => e
|
142
|
-
IORequest.logger.debug(prog_name) { "Data transition
|
149
|
+
IORequest.logger.debug(prog_name) { "Data transition unknown error: #{e}" }
|
143
150
|
break
|
144
151
|
end
|
145
152
|
close_internal
|
@@ -156,7 +163,8 @@ module IORequest
|
|
156
163
|
end
|
157
164
|
|
158
165
|
def handle_request(message)
|
159
|
-
data =
|
166
|
+
data = {}
|
167
|
+
data = @on_request&.call(message.data) if defined?(@on_request)
|
160
168
|
response = Message.new(data, type: :response, to: message.id)
|
161
169
|
send_response(response)
|
162
170
|
end
|
@@ -180,6 +188,8 @@ module IORequest
|
|
180
188
|
IORequest.logger.debug(prog_name) { 'Sending zero size message' }
|
181
189
|
@io_w.write([0].pack('S'))
|
182
190
|
end
|
191
|
+
rescue StandardError => e
|
192
|
+
IORequest.logger.debug(prog_name) { "Failed to send zero-sized message(#{e})" }
|
183
193
|
end
|
184
194
|
|
185
195
|
def send_request_and_wait_for_response(request)
|
@@ -79,8 +79,11 @@ module IORequest
|
|
79
79
|
client = IORequest::Client.new authorizer: @authorizer
|
80
80
|
begin
|
81
81
|
client.open read_write: ssl_socket
|
82
|
-
client.
|
82
|
+
client.on_request { |data| @requests_handler.call(data, client) }
|
83
83
|
@clients << client
|
84
|
+
client.on_close do
|
85
|
+
@clients.select!(&:open?)
|
86
|
+
end
|
84
87
|
rescue StandardError
|
85
88
|
IORequest.debug "Failed to open client: #{e}"
|
86
89
|
ssl_socket.close
|
@@ -121,9 +124,8 @@ module IORequest
|
|
121
124
|
@client = IORequest::Client.new authorizer: @authorizer
|
122
125
|
begin
|
123
126
|
@client.open read_write: ssl_socket
|
124
|
-
@client.
|
127
|
+
@client.on_request(&@requests_handler)
|
125
128
|
rescue StandardError
|
126
|
-
IORequest.debug "Failed to open client: #{e}"
|
127
129
|
ssl_socket.close
|
128
130
|
@client = nil
|
129
131
|
end
|
data/lib/io_request/message.rb
CHANGED
@@ -81,8 +81,8 @@ module IORequest
|
|
81
81
|
# @param io_r [:read]
|
82
82
|
# @return [Message]
|
83
83
|
def self.read_from(io_r)
|
84
|
-
size = io_r.read(2)
|
85
|
-
raise
|
84
|
+
size = io_r.read(2)&.unpack1('S') || 0
|
85
|
+
raise ZeroSizeMessageError if size.zero?
|
86
86
|
|
87
87
|
json_string = io_r.read(size).unpack1("a#{size}")
|
88
88
|
msg = JSON.parse(json_string, symbolize_names: true)
|
data/lib/io_request/version.rb
CHANGED