def can_handle(self, data_type: bytes) -> bool: if data_type == b'ext_set': rpr_print('can handle ext_set') return True if data_type == b'ext_get': rpr_print('can handle ext_get') return True rpr_print(f"can't handle {data_type}") return False
from reaper_python import * from networking import DEF_HOST, DEF_PORT from TCP_funcs import rpr_print retval = RPR_SetExtState('TCP_PACKAGE', 'DATA', 'test string data', True) retval = RPR_SetExtState('TCP_PACKAGE', 'HOST', DEF_HOST, True) retval = RPR_SetExtState('TCP_PACKAGE', 'PORT', str(DEF_PORT), True) rpr_print(f'added ExtState "TCP_PACKAGE"') rpr_print('TCP_PACKAGE: DATA = test string data') rpr_print(f'TCP_PACKAGE: HOST = {DEF_HOST}') rpr_print(f'TCP_PACKAGE: PORT = {DEF_PORT}')
''' Application example using build() + return ========================================== An application can be built if you return a widget on build(), or if you set self.root. ''' from networking import send_data from TCP_funcs import rpr_print from TCP_funcs import get_host_port import socket as st from reaper_python import * # data, host, port = "test string data", '127.0.0.1', 49541 host, port = get_host_port() data = RPR_GetExtState('TCP_PACKAGE', 'DATA') try: retval = send_data("ext_set", data, host, port) except st.timeout as e: retval = f'connection timeout:\n{e}' except ConnectionRefusedError as e: retval = f'connection refused:\n{e}' rpr_print(f'sent "{data}" to {host}: {port}', f'returned: {retval}', sep='\n') # print(f'sent "{data}" to {host}: {port}', f'returned: {retval}', sep='\n')
def at_exit() -> None: rpr_print('closing server') server.server_close()
retval = RPR_SetExtState( self.extname, self.extkey, str(data, 'utf-8'), False ) return bytes(str(retval), 'utf-8') SlaveTCPHandler.register(ExtStateHandler()) server = ss.ThreadingTCPServer( (HOST, PORT), SlaveTCPHandler, bind_and_activate=False ) server.timeout = .01 server.allow_reuse_address = True server.server_bind() server.server_activate() rpr_print(f'serving on {server.server_address}') def main_loop() -> None: # rpr.print('run') server.handle_request() RPR_defer('main_loop()') def at_exit() -> None: rpr_print('closing server') server.server_close() RPR_atexit('at_exit()') main_loop()