<commit_before>-- host debug server
-- 
if puss._debug_proxy then
	local puss_system = puss.require('puss_system')
	local net = puss.import('core.net')
	local puss_debug = puss._debug_proxy
	do
		local ok, res = puss_debug:__host_pcall('puss.trace_dofile', puss._debug_filename)
		if not ok then print('DebugInject failed:', res) end
	end

	local MT = getmetatable(puss_debug)
	function MT:fetch_stack() return self:__host_pcall('puss._debug_fetch_stack') end
	function MT:fetch_vars(level) return level, self:__host_pcall('puss._debug_fetch_vars', level) end
	function MT:fetch_subs(idx) return idx, self:__host_pcall('puss._debug_fetch_subs', idx) end
	function MT:modify_var(idx, val) return self:__host_pcall('puss._debug_modify_var', idx, val) end

	local BROADCAST_PORT = 9999
	local broadcast_udp = puss_system.socket_new()
	broadcast_udp:create(AF_INET, puss_system.SOCK_DGRAM, puss_system.IPPROTO_UDP)
	broadcast_udp:set_broadcast(BROADCAST_PORT)
	broadcast_udp:bind()
	broadcast_udp:set_nonblock(true)

	local listen_sock, listen_addr = net.listen(nil, 0, true)
	local socket, address
	local send_breaked_frame

	local BROADCAST_INFO = string.format('[PussDebug]|%s', listen_addr)
	do
		local ok, title = puss_debug:__host_pcall('puss._debug_fetch_title')
		if ok then BROADCAST_INFO = string.format('%s|%s', BROADCAST_INFO, title) end
	end

	local function dispatch(cmd, ...)
		-- print( 'host recv:', cmd, ... )
		if not cmd:match('^%w[_%w]+$') then error('host command name('..tostring(cmd)..') error!') end
		local handle = puss_debug[cmd]
		if not handle then error('host unknown cmd('..tostring(cmd)..')') end
		return net.send(socket, cmd, handle(puss_debug, ...))
	end

	local function hook_main_update(breaked, frame)
		if not socket then
			broadcast_udp:sendto(BROADCAST_INFO)
			-- print('broadcast', BROADCAST_PORT, BROADCAST_INFO)

			socket, address = net.accept(listen_sock)
			-- print('accept', listen_sock, socket, address)

			if socket then
				print('host attach', socket, address)
			else
				breaked = false
			end
		elseif not socket:valid() then
			print('host detach', socket, address)
			breaked, socket, address = false, nil, nil<commit_msg>close listen socket when debug<commit_after>-- host debug server
-- 
if puss._debug_proxy then
	local puss_system = puss.require('puss_system')
	local net = puss.import('core.net')
	local puss_debug = puss._debug_proxy
	do
		local ok, res = puss_debug:__host_pcall('puss.trace_dofile', puss._debug_filename)
		if not ok then print('DebugInject failed:', res) end
	end

	local MT = getmetatable(puss_debug)
	function MT:fetch_stack() return self:__host_pcall('puss._debug_fetch_stack') end
	function MT:fetch_vars(level) return level, self:__host_pcall('puss._debug_fetch_vars', level) end
	function MT:fetch_subs(idx) return idx, self:__host_pcall('puss._debug_fetch_subs', idx) end
	function MT:modify_var(idx, val) return self:__host_pcall('puss._debug_modify_var', idx, val) end

	local BROADCAST_PORT = 9999
	local broadcast_udp = puss_system.socket_new()
	broadcast_udp:create(AF_INET, puss_system.SOCK_DGRAM, puss_system.IPPROTO_UDP)
	broadcast_udp:set_broadcast(BROADCAST_PORT)
	broadcast_udp:bind()
	broadcast_udp:set_nonblock(true)

	local listen_socket, broadcast_info
	local socket, address
	local send_breaked_frame

	local function dispatch(cmd, ...)
		-- print( 'host recv:', cmd, ... )
		if not cmd:match('^%w[_%w]+$') then error('host command name('..tostring(cmd)..') error!') end
		local handle = puss_debug[cmd]
		if not handle then error('host unknown cmd('..tostring(cmd)..')') end
		return net.send(socket, cmd, handle(puss_debug, ...))
	end

	local function hook_main_update(breaked, frame)
		if not socket then
			if not listen_socket then
				local s, a = net.listen(nil, 0, true)
				if not s then return end

				listen_socket, broadcast_info = s, string.format('[PussDebug]|%s', a)
				local ok, title = puss_debug:__host_pcall('puss._debug_fetch_title')
				if ok then broadcast_info = string.format('%s|%s', broadcast_info, title) end
			end

			broadcast_udp:sendto(broadcast_info)
			-- print('broadcast', BROADCAST_PORT, broadcast_info)

			socket, address = net.accept(listen_socket)
			-- print('accept', listen_socket, socket, address)

			if socket then
				print('host attach', socket, address)
				listen_socket:close()
				listen_socket = nil
			else
				breaked = false
			end
		elseif not socket:valid() then
			print('host detach', socket, address)
			breaked, socket, address = false, nil, nil
{'fraction_non_alphanumeric': 0.11932698693823333, 'fraction_numerical': 0.002213858755811379, 'mean_word_length': 5.223140495867769, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6495914', 'n_tokens_mistral': 1724, 'n_tokens_neox': 1599, 'n_words': 375}