tcp-client 0.4.1 → 0.5.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: 4a9fe010bf08d26652d93212beee9ffe55275b3b051809a3093d2ff066719372
4
- data.tar.gz: bf23f4441be6f32d581dfb069afb09bfbb8a9ca78b90f3e0f2332f16abed89e8
3
+ metadata.gz: b3222937127a374a29b4f69d84a1e45d78ccc74bffee2ed05d849782385c5402
4
+ data.tar.gz: 8a56cb5574794f4bbf0f5fcb1f91fbbdc033120eefc86604f881322ddfeedf44
5
5
  SHA512:
6
- metadata.gz: c929de839e7335c4e8e950d0b96fb6028c74ba1ae6a1acec3bb4a572e54a6564d799a8ee8fdef51b77273a83a2af4c5cc29d66a4ed92c3647e1185e103d82fda
7
- data.tar.gz: 00dacdc8d09584e1f4421c7ca62786ab617eaed1005db955ca36da4c1294738591820159d282d27e24fe3194d84e9cd5cb02b47966f231af5d5a26336bcfa366
6
+ metadata.gz: a096f6f5d41be9f63e5018f014aced375ab5e4574a7ea165bd006be23a9246264fcf77269f6be4335ee6f930794b1ef3332e08ccd580d54601eef2468bdf282d
7
+ data.tar.gz: ee8edc735c7de68b28688043c8bf6d2715f177941c4e14f4bb742a91c1fc8edf4d47d3c5273379d4f0ec029418506dc3e52f3bbacfbff6cc4466fe7d22bf40f5
@@ -2,7 +2,7 @@
2
2
 
3
3
  class TCPClient
4
4
  class Deadline
5
- MONOTONIC = !!defined?(Process::CLOCK_MONOTONIC)
5
+ MONOTONIC = defined?(Process::CLOCK_MONOTONIC) ? true : false
6
6
 
7
7
  def initialize(timeout)
8
8
  timeout = timeout&.to_f
@@ -13,12 +13,11 @@ class TCPClient
13
13
  class SSLSocket < ::OpenSSL::SSL::SSLSocket
14
14
  include IOWithDeadlineMixin
15
15
 
16
- def initialize(socket, address, configuration, exception)
16
+ def initialize(socket, address, configuration, deadline, exception)
17
17
  ssl_params = Hash[configuration.ssl_params]
18
18
  super(socket, create_context(ssl_params))
19
19
  self.sync_close = true
20
20
  self.hostname = address.hostname
21
- deadline = Deadline.new(configuration.connect_timeout)
22
21
  deadline.valid? ? connect_with_deadline(deadline, exception) : connect
23
22
  post_connection_check(address.hostname) if should_verify?(ssl_params)
24
23
  end
@@ -26,9 +25,7 @@ class TCPClient
26
25
  private
27
26
 
28
27
  def create_context(ssl_params)
29
- context = OpenSSL::SSL::SSLContext.new
30
- context.set_params(ssl_params)
31
- context
28
+ OpenSSL::SSL::SSLContext.new.tap { |ctx| ctx.set_params(ssl_params) }
32
29
  end
33
30
 
34
31
  def connect_with_deadline(deadline, exception)
@@ -8,21 +8,20 @@ class TCPClient
8
8
  class TCPSocket < ::Socket
9
9
  include IOWithDeadlineMixin
10
10
 
11
- def initialize(address, configuration, exception)
11
+ def initialize(address, configuration, deadline, exception)
12
12
  super(address.addrinfo.ipv6? ? :INET6 : :INET, :STREAM)
13
13
  configure(configuration)
14
- deadline = Deadline.new(configuration.connect_timeout)
15
- connect_to(address, deadline, exception)
14
+ connect_to(as_addr_in(address), deadline, exception)
16
15
  end
17
16
 
18
17
  private
19
18
 
