Permalink
Browse files

fixed gets() and brothers

  • Loading branch information...
1 parent d2d9ea3 commit d1a1264790bb58e2c5c27c59e6287edb89d2b2f1 @oldmoe oldmoe committed Jul 9, 2009
Showing with 35 additions and 63 deletions.
  1. +18 −29 lib/core/io/file.rb
  2. +17 −34 lib/core/io/io.rb
View
47 lib/core/io/file.rb
@@ -9,44 +9,33 @@ class File < IO
@@queue = Queue.new
@@thread_pool = []
- 10.times do
+ 20.times do
@@thread_pool << Thread.new do
loop do
- io, method, param, fiber, reactor = *(@@queue.shift)
+ io, method, params, fiber, reactor = *(@@queue.shift)
begin
- result = io.__send__(method, param)
- reactor.next_tick{fiber.resume result}
+ result = io.__send__(method, *params)
+ reactor.next_tick{fiber.resume result} if fiber.alive?
rescue Exception => e
- puts e
- reactor.next_tick{fiber.resume e}
+ reactor.next_tick{fiber.resume e} if fiber.alive?
end
end
end
end
- def sysread(length)
- return rb_sysread(length) unless NB.neverblocking?
- fiber = NB::Fiber.current
- @@queue << [self, :sysread, length, fiber, NB.reactor]
- NB::Fiber.yield
- end
-
- def syswrite(data)
- return rb_syswrite(data) unless NB.neverblocking?
- fiber = NB::Fiber.current
- @@queue << [self, :syswrite, data, fiber, NB.reactor]
- NB::Fiber.yield
- end
-
-=begin
- def self.neverblock(method)
- self.alias_method "rb_#{method}".to_sym method
- self.define_method(method) do |*args|
- return self.send("rb_#{method}", *args) unless NB.neverblocking?
- fiber = NB::Fiber.current
- @@queue << [self, method, args, fiber, NB.reactor]
- NB::Fiber.yield
+ def self.neverblock(*methods)
+ methods.each do |method|
+ class_eval %{
+ def #{method}(args)
+ return rb_#{method}(*args) unless NB.neverblocking?
+ @@queue << [self, :#{method}, args, NB::Fiber.current, NB.reactor]
+ NB::Fiber.yield
+ end
+ }
end
end
-=end
+
+ neverblock :syswrite, :sysread, :write, :read, :readline,
+ :readlines, :readchar, :gets, :getc, :print
+
end
View
51 lib/core/io/io.rb
@@ -43,8 +43,7 @@ def read_neverblock(*args)
# Otherwise it uses the original ruby read method.
def sysread(length)
- return rb_sysread(length) unless self.neverblock?
- read_neverblock(length)
+ self.neverblock? ? read_neverblock(length) : rb_sysread(length)
end
def read(length=nil, sbuffer=nil)
@@ -80,8 +79,6 @@ def read(length=nil, sbuffer=nil)
sbuffer << sysread(NB_BUFFER_LENGTH < remaining_length ? remaining_length : NB_BUFFER_LENGTH)
remaining_length = remaining_length - sbuffer.length
rescue EOFError
- return nil if sbuffer.length.zero? && length > 0
- return sbuffer if sbuffer.length <= length
break
end #begin
end #while
@@ -118,48 +115,34 @@ def write(data)
syswrite(data)
end
- def gets(*args)
- return rb_gets(data) if self.file?
+ def gets(sep=$/)
+ return rb_gets(sep) if self.file?
res = ""
- args[0] = "\n\n" if args[0] == ""
- if args.length == 0
- condition = proc{|res|res.index("\n").nil?}
- elsif args.length == 1
- if args[0] == nil
- condition = proc{|res|true}
- else
- condition = proc{|res|res.index(args[0]).nil?}
- end
- elsif args.length == 2
- count = args[1]
- if args[0] == nil
- condition = proc{|res| count = count - 1; count > -1}
- else
- condition = proc{|res| count = count - 1; count > -1 && res.index(args[0]).nil?}
- end
- end
- begin
- while condition.call(res)
- res << read(1)
- end
- rescue EOFError
+ sep = "\n\n" if sep == ""
+ sep = $/ if sep.nil?
+ while res.index(sep).nil?
+ break if (c = read(1)).nil
+ res << c
end
+ $_ = res
res
end
- def readlines
- return rb_readlines if self.file?
+ def readlines(sep=$/)
+ return rb_readlines(sep) if self.file?
res = []
begin
- loop{res << readline}
+ loop{res << readline(sep)}
rescue EOFError
end
res
end
def readchar
return rb_readchar if self.file?
- sysread(1)[0]
+ ch = read(1)
+ raise EOFError if ch.nil?
+ ch
end
def getc
@@ -171,8 +154,8 @@ def getc
end
end
- def readline(sep = "\n")
- return rb_readline if self.file?
+ def readline(sep = $/)
+ return rb_readline(sep) if self.file?
res = gets(sep)
raise EOFError if res == nil
res

0 comments on commit d1a1264

Please sign in to comment.