fix: string and blob parsing
feat: pass argument values to callback as array Signed-off-by: Christopher Arndt <chris@chrisarndt.de>
This commit is contained in:
		
							parent
							
								
									91ce62f680
								
							
						
					
					
						commit
						4f0bb2fa93
					
				@ -74,22 +74,22 @@ func _poll():
 | 
			
		||||
		_debug("OSC arg types: %s" % types)
 | 
			
		||||
 | 
			
		||||
		if _observers.has([address, types]):
 | 
			
		||||
			var values = _parse_osc_values(packet, types, offset)
 | 
			
		||||
			var values = _parse_osc_values(packet.slice(offset), types)
 | 
			
		||||
 | 
			
		||||
			if values == null:
 | 
			
		||||
				_debug("Invalid/Unsupported OSC message.")
 | 
			
		||||
			elif values.size() != types.length():
 | 
			
		||||
				_debug("Mismatch between expected / received number of OSC arguments.")
 | 
			
		||||
			else:
 | 
			
		||||
				values.append({
 | 
			
		||||
				var msg_info = {
 | 
			
		||||
					"ip": sender_ip,
 | 
			
		||||
					"port": sender_port,
 | 
			
		||||
					"address": address,
 | 
			
		||||
					"types": types
 | 
			
		||||
				})
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
				for callback in _observers[[address, types]]:
 | 
			
		||||
					callback.callv(values)
 | 
			
		||||
					callback.call(msg_info, values)
 | 
			
		||||
 | 
			
		||||
func _parse_osc_addr_and_types(packet):
 | 
			
		||||
	var asep = packet.find(0)
 | 
			
		||||
@ -103,10 +103,11 @@ func _parse_osc_addr_and_types(packet):
 | 
			
		||||
	return [address, types, tsep + (4 - tsep % 4)]
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
func _parse_osc_values(packet, types, offset):
 | 
			
		||||
func _parse_osc_values(packet, types):
 | 
			
		||||
	var result
 | 
			
		||||
	var values = []
 | 
			
		||||
	var stream = StreamPeerBuffer.new()
 | 
			
		||||
	stream.set_data_array(packet.slice(offset))
 | 
			
		||||
	stream.set_data_array(packet)
 | 
			
		||||
	stream.set_big_endian(true)
 | 
			
		||||
 | 
			
		||||
	for type_id in types:
 | 
			
		||||
@ -126,7 +127,7 @@ func _parse_osc_values(packet, types, offset):
 | 
			
		||||
				var null_found = false
 | 
			
		||||
 | 
			
		||||
				while not null_found:
 | 
			
		||||
					for _i in range(4):
 | 
			
		||||
					for _dummy in range(4):
 | 
			
		||||
						var ch = stream.get_u8()
 | 
			
		||||
						if not null_found and ch != 0:
 | 
			
		||||
							value.append(char(ch))
 | 
			
		||||
@ -136,12 +137,24 @@ func _parse_osc_values(packet, types, offset):
 | 
			
		||||
				values.append("".join(value))
 | 
			
		||||
			"b":
 | 
			
		||||
				var count = stream.get_u32()
 | 
			
		||||
				values.append(stream.get_data(count))
 | 
			
		||||
				result = stream.get_data(count)
 | 
			
		||||
				
 | 
			
		||||
				if count % 4:
 | 
			
		||||
					stream.seek(stream.get_position() + (count % 4))
 | 
			
		||||
				if result[0] == OK:
 | 
			
		||||
					values.append(result[1])
 | 
			
		||||
 | 
			
		||||
					if count % 4:
 | 
			
		||||
						stream.seek(stream.get_position() + (4 - count % 4))
 | 
			
		||||
				else:
 | 
			
		||||
					_debug("Could not read OSC blob argument.")
 | 
			
		||||
					return
 | 
			
		||||
			"t":
 | 
			
		||||
				values.append(stream.get_data(8))
 | 
			
		||||
				result = stream.get_data(8)
 | 
			
		||||
 | 
			
		||||
				if result[0] == OK:
 | 
			
		||||
					values.append(result[1])
 | 
			
		||||
				else:
 | 
			
		||||
					_debug("Could not read OSC timetag argument.")
 | 
			
		||||
					return
 | 
			
		||||
			"m", "r":
 | 
			
		||||
				values.append([
 | 
			
		||||
					stream.get_u8(),
 | 
			
		||||
@ -156,7 +169,7 @@ func _parse_osc_values(packet, types, offset):
 | 
			
		||||
			"I", "N":
 | 
			
		||||
				values.append(null)
 | 
			
		||||
			_:
 | 
			
		||||
				_debug("Argument type '%s' not yet supported." % type_id)
 | 
			
		||||
				_debug("Argument type '%s' not supported." % type_id)
 | 
			
		||||
				return
 | 
			
		||||
 | 
			
		||||
	return values
 | 
			
		||||
 | 
			
		||||
@ -1,9 +1,9 @@
 | 
			
		||||
extends VSlider
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
func recv_osc(val, msg_info):
 | 
			
		||||
func recv_osc(msg_info, values):
 | 
			
		||||
	print("Sender IP: %s" % msg_info["ip"])
 | 
			
		||||
	print("Sender Port: %d" % msg_info["port"])
 | 
			
		||||
	print("Adress: %s" % msg_info["address"])
 | 
			
		||||
	print("Address: %s" % msg_info["address"])
 | 
			
		||||
	print("Types: %s" % msg_info["types"])
 | 
			
		||||
	set_value(clampf(val, 0.0, 1.0))
 | 
			
		||||
	set_value(clampf(values[0], 0.0, 1.0))
 | 
			
		||||
 | 
			
		||||
@ -1,9 +1,9 @@
 | 
			
		||||
extends Button
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
func recv_osc(val, msg_info):
 | 
			
		||||
func recv_osc(msg_info, values):
 | 
			
		||||
	print("Sender IP: %s" % msg_info["ip"])
 | 
			
		||||
	print("Sender Port: %d" % msg_info["port"])
 | 
			
		||||
	print("Adress: %s" % msg_info["address"])
 | 
			
		||||
	print("Address: %s" % msg_info["address"])
 | 
			
		||||
	print("Types: %s" % msg_info["types"])
 | 
			
		||||
	set_pressed_no_signal(bool(val))
 | 
			
		||||
	set_pressed_no_signal(bool(values[0]))
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										12
									
								
								UI.gd
									
									
									
									
									
								
							
							
						
						
									
										12
									
								
								UI.gd
									
									
									
									
									
								
							@ -19,5 +19,17 @@ func _ready():
 | 
			
		||||
		var button_node = find_child(nodename)
 | 
			
		||||
		osc_server.register_callback(osc_addr, "i", button_node.recv_osc)
 | 
			
		||||
 | 
			
		||||
	osc_server.register_callback("/string", "s", recv_osc)
 | 
			
		||||
	osc_server.register_callback("/stringint", "si", recv_osc)
 | 
			
		||||
	osc_server.register_callback("/blob", "b", recv_osc)
 | 
			
		||||
	osc_server.register_callback("/blobint", "bi", recv_osc)
 | 
			
		||||
 | 
			
		||||
	# start listening for osc messages
 | 
			
		||||
	osc_server.start_server()
 | 
			
		||||
 | 
			
		||||
func recv_osc(msg_info, values):
 | 
			
		||||
	print("Sender IP: %s" % msg_info["ip"])
 | 
			
		||||
	print("Sender Port: %d" % msg_info["port"])
 | 
			
		||||
	print("Address: %s" % msg_info["address"])
 | 
			
		||||
	print("Types: %s" % msg_info["types"])
 | 
			
		||||
	print("Values:", values)
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user