void OnEnable() { // Load settings and apply if ( PlayerPrefs.HasKey(portPrefKey) && PlayerPrefs.HasKey(modePrefKey) && PlayerPrefs.HasKey(multicastAddressPrefKey) && PlayerPrefs.HasKey(messagesVisibilityPrefKey) ) { int tempPort = PlayerPrefs.GetInt(portPrefKey); OscReceiveMode tempMode = (OscReceiveMode)PlayerPrefs.GetInt(modePrefKey); string tempMulticastAddress = PlayerPrefs.GetString(multicastAddressPrefKey); modeDropdown.value = (int)tempMode; // avoid onChanged call Open(tempPort, tempMode, tempMulticastAddress); messagesToggle.isOn = PlayerPrefs.GetInt(messagesVisibilityPrefKey) == 1 ? true : false; OnMessageVisibilityChanged(messagesToggle.isOn); } // Subcribe to UI events openToggle.onValueChanged.AddListener(OnOpenChanged); portInputField.onEndEdit.AddListener(OnPortEndEdit); modeDropdown.onValueChanged.AddListener(OnModeChanged); multicastAddressInputField.onEndEdit.AddListener(OnIpAddressEndEdit); messagesToggle.onValueChanged.AddListener(OnMessageVisibilityChanged); }
void Update() { if (oscIn == null) { Destroy(this); return; } // Update UI if (oscIn.isOpen != openToggle.isOn) { openToggle.isOn = oscIn.isOpen; } if (oscIn.port != _port) { _port = oscIn.port; portInputField.text = _port.ToString(); } if (oscIn.mode != (OscReceiveMode)modeDropdown.value) { _mode = oscIn.mode; modeDropdown.value = (int)oscIn.mode; } if (OscIn.localIpAddress != localIpAddressLabel.text) { if (string.IsNullOrEmpty(OscIn.localIpAddress)) { localIpAddressLabel.text = "Local IP Not found"; } else { localIpAddressLabel.text = OscIn.localIpAddress; } } if (oscIn.multicastAddress != _multicastAddress) { _multicastAddress = oscIn.multicastAddress; multicastAddressInputField.text = _multicastAddress; } if (messagesToggle.isOn) { _sb.Clear(); _messageStringQueue.CopyTo(_messageStringBuffer, 0); // Copy to array so we can iterate backswards. for (int i = _messageStringBuffer.Length - 1; i >= 0; i--) { _sb.AppendLine(_messageStringBuffer[i]); } messageBufferText.text = _sb.ToString(); } }
void Update() { if (oscIn == null) { Destroy(this); return; } // Update UI if (oscIn.isOpen != openToggle.isOn) { openToggle.isOn = oscIn.isOpen; } if (oscIn.port != port) { port = oscIn.port; portInputField.text = port.ToString(); } if (oscIn.mode != (OscReceiveMode)modeDropdown.value) { mode = oscIn.mode; modeDropdown.value = (int)oscIn.mode; } if (OscIn.ipAddress != localIpAddressLabel.text) { if (string.IsNullOrEmpty(OscIn.ipAddress)) { localIpAddressLabel.text = "Local IP Not found"; } else { localIpAddressLabel.text = OscIn.ipAddress; } } if (oscIn.multicastAddress != multicastAddress) { multicastAddress = oscIn.multicastAddress; multicastAddressInputField.text = multicastAddress; } if (messagesToggle.isOn) { OscMessage[] messages = messageBuffer.ToArray(); StringBuilder messagesText = new StringBuilder(); for (int m = messages.Length - 1; m >= 0; m--) { messagesText.AppendLine(messages[m].ToString()); } messageBufferText.text = messagesText.ToString(); } }
void Open(int port, OscReceiveMode mode, string multicastAddress) { switch (mode) { case OscReceiveMode.UnicastBroadcast: oscIn.Open(port); _componentRect.sizeDelta = new Vector2(_componentRect.sizeDelta.x, componentHeightNonMulticast); break; case OscReceiveMode.UnicastBroadcastMulticast: oscIn.Open(port, multicastAddress); _componentRect.sizeDelta = new Vector2(_componentRect.sizeDelta.x, componentHeightMulticast); break; } }
void Update() { if( oscIn == null ){ Destroy( this ); return; } // Update UI if( oscIn.isOpen != openToggle.isOn ) openToggle.isOn = oscIn.isOpen; if( oscIn.port != port ){ port = oscIn.port; portInputField.text = port.ToString(); } if( oscIn.mode != (OscReceiveMode) modeDropdown.value ){ mode = oscIn.mode; modeDropdown.value = (int) oscIn.mode; } if( OscIn.ipAddress != localIpAddressLabel.text ){ if( string.IsNullOrEmpty( OscIn.ipAddress ) ) localIpAddressLabel.text = "Local IP Not found"; else localIpAddressLabel.text = OscIn.ipAddress; } if( oscIn.multicastAddress != multicastAddress ){ multicastAddress = oscIn.multicastAddress; multicastAddressInputField.text = multicastAddress; } if( messagesToggle.isOn ){ OscMessage[] messages = messageBuffer.ToArray(); StringBuilder messagesText = new StringBuilder(); for( int m=messages.Length-1; m>=0; m-- ) messagesText.AppendLine( messages[m].ToString() ); messageBufferText.text = messagesText.ToString(); } }
void Open( int port, OscReceiveMode mode, string multicastAddress ) { switch( mode ){ case OscReceiveMode.UnicastBroadcast: oscIn.Open( port ); componentRect.sizeDelta = new Vector2( componentRect.sizeDelta.x, componentHeightNonMulticast ); break; case OscReceiveMode.UnicastBroadcastMulticast: oscIn.Open( port, multicastAddress ); componentRect.sizeDelta = new Vector2( componentRect.sizeDelta.x, componentHeightMulticast ); break; } }
void OnModeChanged( int modeInt ) { mode = (OscReceiveMode) modeInt; Open( port, mode, multicastAddress ); }
void OnModeChanged(int modeInt) { _mode = (OscReceiveMode)modeInt; Open(_port, _mode, _multicastAddress); }
/// <summary> /// Open to receive messages on specified port and (optionally) from specified multicast IP address. /// Returns success status. /// </summary> public bool Open(int port, string multicastAddress = "") { // Close and garbage existing receiver if (_udpClient != null) { Close(); } // Validate port number range if (port < OscHelper.portMin || port > OscHelper.portMax) { Debug.LogWarning("<b>[OscIn]</b> Open failed. Port " + port + " is out of range." + Environment.NewLine); return(false); } _port = port; // Derive mode from multicastAddress if (!string.IsNullOrEmpty(multicastAddress)) { if (Regex.IsMatch(multicastAddress, OscHelper.multicastAddressPattern)) { _mode = OscReceiveMode.UnicastBroadcastMulticast; _multicastAddress = multicastAddress; } else { Debug.LogWarning("<b>[OscIn]</b> Open failed. Multicast IP address " + multicastAddress + " is out not valid. It must be in range 224.0.0.0 to 239.255.255.255." + Environment.NewLine); return(false); } } else { _mode = OscReceiveMode.UnicastBroadcast; } // Attempt to open socket try { _udpClient = new UdpClient(); // Ensure that we can have multiple OscIn objects listening to the same port. Must be set before bind. // Note that only one OscIn object will receive the packet anyway: http://stackoverflow.com/questions/22810511/bind-multiple-listener-to-the-same-port _udpClient.ExclusiveAddressUse = false; _udpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); // Bind the socket to the endpoint IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Any, port); _udpClient.Client.Bind(ipEndPoint); // Join multicast group if in multicast mode if (_mode == OscReceiveMode.UnicastBroadcastMulticast) { IPAddress multicastIp = IPAddress.Parse(_multicastAddress); _udpClient.JoinMulticastGroup(multicastIp, OscHelper.timeToLiveMax); } // Begin recieve UdpState udpState = new UdpState(ipEndPoint, _udpClient); _callback = new AsyncCallback(EndReceive); _isReceiving = true; _udpClient.BeginReceive(_callback, udpState); } catch (Exception e) { // Socket error reference: https://msdn.microsoft.com/en-us/library/windows/desktop/ms740668(v=vs.85).aspx if (e is SocketException && (e as SocketException).ErrorCode == 10048) // "Address already in use" { Debug.LogWarning("<b>[OscIn]</b> Could not open port " + _port + " because another application is listening on it." + Environment.NewLine); } else if (e is SocketException && _mode == OscReceiveMode.UnicastBroadcastMulticast) { Debug.LogWarning("<b>[OscIn]</b> Could not subscribe to multicast group. Perhaps you are offline, or your router is not multicast enabled." + Environment.NewLine + (e as SocketException).ErrorCode + ": " + e.ToString()); } else if (e.Data is ArgumentOutOfRangeException) { Debug.LogWarning(string.Format("[OscIn] Could not open port {0}. Invalid Port Number.\n{1}", _port, e.ToString())); } else { //Debug.Log( e ); } Close(); return(false); } // Deal with the success if (Application.isPlaying) { string logString; if (_mode == OscReceiveMode.UnicastBroadcast) { logString = "[OscIn] Listening for unicast and broadcast messages on port " + _port + Environment.NewLine; } else { logString = "<b>[OscIn]</b> Listening for multicast messages on address " + _multicastAddress + ", unicast and broadcast messages on port " + _port + Environment.NewLine; } //logString += "Buffer size: " + _udpClient.Client.ReceiveBufferSize + " bytes. " + Environment.NewLine; Debug.Log(logString); } _port = port; return(true); }
/// <summary> /// Open to receive messages on specified port and (optionally) from specified multicast IP address. /// Returns success status. /// </summary> public bool Open(int port, string multicastAddress = "") { // Ensure that we have a receiver, even when not in Play mode. if (_receiver == null) { _receiver = new UdpReceiver(OnDataReceivedAsync); } // Close and existing receiver. if (isOpen) { Close(); } // Validate port number range. if (port < OscConst.portMin || port > OscConst.portMax) { StringBuilder sb = OscDebug.BuildText(this); sb.Append("Open failed. Port "); sb.Append(port); sb.Append(" is out of range.\n"); Debug.LogWarning(sb.ToString()); return(false); } _port = port; // Derive mode from multicastAddress. IPAddress multicastIP; if (!string.IsNullOrEmpty(multicastAddress) && IPAddress.TryParse(multicastAddress, out multicastIP)) { if (Regex.IsMatch(multicastAddress, OscConst.multicastAddressPattern)) { _mode = OscReceiveMode.UnicastBroadcastMulticast; _multicastAddress = multicastAddress; } else { StringBuilder sb = OscDebug.BuildText(this); sb.Append("Open failed. Multicast IP address "); sb.Append(multicastAddress); sb.Append(" is out not valid. It must be in range 224.0.0.0 to 239.255.255.255.\n"); Debug.LogWarning(sb.ToString()); return(false); } } else { _multicastAddress = string.Empty; _mode = OscReceiveMode.UnicastBroadcast; } // Set buffer size. _receiver.bufferSize = _udpBufferSize; // Try open. if (!_receiver.Open(_port, _multicastAddress)) { Debug.Log("Failed to open"); return(false); } // Deal with the success if (Application.isPlaying) { StringBuilder sb = OscDebug.BuildText(this); if (_mode == OscReceiveMode.UnicastBroadcast) { sb.Append("Ready to receive unicast and broadcast messages on port "); } else { sb.Append("Ready to receive multicast messages on address "); sb.Append(_multicastAddress); sb.Append(", unicast and broadcast messages on port "); } sb.Append(_port); sb.AppendLine(); Debug.Log(sb.ToString()); } return(true); }
void OnModeChanged(int modeInt) { mode = (OscReceiveMode)modeInt; Open(port, mode, multicastAddress); }