<s> import monocle <EOL> from monocle import _o , Return <EOL> from monocle . callback import Callback , defer <EOL> from monocle . stack import eventloop as evlp <EOL> class ConnectionLost ( Exception ) : <EOL> pass <EOL> class Connection ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , stack_conn = None ) : <EOL> self . _stack_conn = stack_conn <EOL> self . writing = False <EOL> self . flush_cb = Callback ( ) <EOL> self . write_encoding = '<STR_LIT:utf-8>' <EOL> self . timeout = None <EOL> self . _current_timeout = None <EOL> def _time_out ( self , instigating_cb , timeout ) : <EOL> self . _current_timeout = None <EOL> if hasattr ( instigating_cb , '<STR_LIT:result>' ) : <EOL> return <EOL> current_cbs = [ self . _stack_conn . read_cb , <EOL> self . flush_cb , <EOL> self . _stack_conn . connect_cb ] <EOL> self . _stack_conn . read_cb = None <EOL> self . flush_cb = None <EOL> self . _stack_conn . connect_cb = None <EOL> self . _stack_conn . disconnect ( ) <EOL> for cb in current_cbs : <EOL> if cb and not hasattr ( cb , '<STR_LIT:result>' ) : <EOL> cb ( ConnectionLost ( "<STR_LIT>" % <EOL> timeout ) ) <EOL> def _queue_timeout ( self , cb ) : <EOL> if self . timeout is not None : <EOL> if self . _current_timeout : <EOL> self . _current_timeout . cancel ( ) <EOL> self . _current_timeout = evlp . queue_task ( self . timeout , <EOL> self . _time_out , <EOL> cb , <EOL> self . timeout ) <EOL> @ _o <EOL> def read_some ( self ) : <EOL> self . _check_reading ( ) <EOL> if not self . _stack_conn . buffer : <EOL> self . _check_closed ( ) <EOL> self . _stack_conn . resume ( ) <EOL> self . _queue_timeout ( self . _stack_conn . read_cb ) <EOL> yield self . _stack_conn . read_cb <EOL> tmp = self . _stack_conn . buffer <EOL> self . _stack_conn . buffer = "<STR_LIT>" <EOL> yield Return ( tmp ) <EOL> @ _o <EOL> def read ( self , size ) : <EOL> self . _check_reading ( ) <EOL> while len ( self . _stack_conn . buffer ) < size : <EOL> self . _check_closed ( ) <EOL> self . _stack_conn . resume ( ) <EOL> self . _queue_timeout ( self . _stack_conn . read_cb ) <EOL> yield self . _stack_conn . read_cb <EOL> tmp = self . _stack_conn . buffer [ : size ] <EOL> self . _stack_conn . buffer = self . _stack_conn . buffer [ size : ] <EOL> yield Return ( tmp ) <EOL> @ _o <EOL> def read_until ( self , s ) : <EOL> self . _check_reading ( ) <EOL> while True : <EOL> size = self . _stack_conn . buffer . find ( s ) <EOL> if size != - <NUM_LIT:1> : <EOL> size += len ( s ) <EOL> break <EOL> self . _check_closed ( ) <EOL> self . _stack_conn . resume ( ) <EOL> self . _queue_timeout ( self . _stack_conn . read_cb ) <EOL> yield self . _stack_conn . read_cb <EOL> tmp = self . _stack_conn . buffer [ : size ] <EOL> self . _stack_conn . buffer = self . _stack_conn . buffer [ size : ] <EOL> yield Return ( tmp ) <EOL> def readline ( self ) : <EOL> return self . read_until ( "<STR_LIT:\n>" ) <EOL> def write ( self , data ) : <EOL> if isinstance ( data , unicode ) : <EOL> data = data . encode ( self . write_encoding ) <EOL> self . _check_closed ( ) <EOL> if data == "<STR_LIT>" : <EOL> return defer ( None ) <EOL> self . writing = True <EOL> self . _stack_conn . write ( data ) <EOL> cb = self . flush ( ) <EOL> self . _queue_timeout ( cb ) <EOL> return cb <EOL> def _write_flushed ( self , result = None ) : <EOL> self . writing = False <EOL> cb = self . flush_cb <EOL> self . flush_cb = Callback ( ) <EOL> if cb : <EOL> cb ( result ) <EOL> def flush ( self ) : <EOL> self . _check_closed ( ) <EOL> cb = self . flush_cb <EOL> if not self . writing : <EOL> self . _write_flushed ( ) <EOL> return cb <EOL> def _closed ( self , reason ) : <EOL> if self . _current_timeout : <EOL> self . _current_timeout . cancel ( ) <EOL> self . _current_timeout = None <EOL> cl = ConnectionLost ( str ( reason ) ) <EOL> cl . original = reason <EOL> if self . _stack_conn . connect_cb : <EOL> cb = self . _stack_conn . connect_cb <EOL> self . _stack_conn . connect_cb = None <EOL> cb ( cl ) <EOL> self . _write_flushed ( cl ) <EOL> if self . _stack_conn . read_cb : <EOL> cb = self . _stack_conn . read_cb <EOL> self . _stack_conn . read_cb = None <EOL> cb ( cl ) <EOL> def close ( self ) : <EOL> self . _stack_conn . disconnect ( ) <EOL> def _check_reading ( self ) : <EOL> if self . _stack_conn . reading ( ) : <EOL> raise IOError ( "<STR_LIT>" ) <EOL> def _check_closed ( self ) : <EOL> if self . _stack_conn . closed ( ) : <EOL> raise IOError ( "<STR_LIT>" ) <EOL> def is_closed ( self ) : <EOL> return self . _stack_conn . closed ( ) <EOL> if monocle . _stack_name == '<STR_LIT>' : <EOL> from monocle . twisted_stack . network import * <EOL> elif monocle . _stack_name == '<STR_LIT>' : <EOL> from monocle . tornado_stack . network import * <EOL> elif monocle . _stack_name == '<STR_LIT>' : <EOL> from monocle . asyncore_stack . network import * </s>
