Package DebTorrent :: Module SocketHandler :: Class SocketHandler
[hide private]
[frames] | no frames]

Class SocketHandler

source code

The collection of all open sockets.

Instance Methods [hide private]
 
__init__(self, timeout, ipv6_enable, readsize=100000)
Initialize the instance.
source code
 
scan_for_timeouts(self)
Check the sockets for timeouts.
source code
 
bind(self, port, bind='', reuse=True, ipv6_socket_style=1)
Bind to listen on a single port.
source code
int
find_and_bind(self, minport, maxport, bind='', reuse=True, ipv6_socket_style=1, randomizer=True)
Bind to listen on a single port within a range.
source code
 
set_handler(self, handler, port=None)
Set the handler to use for a port (or the default handler).
source code
SocketHandler.SingleSocket
start_connection_raw(self, dns, socktype=2, handler=None)
Initiate a new connection to a peer (setting the type of socket).
source code
SocketHandler.SingleSocket
start_connection(self, dns, handler=None, randomize=True)
Initiate a new connection to a peer.
source code
 
_sleep(self)
Sleep for one second.
source code
 
handle_events(self, events)
Handle any events that have occurred on the open sockets.
source code
 
close_dead(self)
Close sockets that have failed to be written.
source code
 
_close_socket(self, s)
Close an open socket.
source code
 
do_poll(self, t)
Poll the open sockets.
source code
dictionary
get_stats(self)
Get some information about the bound interfaces and ports.
source code
 
shutdown(self)
Close all open sockets and servers.
source code
Instance Variables [hide private]
list of SingleSocket dead_from_write
the sockets that have failed due to writing
dictionary of {int: unknown} handlers
the handlers that are used for the listened ports, keys are the ports
list of string interfaces
the interfaces that have been bound to
boolean ipv6_enable
allow the client to connect to peers via IPv6
int max_connects
the maximum number of sockets to have open at atime
select.poll poll
the poll object to use to poll the sockets
list of int ports
the ports that are being listened on
int readsize
the maximum amount of data to read from a socket
dictionary of {int: socket.socket} servers
the socket listeners, keys are the file numbers
dictionary of {int: SingleSocket} single_sockets
the collection of all open sockets, keys are the socket's file number
float timeout
seconds to wait between closing sockets on which nothing has been received on
Method Details [hide private]

__init__(self, timeout, ipv6_enable, readsize=100000)
(Constructor)

source code 
Initialize the instance.
Parameters:
  • timeout (float) - seconds to wait between closing sockets on which nothing has been received on
  • ipv6_enable (boolean) - allow the client to connect to peers via IPv6
  • readsize (int) - the maximum amount of data to read from a socket (optional, defaults to 100000)

bind(self, port, bind='', reuse=True, ipv6_socket_style=1)

source code 
Bind to listen on a single port.
Parameters:
  • port (int) - the port to listen on
  • bind (string) - the IP address to bind to (optional, defaults to all)
  • reuse (boolean) - whether to use SO_REUSEADDR to bind (optional, defaults to False). This allows the bind to work if the socket is still open in the TIME_WAIT state from a recently shutdown server.
  • ipv6_socket_style (int) - whether an IPv6 server socket will also field IPv4 connections (optional, defaults to yes)
Raises:
  • socket.error - if the port can not be bound

find_and_bind(self, minport, maxport, bind='', reuse=True, ipv6_socket_style=1, randomizer=True)

source code 
Bind to listen on a single port within a range.
Parameters:
  • minport (int) - the minimum port to listen on
  • maxport (int) - the maximum port to listen on
  • bind (string) - the addresses to bind to (optional, defaults to the default IPv6 and IPv4 addreses). Parsed as a comma seperated list (can be IP addresses or hostnames).
  • reuse (boolean) - whether to use SO_REUSEADDR to bind (optional, defaults to False). This allows the bind to work if the socket is still open in the TIME_WAIT state from a recently shutdown server.
  • ipv6_socket_style (int) - whether an IPv6 server socket will also field IPv4 connections (optional, defaults to yes)
  • randomizer (boolean) - whether to randomize the range or use it sequentially
Returns: int
the port that was bound to
Raises:
  • socket.error - if none of the ports in the range can be bound

set_handler(self, handler, port=None)

source code 
Set the handler to use for a port (or the default handler).
Parameters:
  • handler (unknown) - the data handler to use to process data on the port
  • port (int) - the port to use the handler for (optional, defaults to setting the default handler)

start_connection_raw(self, dns, socktype=2, handler=None)

source code 
Initiate a new connection to a peer (setting the type of socket).
Parameters:
  • dns ((string, int)) - the IP address and port number to contact the peer on
  • socktype (int) - the type of socket to open
  • handler (unknown) - the data handler to use to process data on the connection (optional, defaults to using the defualt handler)
Returns: SocketHandler.SingleSocket
the new connection made to the peer
Raises:
  • socket.error - if the connection fails

start_connection(self, dns, handler=None, randomize=True)

source code 
Initiate a new connection to a peer.
Parameters:
  • dns ((string, int)) - the IP address and port number to contact the peer on
  • handler (unknown) - the data handler to use to process data on the connection (optional, defaults to using the defualt handler)
  • randomize (boolean) - whether to randomize the possible sockets or choose one sequentially
Returns: SocketHandler.SingleSocket
the new connection made to the peer
Raises:
  • socket.error - if the connection fails

handle_events(self, events)

source code 
Handle any events that have occurred on the open sockets.
Parameters:
  • events (list of (int, int)) - the socket file descriptors and event types that have occurred on them

_close_socket(self, s)

source code 
Close an open socket.
Parameters:

do_poll(self, t)

source code 

Poll the open sockets.

If the poll returns None (which it shouldn't), then 5% of the open sockets will be closed.
Parameters:
  • t (float) - seconds to wait for an event before timing out

get_stats(self)

source code 
Get some information about the bound interfaces and ports.
Returns: dictionary
info about the bound interfaces