indexing
	description: "A network stream socket."
	legal: "See notice at end of class."
	status: "See notice at end of class."
	date: "$Date: 2006-07-19 15:54:37 -0700 (Wed, 19 Jul 2006) $"
	revision: "$Revision: 61492 $"

class interface
	NETWORK_STREAM_SOCKET


create {NETWORK_STREAM_SOCKET}
	create_from_descriptor (fd: INTEGER_32)
			-- Create socket from descriptor `fd'.
			-- (from NETWORK_SOCKET)
		require -- from  SOCKET
			True
		ensure -- from SOCKET
			family_valid: family = address.family
			opened_all: is_open_write and is_open_read
		ensure then -- from NETWORK_SOCKET
			timeout_set_to_default: timeout = default_timeout


create 
	make
			-- Create a network stream socket.
		ensure
			timeout_set_to_default: timeout = default_timeout

	make_client_by_port (a_peer_port: INTEGER_32; a_peer_host: STRING_8)
			-- Create a client connection to `a_peer_host' on
			-- `a_peer_port'.
		require
			valid_peer_host: a_peer_host /= Void and then not a_peer_host.is_empty
			valid_port: a_peer_port >= 0

	make_server_by_port (a_port: INTEGER_32)
			-- Create server socket on `a_port'.
		require
			valid_port: a_port >= 0

feature -- Initialization

	create_from_descriptor (fd: INTEGER_32)
			-- Create socket from descriptor `fd'.
			-- (from NETWORK_SOCKET)
		require -- from  SOCKET
			True
		ensure -- from SOCKET
			family_valid: family = address.family
			opened_all: is_open_write and is_open_read
		ensure then -- from NETWORK_SOCKET
			timeout_set_to_default: timeout = default_timeout

	make
			-- Create a network stream socket.
		ensure
			timeout_set_to_default: timeout = default_timeout

	make_client_by_port (a_peer_port: INTEGER_32; a_peer_host: STRING_8)
			-- Create a client connection to `a_peer_host' on
			-- `a_peer_port'.
		require
			valid_peer_host: a_peer_host /= Void and then not a_peer_host.is_empty
			valid_port: a_peer_port >= 0

	make_server_by_port (a_port: INTEGER_32)
			-- Create server socket on `a_port'.
		require
			valid_port: a_port >= 0
	
feature -- Access

	generating_type: STRING_8
			-- Name of current object's generating type
			-- (type of which it is a direct instance)
			-- (from ANY)
		ensure -- from ANY
			generating_type_not_void: Result /= Void
			generating_type_not_empty: not Result.is_empty

	generator: STRING_8
			-- Name of current object's generating class
			-- (base class of the type of which it is a direct instance)
			-- (from ANY)
		ensure -- from ANY
			generator_not_void: Result /= Void
			generator_not_empty: not Result.is_empty

	retrieved: ANY
			-- Retrieved object structure
			-- To access resulting object under correct type,
			-- use assignment attempt.
			-- Will raise an exception (code Retrieve_exception)
			-- if content is not a stored Eiffel structure.
			-- (from SOCKET)
		require -- from IO_MEDIUM
			is_readable: readable
			support_storable: support_storable
		require else -- from SOCKET
			socket_exists: exists
			opened_for_read: is_open_read
			support_storable: support_storable
		ensure -- from IO_MEDIUM
			result_exists: Result /= Void
	
