summaryrefslogtreecommitdiff
path: root/lib/net/protocol.rb
diff options
context:
space:
mode:
authoraamine <aamine@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2001-02-06 11:14:51 +0000
committeraamine <aamine@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2001-02-06 11:14:51 +0000
commitcdc7602379c9d911983db2c044d69ac417869266 (patch)
treef3ac9acbdf4a9e19805dbb4be0b7ee7fc01f9629 /lib/net/protocol.rb
parent765255b737235a65daea6679c4672541bb67ecb4 (diff)
aamine
* lib/net/http.rb: add HTTP#request. * lib/net/http.rb: take HTTP 1.0 server into account (incomplete). * lib/net/protocol.rb: timeout for open/read. * lib/net/protocol.rb: add Protocol#on_connect,on_disconnect. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@1160 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/net/protocol.rb')
-rw-r--r--lib/net/protocol.rb132
1 files changed, 86 insertions, 46 deletions
diff --git a/lib/net/protocol.rb b/lib/net/protocol.rb
index 7f99a64..9e97bee 100644
--- a/lib/net/protocol.rb
+++ b/lib/net/protocol.rb
@@ -15,7 +15,7 @@ You can get it from RAA
== Net::Protocol
-the abstract class for Internet protocol
+the abstract class for some internet protocols
=== Super Class
@@ -59,6 +59,7 @@ Object
=end
require 'socket'
+require 'timeout'
module Net
@@ -116,8 +117,12 @@ module Net
@command = nil
@socket = nil
- @active = false
- @pipe = nil
+ @active = false
+
+ @open_timeout = nil
+ @read_timeout = nil
+
+ @pipe = nil
end
attr_reader :address
@@ -126,10 +131,24 @@ module Net
attr_reader :command
attr_reader :socket
+ attr_accessor :open_timeout
+ attr_accessor :read_timeout
+
+ def active?
+ @active
+ end
+
+ def set_pipe( arg ) # un-documented
+ @pipe = arg
+ end
+
def inspect
"#<#{type} #{address}:#{port} open=#{active?}>"
end
+ #
+ # open session
+ #
def start( *args )
return false if active?
@@ -146,45 +165,54 @@ module Net
end
end
+ private
+
def _start( args )
connect
do_start( *args )
@active = true
end
- private :_start
- def finish
- return false unless active?
+ def connect
+ conn_socket @address, @port
+ conn_command @socket
+ on_connect
+ end
- do_finish unless @command.critical?
- disconnect
- @active = false
- true
+ def conn_socket( addr, port )
+ @socket = type.socket_type.open(
+ addr, port, @open_timeout, @read_timeout, @pipe )
end
- def active?
- @active
+ def conn_command( sock )
+ @command = type.command_type.new( sock )
end
- def set_pipe( arg ) # un-documented
- @pipe = arg
+ def on_connect
end
+ def do_start
+ end
- private
+ #
+ # close session
+ #
+ public
- def do_start
- end
+ def finish
+ return false unless active?
- def do_finish
- @command.quit
+ do_finish if @command and not @command.critical?
+ disconnect
+ @active = false
+ true
end
+ private
- def connect( addr = @address, port = @port )
- @socket = type.socket_type.open( addr, port, @pipe )
- @command = type.command_type.new( @socket )
+ def do_finish
+ @command.quit
end
def disconnect
@@ -192,7 +220,11 @@ module Net
if @socket and not @socket.closed? then
@socket.close
end
- @socket = nil
+ @socket = nil
+ on_disconnect
+ end
+
+ def on_disconnect
end
end
@@ -311,6 +343,7 @@ module Net
def write( str )
@sock.__send__ @mid, str
end
+
alias << write
end
@@ -407,6 +440,7 @@ module Net
@critical = false
end
+
private
def critical
@@ -431,9 +465,12 @@ module Net
class Socket
- def initialize( addr, port, pipe = nil )
+ def initialize( addr, port, otime = nil, rtime = nil, pipe = nil )
@addr = addr
@port = port
+
+ @read_timeout = rtime
+
@pipe = pipe
@prepipe = nil
@@ -442,7 +479,9 @@ module Net
@sending = ''
@buffer = ''
- @socket = TCPsocket.new( addr, port )
+ timeout( otime ) {
+ @socket = TCPsocket.new( addr, port )
+ }
@closed = false
@ipaddr = @socket.addr[3]
end
@@ -494,13 +533,15 @@ module Net
attr_reader :sending
- ###
- ### read
- ###
+ #
+ # read
+ #
+
+ public
CRLF = "\r\n"
- def read( len, dest = '' )
+ def read( len, dest = '', ignerr = false )
@pipe << "reading #{len} bytes...\n" if @pipe; pipeoff
rsize = 0
@@ -509,16 +550,15 @@ module Net
rsize += writeinto( dest, @buffer.size )
fill_rbuf
end
+ writeinto( dest, len - rsize )
rescue EOFError
- len = rsize
+ raise unless igneof
end
- writeinto( dest, len - rsize )
@pipe << "read #{len} bytes\n" if pipeon
dest
end
-
def read_all( dest = '' )
@pipe << "reading all...\n" if @pipe; pipeoff
@@ -536,8 +576,7 @@ module Net
dest
end
-
- def readuntil( target )
+ def readuntil( target, igneof = false )
dest = ''
begin
while true do
@@ -547,11 +586,11 @@ module Net
end
writeinto( dest, idx + target.size )
rescue EOFError
+ raise unless igneof
writeinto( dest, @buffer.size )
end
dest
end
-
def readline
ret = readuntil( "\n" )
@@ -559,7 +598,6 @@ module Net
ret
end
-
def read_pendstr( dest )
@pipe << "reading text...\n" if @pipe; pipeoff
@@ -574,7 +612,6 @@ module Net
dest
end
-
# private use only (can not handle 'break')
def read_pendlist
@pipe << "reading list...\n" if @pipe; pipeoff
@@ -594,10 +631,17 @@ module Net
private
- READ_BLOCK = 1024 * 8
+ READ_SIZE = 1024 * 4
def fill_rbuf
- @buffer << @socket.sysread( READ_BLOCK )
+ unless IO.select [@socket], nil, nil, @read_timeout then
+ on_read_timeout
+ end
+ @buffer << @socket.sysread( READ_SIZE )
+ end
+
+ def on_read_timeout
+ raise TimeoutError, "socket read timeout (#{@read_timeout} sec)"
end
def writeinto( dest, len )
@@ -610,20 +654,18 @@ module Net
end
- ###
- ### write
- ###
+ #
+ # write interfece
+ #
public
-
def write( str )
writing {
do_write str
}
end
-
def writeline( str )
writing {
do_write str
@@ -631,7 +673,6 @@ module Net
}
end
-
def write_bin( src, block )
writing {
if block then
@@ -644,7 +685,6 @@ module Net
}
end
-
def write_pendstr( src, block )
@pipe << "writing text from #{src.type}\n" if @pipe; pipeoff