diff -ur v9/sock_ut.py v10/sock_ut.py --- v9/sock_ut.py 2021-07-17 01:34:19.000000000 +0900 +++ v10/sock_ut.py 2021-07-19 00:10:50.000000000 +0900 @@ -2,119 +2,16 @@ import sys import socket -import select -import time import empty import thr -import cmd_ut +import fio_ut import dbg -can_read = lambda sc, tmout=0: select.select( [ sc ], [], [], tmout )[ 0 ] == [ sc ] - add_tail = lambda s, add: s + add - cut_tail = lambda s, cut: s[ : -len( cut ) ] if s.endswith( cut ) else s -def fio_new( get_b1_, put_b_, get_b_=None ): - - to_s = lambda bt: bt.decode( 'utf-8' ) - to_b = lambda s: s.encode( 'utf-8' ) - - def get_b1( f ): - b1 = get_b1_( f ) - return b1 if b1 else b'' - - get_s1 = lambda f : to_s( get_b1( f ) ) - - def get_b_line( f ): - b = b'' - while True: - b1 = get_b1( f ) - if not b1: - break - b += b1 - if b1 == b'\n': - break - return b - - get_s_line = lambda f: to_s( get_b_line( f ) ) - - def get_b( f ): - if get_b_: - return get_b_( f ) - - b = b'' - while not b or can_read( f ): - b1 = get_b1( f ) - if not b1: - break - b += b1 - return b - - get_s = lambda f: to_s( get_b( f ) ) - - def get_b_all( f ): - b = b'' - while not b or can_read( f ): - b_ = get_b( f ) - if not b_: - break - b += b_ - return b - - get_s_all = lambda f: to_s( get_b_all( f ) ) - - - def put_b( f, b ): - try: - put_b_( f, b ) - except: - return False - if hasattr( f, 'flush' ): - f.flush() - return True - - put_s = lambda f, s: put_b( f, to_b( s ) ) - - - def get_func( use_b, use_all ): - if use_b: - return get_b_all if use_all else get_b - return get_s_all if use_all else get_s - - def put_func( use_b ): - return put_b if use_b else put_s - - - return empty.new( locals() ) - - -def get_std_b( f ): - return f.buffer if hasattr( f, 'buffer' ) else f - -stdin_b = get_std_b( sys.stdin ) -stdout_b = get_std_b( sys.stdout ) -std_fio = fio_new( lambda f: f.read( 1 ), lambda f, b: f.write( b ) ) - - -def proc_new( cmd ): - proc = cmd_ut.proc_new( cmd, stdin=cmd_ut.PIPE, stdout=cmd_ut.PIPE ) - - stdin = proc.get_stdin() - stdout = proc.get_stdout() - - get_b1 = lambda f: f.read( 1 ) - put_b = lambda f, b: f.write( b ) if f else 0 - - fio = fio_new( get_b1, put_b ) - - def stop(): - proc.kill() - proc.wait() - - return empty.new( locals() ) - +close = fio_ut.close def port_conn( port, host='localhost' ): cs = socket.socket( socket.AF_INET, socket.SOCK_STREAM ) @@ -122,29 +19,27 @@ cs.connect( ( host, port ) ) except: return None + fio_ut.set_fs_sock( cs ) return cs -bufmax = 16 * 1024 -sc_fio = fio_new( lambda f: f.recv( 1 ), lambda f, b: f.sendall( b ), lambda f: f.recv( bufmax ) ) - def send_str( sc, s ): s = add_tail( s, '\n' ) - return sc_fio.put_s( sc, s ) + return fio_ut.p_s( sc, s ) def recv_str_line( sc ): - s = sc_fio.get_s_line( sc ) + s = fio_ut.g_s_line( sc ) return cut_tail( s, '\n' ) def recv_str_all( sc ): - s = sc_fio.get_s_all( sc ) + s = fio_ut.g_s_all( sc ) return cut_tail( s, '\n' ) def pipe_loop( sc_from, sc_to ): while True: - b = sc_fio.get_b( sc_from ) + b = fio_ut.g_b( sc_from ) if not b: break - if not sc_fio.put_b( sc_to, b ): + if not fio_ut.p_b( sc_to, b ): break # srv=name,port=xxx,host=xxx @@ -232,9 +127,11 @@ pipe_loop( sc, sc2 ) def base_th( port, sc, ev_quit ): + fio_ut.set_fs_sock( sc ) + s = recv_str_line( sc ) if not s: - sc.close() + close( sc ) return d = get_dic( s ) @@ -262,7 +159,7 @@ elif s in srvs: base_th_connect( sc, s ) - sc.close() + close( sc ) def ping_th(): id = 'ping' @@ -295,7 +192,7 @@ s = 'srv={},port={},host={}'.format( name, port, host ) if not send_str( ss, s ): - ss.close() + close( ss ) return None return ss @@ -328,11 +225,11 @@ ( port, host ) = get_port_host_from_dic( d ) cs = port_conn( port, host ) if not send_str( cs, s ): - cs.close() + close( cs ) return None s = recv_str_line( cs ) if s != 'ok': - cs.close() + close( cs ) return None return cs @@ -350,11 +247,21 @@ break th = thr.th_new( th_func, ( cs, ) ) thr.ths.start( th ) - ss.close() + close( ss ) def srv_func_new( cb, use_b=False, use_all=False ): - send = send_ = sc_fio.put_func( use_b ) - recv = recv_ = sc_fio.get_func( use_b, use_all ) + send = fio_ut.p_s + recv = fio_ut.g_s + if use_all: + recv = fio_ut.g_s_all + if use_b: + send = fio_ut.p_b + recv = fio_ut.g_b + if use_all: + recv = fio_ut.g_b_all + + send_ = send + recv_ = recv if not use_b: send = lambda f, s: send_( f, add_tail( s, '\n' ) ) recv = lambda f: cut_tail( recv_( f ), '\n' ) @@ -369,18 +276,18 @@ break if not send( sc, r ): break - sc.close() + close( sc ) return empty.new( locals() ) def srv_loop_cmd( host, port, name, cmd ): def th_func( sc ): - proc = proc_new( cmd ) + proc = fio_ut.proc_new( cmd ) def cb( b ): - proc.fio.put_b( proc.stdin, b ) - return proc.fio.get_b_line( proc.stdout ) + fio_ut.p_b( proc.fo, b ) + return fio_ut.g_b_all( proc.fi ) srv_func = srv_func_new( cb, use_b=True, use_all=True ) srv_func.func( sc ) @@ -394,12 +301,12 @@ return None if not send_str( cs, name ): - cs.close() + close( cs ) return None s = recv_str_line( cs ) if s != 'ok': - cs.close() + close( cs ) return None return cs @@ -409,7 +316,7 @@ return send_str( cs, 'get_srvs' ) r = recv_str_all( cs ) - cs.close() + close( cs ) return r.split( '\n' ) def kill_srv( port, name, host='loaclhost' ): @@ -419,7 +326,7 @@ s = 'kill=' + name #dbg.out( s ) send_str( cs, s ) - cs.close() + close( cs ) def kill_base_srv( port, host='loaclhost' ): cs = port_conn( port, host ) @@ -428,11 +335,11 @@ dbg.out( 'kill_base_srv' ) send_str( cs, 'kill_base_srv' ) r = recv_str_line( cs ) - cs.close() + close( cs ) cs = port_conn( port, host ) # for accept loop if cs: - cs.close() + close( cs ) def help(): lst = [ @@ -507,21 +414,22 @@ break dbg.out( '{} --> {}'.format( w, r ) ) else: + ( fi, fo ) = fio_ut.set_fs_sys_stdio() while True: - b = std_fio.get_b_line( stdin_b ) + b = fio_ut.g_b_line( fi ) if not b: break - if not sc_fio.put_b( sc, b ): + if not fio_ut.p_b( sc, b ): break - b = sc_fio.get_b_all( sc ) + b = fio_ut.g_b_all( sc ) if not b: break - if not std_fio.put_b( stdout_b, b ): + if not fio_ut.p_b( fo, b ): break - sc.close() + close( sc ) elif cmd == 'srvs': lst = get_srvs( host, port )