tcp-client 0.4.1 → 0.5.0
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 +4 -4
- data/lib/tcp-client/deadline.rb +1 -1
- data/lib/tcp-client/ssl_socket.rb +2 -5
- data/lib/tcp-client/tcp_socket.rb +8 -9
- data/lib/tcp-client/version.rb +1 -1
- data/lib/tcp-client.rb +37 -19
- data/lib/tcp_client.rb +2 -0
- data/tcp-client.gemspec +5 -3
- metadata +6 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b3222937127a374a29b4f69d84a1e45d78ccc74bffee2ed05d849782385c5402
|
4
|
+
data.tar.gz: 8a56cb5574794f4bbf0f5fcb1f91fbbdc033120eefc86604f881322ddfeedf44
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a096f6f5d41be9f63e5018f014aced375ab5e4574a7ea165bd006be23a9246264fcf77269f6be4335ee6f930794b1ef3332e08ccd580d54601eef2468bdf282d
|
7
|
+
data.tar.gz: ee8edc735c7de68b28688043c8bf6d2715f177941c4e14f4bb742a91c1fc8edf4d47d3c5273379d4f0ec029418506dc3e52f3bbacfbff6cc4466fe7d22bf40f5
|
data/lib/tcp-client/deadline.rb
CHANGED
@@ -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
|
-
|
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
|
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
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
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)
|
data/lib/tcp-client/version.rb
CHANGED
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(
|
13
|
+
def self.open(address, configuration = Configuration.default)
|
14
14
|
client = new
|
15
|
-
client.connect(Address.new(
|
16
|
-
|
17
|
-
|
18
|
-
|
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 = @
|
27
|
+
@socket = @address = @deadline = @configuration = nil
|
25
28
|
end
|
26
29
|
|
27
30
|
def to_s
|
28
|
-
@address
|
31
|
+
@address&.to_s || ''
|
29
32
|
end
|
30
33
|
|
31
|
-
def connect(
|
34
|
+
def connect(address, configuration, exception: nil)
|
32
35
|
raise(NoOpenSSL) if configuration.ssl? && !defined?(SSLSocket)
|
33
36
|
close
|
34
|
-
@address = Address.new(
|
35
|
-
@
|
36
|
-
|
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 || @
|
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 || @
|
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 ||= @
|
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 ||= @
|
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
data/tcp-client.gemspec
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
require_relative './lib/tcp-client/version'
|
4
4
|
|
5
|
-
|
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'] =
|
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
|
+
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-
|
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.
|
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.
|