rex-core 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +3 -2
- data/lib/rex/core/version.rb +1 -1
- data/lib/rex/exceptions.rb +307 -0
- data/lib/rex/io/datagram_abstraction.rb +26 -0
- data/lib/rex/io/ring_buffer.rb +369 -0
- data/lib/rex/io/socket_abstraction.rb +205 -0
- data/lib/rex/io/stream.rb +312 -0
- data/lib/rex/io/stream_abstraction.rb +32 -0
- data/lib/rex/io/stream_server.rb +221 -0
- data/lib/rex/sync.rb +6 -0
- data/lib/rex/sync/event.rb +85 -0
- data/lib/rex/sync/read_write_lock.rb +177 -0
- data/lib/rex/sync/ref.rb +58 -0
- data/lib/rex/sync/thread_safe.rb +83 -0
- metadata +14 -2
- metadata.gz.sig +0 -0
data/lib/rex/sync/ref.rb
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
# -*- coding: binary -*-
|
2
|
+
require 'thread'
|
3
|
+
|
4
|
+
module Rex
|
5
|
+
|
6
|
+
###
|
7
|
+
#
|
8
|
+
# This module provides a uniform reference counted interface for classes to
|
9
|
+
# use.
|
10
|
+
#
|
11
|
+
###
|
12
|
+
module Ref
|
13
|
+
|
14
|
+
#
|
15
|
+
# Initializes the reference count to one.
|
16
|
+
#
|
17
|
+
def refinit
|
18
|
+
@_references = 1
|
19
|
+
@_references_mutex = Mutex.new
|
20
|
+
|
21
|
+
self
|
22
|
+
end
|
23
|
+
|
24
|
+
#
|
25
|
+
# Increments the total number of references.
|
26
|
+
#
|
27
|
+
def ref
|
28
|
+
@_references_mutex.synchronize {
|
29
|
+
@_references += 1
|
30
|
+
}
|
31
|
+
|
32
|
+
self
|
33
|
+
end
|
34
|
+
|
35
|
+
#
|
36
|
+
# Decrements the total number of references. If the reference count
|
37
|
+
# reaches zero, true is returned. Otherwise, false is returned.
|
38
|
+
#
|
39
|
+
def deref
|
40
|
+
@_references_mutex.synchronize {
|
41
|
+
if ((@_references -= 1) == 0)
|
42
|
+
cleanup
|
43
|
+
|
44
|
+
true
|
45
|
+
else
|
46
|
+
false
|
47
|
+
end
|
48
|
+
}
|
49
|
+
end
|
50
|
+
|
51
|
+
#
|
52
|
+
# Called to clean up resources once the ref count drops to zero.
|
53
|
+
#
|
54
|
+
def cleanup
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
# -*- coding: binary -*-
|
2
|
+
require 'timeout'
|
3
|
+
|
4
|
+
module Rex
|
5
|
+
|
6
|
+
###
|
7
|
+
#
|
8
|
+
# This module provides a set of methods for performing various blocking
|
9
|
+
# operations in a manner that is compatible with ruby style threads.
|
10
|
+
#
|
11
|
+
###
|
12
|
+
module ThreadSafe
|
13
|
+
|
14
|
+
DefaultCycle = 0.2
|
15
|
+
|
16
|
+
#
|
17
|
+
# Wraps calls to select with a lower timeout period and does the
|
18
|
+
# calculations to walk down to zero timeout. This has a little room for
|
19
|
+
# improvement in that it should probably check how much time actually
|
20
|
+
# elapsed during the select call considering ruby threading wont be exactly
|
21
|
+
# accurate perhaps.
|
22
|
+
#
|
23
|
+
def self.select(rfd = nil, wfd = nil, efd = nil, t = nil)
|
24
|
+
left = t
|
25
|
+
|
26
|
+
# Immediately raise a StreamClosedError if the socket was closed. This
|
27
|
+
# prevents a bad fd from being passed downstream and solves an issue
|
28
|
+
# with Ruby on Windows.
|
29
|
+
rfd.each { |fd| raise StreamClosedError.new(fd) if (fd.closed?) } if rfd
|
30
|
+
|
31
|
+
begin
|
32
|
+
orig_size = rfd.length if (rfd)
|
33
|
+
|
34
|
+
# Poll the set supplied to us at least once.
|
35
|
+
begin
|
36
|
+
rv = ::IO.select(rfd, wfd, efd, DefaultCycle)
|
37
|
+
rescue ::IOError, ::Errno::EBADF, ::Errno::ENOTSOCK
|
38
|
+
# If a stream was detected as being closed, re-raise the error as
|
39
|
+
# a StreamClosedError with the specific file descriptor that was
|
40
|
+
# detected as being closed. This is to better handle the case of
|
41
|
+
# a closed socket being detected so that it can be cleaned up and
|
42
|
+
# removed.
|
43
|
+
rfd.each { |fd| raise StreamClosedError.new(fd) if (fd.closed?) } if rfd
|
44
|
+
|
45
|
+
# If the original rfd length is not the same as the current
|
46
|
+
# length, then the list may have been altered and as such may not
|
47
|
+
# contain the socket that caused the IOError. This is a bad way
|
48
|
+
# to do this since it's possible that the array length could be
|
49
|
+
# back to the size that it was originally and yet have had the
|
50
|
+
# socket that caused the IOError to be removed.
|
51
|
+
return nil if (rfd and rfd.length != orig_size)
|
52
|
+
|
53
|
+
# Re-raise the exception since we didn't handle it here.
|
54
|
+
raise $!
|
55
|
+
# rescue ::Exception => e
|
56
|
+
# $stderr.puts "SELECT(#{t}) #{[rfd,wfd,efd].inspect} #{e.class} #{e} #{e.backtrace}"
|
57
|
+
end
|
58
|
+
|
59
|
+
return rv if (rv)
|
60
|
+
|
61
|
+
# Decrement the amount of time left by the polling cycle
|
62
|
+
left -= DefaultCycle if (left)
|
63
|
+
|
64
|
+
# Keep chugging until we run out of time, if time was supplied.
|
65
|
+
end while ((left == nil) or (left > 0))
|
66
|
+
|
67
|
+
# Nothin.
|
68
|
+
nil
|
69
|
+
end
|
70
|
+
|
71
|
+
#
|
72
|
+
# Simulates a sleep operation by selecting on nil until a timeout period
|
73
|
+
# expires.
|
74
|
+
#
|
75
|
+
def self.sleep(seconds=nil)
|
76
|
+
self.select(nil, nil, nil, seconds)
|
77
|
+
|
78
|
+
seconds
|
79
|
+
end
|
80
|
+
|
81
|
+
end
|
82
|
+
|
83
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rex-core
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- David Maloney
|
@@ -85,7 +85,7 @@ cert_chain:
|
|
85
85
|
2SpuQH+SWteq3NXkAmFEEqvLJQ4sbptZt8OP8ghL3pVAvZNFmww/YVszSkShSzcg
|
86
86
|
QdihYCSEL2drS2cFd50jBeq71sxUtxbv82DUa2b+
|
87
87
|
-----END CERTIFICATE-----
|
88
|
-
date: 2016-08-
|
88
|
+
date: 2016-08-26 00:00:00.000000000 Z
|
89
89
|
dependencies:
|
90
90
|
- !ruby/object:Gem::Dependency
|
91
91
|
name: bundler
|
@@ -148,7 +148,19 @@ files:
|
|
148
148
|
- lib/rex/compat.rb
|
149
149
|
- lib/rex/core.rb
|
150
150
|
- lib/rex/core/version.rb
|
151
|
+
- lib/rex/exceptions.rb
|
151
152
|
- lib/rex/file.rb
|
153
|
+
- lib/rex/io/datagram_abstraction.rb
|
154
|
+
- lib/rex/io/ring_buffer.rb
|
155
|
+
- lib/rex/io/socket_abstraction.rb
|
156
|
+
- lib/rex/io/stream.rb
|
157
|
+
- lib/rex/io/stream_abstraction.rb
|
158
|
+
- lib/rex/io/stream_server.rb
|
159
|
+
- lib/rex/sync.rb
|
160
|
+
- lib/rex/sync/event.rb
|
161
|
+
- lib/rex/sync/read_write_lock.rb
|
162
|
+
- lib/rex/sync/ref.rb
|
163
|
+
- lib/rex/sync/thread_safe.rb
|
152
164
|
- rex-core.gemspec
|
153
165
|
homepage: https://github.com/rapid7/rex-core
|
154
166
|
licenses: []
|
metadata.gz.sig
CHANGED
Binary file
|