20
- def connect_to(address, deadline, exception)
21
- addr =
22
- ::Socket.pack_sockaddr_in(
23
- address.addrinfo.ip_port,
24
- address.addrinfo.ip_address
25
- )
19
+ def as_addr_in(address)
20
+ addrinfo = address.addrinfo
21
+ ::Socket.pack_sockaddr_in(addrinfo.ip_port, addrinfo.ip_address)
22
+ end
23
+
24
+ def connect_to(addr, deadline, exception)
26
25
  return connect(addr) unless deadline.valid?
27
26
  with_deadline(deadline, exception) do
28
27
  connect_nonblock(addr, exception: false)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class TCPClient
4
- VERSION = '0.4.1'
4
+ VERSION = '0.5.0'
5
5
  end
data/lib/tcp-client.rb CHANGED
@@ -10,33 +10,33 @@ require_relative 'tcp-client/default_configuration'
10
10
  require_relative 'tcp-client/version'
11
11
 
12
12
  class TCPClient
13
- def self.open(addr, configuration = Configuration.default)
13
+ def self.open(address, configuration = Configuration.default)
14
14
  client = new
15
- client.connect(Address.new(addr), configuration)
16
- block_given? ? yield(client) : client
17
- ensure
18
- client&.close if block_given?
15
+ client.connect(Address.new(address), configuration)
16
+ return client unless block_given?
17
+ begin
18
+ yield(client)
19
+ ensure
20
+ client.close
21
+ end
19
22
  end
20
23
 
21
- attr_reader :address
24
+ attr_reader :address, :configuration
22
25
 
23
26
  def initialize
24
- @socket = @address = @deadline = @cfg = nil
27
+ @socket = @address = @deadline = @configuration = nil
25
28
  end
26
29
 
27
30
  def to_s
28
- @address ? @address.to_s : ''
31
+ @address&.to_s || ''
29
32
  end
30
33
 
31
- def connect(addr, configuration, exception: nil)
34
+ def connect(address, configuration, exception: nil)
32
35
  raise(NoOpenSSL) if configuration.ssl? && !defined?(SSLSocket)
33
36
  close
34
- @address = Address.new(addr)
35
- @cfg = configuration.dup
36
- exception ||= configuration.connect_timeout_error
37
- @socket = TCPSocket.new(@address, @cfg, exception)
38
- @cfg.ssl? &&
39
- @socket = SSLSocket.new(@socket, @address, configuration, exception)
37
+ @address = Address.new(address)
38
+ @configuration = configuration.dup.freeze
39
+ @socket = create_socket(exception)
40
40
  self
41
41
  end
42
42
 
@@ -67,7 +67,7 @@ class TCPClient
67
67
  raise(NotConnected) if closed?
68
68
  timeout.nil? && @deadline and
69
69
  return read_with_deadline(nbytes, @deadline, exception)
70
- deadline = Deadline.new(timeout || @cfg.read_timeout)
70
+ deadline = Deadline.new(timeout || @configuration.read_timeout)
71
71
  return @socket.read(nbytes) unless deadline.valid?
72
72
  read_with_deadline(nbytes, deadline, exception)
73
73
  end
@@ -76,7 +76,7 @@ class TCPClient
76
76
  raise(NotConnected) if closed?
77
77
  timeout.nil? && @deadline and
78
78
  return write_with_deadline(msg, @deadline, exception)
79
- deadline = Deadline.new(timeout || @cfg.read_timeout)
79
+ deadline = Deadline.new(timeout || @configuration.read_timeout)
80
80
  return @socket.write(*msg) unless deadline.valid?
81
81
  write_with_deadline(msg, deadline, exception)
82
82
  end
@@ -88,13 +88,31 @@ class TCPClient
88
88
 
89
89
  private
90
90
 
