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

Class NewSocketHandler

source code

Read the handshake and hand a new socket connection off to where it belongs.

This class wraps some of the functionality of the BT1.Encrypter.Connection class. It will receive connections from the Server, read the protocol handshake, assign them to the proper torrent server, and pass the connection on to the Encrypter Connection.

Instance Methods [hide private]
 
__init__(self, multihandler, connection)
Initialize the instance.
source code
 
_auto_close(self)
Automatically close the connection if it is not fully connected.
source code
 
close(self)
Close the connection.
source code
int, method
_read_header(self, s)
Check if the protocol header matches.
source code
int, method
read_header(self, s)
Process the (possibly encrypted) protocol header from the connection.
source code
int, method
read_crypto_header(self, s)
Start to read an encrypted header from the connection.
source code
boolean
_search_for_pattern(self, s, pat)
Search for a pattern in the initial connection data.
source code
int, method
read_crypto_block3a(self, s)
Find the block3a crypto information in the connection.
source code
boolean
read_crypto_block3b(self, s)
Process the block3b crypto information in the connection.
source code
int, method
read_options(self, s)
Process the protocol options from the connection.
source code
boolean
read_download_id(self, s)
Read the torrent infohash from the connection.
source code
none
read_dead(self, s)
Do nothing.
source code
 
data_came_in(self, garbage, s)
Process the read data from the connection.
source code
 
_write_buffer(self, s)
Add the read data from the connection back onto the start of the buffer.
source code
 
_read(self, s)
Process the data read from the connection.
source code
 
connection_flushed(self, ss)
Do nothing.
source code
 
connection_lost(self, ss)
Close the lost connection.
source code
Instance Variables [hide private]
int _max_search
the number of remaining bytes to search for the pattern
string buffer
the buffer of unprocessed data received on the connection
boolean closed
whether the connection has been closed
boolean complete
whether the handshake is complete
SocketServer.SingleSocket connection
the connection to handle
boolean encrypted
whether the connection is encrypted
BTcrypto.Crypto encrypter
the encrypter to use for the connection
MultiHandler multihandler
the collection of all torrent Servers
method next_func
the method to call to read the protocol name from the connection
int next_len
the length of the protocol name header in the connection
string options
the protocol options read from the connection
string protocol
the protocol name used by the connection
method read
the method to call to read data from the connection
method write
the method to call to write data to the connnection
Method Details [hide private]

__init__(self, multihandler, connection)
(Constructor)

source code 
Initialize the instance.
Parameters:

_read_header(self, s)

source code 
Check if the protocol header matches.
Parameters:
  • s (string) - the data read from the conection
Returns: int, method
the next length to read and method to call with the data (or None if something went wrong)

read_header(self, s)

source code 
Process the (possibly encrypted) protocol header from the connection.
Parameters:
  • s (string) - the data read from the conection
Returns: int, method
the next length to read and method to call with the data (or None if something went wrong)

read_crypto_header(self, s)

source code 
Start to read an encrypted header from the connection.
Parameters:
  • s (string) - the data read from the conection
Returns: int, method
the next length to read and method to call with the data

_search_for_pattern(self, s, pat)

source code 
Search for a pattern in the initial connection data.
Parameters:
  • s (string) - the data read from the conection
  • pat (string) - the data to search for
Returns: boolean
whether the pattern was found

read_crypto_block3a(self, s)

source code 
Find the block3a crypto information in the connection.
Parameters:
  • s (string) - the data read from the conection
Returns: int, method
the next length to read and method to call with the data

read_crypto_block3b(self, s)

source code 

Process the block3b crypto information in the connection.

Passes the connection off to the appropriate torrent's Server if the correct block is found.
Parameters:
  • s (string) - the data read from the conection
Returns: boolean
whether the crypto block was found

read_options(self, s)

source code 
Process the protocol options from the connection.
Parameters:
  • s (string) - the data read from the conection
Returns: int, method
the next length to read and method to call with the data

read_download_id(self, s)

source code 

Read the torrent infohash from the connection.

Passes the connection off to the appropriate torrent's Server.
Parameters:
  • s (string) - the data read from the conection
Returns: boolean
whether a torrent was found to assign the connection to

read_dead(self, s)

source code 
Do nothing.
Parameters:
  • s (string) - the data read from the conection
Returns: none
None

data_came_in(self, garbage, s)

source code 
Process the read data from the connection.
Parameters:
  • garbage (unknown) - thrown away
  • s (string) - the data read from the conection

_write_buffer(self, s)

source code 
Add the read data from the connection back onto the start of the buffer.
Parameters:
  • s (string) - the data read from the conection

_read(self, s)

source code 

Process the data read from the connection.

Processes incoming data on the connection. The data is bufferred, then the next_func method is called with the next_len amount of the data. If it returns None, the connection is closed. If it returns True, the connection handshake is complete and the connection is established. Otherwise it returns int,method, which is the next length to read and method to call with the data. If the length is 0, it will read all the available data. If the length is -1 it will wait for more data to caome in.
Parameters:
  • s (string) - the data read from the conection

connection_flushed(self, ss)

source code 
Do nothing.
Parameters:

connection_lost(self, ss)

source code 
Close the lost connection.
Parameters: