| CODENOTIFIER | HelpYou are not signed inSign in |
Project: rev
Revision: 235
Author: rogerdpack
Date: 28 Jul 2008 13:05:48
Changes:attempt to add a connection timeout to revem--however, it is broken so currently commented out
Files:| ... | ...@@ -5,12 +5,13 @@ | |
| 5 | 5 | #++ |
| 6 | 6 | |
| 7 | 7 | #require 'rubygems' |
| 8 | #require 'rev_here' | |
| 9 | require 'rev' | |
| 10 | require 'eventmachine' | |
| 8 | require File.dirname(__FILE__) + '/rev_here' | |
| 9 | #require 'rev' | |
| 10 | #require 'eventmachine' | |
| 11 | 11 | # now rewrite it |
| 12 | 12 | |
| 13 | 13 | module EventMachine |
| 14 | ||
| 14 | 15 | class << self |
| 15 | 16 | # Start the Reactor loop |
| 16 | 17 | def run |
| ... | ...@@ -34,6 +35,7 @@ | |
| 34 | 35 | end |
| 35 | 36 | |
| 36 | 37 | # ltodo: use Rev's PeriodicTimer to wrap EM's two similar to it |
| 38 | # todo: close all connections on 'stop', I believe | |
| 37 | 39 | |
| 38 | 40 | def add_timer interval, proc = nil, &block |
| 39 | 41 | block ||= proc |
| ... | ...@@ -104,6 +106,13 @@ | |
| 104 | 106 | end |
| 105 | 107 | |
| 106 | 108 | class CallsBackToEM < Rev::TCPSocket |
| 109 | class ConnectTimer < Rev::TimerWatcher | |
| 110 | attr_accessor :parent | |
| 111 | def on_timer | |
| 112 | @parent.connection_has_timed_out | |
| 113 | end | |
| 114 | ||
| 115 | end | |
| 107 | 116 | |
| 108 | 117 | def call_back_to_this parent |
| 109 | 118 | @call_back_to_this = parent |
| ... | ...@@ -111,32 +120,52 @@ | |
| 111 | 120 | end |
| 112 | 121 | |
| 113 | 122 | def on_connect |
| 114 | @call_back_to_this.connection_completed if @call_back_to_this # TODO should server accepted's call this? They don't currently [and can't, since on_connect gets called basically in the initializer--needs some code love for that to happen :) | |
| 123 | # @connection_timer.detach if @connection_timer # won't need that anymore :) -- with server connecteds we don't have it, anyway | |
| 124 | @call_back_to_this.connection_completed if @call_back_to_this # TODO should server accepted's call this? They don't currently [and can't, since on_connect gets called basically in the initializer--needs some code love for that to happen :) | |
| 125 | end | |
| 126 | ||
| 127 | def connection_has_timed_out | |
| 128 | return if closed? | |
| 129 | close unless closed? # wonder if this works when you're within a half-connected phase. I think it does. What about TCP state? | |
| 130 | @call_back_to_this.unbind | |
| 115 | 131 | end |
| 116 | 132 | |
| 117 | #def on_write_complete; end # no op since EM doesn't do this | |
| 118 | 133 | def on_write_complete; close if @should_close_after_writing; end |
| 119 | 134 | |
| 120 | 135 | def should_close_after_writing |
| 121 | 136 | @should_close_after_writing = true; |
| 122 | ||
| 123 | 137 | end |
| 124 | 138 | |
| 125 | 139 | def on_close |
| 126 | 140 | @call_back_to_this.unbind # about the same ltodo check if they ARE the same here |
| 127 | 141 | end |
| 128 | 142 | |
| 129 | def on_failure | |
| 130 | @call_back_to_this.unbind | |
| 143 | def on_resolve_failed | |
| 144 | fail | |
| 131 | 145 | end |
| 132 | 146 | |
| 133 | def on_resolve_failed | |
| 134 | @call_back_to_this.unbind | |
| 147 | def on_connect_failed | |
| 148 | fail | |
| 135 | 149 | end |
| 136 | 150 | |
| 137 | 151 | def on_read(data) |
| 138 | 152 | @call_back_to_this.receive_data data |
| 139 | 153 | end |
| 154 | ||
| 155 | def fail | |
| 156 | #@connection_timer.detch if @connection_timer | |
| 157 | @call_back_to_this.unbind | |
| 158 | end | |
| 159 | ||
| 160 | def self.connect *args | |
| 161 | a = super *args | |
| 162 | # the connect timer currently kills TCPServer classes. I'm not sure why. | |
| 163 | #@connection_timer = ConnectTimer.new(14) # needs to be at least higher than 12 :) | |
| 164 | #@connection_timer.parent = a | |
| 165 | #@connection_timer.attach(Rev::Loop.default) | |
| 166 | a | |
| 167 | end | |
| 168 | ||
| 140 | 169 | end |
| 141 | 170 | |
| 142 | 171 | class Connection |