Changeset 11406
- Timestamp:
- 05/11/08 22:59:27 (5 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
lang/perl/tiarra/trunk/main/Tiarra/Socket/Connect.pm
r11203 r11406 16 16 utils->define_attr_accessor(0, qw(domain host addr port callback), 17 17 qw(bind_addr prefer timeout), 18 qw(retry_int retry_count try_count)); 18 qw(retry_int retry_count try_count), 19 qw(hooks)); 19 20 utils->define_attr_enum_accessor('domain', 'eq', 20 21 qw(tcp unix)); … … 29 30 # my ($genre, $connector, $msg_or_sock, $errno) = @_; 30 31 # if ($genre eq 'warn') { 31 # # $msg_or_sock: msg32 # # maybe don't have $errno32 # # warnings 33 # # $msg_or_sock: msg 33 34 # warn $msg_or_sock; 34 35 # } elsif ($genre eq 'error') { 35 # # $msg_or_sock: msg 36 # # maybe has $errno 36 # # error: error detected and connect attempt aborted. 37 # # if you need, re-initialize connect. 38 # # $msg_or_sock: msg 37 39 # die $msg_or_sock; 38 40 # } elsif ($genre eq 'sock') { 39 # # $msg_or_sock: sock40 # # maybe don't have $errno41 # # connection successful. return socket. 42 # # $msg_or_sock: sock 41 43 # attach($connector->current_addr, $connector->current_port, 42 44 # $msg_or_sock); 43 45 # # optional genre 46 # } elsif ($genre eq 'skip') { 47 # # skipped by hook closure 48 # # $msg_or_sock: msg 49 # warn $msg_or_sock; 44 50 # } elsif ($genre eq 'interrupt') { 45 # # $msg_or_sock: undef46 # # maybe don't have $errno51 # # connection interrupted by user 52 # # $msg_or_sock: undef 47 53 # die 'interrupted'; 48 54 # } elsif ($genre eq 'timeout') { 49 # # $msg_or_sock: undef50 # # maybe don't have $errno55 # # connection interrupted by timer 56 # # $msg_or_sock: undef 51 57 # die 'timeout'; 52 58 # } … … 61 67 # array ref, default ipv6, ipv4], 62 68 # domain => 'tcp', # default 69 # hooks => { 70 # before_connect => { 71 # my ($stage, $connecting) = @_; 72 # # hook before connection attempt 73 # # $stage: 'before_connect' 74 # # $connecting: { # be able to modify this hash. 75 # # addr => $addr, port => $port, 76 # # type => 'ipv4' or 'ipv6', 77 # # bind_addr => undef or set connection-local bind_addr, 78 # # } 79 # if ( /* not_want_to_connect */ ) { 80 # die 'skip this connection'; 81 # } 82 # } 83 # } 63 84 # ); 64 85 # $connector->interrupt; … … 80 101 map { 81 102 $this->$_($opts{$_}); 82 } qw(host addr port callback bind_addr timeout retry_int retry_count );103 } qw(host addr port callback bind_addr timeout retry_int retry_count hooks); 83 104 84 105 if (!defined $this->callback) { … … 200 221 201 222 if (!Tiarra::OptionalModules->ipv6) { 202 $this->_ error(223 $this->_warn( 203 224 qq{Host $this->{host} seems to be an IPv6 address, }. 204 225 qq{but IPv6 support is not enabled. }. 205 226 qq{Use IPv4 or install Socket6 or IO::Socket::INET6 if possible.\n}); 227 $this->_connect_try_next; 228 return; 206 229 } 207 230 … … 212 235 my ($this, $package, $addr, %additional) = @_; 213 236 237 eval { 238 $this->_call_hooks('before_connect', $this->{connecting}); 239 }; if ($@) { 240 $this->_call_skip($@); 241 $this->_connect_try_next; 242 return; 243 } 244 if (defined $this->{hooks}->{before_connect}) { 245 #eval { $this->{hooks}->{before_connect}->('before_connect', ) } 246 } 214 247 if (!eval("require $package")) { 215 $this->_connect_error("Couldn\'t require socket package: $package"); 248 $this->_connect_warn("Couldn\'t require socket package: $package"); 249 $this->_connect_try_next; 216 250 return; 217 251 } 252 my $bind_addr = $this->current_bind_addr; 218 253 my $sock = $package->new( 219 254 %additional, 220 (defined $ this->{bind_addr}?221 (LocalAddr => $ this->{bind_addr}) : ()),255 (defined $bind_addr ? 256 (LocalAddr => $bind_addr) : ()), 222 257 Timeout => undef, 223 258 Proto => 'tcp'); … … 273 308 qq{but Unix Domain Socket support is not enabled. }. 274 309 qq{Use other protocol if possible.\n}); 310 return; 275 311 } 276 312 … … 335 371 } 336 372 373 sub current_bind_addr { 374 my $this = shift; 375 376 utils->get_first_defined( 377 $this->{connecting}->{bind_addr}, 378 $this->bind_addr); 379 } 380 337 381 sub current_type { 338 382 my $this = shift; … … 360 404 361 405 $this->callback->('sock', $this, $this->sock); 406 } 407 408 sub _call_skip { 409 # this address/port skipped; but continue trying 410 my ($this, $str, $errno) = @_; 411 412 $this->callback->('skip', $this, 413 "skip connection attempt to ".$this->destination.$str, 414 $errno); 415 } 416 417 sub _call_hooks { 418 # method may died by callback. 419 # please cover with eval, if you need. 420 my $this = shift; 421 my $genre = shift; 422 423 if (defined $this->{hooks}->{$genre}) { 424 $this->{hooks}->{$genre}->($genre, @_); 425 } 362 426 } 363 427
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)