Changeset 602
- Timestamp:
- 12/05/07 07:06:30 (1 year ago)
- Files:
-
- version_0/lib/pr_eventmachine.rb (modified) (4 diffs)
- version_0/tests/test_epoll.rb (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
version_0/lib/pr_eventmachine.rb
r600 r602 106 106 end 107 107 108 # #start_unix_server 109 def start_unix_server chain 110 (s = EvmaUNIXServer.start_server chain) or raise "no acceptor" 111 s.uuid 112 end 113 114 # #connect_unix_server 115 def connect_unix_server chain 116 EvmaUNIXClient.connect(chain).uuid 117 end 118 108 119 # #signal_loopbreak 109 120 def signal_loopbreak … … 134 145 def set_timer_quantum interval 135 146 Reactor.instance.set_timer_quantum(( 1.0 * interval) / 1000.0) 147 end 148 149 # #epoll is a harmless no-op in the pure-Ruby implementation. This is intended to ensure 150 # that user code behaves properly across different EM implementations. 151 def epoll 152 end 153 154 # #set_rlimit_nofile is a no-op in the pure-Ruby implementation. We simply return Ruby's built-in 155 # per-process file-descriptor limit. 156 def set_rlimit_nofile n 157 1024 136 158 end 137 159 … … 530 552 #-------------------------------------------------------------- 531 553 554 555 556 module EventMachine 557 class EvmaUNIXClient < StreamObject 558 559 def self.connect chain 560 sd = Socket.new( Socket::AF_LOCAL, Socket::SOCK_STREAM, 0 ) 561 begin 562 # TODO, this assumes a current Ruby snapshot. 563 # We need to degrade to a nonblocking connect otherwise. 564 sd.connect_nonblock( Socket.pack_sockaddr_un( chain )) 565 rescue Errno::EINPROGRESS 566 end 567 EvmaUNIXClient.new sd 568 end 569 570 571 def initialize io 572 super 573 @pending = true 574 end 575 576 577 def select_for_writing? 578 @pending ? true : super 579 end 580 581 def select_for_reading? 582 @pending ? false : super 583 end 584 585 def eventable_write 586 if @pending 587 @pending = false 588 EventMachine::event_callback uuid, ConnectionCompleted, "" 589 else 590 super 591 end 592 end 593 594 595 596 end 597 end 598 599 600 #-------------------------------------------------------------- 601 532 602 module EventMachine 533 603 class EvmaTCPServer < Selectable 604 605 # TODO, refactor and unify with EvmaUNIXServer. 534 606 535 607 class << self … … 544 616 sd.listen( 50 ) # 5 is what you see in all the books. Ain't enough. 545 617 EvmaTCPServer.new sd 618 end 619 end 620 621 def initialize io 622 super io 623 end 624 625 626 def select_for_reading? 627 true 628 end 629 630 #-- 631 # accept_nonblock returns an array consisting of the accepted 632 # socket and a sockaddr_in which names the peer. 633 # Don't accept more than 10 at a time. 634 def eventable_read 635 begin 636 10.times { 637 descriptor,peername = io.accept_nonblock 638 sd = StreamObject.new descriptor 639 EventMachine::event_callback uuid, ConnectionAccepted, sd.uuid 640 } 641 rescue Errno::EWOULDBLOCK, Errno::EAGAIN 642 end 643 end 644 645 #-- 646 # 647 def schedule_close 648 @close_scheduled = true 649 end 650 651 end 652 end 653 654 655 #-------------------------------------------------------------- 656 657 module EventMachine 658 class EvmaUNIXServer < Selectable 659 660 # TODO, refactor and unify with EvmaTCPServer. 661 662 class << self 663 # Versions of ruby 1.8.4 later than May 26 2006 will work properly 664 # with an object of type TCPServer. Prior versions won't so we 665 # play it safe and just build a socket. 666 # 667 def start_server chain 668 sd = Socket.new( Socket::AF_LOCAL, Socket::SOCK_STREAM, 0 ) 669 sd.setsockopt( Socket::SOL_SOCKET, Socket::SO_REUSEADDR, true ) 670 sd.bind( Socket.pack_sockaddr_un( chain )) 671 sd.listen( 50 ) # 5 is what you see in all the books. Ain't enough. 672 EvmaUNIXServer.new sd 546 673 end 547 674 end version_0/tests/test_epoll.rb
r456 r602 144 144 s = EM.set_descriptor_table_size 60000 145 145 EM.run { 146 # The pure-Ruby version won't let us open the socket if the node already exists. 147 # Not sure, that actually may be correct and the compiled version is wrong. 148 # Pure Ruby also oddly won't let us make that many connections. This test used 149 # to run 100 times. Not sure where that lower connection-limit is coming from in 150 # pure Ruby. 151 File.unlink("./xxx.chain") 146 152 EM.start_unix_domain_server "./xxx.chain", TestEchoServer 147 153 $n = 0 148 154 $max = 0 149 100.times {155 50.times { 150 156 EM.connect_unix_domain("./xxx.chain", TestEchoClient) {$n += 1} 151 157 } 152 158 } 153 159 assert_equal(0, $n) 154 assert_equal( 100, $max)160 assert_equal(50, $max) 155 161 end 156 162
