1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
|
## io.rb --- convenience features for IO objects
# Copyright (C) 2005, 2006 Daniel Brockman
# This program is free software; you can redistribute it
# and/or modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation;
# either version 2 of the License, or (at your option) any
# later version.
# This file is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
# You should have received a copy of the GNU General Public
# License along with this program; if not, write to the Free
# Software Foundation, 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301, USA.
require "event-loop"
require "fcntl"
class Symbol
def io_state?
EventLoop::IO_STATES.include? self
end
end
module EventLoop::Watchable
include SignalEmitter
define_signals :readable, :writable, :exceptional
def monitor_events (*events)
EventLoop.monitor_io(self, *events) end
def ignore_events (*events)
EventLoop.ignore_io(self, *events) end
define_soft_aliases \
:monitor_event => :monitor_events,
:ignore_event => :ignore_events
def close ; super
ignore_events end
def close_read ; super
ignore_event :readable end
def close_write ; super
ignore_event :writable end
module Automatic
include EventLoop::Watchable
def add_signal_handler (name, &handler) super
monitor_event(name) if name.io_state?
end
def remove_signal_handler (name, handler) super
if @signal_handlers[name].empty?
ignore_event(name) if name.io_state?
end
end
end
end
class IO
def on_readable &block
extend EventLoop::Watchable::Automatic
on_readable(&block)
end
def on_writable &block
extend EventLoop::Watchable::Automatic
on_writable(&block)
end
def on_exceptional &block
extend EventLoop::Watchable::Automatic
on_exceptional(&block)
end
def will_block?
require "fcntl"
fcntl(Fcntl::F_GETFL, 0) & Fcntl::O_NONBLOCK == 0
end
def will_block= (wants_blocking)
require "fcntl"
flags = fcntl(Fcntl::F_GETFL, 0)
if wants_blocking
flags &= ~Fcntl::O_NONBLOCK
else
flags |= Fcntl::O_NONBLOCK
end
fcntl(Fcntl::F_SETFL, flags)
end
end
## io.rb ends here.
|