socketry 0.1.0 → 0.2.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/.rspec +0 -1
- data/CHANGES.md +4 -0
- data/LICENSE.txt +1 -1
- data/README.md +43 -6
- data/lib/socketry/ssl/socket.rb +8 -2
- data/lib/socketry/tcp/socket.rb +8 -8
- data/lib/socketry/version.rb +1 -1
- data/socketry.gemspec +2 -2
- metadata +2 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3035f86108ed663a3bae8c6ef3d50b09925e9628
|
4
|
+
data.tar.gz: f21df11ab03cbddfe484f6f73ec2c08e501d7d88
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a659f5c5b1be2398364efe1a0a088476d1ec85594c8986c88836baea15fe235d99d58e29b8e0a765de89691a843f35658757dab17586f490bb21adf7be80997d
|
7
|
+
data.tar.gz: 4e96517be39871436dcd8afe7bf22752a58f906fc1da5aef8e0edf3189987fe11905fb7cb6cd2f01332083e1146e4312280c41d51f9c4152bce24aa10ff6a6ac
|
data/.rspec
CHANGED
data/CHANGES.md
CHANGED
data/LICENSE.txt
CHANGED
data/README.md
CHANGED
@@ -19,12 +19,29 @@ Socketry can also be used to provide asynchronous I/O with [Celluloid::IO].
|
|
19
19
|
|
20
20
|
[Celluloid::IO]: https://github.com/celluloid/celluloid-io
|
21
21
|
|
22
|
+
## Motivation
|
23
|
+
|
24
|
+
By default, Ruby sockets do not provide a built-in timeout mechanism. The only
|
25
|
+
timeout mechanism provided by the language leverages [timeout.rb], which uses
|
26
|
+
[unsafe multithreaded behaviors] to implement timeouts.
|
27
|
+
|
28
|
+
While Socketry provides a synchronous, blocking API similar to Ruby's own
|
29
|
+
`TCPSocket` and `UDPSocket` classes, behind the scenes it uses non-blocking I/O
|
30
|
+
to implement thread-safe timeouts.
|
31
|
+
|
32
|
+
Highly modular and pluggable, Socketry also provides the flexibility to
|
33
|
+
seamlessly leverage [Celluloid::IO] for event-driven I/O.
|
34
|
+
|
35
|
+
[timeout.rb]: http://ruby-doc.org/stdlib-2.3.1/libdoc/timeout/rdoc/Timeout.html
|
36
|
+
[unsafe multithreaded behaviors]: http://blog.headius.com/2008/02/ruby-threadraise-threadkill-timeoutrb.html
|
37
|
+
[Celluloid::IO]: https://github.com/celluloid/celluloid-io
|
38
|
+
|
22
39
|
## Installation
|
23
40
|
|
24
41
|
Add this line to your application's Gemfile:
|
25
42
|
|
26
43
|
```ruby
|
27
|
-
gem
|
44
|
+
gem "socketry"
|
28
45
|
```
|
29
46
|
|
30
47
|
And then execute:
|
@@ -35,9 +52,31 @@ Or install it yourself as:
|
|
35
52
|
|
36
53
|
$ gem install socketry
|
37
54
|
|
38
|
-
## Usage
|
55
|
+
## Basic Usage
|
56
|
+
|
57
|
+
Below is a basic example of how to use Socketry to make an HTTPS request:
|
58
|
+
|
59
|
+
```ruby
|
60
|
+
require "socketry"
|
61
|
+
|
62
|
+
socket = Socketry::SSL::Socket.connect("github.com", 443)
|
63
|
+
socket.writepartial("GET / HTTP/1.0\r\nHost: github.com\r\n\r\n")
|
64
|
+
p socket.readpartial(1024)
|
65
|
+
```
|
66
|
+
|
67
|
+
[TCP], [UDP], and [SSL] servers and sockets also available.
|
39
68
|
|
40
|
-
|
69
|
+
[TCP]: https://github.com/socketry/socketry/wiki/TCP
|
70
|
+
[UDP]: https://github.com/socketry/socketry/wiki/UDP
|
71
|
+
[SSL]: https://github.com/socketry/socketry/wiki/SSL
|
72
|
+
|
73
|
+
## Documentation
|
74
|
+
|
75
|
+
[Please see the Socketry wiki](https://github.com/socketry/socketry/wiki)
|
76
|
+
for more detailed documentation and usage notes.
|
77
|
+
|
78
|
+
[YARD API documentation](http://www.rubydoc.info/gems/socketry/)
|
79
|
+
is also available.
|
41
80
|
|
42
81
|
## Contributing
|
43
82
|
|
@@ -48,8 +87,6 @@ TODO: Coming soon!
|
|
48
87
|
|
49
88
|
## License
|
50
89
|
|
51
|
-
Copyright (c) 2016 Tony Arcieri
|
52
|
-
|
53
|
-
Distributed under the MIT License. See
|
90
|
+
Copyright (c) 2016 Tony Arcieri. Distributed under the MIT License. See
|
54
91
|
[LICENSE.txt](https://github.com/socketry/socketry/blob/master/LICENSE.txt)
|
55
92
|
for further details.
|
data/lib/socketry/ssl/socket.rb
CHANGED
@@ -100,9 +100,15 @@ module Socketry
|
|
100
100
|
# @param outbuf [String, NilClass] an optional buffer into which data should be read
|
101
101
|
# @raise [Socketry::Error] an I/O operation failed
|
102
102
|
# @return [String, :wait_readable] data read, or :wait_readable if operation would block
|
103
|
-
def read_nonblock(size)
|
103
|
+
def read_nonblock(size, outbuf: nil)
|
104
104
|
ensure_connected
|
105
|
-
|
105
|
+
case outbuf
|
106
|
+
when String
|
107
|
+
@ssl_socket.read_nonblock(size, outbuf, exception: false)
|
108
|
+
when NilClass
|
109
|
+
@ssl_socket.read_nonblock(size, exception: false)
|
110
|
+
else raise TypeError, "unexpected outbuf class: #{outbuf.class}"
|
111
|
+
end
|
106
112
|
# Some buggy Rubies continue to raise exceptions in these cases
|
107
113
|
rescue IO::WaitReadable
|
108
114
|
:wait_readable
|
data/lib/socketry/tcp/socket.rb
CHANGED
@@ -243,14 +243,14 @@ module Socketry
|
|
243
243
|
#
|
244
244
|
# @return [true, false] true if the socket was open, false if closed
|
245
245
|
def close
|
246
|
-
return false
|
246
|
+
return false if closed?
|
247
247
|
@socket.close
|
248
248
|
true
|
249
249
|
ensure
|
250
250
|
@socket = nil
|
251
251
|
end
|
252
252
|
|
253
|
-
# Is the socket
|
253
|
+
# Is the socket closed?
|
254
254
|
#
|
255
255
|
# This method returns the local connection state. However, it's possible
|
256
256
|
# the remote side has closed the connection, so it's not actually
|
@@ -258,20 +258,20 @@ module Socketry
|
|
258
258
|
# reading from or writing to it. It's sort of like the Heisenberg
|
259
259
|
# uncertainty principle of sockets.
|
260
260
|
#
|
261
|
-
# @return [true, false] do we locally think the socket is
|
262
|
-
def
|
263
|
-
@socket
|
261
|
+
# @return [true, false] do we locally think the socket is closed?
|
262
|
+
def closed?
|
263
|
+
@socket.nil?
|
264
264
|
end
|
265
265
|
|
266
266
|
private
|
267
267
|
|
268
268
|
def ensure_connected
|
269
|
-
|
270
|
-
|
269
|
+
raise StateError, "not connected" if closed?
|
270
|
+
true
|
271
271
|
end
|
272
272
|
|
273
273
|
def ensure_disconnected
|
274
|
-
return true
|
274
|
+
return true if closed?
|
275
275
|
raise StateError, "already connected"
|
276
276
|
end
|
277
277
|
end
|
data/lib/socketry/version.rb
CHANGED
data/socketry.gemspec
CHANGED
@@ -6,10 +6,10 @@ require "socketry/version"
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
7
|
spec.name = "socketry"
|
8
8
|
spec.version = Socketry::VERSION
|
9
|
-
spec.authors = ["Tony Arcieri"
|
9
|
+
spec.authors = ["Tony Arcieri"]
|
10
10
|
spec.email = ["bascule@gmail.com"]
|
11
11
|
spec.licenses = ["MIT"]
|
12
|
-
spec.homepage = "https://github.com/
|
12
|
+
spec.homepage = "https://github.com/socketry/socketry/"
|
13
13
|
spec.summary = "High-level wrappers for Ruby sockets with advanced thread-safe timeout support"
|
14
14
|
spec.description = <<-DESCRIPTION.strip.gsub(/\s+/, " ")
|
15
15
|
Socketry wraps Ruby's sockets with an advanced timeout engine which is able to provide multiple
|
metadata
CHANGED
@@ -1,11 +1,10 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: socketry
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tony Arcieri
|
8
|
-
- Zachary Anker
|
9
8
|
autorequire:
|
10
9
|
bindir: exe
|
11
10
|
cert_chain: []
|
@@ -70,7 +69,7 @@ files:
|
|
70
69
|
- lib/socketry/udp/socket.rb
|
71
70
|
- lib/socketry/version.rb
|
72
71
|
- socketry.gemspec
|
73
|
-
homepage: https://github.com/
|
72
|
+
homepage: https://github.com/socketry/socketry/
|
74
73
|
licenses:
|
75
74
|
- MIT
|
76
75
|
metadata: {}
|