91
+ def create_socket(exception)
92
+ exception ||= @configuration.connect_timeout_error
93
+ deadline = Deadline.new(@configuration.connect_timeout)
94
+ socket = TCPSocket.new(@address, @configuration, deadline, exception)
95
+ @configuration.ssl? ? as_ssl_socket(socket, deadline, exception) : socket
96
+ end
97
+
98
+ def as_ssl_socket(socket, deadline, exception)
99
+ SSLSocket.new(socket, @address, @configuration, deadline, exception)
100
+ rescue StandardError => e
101
+ begin
102
+ socket.close
103
+ rescue IOError
104
+ # ignore!
105
+ end
106
+ raise(e, cause: e.cause)
107
+ end
108
+
91
109
  def read_with_deadline(nbytes, deadline, exception)
92
- exception ||= @cfg.read_timeout_error
110
+ exception ||= @configuration.read_timeout_error
93
111
  @socket.read_with_deadline(nbytes, deadline, exception)
94
112
  end
95
113
 
96
114
  def write_with_deadline(msg, deadline, exception)
97
- exception ||= @cfg.write_timeout_error
115
+ exception ||= @configuration.write_timeout_error
98
116
  msg.sum do |chunk|
99
117
  @socket.write_with_deadline(chunk.b, deadline, exception)
100
118
  end
data/lib/tcp_client.rb CHANGED
@@ -1 +1,3 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative 'tcp-client'
data/tcp-client.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  require_relative './lib/tcp-client/version'
4
4
 
5
- GemSpec = Gem::Specification.new do |spec|
5
+ Gem::Specification.new do |spec|
6
6
  spec.name = 'tcp-client'
7
7
  spec.version = TCPClient::VERSION
8
8
  spec.author = 'Mike Blumtritt'
@@ -20,9 +20,11 @@ GemSpec = Gem::Specification.new do |spec|
20
20
  actions can also be monitored.
21
21
  DESCRIPTION
22
22
  spec.homepage = 'https://github.com/mblumtritt/tcp-client'
23
+ spec.license = 'BSD-3-Clause'
23
24
 
24
25
  spec.metadata['source_code_uri'] = 'https://github.com/mblumtritt/tcp-client'
25
- spec.metadata['bug_tracker_uri'] = 'https://github.com/mblumtritt/tcp-client/issues'
26
+ spec.metadata['bug_tracker_uri'] =
27
+ 'https://github.com/mblumtritt/tcp-client/issues'
26
28
 
27
29
  spec.add_development_dependency 'bundler'
28
30
  spec.add_development_dependency 'minitest'
@@ -32,5 +34,5 @@ GemSpec = Gem::Specification.new do |spec|
32
34
  spec.test_files = all_files.grep(%r{^test/})
33
35
  spec.files = all_files - spec.test_files
34
36
 
35
- spec.extra_rdoc_files = %w[README.md]
37
+ spec.extra_rdoc_files = %w[README.md LICENSE]
36
38
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tcp-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Blumtritt
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-08-01 00:00:00.000000000 Z
11
+ date: 2021-09-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -65,6 +65,7 @@ executables: []
65
65
  extensions: []
66
66
  extra_rdoc_files:
67
67
  - README.md
68
+ - LICENSE
68
69
  files:
69
70
  - ".gitignore"
70
71
  - LICENSE
@@ -93,7 +94,8 @@ files:
93
94
  - test/tcp_client_test.rb
94
95
  - test/test_helper.rb
95
96
  homepage: https://github.com/mblumtritt/tcp-client
96
- licenses: []
97
+ licenses:
98
+ - BSD-3-Clause
97
99
  metadata:
98
100
  source_code_uri: https://github.com/mblumtritt/tcp-client
99
101
  bug_tracker_uri: https://github.com/mblumtritt/tcp-client/issues
@@ -112,7 +114,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
112
114
  - !ruby/object:Gem::Version
113
115
  version: '0'
114
116
  requirements: []
115
- rubygems_version: 3.2.22
117
+ rubygems_version: 3.2.15
116
118
  signing_key:
117
119
  specification_version: 4
118
120
  summary: A TCP client implementation with working timeout support.