tcp-client 0.6.0 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/tcp-client/configuration.rb +5 -5
- data/lib/tcp-client/errors.rb +20 -8
- data/lib/tcp-client/mixin/io_with_deadline.rb +22 -1
- data/lib/tcp-client/ssl_socket.rb +2 -1
- data/lib/tcp-client/version.rb +1 -1
- data/lib/tcp-client.rb +6 -6
- data/test/tcp_client_test.rb +8 -4
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 582575d6b5a66a264900141fd5010a1a80a363e15d0ca369f262889d99d932d0
|
4
|
+
data.tar.gz: a0c2335a50addbf5f424d869cd2931256a1334a0a075530ef3873d9065246834
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b160bd656c4f6091a669cf65349e54aeaa2e7c057e0a5a3c896edf5e8ab91166a3a5cdafedfe84e4b04d5a79e0602fb2433c6ff9144e6881746f3607b8d8290b
|
7
|
+
data.tar.gz: 6726ebdfbc777258e62b3d915fa7e4a57f7ae514d6259ce5f0051ee649294142acf09faac490901e32083072d7a68dd4ce71fa697e0bf05c0d9556c0d57bb6a5
|
@@ -83,23 +83,23 @@ class TCPClient
|
|
83
83
|
end
|
84
84
|
|
85
85
|
def timeout_error=(exception)
|
86
|
-
raise(
|
86
|
+
raise(NotAnExceptionError, exception) unless exception_class?(exception)
|
87
87
|
@connect_timeout_error =
|
88
88
|
@read_timeout_error = @write_timeout_error = exception
|
89
89
|
end
|
90
90
|
|
91
91
|
def connect_timeout_error=(exception)
|
92
|
-
raise(
|
92
|
+
raise(NotAnExceptionError, exception) unless exception_class?(exception)
|
93
93
|
@connect_timeout_error = exception
|
94
94
|
end
|
95
95
|
|
96
96
|
def read_timeout_error=(exception)
|
97
|
-
raise(
|
97
|
+
raise(NotAnExceptionError, exception) unless exception_class?(exception)
|
98
98
|
@read_timeout_error = exception
|
99
99
|
end
|
100
100
|
|
101
101
|
def write_timeout_error=(exception)
|
102
|
-
raise(
|
102
|
+
raise(NotAnExceptionError, exception) unless exception_class?(exception)
|
103
103
|
@write_timeout_error = exception
|
104
104
|
end
|
105
105
|
|
@@ -136,7 +136,7 @@ class TCPClient
|
|
136
136
|
def set(attribute, value)
|
137
137
|
public_send("#{attribute}=", value)
|
138
138
|
rescue NoMethodError
|
139
|
-
raise(
|
139
|
+
raise(UnknownAttributeError, attribute)
|
140
140
|
end
|
141
141
|
|
142
142
|
def seconds(value)
|
data/lib/tcp-client/errors.rb
CHANGED
@@ -1,37 +1,37 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
class TCPClient
|
4
|
-
class
|
4
|
+
class NoOpenSSLError < RuntimeError
|
5
5
|
def initialize
|
6
6
|
super('OpenSSL is not available')
|
7
7
|
end
|
8
8
|
end
|
9
9
|
|
10
|
-
class
|
10
|
+
class NoBlockGivenError < ArgumentError
|
11
11
|
def initialize
|
12
12
|
super('no block given')
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
16
|
-
class
|
16
|
+
class InvalidDeadLineError < ArgumentError
|
17
17
|
def initialize(timeout)
|
18
18
|
super("invalid deadline - #{timeout}")
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
22
|
-
class
|
22
|
+
class UnknownAttributeError < ArgumentError
|
23
23
|
def initialize(attribute)
|
24
24
|
super("unknown attribute - #{attribute}")
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
28
|
-
class
|
28
|
+
class NotAnExceptionError < TypeError
|
29
29
|
def initialize(object)
|
30
30
|
super("exception class required - #{object.inspect}")
|
31
31
|
end
|
32
32
|
end
|
33
33
|
|
34
|
-
class
|
34
|
+
class NotConnectedError < IOError
|
35
35
|
def initialize
|
36
36
|
super('client not connected')
|
37
37
|
end
|
@@ -65,6 +65,18 @@ class TCPClient
|
|
65
65
|
end
|
66
66
|
end
|
67
67
|
|
68
|
-
|
69
|
-
|
68
|
+
NoOpenSSL = NoOpenSSLError
|
69
|
+
NoBlockGiven = NoBlockGivenError
|
70
|
+
InvalidDeadLine = InvalidDeadLineError
|
71
|
+
UnknownAttribute = UnknownAttributeError
|
72
|
+
NotAnException = NotAnExceptionError
|
73
|
+
NotConnected = NotConnectedError
|
74
|
+
deprecate_constant(
|
75
|
+
:NoOpenSSL,
|
76
|
+
:NoBlockGiven,
|
77
|
+
:InvalidDeadLine,
|
78
|
+
:UnknownAttribute,
|
79
|
+
:NotAnException,
|
80
|
+
:NotConnected
|
81
|
+
)
|
70
82
|
end
|
@@ -5,6 +5,8 @@ module IOWithDeadlineMixin
|
|
5
5
|
methods = mod.instance_methods
|
6
6
|
if methods.index(:wait_writable) && methods.index(:wait_readable)
|
7
7
|
mod.include(ViaWaitMethod)
|
8
|
+
elsif methods.index(:to_io)
|
9
|
+
mod.include(ViaIOWaitMethod)
|
8
10
|
else
|
9
11
|
mod.include(ViaSelect)
|
10
12
|
end
|
@@ -66,6 +68,25 @@ module IOWithDeadlineMixin
|
|
66
68
|
end
|
67
69
|
end
|
68
70
|
|
71
|
+
module ViaIOWaitMethod
|
72
|
+
private def with_deadline(deadline, exception)
|
73
|
+
loop do
|
74
|
+
case ret = yield
|
75
|
+
when :wait_writable
|
76
|
+
remaining_time = deadline.remaining_time or raise(exception)
|
77
|
+
raise(exception) if to_io.wait_writable(remaining_time).nil?
|
78
|
+
when :wait_readable
|
79
|
+
remaining_time = deadline.remaining_time or raise(exception)
|
80
|
+
raise(exception) if to_io.wait_readable(remaining_time).nil?
|
81
|
+
else
|
82
|
+
return ret
|
83
|
+
end
|
84
|
+
end
|
85
|
+
rescue Errno::ETIMEDOUT
|
86
|
+
raise(exception)
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
69
90
|
module ViaSelect
|
70
91
|
private def with_deadline(deadline, exception)
|
71
92
|
loop do
|
@@ -85,5 +106,5 @@ module IOWithDeadlineMixin
|
|
85
106
|
end
|
86
107
|
end
|
87
108
|
|
88
|
-
private_constant(:ViaWaitMethod, :ViaSelect)
|
109
|
+
private_constant(:ViaWaitMethod, :ViaIOWaitMethod, :ViaSelect)
|
89
110
|
end
|
data/lib/tcp-client/version.rb
CHANGED
data/lib/tcp-client.rb
CHANGED
@@ -24,7 +24,7 @@ class TCPClient
|
|
24
24
|
configuration = Configuration.default
|
25
25
|
)
|
26
26
|
client = nil
|
27
|
-
raise(
|
27
|
+
raise(NoBlockGivenError) unless block_given?
|
28
28
|
address = Address.new(address)
|
29
29
|
client = new
|
30
30
|
client.with_deadline(timeout) do
|
@@ -46,7 +46,7 @@ class TCPClient
|
|
46
46
|
|
47
47
|
def connect(address, configuration, timeout: nil, exception: nil)
|
48
48
|
close if @socket
|
49
|
-
raise(
|
49
|
+
raise(NoOpenSSLError) if configuration.ssl? && !defined?(SSLSocket)
|
50
50
|
@address = Address.new(address)
|
51
51
|
@configuration = configuration.dup.freeze
|
52
52
|
@socket = create_socket(timeout, exception)
|
@@ -68,16 +68,16 @@ class TCPClient
|
|
68
68
|
|
69
69
|
def with_deadline(timeout)
|
70
70
|
previous_deadline = @deadline
|
71
|
-
raise(
|
71
|
+
raise(NoBlockGivenError) unless block_given?
|
72
72
|
@deadline = Deadline.new(timeout)
|
73
|
-
raise(
|
73
|
+
raise(InvalidDeadLineError, timeout) unless @deadline.valid?
|
74
74
|
yield(self)
|
75
75
|
ensure
|
76
76
|
@deadline = previous_deadline
|
77
77
|
end
|
78
78
|
|
79
79
|
def read(nbytes = nil, timeout: nil, exception: nil)
|
80
|
-
raise(
|
80
|
+
raise(NotConnectedError) if closed?
|
81
81
|
deadline = create_deadline(timeout, configuration.read_timeout)
|
82
82
|
return @socket.read(nbytes) unless deadline.valid?
|
83
83
|
exception ||= configuration.read_timeout_error
|
@@ -85,7 +85,7 @@ class TCPClient
|
|
85
85
|
end
|
86
86
|
|
87
87
|
def write(*msg, timeout: nil, exception: nil)
|
88
|
-
raise(
|
88
|
+
raise(NotConnectedError) if closed?
|
89
89
|
deadline = create_deadline(timeout, configuration.write_timeout)
|
90
90
|
return @socket.write(*msg) unless deadline.valid?
|
91
91
|
exception ||= configuration.write_timeout_error
|
data/test/tcp_client_test.rb
CHANGED
@@ -25,8 +25,10 @@ class TCPClientTest < Test
|
|
25
25
|
assert_equal('', subject.to_s)
|
26
26
|
assert_nil(subject.address)
|
27
27
|
subject.close
|
28
|
-
assert_raises(TCPClient::
|
29
|
-
|
28
|
+
assert_raises(TCPClient::NotConnectedError) do
|
29
|
+
subject.write('hello world!')
|
30
|
+
end
|
31
|
+
assert_raises(TCPClient::NotConnectedError) { subject.read(42) }
|
30
32
|
end
|
31
33
|
|
32
34
|
def create_nonconnected_client
|
@@ -46,8 +48,10 @@ class TCPClientTest < Test
|
|
46
48
|
assert_equal('localhost', subject.address.hostname)
|
47
49
|
assert_instance_of(Addrinfo, subject.address.addrinfo)
|
48
50
|
assert_same(0, subject.address.addrinfo.ip_port)
|
49
|
-
assert_raises(TCPClient::
|
50
|
-
|
51
|
+
assert_raises(TCPClient::NotConnectedError) do
|
52
|
+
subject.write('hello world!')
|
53
|
+
end
|
54
|
+
assert_raises(TCPClient::NotConnectedError) { subject.read(42) }
|
51
55
|
end
|
52
56
|
|
53
57
|
def test_connected_state
|
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.7.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-11-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|