io_request 2.3.1 → 2.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e57d41dbda1d6665dd595e36da99ce52b60fd2d979957583570224e752b6e8ae
4
- data.tar.gz: eb50b39358a127a3b6b231d37606a4835b92030b682987bdd6f5a973d3c5b8bd
3
+ metadata.gz: a560819ec6ad8dc50801411fcea4ca4263b29e88067187cc2035e3cbad133394
4
+ data.tar.gz: 1d82e187a4044f25e3ee061a7f382423e1013644dff7663467e694e4139fc8f2
5
5
  SHA512:
6
- metadata.gz: d9680f420318ec5d177ada3d79cccd5bab722e1993f541e802d877603bfa9011fb22cd0df5b9f01602a7a4ba54131203d1f2a7645950cdc4ef0adc151fb85c15
7
- data.tar.gz: 92ea531ada90eda834b847d86009c4ede9594185540acbe7f76a65d0f579b62ff739b9a5ef01acc3d3b08657b97a7ab3a8e36c0dacc90024bd60c53c53b85866
6
+ metadata.gz: ff19727550d073a8cccc146a2c1de65a6f67458a276b003e122e471ef73d584a52741bdaf07da51e0ed2513f33d0cf8a139eb4e5f0c342e2f6a77cc929f2982a
7
+ data.tar.gz: 0eaf1f54fcac605a4dc62a8ff7b7bcc41a7d68ee3e75c2020971e9bfb432cc08f22799506ae6e80409f4892b2e5e12cee2e5da28f4657f13a059dfad900402e2
@@ -45,15 +45,17 @@ module IORequest
45
45
  # @param r [IO] object to read from.
46
46
  # @param w [IO] object to write to.
47
47
  # @param rw [IO] read-write object (replaces `r` and `w` arguments).
48
+ # @return [Object] data from {Authorizer}
48
49
  def open(read: nil, write: nil, read_write: nil)
49
50
  @io_r = read_write || read
50
51
  @io_w = read_write || write
51
52
 
52
53
  IORequest.logger.debug(prog_name) { 'Starting connection' }
53
54
 
54
- authorization
55
+ auth_data = authorization
55
56
  @open = true
56
57
  @data_transition_thread = in_thread(name: 'connection') { data_transition_loop }
58
+ auth_data
57
59
  end
58
60
 
59
61
  def open?
@@ -124,15 +126,19 @@ module IORequest
124
126
  end
125
127
 
126
128
  def authorization
127
- auth_successful = @mutex_r.synchronize do
129
+ auth_successful = false
130
+ data = nil
131
+ @mutex_r.synchronize do
128
132
  @mutex_w.synchronize do
129
133
  IORequest.logger.debug(prog_name) { 'Authorizing new client' }
130
- @authorizer.authorize(@io_r, @io_w)
134
+ auth_successful = @authorizer.authorize(@io_r, @io_w)
135
+ data = @authorizer.data
131
136
  end
132
137
  end
133
138
  raise AuthorizationFailureError unless auth_successful
134
139
 
135
- IORequest.logger.debug(prog_name) { "New client authorized with data #{@authorizer.data}" }
140
+ IORequest.logger.debug(prog_name) { "New client authorized with data #{data}" }
141
+ data
136
142
  end
137
143
 
138
144
  def data_transition_loop
@@ -32,11 +32,20 @@ module IORequest
32
32
  end
33
33
 
34
34
  # @return [Array<IORequest::Client>]
35
- attr_reader :clients
35
+ def clients
36
+ @clients_data.keys
37
+ end
38
+
39
+ # @param client [IORequest::Client]
40
+ # @return [Hash, nil] you are free to store anything you want in hash.
41
+ # Only field you will find in it is `auth` with authenticator data.
42
+ def data(client)
43
+ @clients_data[client]
44
+ end
36
45
 
37
46
  # Start server.
38
47
  def start
39
- @clients = []
48
+ @clients_data = {}
40
49
 
41
50
  @server = TCPServer.new(@port)
42
51
 
@@ -45,8 +54,8 @@ module IORequest
45
54
 
46
55
  # Fully stop server.
47
56
  def stop
48
- @clients.each(&:close)
49
- @clients = []
57
+ clients.each(&:close)
58
+ @clients_data.clear
50
59
 
51
60
  @server.close
52
61
  @server = nil
@@ -68,7 +77,7 @@ module IORequest
68
77
  while (socket = @server.accept)
69
78
  handle_socket(socket)
70
79
  end
71
- rescue
80
+ rescue StandardError
72
81
  stop
73
82
  end
74
83
 
@@ -76,21 +85,22 @@ module IORequest
76
85
  ssl_socket = OpenSSL::SSL::SSLSocket.new(socket, @ctx)
77
86
  ssl_socket.accept
78
87
 
79
- client = IORequest::Client.new authorizer: @authorizer
80
- begin
81
- client.open read_write: ssl_socket
82
- client.on_request { |data| @requests_handler.call(data, client) }
83
- @clients << client
84
- client.on_close do
85
- @clients.select!(&:open?)
86
- end
87
- rescue StandardError => e
88
- IORequest.logger.debug "Failed to open client: #{e}"
89
- ssl_socket.close
90
- end
88
+ handle_client(ssl_socket, IORequest::Client.new(authorizer: @authorizer))
91
89
  rescue StandardError => e
92
90
  IORequest.logger.warn "Unknown error while handling sockets: #{e}"
93
91
  end
92
+
93
+ def handle_client(ssl_socket, client)
94
+ auth_data = client.open read_write: ssl_socket
95
+ client.on_request { |data| @requests_handler.call(data, client) }
96
+ @clients_data[client] = { auth: auth_data }
97
+ client.on_close do
98
+ @clients_data.select! { |c, _d| c.open? }
99
+ end
100
+ rescue StandardError => e
101
+ IORequest.logger.debug "Failed to open client: #{e}"
102
+ ssl_socket.close
103
+ end
94
104
  end
95
105
 
96
106
  # SSL socket client.
@@ -2,5 +2,5 @@
2
2
 
3
3
  module IORequest
4
4
  # Gem version.
5
- VERSION = '2.3.1'
5
+ VERSION = '2.4.0'
6
6
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: io_request
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.3.1
4
+ version: 2.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fizvlad
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-07-19 00:00:00.000000000 Z
11
+ date: 2020-07-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler