diff options
author | Eric Wong <e@80x24.org> | 2015-10-09 01:01:53 +0000 |
---|---|---|
committer | Eric Wong <e@80x24.org> | 2015-10-09 21:54:19 +0000 |
commit | 64dc570f4b99f68b5ed792b36e7e8abc3df74927 (patch) | |
tree | 9c953120593bf303d90eda4c6d5afc0b0aa9691d /lib/kgio/autopush/acceptor.rb | |
parent | 26d81c709a842c2435c2ef8acd47ffc0976978ed (diff) | |
download | kgio-64dc570f4b99f68b5ed792b36e7e8abc3df74927.tar.gz |
This avoids breaking compatibility for existing apps, but is less performant (although safer and more resilient to future changes in Ruby) than the previous C version.
Diffstat (limited to 'lib/kgio/autopush/acceptor.rb')
-rw-r--r-- | lib/kgio/autopush/acceptor.rb | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/lib/kgio/autopush/acceptor.rb b/lib/kgio/autopush/acceptor.rb new file mode 100644 index 0000000..2bf6dd9 --- /dev/null +++ b/lib/kgio/autopush/acceptor.rb @@ -0,0 +1,42 @@ +# Copyright (C) 2015 all contributors <kgio-public@bogomips.org> +# License: LGPLv2.1 or later (https://www.gnu.org/licenses/lgpl-2.1.txt) + +# using this code is not recommended, for backwards compatibility only +class Kgio::TCPServer + include Kgio::Autopush + + alias_method :kgio_accept_orig, :kgio_accept + undef_method :kgio_accept + def kgio_accept(*args) + kgio_autopush_post_accept(kgio_accept_orig(*args)) + end + + alias_method :kgio_tryaccept_orig, :kgio_tryaccept + undef_method :kgio_tryaccept + def kgio_tryaccept(*args) + kgio_autopush_post_accept(kgio_tryaccept_orig(*args)) + end + +private + + def kgio_autopush_post_accept(rv) # :nodoc: + return rv unless Kgio.autopush? && rv.respond_to?(:kgio_autopush=) + if my_state = FDMAP[fileno] + if my_state.obj == self + rv.kgio_autopush = true if my_state.ap_state == :acceptor + return rv + end + else + my_state = FDMAP[fileno] ||= Kgio::Autopush::APState.new + end + my_state.obj = self + my_state.ap_state = nil + begin + n = getsockopt(Socket::IPPROTO_TCP, Kgio::Autopush::NOPUSH).unpack('i') + my_state.ap_state = :acceptor if n[0] == 1 + rescue Errno::ENOTSUPP # non-TCP socket + end + rv.kgio_autopush = true if my_state.ap_state == :acceptor + rv + end +end |