surro-gate 0.2.1 → 0.2.2

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
  SHA1:
3
- metadata.gz: 5a9c98fde8240310f3de62a3e4f53d933c726885
4
- data.tar.gz: 11ce2faa9ce61b248954a61ad2c6a25175e2bdfd
3
+ metadata.gz: a27ce2403acd4f16e3d9d0e67209773563b24e9d
4
+ data.tar.gz: d8eb8edee4f24b9b66c03cea8e0df9cb2f6542da
5
5
  SHA512:
6
- metadata.gz: e0245a150b59b21e4a852224386fb6cb00f4f811dc5f91ca4836778a1ca4f61ca089deb29ddf7ff74eb988314fb348f79945f781e5a7b51255ff9807e5aad5b1
7
- data.tar.gz: 6905754a0c9d8a09f5187b0d420852183d3520a0704f81cf42594e9a37de507c3d0f32b2a81b72729aa22547108e2732680d31bd2ec864efb2b6d32d3a0fe20b
6
+ metadata.gz: '0801c6d8fa84ea67afab352c434fcbdcb56b0518ffe5886d372e371dc22ae35cc8de4571b23313ea18612d28c0d2f3e4889b4cee447573c1b8e64635a6ab8f0c'
7
+ data.tar.gz: 9b0324b3ca7638b9869285ea0993994fbabbf297f293eb0bd10b416fb37b4e2c77bc01ca75c7e9b92b7db8f042a906540ca5a94ecdbf237bcd0083ae1d55cd24
data/.travis.yml CHANGED
@@ -2,7 +2,10 @@ sudo: false
2
2
  language: ruby
3
3
  cache: bundler
4
4
  rvm:
5
- - 2.3.1
5
+ - 2.0.0
6
+ - 2.1.9
7
+ - 2.2.6
8
+ - 2.3.3
6
9
  before_install:
7
10
  - "echo 'gem: --no-ri --no-rdoc --no-document' > ~/.gemrc"
8
11
  - "gem install bundler -v 1.13.0"
@@ -13,7 +13,9 @@ module SurroGate
13
13
  class Proxy
14
14
  def initialize(logger)
15
15
  @mutex = Mutex.new
16
- @selector = NIO::Selector.new
16
+ @reader = NIO::Selector.new
17
+ @writer = NIO::Selector.new
18
+ @selectors = [@reader, @writer]
17
19
  @log = logger || Logger.new(STDOUT)
18
20
  end
19
21
 
@@ -51,14 +53,13 @@ module SurroGate
51
53
  private
52
54
 
53
55
  def proxy(left, right, block = nil)
54
- # Pass boths sockets to the Nio4r selector
55
- monitors = [left, right].map { |socket| @selector.register(socket, :rw) }
56
-
57
- # Set up handlers for both monitors
58
- monitors.each do |src|
59
- # Get the destination paired with the source
60
- dst = monitors.reject { |m| m == src }.first
61
- # Set up a proc for future transmissions
56
+ # Register the proxying in both directions
57
+ [[left, right], [right, left]].each do |rd, wr|
58
+ # Set up monitors for read/write separately
59
+ src = @reader.register(rd, :r)
60
+ dst = @writer.register(wr, :w)
61
+
62
+ # Set up handlers for the reader monitor
62
63
  src.value = proc do
63
64
  # Clean up the connection if one of the endpoints gets closed
64
65
  cleanup(src.io, dst.io, &block) if src.io.closed? || dst.io.closed?
@@ -67,8 +68,7 @@ module SurroGate
67
68
  end
68
69
  end
69
70
 
70
- # Make sure that the internal thread is started
71
- thread_start unless @selector.empty?
71
+ thread_start unless @reader.empty? || @writer.empty?
72
72
  end
73
73
 
74
74
  def transmit(src, dst, block)
@@ -81,7 +81,7 @@ module SurroGate
81
81
  def cleanup(*sockets)
82
82
  # Deregister and close the sockets
83
83
  sockets.each do |socket|
84
- @selector.deregister(socket) if @selector.registered?(socket)
84
+ @selectors.each { |selector| selector.deregister(socket) if selector.registered?(socket) }
85
85
  socket.close unless socket.closed?
86
86
  end
87
87
 
@@ -90,7 +90,7 @@ module SurroGate
90
90
  yield if block_given?
91
91
 
92
92
  # Make sure that the internal thread is stopped if no sockets remain
93
- thread_stop if @selector.empty?
93
+ thread_stop if @reader.empty? && @writer.empty?
94
94
  end
95
95
 
96
96
  def thread_start
@@ -111,16 +111,19 @@ module SurroGate
111
111
  end
112
112
 
113
113
  def reactor
114
- # Atomically get an array of readable/writable monitors
115
- monitors = @mutex.synchronize { @selector.select(0.1) || [] }
114
+ # Atomically get an array of readable monitors while also polling for writables
115
+ monitors = @mutex.synchronize do
116
+ @writer.select(0.1)
117
+ @reader.select(0.1) || []
118
+ end
116
119
  # Call each transmission proc and collect the results
117
- callers = monitors.map { |m| m.value.call }
118
- # Sleep for a short time if there was no transmission
119
- sleep(0.1) if callers.none? && monitors.any?
120
+ monitors.map { |m| m.value.call }
120
121
  end
121
122
 
122
123
  def includes?(*sockets)
123
- sockets.map { |socket| @selector.registered?(socket) }.any?
124
+ sockets.any? do |socket|
125
+ @selectors.any? { |selector| selector.registered?(socket) }
126
+ end
124
127
  end
125
128
  end
126
129
  end
@@ -1,3 +1,3 @@
1
1
  module SurroGate
2
- VERSION = '0.2.1'.freeze
2
+ VERSION = '0.2.2'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: surro-gate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dávid Halász
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-02-01 00:00:00.000000000 Z
11
+ date: 2017-02-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nio4r