mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* lib/net/protocol.rb: remove method: InternetMessageIO#address, port, ip_address, read_timeout(=), socket.
* lib/net/protocol.rb: simplify code. * lib/net/protocol.rb: apply latest coding style. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@5907 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
3b16f66bf7
commit
dd53813e38
2 changed files with 83 additions and 120 deletions
|
@ -1,3 +1,12 @@
|
||||||
|
Sun Mar 7 00:55:03 2004 Minero Aoki <aamine@loveruby.net>
|
||||||
|
|
||||||
|
* lib/net/protocol.rb: remove method: InternetMessageIO#address,
|
||||||
|
port, ip_address, read_timeout(=), socket.
|
||||||
|
|
||||||
|
* lib/net/protocol.rb: simplify code.
|
||||||
|
|
||||||
|
* lib/net/protocol.rb: apply latest coding style.
|
||||||
|
|
||||||
Sat Mar 6 15:15:05 2004 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
Sat Mar 6 15:15:05 2004 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||||
|
|
||||||
* ext/strscan/depend: depends on re.h and regex.h.
|
* ext/strscan/depend: depends on re.h and regex.h.
|
||||||
|
|
|
@ -2,8 +2,8 @@
|
||||||
# = net/protocol.rb
|
# = net/protocol.rb
|
||||||
#
|
#
|
||||||
#--
|
#--
|
||||||
# Copyright (c) 1999-2003 Yukihiro Matsumoto
|
# Copyright (c) 1999-2004 Yukihiro Matsumoto
|
||||||
# Copyright (c) 1999-2003 Minero Aoki
|
# Copyright (c) 1999-2004 Minero Aoki
|
||||||
#
|
#
|
||||||
# written and maintained by Minero Aoki <aamine@loveruby.net>
|
# written and maintained by Minero Aoki <aamine@loveruby.net>
|
||||||
#
|
#
|
||||||
|
@ -25,7 +25,7 @@ module Net # :nodoc:
|
||||||
|
|
||||||
class Protocol #:nodoc: internal use only
|
class Protocol #:nodoc: internal use only
|
||||||
private
|
private
|
||||||
def Protocol.protocol_param( name, val )
|
def Protocol.protocol_param(name, val)
|
||||||
module_eval(<<-End, __FILE__, __LINE__ + 1)
|
module_eval(<<-End, __FILE__, __LINE__ + 1)
|
||||||
def #{name}
|
def #{name}
|
||||||
#{val}
|
#{val}
|
||||||
|
@ -34,6 +34,7 @@ module Net # :nodoc:
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
class ProtocolError < StandardError; end
|
class ProtocolError < StandardError; end
|
||||||
class ProtoSyntaxError < ProtocolError; end
|
class ProtoSyntaxError < ProtocolError; end
|
||||||
class ProtoFatalError < ProtocolError; end
|
class ProtoFatalError < ProtocolError; end
|
||||||
|
@ -46,14 +47,11 @@ module Net # :nodoc:
|
||||||
|
|
||||||
|
|
||||||
class InternetMessageIO #:nodoc: internal use only
|
class InternetMessageIO #:nodoc: internal use only
|
||||||
|
|
||||||
class << self
|
class << self
|
||||||
alias open new
|
alias open new
|
||||||
end
|
end
|
||||||
|
|
||||||
def initialize( addr, port,
|
def initialize(addr, port, open_timeout = nil, read_timeout = nil, debug_output = nil)
|
||||||
open_timeout = nil, read_timeout = nil,
|
|
||||||
debug_output = nil )
|
|
||||||
@address = addr
|
@address = addr
|
||||||
@port = port
|
@port = port
|
||||||
@read_timeout = read_timeout
|
@read_timeout = read_timeout
|
||||||
|
@ -65,19 +63,7 @@ module Net # :nodoc:
|
||||||
LOG 'opened'
|
LOG 'opened'
|
||||||
end
|
end
|
||||||
|
|
||||||
attr_reader :address
|
def connect(open_timeout)
|
||||||
attr_reader :port
|
|
||||||
|
|
||||||
def ip_address
|
|
||||||
return '' unless @socket
|
|
||||||
@socket.addr[3]
|
|
||||||
end
|
|
||||||
|
|
||||||
attr_accessor :read_timeout
|
|
||||||
|
|
||||||
attr_reader :socket
|
|
||||||
|
|
||||||
def connect( open_timeout )
|
|
||||||
LOG "opening connection to #{@address}..."
|
LOG "opening connection to #{@address}..."
|
||||||
timeout(open_timeout) {
|
timeout(open_timeout) {
|
||||||
@socket = TCPsocket.new(@address, @port)
|
@socket = TCPsocket.new(@address, @port)
|
||||||
|
@ -97,7 +83,7 @@ module Net # :nodoc:
|
||||||
@rbuf = ''
|
@rbuf = ''
|
||||||
end
|
end
|
||||||
|
|
||||||
def reopen( open_timeout = nil )
|
def reopen(open_timeout = nil)
|
||||||
LOG 'reopening...'
|
LOG 'reopening...'
|
||||||
close
|
close
|
||||||
connect open_timeout
|
connect open_timeout
|
||||||
|
@ -118,53 +104,50 @@ module Net # :nodoc:
|
||||||
|
|
||||||
public
|
public
|
||||||
|
|
||||||
def read( len, dest = '', ignore_eof = false )
|
def read(len, dest = '', ignore_eof = false)
|
||||||
LOG "reading #{len} bytes..."
|
LOG "reading #{len} bytes..."
|
||||||
# LOG_off() # experimental: [ruby-list:38800]
|
|
||||||
read_bytes = 0
|
read_bytes = 0
|
||||||
begin
|
begin
|
||||||
while read_bytes + @rbuf.size < len
|
while read_bytes + @rbuf.size < len
|
||||||
read_bytes += rbuf_moveto(dest, @rbuf.size)
|
dest << (s = rbuf_consume(@rbuf.size))
|
||||||
|
read_bytes += s.size
|
||||||
rbuf_fill
|
rbuf_fill
|
||||||
end
|
end
|
||||||
rbuf_moveto dest, len - read_bytes
|
dest << (s = rbuf_consume(len - read_bytes))
|
||||||
|
read_bytes += s.size
|
||||||
rescue EOFError
|
rescue EOFError
|
||||||
raise unless ignore_eof
|
raise unless ignore_eof
|
||||||
end
|
end
|
||||||
# LOG_on()
|
|
||||||
LOG "read #{read_bytes} bytes"
|
LOG "read #{read_bytes} bytes"
|
||||||
dest
|
dest
|
||||||
end
|
end
|
||||||
|
|
||||||
def read_all( dest = '' )
|
def read_all(dest = '')
|
||||||
LOG 'reading all...'
|
LOG 'reading all...'
|
||||||
# LOG_off() # experimental: [ruby-list:38800]
|
|
||||||
read_bytes = 0
|
read_bytes = 0
|
||||||
begin
|
begin
|
||||||
while true
|
while true
|
||||||
read_bytes += rbuf_moveto(dest, @rbuf.size)
|
dest << (s = rbuf_consume(@rbuf.size))
|
||||||
|
read_bytes += s.size
|
||||||
rbuf_fill
|
rbuf_fill
|
||||||
end
|
end
|
||||||
rescue EOFError
|
rescue EOFError
|
||||||
;
|
;
|
||||||
end
|
end
|
||||||
# LOG_on()
|
|
||||||
LOG "read #{read_bytes} bytes"
|
LOG "read #{read_bytes} bytes"
|
||||||
dest
|
dest
|
||||||
end
|
end
|
||||||
|
|
||||||
def readuntil( terminator, ignore_eof = false )
|
def readuntil(terminator, ignore_eof = false)
|
||||||
dest = ''
|
|
||||||
begin
|
begin
|
||||||
until idx = @rbuf.index(terminator)
|
until idx = @rbuf.index(terminator)
|
||||||
rbuf_fill
|
rbuf_fill
|
||||||
end
|
end
|
||||||
rbuf_moveto dest, idx + terminator.size
|
return rbuf_consume(idx + terminator.size)
|
||||||
rescue EOFError
|
rescue EOFError
|
||||||
raise unless ignore_eof
|
raise unless ignore_eof
|
||||||
rbuf_moveto dest, @rbuf.size
|
return rbuf_consume(@rbuf.size)
|
||||||
end
|
end
|
||||||
dest
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def readline
|
def readline
|
||||||
|
@ -199,10 +182,10 @@ module Net # :nodoc:
|
||||||
@rbuf << @socket.sysread(1024)
|
@rbuf << @socket.sysread(1024)
|
||||||
end
|
end
|
||||||
|
|
||||||
def rbuf_moveto( dest, len )
|
def rbuf_consume(len)
|
||||||
dest << (s = @rbuf.slice!(0, len))
|
s = @rbuf.slice!(0, len)
|
||||||
@debug_output << %Q[-> #{s.dump}\n] if @debug_output
|
@debug_output << %Q[-> #{s.dump}\n] if @debug_output
|
||||||
len
|
s
|
||||||
end
|
end
|
||||||
|
|
||||||
###
|
###
|
||||||
|
@ -211,38 +194,42 @@ module Net # :nodoc:
|
||||||
|
|
||||||
public
|
public
|
||||||
|
|
||||||
def write( str )
|
def write(str)
|
||||||
writing {
|
writing {
|
||||||
write0 str
|
write0 str
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
def writeline( str )
|
def writeline(str)
|
||||||
writing {
|
writing {
|
||||||
write0 str + "\r\n"
|
write0 str + "\r\n"
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
def write_message( src )
|
def write_message(src)
|
||||||
LOG "writing message from #{src.class}"
|
LOG "writing message from #{src.class}"
|
||||||
LOG_off()
|
LOG_off()
|
||||||
len = using_each_crlf_line {
|
len = writing {
|
||||||
write_message_0 src
|
using_each_crlf_line {
|
||||||
|
write_message_0 src
|
||||||
|
}
|
||||||
}
|
}
|
||||||
LOG_on()
|
LOG_on()
|
||||||
LOG "wrote #{len} bytes"
|
LOG "wrote #{len} bytes"
|
||||||
len
|
len
|
||||||
end
|
end
|
||||||
|
|
||||||
def write_message_by_block( &block )
|
def write_message_by_block(&block)
|
||||||
LOG 'writing message from block'
|
LOG 'writing message from block'
|
||||||
LOG_off()
|
LOG_off()
|
||||||
len = using_each_crlf_line {
|
len = writing {
|
||||||
begin
|
using_each_crlf_line {
|
||||||
block.call(WriteAdapter.new(self, :write_message_0))
|
begin
|
||||||
rescue LocalJumpError
|
block.call(WriteAdapter.new(self, :write_message_0))
|
||||||
# allow `break' from writer block
|
rescue LocalJumpError
|
||||||
end
|
# allow `break' from writer block
|
||||||
|
end
|
||||||
|
}
|
||||||
}
|
}
|
||||||
LOG_on()
|
LOG_on()
|
||||||
LOG "wrote #{len} bytes"
|
LOG "wrote #{len} bytes"
|
||||||
|
@ -262,7 +249,7 @@ module Net # :nodoc:
|
||||||
bytes
|
bytes
|
||||||
end
|
end
|
||||||
|
|
||||||
def write0( str )
|
def write0(str)
|
||||||
@debug_output << str.dump if @debug_output
|
@debug_output << str.dump if @debug_output
|
||||||
len = @socket.write(str)
|
len = @socket.write(str)
|
||||||
@written_bytes += len
|
@written_bytes += len
|
||||||
|
@ -273,7 +260,7 @@ module Net # :nodoc:
|
||||||
# Reads string from src calling :each, and write to @socket.
|
# Reads string from src calling :each, and write to @socket.
|
||||||
# Escapes '.' on the each line head.
|
# Escapes '.' on the each line head.
|
||||||
#
|
#
|
||||||
def write_message_0( src )
|
def write_message_0(src)
|
||||||
prev = @written_bytes
|
prev = @written_bytes
|
||||||
each_crlf_line(src) do |line|
|
each_crlf_line(src) do |line|
|
||||||
if line[0] == ?.
|
if line[0] == ?.
|
||||||
|
@ -288,69 +275,44 @@ module Net # :nodoc:
|
||||||
# setup @wbuf for each_crlf_line.
|
# setup @wbuf for each_crlf_line.
|
||||||
#
|
#
|
||||||
def using_each_crlf_line
|
def using_each_crlf_line
|
||||||
writing {
|
@wbuf = ''
|
||||||
@wbuf = ''
|
yield
|
||||||
yield
|
if not @wbuf.empty? # unterminated last line
|
||||||
if not @wbuf.empty? # unterminated last line
|
write0 @wbuf.chomp + "\r\n"
|
||||||
if @wbuf[-1] == ?\r
|
@wbuf.string = ''
|
||||||
@wbuf.chop!
|
elsif @written_bytes == 0 # empty src
|
||||||
end
|
write0 "\r\n"
|
||||||
@wbuf.concat "\r\n"
|
end
|
||||||
write0 @wbuf
|
write0 ".\r\n"
|
||||||
elsif @written_bytes == 0 # empty src
|
@wbuf = nil
|
||||||
write0 "\r\n"
|
|
||||||
end
|
|
||||||
write0 ".\r\n"
|
|
||||||
@wbuf = nil
|
|
||||||
}
|
|
||||||
end
|
end
|
||||||
|
|
||||||
#
|
def each_crlf_line(src)
|
||||||
# extract a CR-LF-terminating-line from @wbuf and yield it.
|
buffer_filling(@wbuf, src) do
|
||||||
#
|
while line = @wbuf.slice!(/\A.*(?:\n|\r\n|\r(?!\z))/n)
|
||||||
def each_crlf_line( src )
|
yield line.chomp("\n") + "\r\n"
|
||||||
adding(src) do
|
|
||||||
beg = 0
|
|
||||||
buf = @wbuf
|
|
||||||
while buf.index(/\n|\r\n|\r/, beg)
|
|
||||||
m = Regexp.last_match
|
|
||||||
if (m.begin(0) == buf.length - 1) and buf[-1] == ?\r
|
|
||||||
# "...\r" : can follow "\n..."
|
|
||||||
break
|
|
||||||
end
|
|
||||||
str = buf[beg ... m.begin(0)]
|
|
||||||
str.concat "\r\n"
|
|
||||||
yield str
|
|
||||||
beg = m.end(0)
|
|
||||||
end
|
end
|
||||||
@wbuf = buf[beg ... buf.length]
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
#
|
def buffer_filling(buf, src)
|
||||||
# Reads strings from SRC and add to @wbuf, then yield.
|
|
||||||
#
|
|
||||||
def adding( src )
|
|
||||||
case src
|
case src
|
||||||
when String # for speeding up.
|
when String # for speeding up.
|
||||||
0.step(src.size - 1, 2048) do |i|
|
0.step(src.size - 1, 1024) do |i|
|
||||||
@wbuf << src[i,2048]
|
buf << src[i, 1024]
|
||||||
yield
|
yield
|
||||||
end
|
end
|
||||||
|
|
||||||
when File # for speeding up.
|
when File # for speeding up.
|
||||||
while s = src.read(2048)
|
while s = src.read(1024)
|
||||||
s[0,0] = @wbuf
|
buf << s
|
||||||
@wbuf = s
|
|
||||||
yield
|
yield
|
||||||
end
|
end
|
||||||
|
|
||||||
else # generic reader
|
else # generic reader
|
||||||
src.each do |s|
|
src.each do |s|
|
||||||
@wbuf << s
|
buf << s
|
||||||
yield if @wbuf.size > 2048
|
yield if buf.size > 1024
|
||||||
end
|
end
|
||||||
yield unless @wbuf.empty?
|
yield unless buf.empty?
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -369,12 +331,10 @@ module Net # :nodoc:
|
||||||
@debug_output = @save_debug_out
|
@debug_output = @save_debug_out
|
||||||
end
|
end
|
||||||
|
|
||||||
def LOG( msg )
|
def LOG(msg)
|
||||||
return unless @debug_output
|
return unless @debug_output
|
||||||
@debug_output << msg
|
@debug_output << msg + "\n"
|
||||||
@debug_output << "\n"
|
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
@ -382,41 +342,38 @@ module Net # :nodoc:
|
||||||
# The writer adapter class
|
# The writer adapter class
|
||||||
#
|
#
|
||||||
class WriteAdapter
|
class WriteAdapter
|
||||||
|
def initialize(socket, method)
|
||||||
def initialize( sock, mid )
|
@socket = socket
|
||||||
@socket = sock
|
@method_id = method
|
||||||
@method_id = mid
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def inspect
|
def inspect
|
||||||
"#<#{self.class} socket=#{@socket.inspect}>"
|
"#<#{self.class} socket=#{@socket.inspect}>"
|
||||||
end
|
end
|
||||||
|
|
||||||
def write( str )
|
def write(str)
|
||||||
@socket.__send__(@method_id, str)
|
@socket.__send__(@method_id, str)
|
||||||
end
|
end
|
||||||
|
|
||||||
alias print write
|
alias print write
|
||||||
|
|
||||||
def <<( str )
|
def <<(str)
|
||||||
write str
|
write str
|
||||||
self
|
self
|
||||||
end
|
end
|
||||||
|
|
||||||
def puts( str = '' )
|
def puts(str = '')
|
||||||
write str.sub(/\n?\z/, "\n")
|
write str.chomp("\n") + "\n"
|
||||||
end
|
end
|
||||||
|
|
||||||
def printf( *args )
|
def printf(*args)
|
||||||
write sprintf(*args)
|
write sprintf(*args)
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
class ReadAdapter #:nodoc: internal use only
|
class ReadAdapter #:nodoc: internal use only
|
||||||
|
def initialize(block)
|
||||||
def initialize( block )
|
|
||||||
@block = block
|
@block = block
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -424,21 +381,18 @@ module Net # :nodoc:
|
||||||
"#<#{self.class}>"
|
"#<#{self.class}>"
|
||||||
end
|
end
|
||||||
|
|
||||||
def <<( str )
|
def <<(str)
|
||||||
call_block(str, &@block) if @block
|
call_block(str, &@block) if @block
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
#
|
|
||||||
# This method is needed because @block must be called by yield,
|
# This method is needed because @block must be called by yield,
|
||||||
# not Proc#call. You can see difference when using `break' in
|
# not Proc#call. You can see difference when using `break' in
|
||||||
# the block.
|
# the block.
|
||||||
#
|
def call_block(str)
|
||||||
def call_block( str )
|
|
||||||
yield str
|
yield str
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue