<s> from cStringIO import StringIO <EOL> try : <EOL> True <EOL> except : <EOL> True = <NUM_LIT:1> <EOL> False = <NUM_LIT:0> <EOL> from BT1 . Encrypter import protocol_name <EOL> default_task_id = [ ] <EOL> class SingleRawServer : <EOL> def __init__ ( self , info_hash , multihandler , doneflag , protocol ) : <EOL> self . info_hash = info_hash <EOL> self . doneflag = doneflag <EOL> self . protocol = protocol <EOL> self . multihandler = multihandler <EOL> self . rawserver = multihandler . rawserver <EOL> self . finished = False <EOL> self . running = False <EOL> self . handler = None <EOL> self . taskqueue = [ ] <EOL> def shutdown ( self ) : <EOL> if not self . finished : <EOL> self . multihandler . shutdown_torrent ( self . info_hash ) <EOL> def _shutdown ( self ) : <EOL> if not self . finished : <EOL> self . finished = True <EOL> self . running = False <EOL> self . rawserver . kill_tasks ( self . info_hash ) <EOL> if self . handler : <EOL> self . handler . close_all ( ) <EOL> def _external_connection_made ( self , c , options , already_read ) : <EOL> if self . running : <EOL> c . set_handler ( self . handler ) <EOL> self . handler . externally_handshaked_connection_made ( <EOL> c , options , already_read ) <EOL> def add_task ( self , func , delay = <NUM_LIT:0> , id = default_task_id ) : <EOL> if id is default_task_id : <EOL> id = self . info_hash <EOL> if not self . finished : <EOL> self . rawserver . add_task ( func , delay , id ) <EOL> def start_connection ( self , dns , handler = None ) : <EOL> if not handler : <EOL> handler = self . handler <EOL> c = self . rawserver . start_connection ( dns , handler ) <EOL> return c <EOL> def start_listening ( self , handler ) : <EOL> self . handler = handler <EOL> self . running = True <EOL> return self . shutdown <EOL> def is_finished ( self ) : <EOL> return self . finished <EOL> def get_exception_flag ( self ) : <EOL> return self . rawserver . get_exception_flag ( ) <EOL> class NewSocketHandler : <EOL> def __init__ ( self , multihandler , connection ) : <EOL> self . multihandler = multihandler <EOL> self . connection = connection <EOL> connection . set_handler ( self ) <EOL> self . closed = False <EOL> self . buffer = StringIO ( ) <EOL> self . complete = False <EOL> self . next_len , self . next_func = <NUM_LIT:1> , self . read_header_len <EOL> self . multihandler . rawserver . add_task ( self . _auto_close , <NUM_LIT:15> ) <EOL> def _auto_close ( self ) : <EOL> if not self . complete : <EOL> self . close ( ) <EOL> def close ( self ) : <EOL> if not self . closed : <EOL> self . connection . close ( ) <EOL> self . closed = True <EOL> def read_header_len ( self , s ) : <EOL> l = ord ( s ) <EOL> return l , self . read_header <EOL> def read_header ( self , s ) : <EOL> self . protocol = s <EOL> return <NUM_LIT:8> , self . read_reserved <EOL> def read_reserved ( self , s ) : <EOL> self . options = s <EOL> return <NUM_LIT:20> , self . read_download_id <EOL> def read_download_id ( self , s ) : <EOL> if self . multihandler . singlerawservers . has_key ( s ) : <EOL> if self . multihandler . singlerawservers [ s ] . protocol == self . protocol : <EOL> return True <EOL> return None <EOL> def read_dead ( self , s ) : <EOL> return None <EOL> def data_came_in ( self , garbage , s ) : <EOL> while True : <EOL> if self . closed : <EOL> return <EOL> i = self . next_len - self . buffer . tell ( ) <EOL> if i > len ( s ) : <EOL> self . buffer . write ( s ) <EOL> return <EOL> self . buffer . write ( s [ : i ] ) <EOL> s = s [ i : ] <EOL> m = self . buffer . getvalue ( ) <EOL> self . buffer . reset ( ) <EOL> self . buffer . truncate ( ) <EOL> try : <EOL> x = self . next_func ( m ) <EOL> except : <EOL> self . next_len , self . next_func = <NUM_LIT:1> , self . read_dead <EOL> raise <EOL> if x is None : <EOL> self . close ( ) <EOL> return <EOL> if x == True : <EOL> self . multihandler . singlerawservers [ m ] . _external_connection_made ( <EOL> self . connection , self . options , s ) <EOL> self . complete = True <EOL> return <EOL> self . next_len , self . next_func = x <EOL> def connection_flushed ( self , ss ) : <EOL> pass <EOL> def connection_lost ( self , ss ) : <EOL> self . closed = True <EOL> class MultiHandler : <EOL> def __init__ ( self , rawserver , doneflag ) : <EOL> self . rawserver = rawserver <EOL> self . masterdoneflag = doneflag <EOL> self . singlerawservers = { } <EOL> self . connections = { } <EOL> self . taskqueues = { } <EOL> def newRawServer ( self , info_hash , doneflag , protocol = protocol_name ) : <EOL> new = SingleRawServer ( info_hash , self , doneflag , protocol ) <EOL> self . singlerawservers [ info_hash ] = new <EOL> return new <EOL> def shutdown_torrent ( self , info_hash ) : <EOL> self . singlerawservers [ info_hash ] . _shutdown ( ) <EOL> del self . singlerawservers [ info_hash ] <EOL> def listen_forever ( self ) : <EOL> self . rawserver . listen_forever ( self ) <EOL> for srs in self . singlerawservers . values ( ) : <EOL> srs . finished = True <EOL> srs . running = False <EOL> srs . doneflag . set ( ) <EOL> def external_connection_made ( self , ss ) : <EOL> NewSocketHandler ( self , ss ) </s>