feature -- Comparison

	frozen deep_equal (some: ?ANY; other: like arg #1): BOOLEAN
			-- Are `some' and `other' either both void
			-- or attached to isomorphic object structures?
			-- (from ANY)
		ensure -- from ANY
			shallow_implies_deep: standard_equal (some, other) implies Result
			both_or_none_void: (some = Void) implies (Result = (other = Void))
			same_type: (Result and (some /= Void)) implies (other /= Void and then some.same_type (other))
			symmetric: Result implies deep_equal (other, some)

	frozen equal (some: ?ANY; other: like arg #1): BOOLEAN
			-- Are `some' and `other' either both void or attached
			-- to objects considered equal?
			-- (from ANY)
		ensure -- from ANY
			definition: Result = (some = Void and other = Void) or else ((some /= Void and other /= Void) and then some.is_equal (other))

	frozen is_deep_equal (other: NETWORK_STREAM_SOCKET): BOOLEAN
			-- Are `Current' and `other' attached to isomorphic object structures?
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
		ensure -- from ANY
			shallow_implies_deep: standard_is_equal (other) implies Result
			same_type: Result implies same_type (other)
			symmetric: Result implies other.is_deep_equal (Current)

	is_equal (other: NETWORK_STREAM_SOCKET): BOOLEAN
			-- Is `other' attached to an object considered
			-- equal to current object?
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
		ensure -- from ANY
			symmetric: Result implies other.is_equal (Current)
			consistent: standard_is_equal (other) implies Result

	frozen standard_equal (some: ?ANY; other: like arg #1): BOOLEAN
			-- Are `some' and `other' either both void or attached to
			-- field-by-field identical objects of the same type?
			-- Always uses default object comparison criterion.
			-- (from ANY)
		ensure -- from ANY
			definition: Result = (some = Void and other = Void) or else ((some /= Void and other /= Void) and then some.standard_is_equal (other))

	frozen standard_is_equal (other: NETWORK_STREAM_SOCKET): BOOLEAN
			-- Is `other' attached to an object of the same type
			-- as current object, and field-by-field identical to it?
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
		ensure -- from ANY
			same_type: Result implies same_type (other)
			symmetric: Result implies other.standard_is_equal (Current)
	
feature -- Status report

	address: NETWORK_SOCKET_ADDRESS
			-- Local address of socket
			-- (from NETWORK_SOCKET)

	address_in_use: BOOLEAN
			-- Address is in use by another socket.
			-- (from SOCKET_RESOURCES)

	address_not_readable: BOOLEAN
			-- Unreadable address
			-- (from SOCKET_RESOURCES)

	already_bound: BOOLEAN
			-- Socket has already been bound.
			-- (from SOCKET_RESOURCES)

	bad_socket_handle: BOOLEAN
			-- Socket descriptor is bad.
			-- (from SOCKET_RESOURCES)

	bytes_read: INTEGER_32
			-- Last number of bytes read by read_to_managed_pointer.
			-- (from IO_MEDIUM)

	conforms_to (other: ANY): BOOLEAN
			-- Does type of current object conform to type
			-- of `other' (as per Eiffel: The Language, chapter 13)?
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void

	connect_in_progress: BOOLEAN
			-- Call to connect returned on a non-blocking socket.
			-- (from SOCKET_RESOURCES)

	connection_refused: BOOLEAN
			-- Connection is refused (possibly due to security).
			-- (from SOCKET_RESOURCES)

	dtable_full: BOOLEAN
			-- Descriptor table is full
			-- (from SOCKET_RESOURCES)

	error: STRING_8
			-- Output a related error message.
			-- (from SOCKET_RESOURCES)

	error_number: INTEGER_32
			-- Returned error number
			-- (from SOCKET_RESOURCES)

	expired_socket: BOOLEAN
			-- Socket connection has expired.
			-- (from SOCKET_RESOURCES)

	has_delay: BOOLEAN
			-- Is option TCPNO_DELAY off.
		require
			socket_exists: exists

	has_exception_state: BOOLEAN
			-- Is socket in exception state within timeout seconds?
			-- (from NETWORK_SOCKET)

	invalid_address: BOOLEAN
			-- Address is not valid.
			-- (from SOCKET_RESOURCES)

	invalid_socket_handle: BOOLEAN
			-- Socket descriptor is not valid.
			-- (from SOCKET_RESOURCES)

	is_linger_on: BOOLEAN
			-- Is lingering switched on?
		require
			socket_exists: exists

	is_out_of_band_inline: BOOLEAN
			-- Are out-of-band packets sent inline?
		require
			socket_exists: exists

	is_plain_text: BOOLEAN
			-- Is file reserved for text (character sequences)?
			-- (from IO_MEDIUM)

	is_valid_peer_address (addr: like address): BOOLEAN
			-- Is `addr' a valid peer address?
			-- (from NETWORK_SOCKET)
		require -- from SOCKET
			address_exists: addr /= Void

	last_character: CHARACTER_8
			-- Last character read by read_character
			-- (from IO_MEDIUM)

	last_double: REAL_64
			-- Last double read by read_double
			-- (from IO_MEDIUM)

	last_integer: INTEGER_32
			-- Last integer read by read_integer
			-- (from IO_MEDIUM)

	last_integer_16: INTEGER_16
			-- Last 16-bit integer read by read_integer_16
			-- (from IO_MEDIUM)

	last_integer_32: INTEGER_32
			-- Synonymy of last_integer
			-- (from IO_MEDIUM)

	last_integer_64: INTEGER_64
			-- Last 64-bit integer read by read_integer_64
			-- (from IO_MEDIUM)

	last_integer_8: INTEGER_8
			-- Last 8-bit integer read by read_integer_8
			-- (from IO_MEDIUM)

	last_natural: NATURAL_32
			-- Last 32-bit natural read by read_natural
			-- (from IO_MEDIUM)

	last_natural_16: NATURAL_16
			-- Last 16-bit natural read by read_natural_16
			-- (from IO_MEDIUM)

	last_natural_32: NATURAL_32
			-- Synonymy of last_natural
			-- (from IO_MEDIUM)

	last_natural_64: NATURAL_64
			-- Last 64-bit natural read by read_natural_64
			-- (from IO_MEDIUM)

	last_natural_8: NATURAL_8
			-- Last 8-bit natural read by read_natural_8
			-- (from IO_MEDIUM)

	last_real: REAL_32
			-- Last real read by read_real
			-- (from IO_MEDIUM)

	last_string: ?STRING_8
			-- Last string read
			-- (from IO_MEDIUM)

	linger_time: INTEGER_32
			-- Linger time
		require
			socket_exists: exists

	maximum_seg_size: INTEGER_32
			-- Maximum segment size
		require
			socket_exists: exists

	network: BOOLEAN
			-- Socket failed due to network problems.
			-- (from SOCKET_RESOURCES)

	no_buffers: BOOLEAN
			-- No more buffers available
			-- (from SOCKET_RESOURCES)

	no_permission: BOOLEAN
			-- No permission is given to user for this socket.
			-- (from SOCKET_RESOURCES)

	not_connected: BOOLEAN
			-- Socket is not connect.
			-- (from SOCKET_RESOURCES)

	port: INTEGER_32
			-- Port socket is bound to.
			-- (from NETWORK_SOCKET)
		require -- from NETWORK_SOCKET
			valid_socket: exists

	protected_address: BOOLEAN
			-- No access to this address is allowed.
			-- (from SOCKET_RESOURCES)

	protocol_not_supported: BOOLEAN
			-- Protocol is not supported on this platform.
			-- (from SOCKET_RESOURCES)

	ready_for_reading: BOOLEAN
			-- Is data available for reading from the socket within 
			-- timeout seconds?
			-- (from NETWORK_SOCKET)

	ready_for_writing: BOOLEAN
			-- Can data be written to the socket within timeout seconds?
			-- (from NETWORK_SOCKET)

	reuse_address: BOOLEAN
			-- Is reuse_address option set?
			-- (from NETWORK_SOCKET)
		require -- from NETWORK_SOCKET
			socket_exists: exists

	same_type (other: ANY): BOOLEAN
			-- Is type of current object identical to type of `other'?
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
		ensure -- from ANY
			definition: Result = (conforms_to (other) and other.conforms_to (Current))

	socket_family_not_supported: BOOLEAN
			-- Requested family is not supported.
			-- (from SOCKET_RESOURCES)

	socket_in_use: BOOLEAN
			-- Socket is already in use.
			-- (from SOCKET_RESOURCES)

	socket_ok: BOOLEAN
			-- No error
			-- (from SOCKET_RESOURCES)

	socket_would_block: BOOLEAN
			-- Call to read, write, etc would have blocked.
			-- (from SOCKET_RESOURCES)

	support_storable: BOOLEAN is True
			-- Can medium be used to store an Eiffel structure?
			-- (from STREAM_SOCKET)

	timeout: INTEGER_32
			-- Duration of timeout in seconds
			-- (from NETWORK_SOCKET)

	zero_option: BOOLEAN
			-- No options provided
			-- (from SOCKET_RESOURCES)
	
feature -- Status setting

	do_not_reuse_address
			-- Turn reuse_address option off.
			-- (from NETWORK_SOCKET)
		require -- from NETWORK_SOCKET
			socket_exists: exists

	set_delay
			-- Switch option TCPNO_DELAY off.
		require
			socket_exists: exists

	set_linger_off
			-- Switch lingering off.
		require
			socket_exists: exists

	set_linger_on (time: INTEGER_32)
			-- Switch lingering on and set linger time to `time'.
		require
			socket_exists: exists

	set_nodelay
			-- Switch option TCPNO_DELAY on.
		require
			socket_exists: exists

	set_out_of_band_inline
			-- Switch "out of band packets inline" on.
		require
			socket_exists: exists

	set_out_of_band_not_inline
			-- Switch "out of band packets inline" off.
		require
			socket_exists: exists

	set_reuse_address
			-- Turn reuse_address option on.
			-- (from NETWORK_SOCKET)
		require -- from NETWORK_SOCKET
			socket_exists: exists

	set_timeout (n: INTEGER_32)
			-- Set timeout to `n' seconds.
			-- (from NETWORK_SOCKET)
		require -- from NETWORK_SOCKET
			non_negative: n >= 0
		ensure -- from NETWORK_SOCKET
			timeout_set: timeout = n or timeout = default_timeout
	
feature -- Element change

	basic_store (object: ANY)
			-- Produce an external representation of the
			-- entire object structure reachable from `object'.
			-- Retrievable within current system only.
			-- (from SOCKET)
		require -- from IO_MEDIUM
			object_not_void: object /= Void
			extendible: extendible
			support_storable: support_storable
		require else -- from SOCKET
			socket_exists: exists
			opened_for_write: is_open_write
			support_storable: support_storable

	general_store (object: ANY)
			-- Produce an external representation of the
			-- entire object structure reachable from `object'.
			-- Retrievable from other systems for same platform
			-- (machine architecture).
			-- (from SOCKET)
		require -- from IO_MEDIUM
			object_not_void: object /= Void
			extendible: extendible
			support_storable: support_storable
		require else -- from SOCKET
			socket_exists: exists
			opened_for_write: is_open_write
			support_storable: support_storable

	independent_store (object: ANY)
			-- Produce an external representation of the
			-- entire object structure reachable from `object'.
			-- Retrievable from other systems for the same or other
			-- platform (machine architecture).
			-- (from SOCKET)
		require -- from IO_MEDIUM
			object_not_void: object /= Void
			extendible: extendible
			support_storable: support_storable
		require else -- from SOCKET
			socket_exists: exists
			opened_for_write: is_open_write
			support_storable: support_storable
	
feature -- Removal

	dispose
			-- Ensure this medium is closed when garbage collected.
			-- (from IO_MEDIUM)
	
feature -- Duplication

	copy (other: NETWORK_STREAM_SOCKET)
			-- Update current object using fields of object attached
			-- to `other', so as to yield equal objects.
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
			type_identity: same_type (other)
		ensure -- from ANY
			is_equal: is_equal (other)

	frozen deep_copy (other: NETWORK_STREAM_SOCKET)
			-- Effect equivalent to that of:
			--		copy (`other' . deep_twin)
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
		ensure -- from ANY
			deep_equal: deep_equal (Current, other)

	frozen deep_twin: NETWORK_STREAM_SOCKET
			-- New object structure recursively duplicated from Current.
			-- (from ANY)
		ensure -- from ANY
			deep_twin_not_void: Result /= Void
			deep_equal: deep_equal (Current, Result)

	frozen standard_copy (other: NETWORK_STREAM_SOCKET)
			-- Copy every field of `other' onto corresponding field
			-- of current object.
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
			type_identity: same_type (other)
		ensure -- from ANY
			is_standard_equal: standard_is_equal (other)

	frozen standard_twin: NETWORK_STREAM_SOCKET
			-- New object field-by-field identical to `other'.
			-- Always uses default copying semantics.
			-- (from ANY)
		ensure -- from ANY
			standard_twin_not_void: Result /= Void
			equal: standard_equal (Result, Current)

	frozen twin: NETWORK_STREAM_SOCKET
			-- New object equal to `Current'
			-- twin calls copy; to change copying/twining semantics, redefine copy.
			-- (from ANY)
		ensure -- from ANY
			twin_not_void: Result /= Void
			is_equal: Result.is_equal (Current)
	
feature -- Miscellaneous

	name: STRING_8
			-- Socket name
			-- (from SOCKET)
		require -- from  IO_MEDIUM
			True
		require else -- from SOCKET
			socket_exists: exists
	
feature -- Basic operations

	frozen default: ?NETWORK_STREAM_SOCKET
			-- Default value of object's type
			-- (from ANY)

	frozen default_pointer: POINTER
			-- Default value of type `POINTER'
			-- (Avoid the need to write `p'.default for
			-- some `p' of type `POINTER'.)
			-- (from ANY)

	default_rescue
			-- Process exception for routines with no Rescue clause.
			-- (Default: do nothing.)
			-- (from ANY)

	frozen do_nothing
			-- Execute a null action.
			-- (from ANY)
	
feature -- Obsolete

	lastchar: CHARACTER_8
			-- Last character read by read_character
			-- (from IO_MEDIUM)

	lastdouble: REAL_64
			-- Last double read by read_double
			-- (from IO_MEDIUM)

	lastint: INTEGER_32
			-- Last integer read by read_integer
			-- (from IO_MEDIUM)

	lastreal: REAL_32
			-- Last real read by read_real
			-- (from IO_MEDIUM)

	laststring: ?STRING_8
			-- Last string read
			-- (from IO_MEDIUM)
	
feature 

	accept
			-- Accept a new connection on listen socket.
			-- Accepted service socket available in accepted.
			-- (from STREAM_SOCKET)
		require -- from STREAM_SOCKET
			socket_exists: exists

	accepted: NETWORK_STREAM_SOCKET
			-- Last accepted socket.
			-- (from STREAM_SOCKET)

	listen (queue: INTEGER_32)
			-- Listen on socket for at most `queue' connections.
			-- (from STREAM_SOCKET)
		require -- from STREAM_SOCKET
			socket_exists: exists
	
feature -- Basic commands

	old_socket_address: SOCKET_ADDRESS
			-- Local address of socket
			-- (from SOCKET)

	bind
			-- Bind socket to local address in address.
			-- (from SOCKET)
		require -- from SOCKET
			socket_exists: exists
			valid_local_address: address /= Void

	cleanup
			-- Cleanup socket.
			-- (from SOCKET)

	close
			-- Close socket for all context.
			-- (from SOCKET)
		require -- from IO_MEDIUM
			medium_is_open: not is_closed
		require else -- from SOCKET
			socket_exists: exists

	close_socket
			-- Close socket for current context.
			-- (from SOCKET)
		require -- from SOCKET
			socket_exists: exists
			is_open: is_open_read or is_open_write
		ensure -- from SOCKET
			is_closed: is_closed
			not_is_open: not is_open_read and not is_open_write

	connect
			-- Connect socket to peer address.
			-- (from SOCKET)
		require -- from SOCKET
			socket_exists: exists
			valid_peer_address: peer_address /= Void

	descriptor: INTEGER_32
			-- Socket descriptor of current socket
			-- (from SOCKET)

	descriptor_available: BOOLEAN
			-- Is descriptor available?
			-- (from SOCKET)

	family: INTEGER_32
			-- Socket family eg. af_inet, af_unix
			-- (from SOCKET)

	is_closed: BOOLEAN
			-- Is socket closed?
			-- (from SOCKET)

	make_socket
			-- Create socket descriptor.
			-- (from SOCKET)
		require -- from SOCKET
			valid_family: family >= 0
			valid_type: type >= 0
			valid_protocol: protocol >= 0

	peer_address: like address
			-- Peer address of socket
			-- (from SOCKET)

	protocol: INTEGER_32
			-- Protocol of the socket. default 0
			-- means for the system to chose the default
			-- protocol for the family chosen. eg. `udp', `tcp'.
			-- (from SOCKET)

	set_address (addr: like address)
			-- Set local address to `addr'.
			-- (from SOCKET)
		require -- from SOCKET
			same_type: addr.family = family
		ensure -- from SOCKET
			address_set: address = addr

	old_socket_set_peer_address (addr: like address)
			-- Set peer address to `addr'.
			-- (from SOCKET)
		require -- from SOCKET
			address_exists: addr /= Void
			address_valid: is_valid_peer_address (addr)
		ensure -- from SOCKET
			address_set: peer_address = addr

	set_peer_address (addr: like address)
			-- Set peer address to `addr'.
			-- (from SOCKET)
		require -- from SOCKET
			address_exists: addr /= Void
			address_valid: is_valid_peer_address (addr)
		ensure -- from SOCKET
			address_set: peer_address = addr

	type: INTEGER_32
			-- Type of socket. eg stream, datagram
			-- (from SOCKET)
	
feature -- Externals: flags for send, sendto recv and recvfrom socket calls

	c_msgdontroute: INTEGER_32
			-- Do not route message
			-- (from SOCKET_RESOURCES)

	c_oobmsg: INTEGER_32
			-- Out of bound message 
			-- (from SOCKET_RESOURCES)

	c_peekmsg: INTEGER_32
			-- Peek message
			-- (from SOCKET_RESOURCES)
	
feature -- Input

	last_boolean: BOOLEAN
			-- Last boolean read by read_boolean
			-- (from SOCKET)

	read (size: INTEGER_32): PACKET
			-- Read a packet of data of maximum size `size'.
			-- (from SOCKET)
		require -- from SOCKET
			socket_exists: exists
			opened_for_read: is_open_read

	read_boolean
			-- Read a new boolean.
			-- Maker result available in last_boolean.
			-- Was declared in SOCKET as synonym of readbool.
			-- (from SOCKET)
		require else -- from SOCKET
			socket_exists: exists
			opened_for_read: is_open_read

	read_character
			-- Read a new character.
			-- Make result available in last_character.
			-- Was declared in SOCKET as synonym of readchar.
			-- (from SOCKET)
		require -- from IO_MEDIUM
			is_readable: readable
		require else -- from SOCKET
			socket_exists: exists
			opened_for_read: is_open_read

	read_double
			-- Read a new double.
			-- Make result available in last_double.
			-- Was declared in SOCKET as synonym of readdouble.
			-- (from SOCKET)
		require -- from IO_MEDIUM
			is_readable: readable
		require else -- from SOCKET
			socket_exists: exists
			opened_for_read: is_open_read

	read_integer
			-- Read a new 32-bit integer.
			-- Make result available in last_integer.
			-- Was declared in SOCKET as synonym of readint and read_integer_32.
			-- (from SOCKET)
		require -- from IO_MEDIUM
			is_readable: