Skip to content

Commit

Permalink
using the new read_ and write_nonblock signature (ruby 2.1) in which …
Browse files Browse the repository at this point in the history
…one doesn't need to handle Wait* Exception (Closes celluloid#139)
  • Loading branch information
Tiago Cardoso committed Jan 8, 2016
1 parent 21ddab6 commit 5321f59
Showing 1 changed file with 24 additions and 12 deletions.
36 changes: 24 additions & 12 deletions lib/celluloid/io/stream.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,15 @@ def sysread(length = nil, buffer = nil)
buffer ||= ''.force_encoding(Encoding::ASCII_8BIT)

@read_latch.synchronize do
begin
read_nonblock(length, buffer)
rescue ::IO::WaitReadable
wait_readable
retry
if RUBY_VERSION < '2.1'
begin
read_nonblock(length, buffer)
rescue ::IO::WaitReadable
wait_readable
retry
end
else
loop while read_nonblock(length, buffer, exception: false) == :wait_readable
end
end

Expand All @@ -57,13 +61,21 @@ def syswrite(string)

@write_latch.synchronize do
while total_written < length
begin
written = write_nonblock(remaining)
rescue ::IO::WaitWritable
wait_writable
retry
rescue EOFError
return total_written
if RUBY_VERSION < '2.1'
begin
written = write_nonblock(remaining)
rescue ::IO::WaitWritable
wait_writable
retry
rescue EOFError
return total_written
end
else
begin
loop while (written = write_nonblock(remaining, exception: false)) == :wait_writable
rescue EOFError
return total_written
end
end

total_written += written
Expand Down

2 comments on commit 5321f59

@ioquatix
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really prefer to have two implementation files and only require one based on version, rather than having an if block.

@HoneyryderChuck
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ioquatix this has been rewritten, see my last commit.

Please sign in to comment.