about summary refs log tree commit homepage
path: root/lib/rainbows/acceptor.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/rainbows/acceptor.rb')
-rw-r--r--lib/rainbows/acceptor.rb26
1 files changed, 26 insertions, 0 deletions
diff --git a/lib/rainbows/acceptor.rb b/lib/rainbows/acceptor.rb
new file mode 100644
index 0000000..c67bf20
--- /dev/null
+++ b/lib/rainbows/acceptor.rb
@@ -0,0 +1,26 @@
+# -*- encoding: binary -*-
+
+# :enddoc:
+require 'fcntl'
+
+# this should make life easier for Zbatery if compatibility with
+# fcntl-crippled platforms is required (or if FD_CLOEXEC is inherited)
+# and we want to microptimize away fcntl(2) syscalls.
+module Rainbows::Acceptor
+
+  # returns nil if accept fails
+  def sync_accept(sock)
+    rv = sock.accept
+    rv.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC)
+    rv
+  rescue Errno::EAGAIN, Errno::ECONNABORTED, Errno::EINTR
+  end
+
+  # returns nil if accept fails
+  def accept(sock)
+    rv = sock.accept_nonblock
+    rv.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC)
+    rv
+  rescue Errno::EAGAIN, Errno::ECONNABORTED
+  end
+end