///////////////////////////////////////////////////////////////////////////// // A function responsible for decoding and reacting to received UDP data. private bool InterpreteData(string message) { if (!string.IsNullOrEmpty(message)) { message = EncodeUtilities.StripSplitter(message); if (this.lastMessage != message) { this.lastMessage = message; #if DEBUG DebugUtilities.UniversalDebug(this.sourceName, "New message found: " + message); #endif string[] messageComponents = message.Split(new string[] { EncodeUtilities.headerSplitter }, 2, StringSplitOptions.RemoveEmptyEntries); if (messageComponents.Length > 1) { string header = messageComponents[0], content = messageComponents[1]; #if DEBUG DebugUtilities.UniversalDebug(this.sourceName, "Header: " + header + ", content: " + content); #endif if (header == "MESHSTREAMING") { InterpreteMesh(content, SourceType.UDP); return(true); } else if (header == "CONTROLLER") { InterpreteRobotController(content); return(true); } else if (header == "HOLOTAG") { InterpreteTag(content); return(true); } else if (header == "IPADDRESS") { InterpreteIPAddress(content); return(true); } else { #if DEBUGWARNING DebugUtilities.UniversalWarning(this.sourceName, "Header Not Recognized"); #endif } } else { #if DEBUGWARNING DebugUtilities.UniversalWarning(this.sourceName, "Improper message"); #endif } } } return(true); // Since we have one interpreter anyway }
// Disable connection. public async void Disconnect() { // Reset. if (this.client != null) { this.client.Dispose(); this.client = null; // Good Practice? #if DEBUG DebugUtilities.UniversalDebug(this.sourceName, "Stopping Client.", ref this.debugMessages); #endif } }
// Collect environment meshes public List <byte[]> EncodeEnvironmentMesh(out string currentMessage) { currentMessage = string.Empty; List <byte[]> data = new List <byte[]>(); FindMeshes(); if (this.scannedEnvironment.Count > 0) { // Combine meshes CombineInstance[] combineStructure = new CombineInstance[this.scannedEnvironment.Count]; int i = 0; foreach (MeshRenderer meshRenderer in this.scannedEnvironment) { MeshFilter meshFilter = meshRenderer.gameObject.GetComponent <MeshFilter>(); combineStructure[i].mesh = meshFilter.sharedMesh; combineStructure[i].transform = meshRenderer.transform.localToWorldMatrix; i++; } Mesh mesh = new Mesh(); mesh.CombineMeshes(combineStructure); // Encode mesh MeshData meshData = MeshUtilities.EncodeMesh(mesh); byte[] localData = EncodeUtilities.EncodeData("ENVIRONMENT", meshData, out string currentLocalMessage); data.Add(localData); #if DEBUG DebugUtilities.UniversalDebug(this.sourceName, "Mesh Encoding: " + currentLocalMessage); #endif currentMessage = currentLocalMessage; // // Encode meshes separately // // { // // MeshRenderer meshRenderer = this.scannedEnvironment[0]; // foreach (MeshRenderer meshRenderer in this.scannedEnvironment) { // MeshFilter meshFilter = meshRenderer.gameObject.GetComponent<MeshFilter>(); // MeshData meshData = MeshUtilities.EncodeMesh(meshFilter.sharedMesh); // byte[] localData = EncodeUtilities.EncodeData("ENVIRONMENT", meshData, out string currentLocalMessage); // #if DEBUG // DebugUtilities.UniversalDebug(this.sourceName, "Mesh Encoding: " + currentLocalMessage); // #endif // data.Add(localData); // currentMessage += currentLocalMessage; // } } else { #if DEBUG DebugUtilities.UniversalDebug(this.sourceName, "No meshes found."); #endif } return(data); }
void OnEnable() { DebugUtilities.UserMessage("Hollo World . . ."); #if UNITY_ANDROID Thread.Sleep(1500); #endif DebugUtilities.UserMessage("Your IP is:\n" + NetworkUtilities.LocalIPAddress()); #if UNITY_ANDROID Thread.Sleep(3500); #endif DebugUtilities.UserMessage("Place your CPlane by tapping on scanned mesh."); }
void Update() { if (this.udpReceiver.dataMessages.Count > 0) { #if DEBUG DebugUtilities.UniversalDebug(this.sourceName, "Parsing input . . ."); #endif // Try to interprete message. If recognized - remove from the queue. if (InterpreteData(this.udpReceiver.dataMessages.Peek())) //[this.udpReceiver.dataMessages.Count-1]); { this.udpReceiver.dataMessages.Dequeue(); } } }
////////////////////////////////////////////////////////////////////////// #if WINDOWS_UWP private async void StartListenning(int _localPort) { if (this.localPort != _localPort) { this.localPort = _localPort; } // Start the thread. this.connectionCancellationTokenSource = new CancellationTokenSource(); this.connectionReceiver = new Task(() => ReceiveConnection(), this.connectionCancellationTokenSource.Token); this.connectionReceiver.Start(); #if DEBUG DebugUtilities.UniversalDebug(this.sourceName, "Client receivng thread Started.", ref this.debugMessages); #endif }
// - IP address private void InterpreteIPAddress(string data) { string remoteIP = EncodeUtilities.InterpreteIPAddress(data); #if DEBUG DebugUtilities.UniversalDebug(this.sourceName, "Remote IP: " + remoteIP); #endif // TODO: Add ip integrity check // TODO: Should not be stored in udp sender. UDPSendComponent sender = gameObject.GetComponent <UDPSendComponent>(); sender.remoteIP = remoteIP; UDPReceiveComponent.flagUICommunicationStarted = true; // Inform UI Manager. ParameterUIMenu.instance.OnValueChanged(); }
// When splitter is found - react to it. private void ReactToMessage() { string[] messages = this.currentHistory.Split(new string[] { EncodeUtilities.messageSplitter }, StringSplitOptions.RemoveEmptyEntries); int index = (messages.Length > 1) ? messages.Length - 2 : 0; string receiveString = messages[index]; this.currentHistory = (messages.Length > 1) ? messages[index + 1] : string.Empty; if (!string.IsNullOrEmpty(receiveString)) { #if DEBUG2 DebugUtilities.UniversalDebug(this.sourceName, "Total Data found: " + receiveString, ref this.debugMessages); #endif this.dataMessages.Enqueue(receiveString); } }
////////////////////////////////////////////////////////////////////////// #if WINDOWS_UWP private async void StartReceiving() { // Reset. // Start receiving. this.client = new DatagramSocket(); this.client.MessageReceived += ReceiveData; try { await client.BindEndpointAsync(new HostName(NetworkUtilities.LocalIPAddress()), this.localPort.ToString()); } catch (Exception exception) { #if DEBUGWARNING DebugUtilities.UniversalWarning(this.sourceName, "Exception: " + exception.ToString() + ":" + SocketError.GetStatus(exception.HResult).ToString(), ref this.debugMessages); #endif } #if DEBUG DebugUtilities.UniversalDebug(this.sourceName, "Client receivng thread Started.", ref this.debugMessages); #endif }
// Stop Connection. public void Disconnect() { // Reset. if (this.client != null) { this.client.Close(); this.client = null; } if (this.stream != null) { this.stream.Close(); this.stream = null; // Good Practice? } StopSending(); #if DEBUG DebugUtilities.UniversalDebug(this.sourceName, "DisflagConnected.", ref this.debugMessages); #endif }
// If clicked on mesh - try to snap if the object is currently placing. // NB! check the distance if the object is hovering (not on mesh) - ignore click. public bool OnTrySnap() { if ((this.flagPlacing) && (InteractionManager.instance.flagHitGaze)) { float distance = Distance2Camera(InteractionManager.instance.hitGaze.point); if (distance < this.maxSnapDistance) { this.flagPlacing = false; UpdateAppearance(); return(true); } } else { DebugUtilities.UserMessage("Try to look at scanned mesh or come closer to it."); } return(false); }
public void OnTap() { // if (!this.flagSelected) { // this.historyPosition = new List<Vector3>(); // this.historyNormal = new List<Vector3>(); // } this.flagTapped = true; this.flagSelected = !this.flagSelected; #if DEBUG DebugUtilities.UniversalDebug(sourceName, "Tapped: New State: " + this.flagSelected); #endif if ((this.flagSelected) && (OnStartPlacing != null)) { OnStartPlacing(); } }
//////////////////////////////////////////////////////////////////////// // Establish Connection public bool Connect() { // Reset. Disconnect(); this.client = new TcpClient(); this.flagConnected = false; try { // Open. if (!this.client.ConnectAsync(this.remoteIP, this.remotePort).Wait(this.timeout)) { // connection failure #if DEBUGWARNING DebugUtilities.UniversalWarning(this.sourceName, "Failed to connect", ref this.debugMessages); #endif return(false); } this.stream = this.client.GetStream(); this.flagConnected = true; StartSending(); // Acknowledge. #if DEBUG DebugUtilities.UniversalDebug(this.sourceName, "Connection Stablished!", ref this.debugMessages); #endif return(true); } catch (ArgumentNullException exception) { // Exception. #if DEBUGWARNING DebugUtilities.UniversalWarning(this.sourceName, "ArgumentNullException: " + exception.ToString(), ref this.debugMessages); #endif return(false); } catch (SocketException exception) { // Exception. #if DEBUGWARNING DebugUtilities.UniversalWarning(this.sourceName, "SocketException: " + exception.ToString(), ref this.debugMessages); #endif return(false); } catch (Exception exception) { #if DEBUGWARNING DebugUtilities.UniversalWarning(this.sourceName, "UnhandledException: " + exception.ToString(), ref this.debugMessages); #endif return(false); } }
//////////////////////////////////////////////////////////////////////// // If c plane is not found - hint user and return false. public bool CheckCPlane() { if (this.cPlane == null) { this.cPlane = GameObject.FindGameObjectWithTag(this.tagCPlane); if (this.cPlane == null) { DebugUtilities.UserMessage("Place your CPlane by tapping on scanned mesh."); return(false); } #if DEBUG DebugUtilities.UniversalDebug(this.sourceName, "CPlane: " + this.cPlane); #endif } // #if UNITY_ANDROID // HoloFabARController.cPlaneInstance = this.cPlane; // #endif return(true); }
// Constantly check for new messages on given port. private async void ReceiveData(DatagramSocket sender, DatagramSocketMessageReceivedEventArgs args) { //Read the message that was received from the UDP client. DataReader reader = args.GetDataReader(); string receiveString = reader.ReadString(reader.UnconsumedBufferLength).Trim(); // If string not empty and not read yet - react to it. if (!string.IsNullOrEmpty(receiveString)) { #if DEBUG2 DebugUtilities.UniversalDebug(this.sourceName, "Total Data found: " + receiveString, ref this.debugMessages); #endif this.dataMessages.Enqueue(receiveString); this.connectionHistory.Enqueue(args.RemoteAddress.RawName); if (OnReceive != null) { OnReceive(); } } }
// Constantly check for new messages on given port. private async void ReceiveConnection() { try { // Open. this.listener = new StreamSocketListener(); this.listener.ConnectionReceived += OnClientFound; await this.listener.BindServiceNameAsync(this.localPort.ToString()); #if DEBUG DebugUtilities.UniversalDebug(this.sourceName, "Started Listening for Incoming Connections.", ref this.debugMessages); #endif } catch (Exception exception) { // Exception. SocketErrorStatus webErrorStatus = SocketError.GetStatus(exception.GetBaseException().HResult); string webError = (webErrorStatus.ToString() != "Unknown") ? webErrorStatus.ToString() : exception.Message; #if DEBUGWARNING DebugUtilities.UniversalWarning(this.sourceName, "Exception: " + webError, ref this.debugMessages); #endif } }
void Update() { // if (!this.tcpReceiver.flagConnectionFound) { // #if DEBUGWARNING && DEBUG2 // DebugUtilities.UniversalWarning(this.sourceName, "Connection not Found."); // #endif // this.tcpReceiver.Connect(); // if (!this.tcpReceiver.flagConnectionFound) return; // } if (this.tcpReceiver.dataMessages.Count > 0) { #if DEBUG DebugUtilities.UniversalDebug(this.sourceName, "Parsing input . . ."); #endif // Try to interprete message. If recognized - remove from the queue. if (InterpreteData(this.tcpReceiver.dataMessages.Peek())) //[this.tcpReceiver.dataMessages.Count-1]); { this.tcpReceiver.dataMessages.Dequeue(); } } }
// Disable connection. public void Disconnect() { // Reset. if (this.receiveThread != null) { this.receiveThread.Abort(); this.receiveThread = null; // Good Practice? this.debugMessages.Add("UDPReceive: Stopping Thread."); #if DEBUG DebugUtilities.UniversalDebug(this.sourceName, "Stopping Connection Reception Thread.", ref this.debugMessages); #endif } if (this.client != null) { this.client.Close(); this.client = null; // Good Practice? #if DEBUG DebugUtilities.UniversalDebug(this.sourceName, "Stopping Client.", ref this.debugMessages); #endif } }
// Check the queue and try send it. public void SendFromQueue() { try { if (this.IsNotEmpty) { byte[] currentData; lock (this.sendQueue) currentData = this.sendQueue.Dequeue(); // Peek message to send Send(currentData); // if no exception caught and data sent successfully - remove from queue. // if (!this.flagSuccess) // lock (this.sendQueue) // this.sendQueue.Dequeue(currentData); } } catch (Exception exception) { #if DEBUGWARNING DebugUtilities.UniversalWarning(this.sourceName, "Queue Exception: " + exception.ToString(), ref this.debugMessages); #endif } }
// Start a connection and send given byte array. private void Send(byte[] sendBuffer) { try { if (!this.client.Connected) { #if DEBUG DebugUtilities.UniversalDebug(this.sourceName, "Client DisflagConnected!", ref this.debugMessages); #endif this.flagSuccess = false; } // Write. this.stream.Write(sendBuffer, 0, sendBuffer.Length); // Acknowledge. #if DEBUG DebugUtilities.UniversalDebug(this.sourceName, "Data Sent!", ref this.debugMessages); #endif this.flagSuccess = true; return; } catch (ArgumentNullException exception) { // Exception. #if DEBUG DebugUtilities.UniversalDebug(this.sourceName, "ArgumentNullException: " + exception.ToString(), ref this.debugMessages); #endif this.flagSuccess = false; } catch (SocketException exception) { // Exception. #if DEBUG DebugUtilities.UniversalDebug(this.sourceName, "SocketException: " + exception.ToString(), ref this.debugMessages); #endif this.flagSuccess = false; } catch (Exception exception) { // Exception. #if DEBUG DebugUtilities.UniversalDebug(this.sourceName, "Exception: " + exception.ToString(), ref this.debugMessages); #endif this.flagSuccess = false; } this.flagSuccess = false; }
// Constantly check for new messages on given port. private void ReceiveData() { IPEndPoint anyIP = new IPEndPoint(IPAddress.Any, 0); byte[] data; string receiveString; try { // Receive Bytes. data = this.client.Receive(ref anyIP); if (data.Length > 0) { // If buffer not empty - decode it. receiveString = EncodeUtilities.DecodeData(data); // If string not empty and not read yet - react to it. if (!string.IsNullOrEmpty(receiveString)) { #if DEBUG2 DebugUtilities.UniversalDebug(this.sourceName, "Total Data found: " + receiveString, ref this.debugMessages); #endif this.dataMessages.Enqueue(receiveString); this.connectionHistory.Enqueue(anyIP.Address.ToString()); if (OnReceive != null) { OnReceive(); } } } } catch (SocketException exception) { // SocketException. #if DEBUGWARNING DebugUtilities.UniversalWarning(this.sourceName, "SocketException: " + exception.ToString(), ref this.debugMessages); #endif } catch (Exception exception) { // Exception. #if DEBUGWARNING DebugUtilities.UniversalWarning(this.sourceName, "Exception: " + exception.ToString(), ref this.debugMessages); #endif } }
//////////////////////////////////////////////////////////////////////// // Find environment meshes public void FindMeshes() { this.scannedEnvironment = new List <MeshRenderer>(); #if WINDOWS_UWP // Microsoft Windows MRTK // Cast the Spatial Awareness system to IMixedRealityDataProviderAccess to get an Observer var access = CoreServices.SpatialAwarenessSystem as IMixedRealityDataProviderAccess; // Get the first Mesh Observer available, generally we have only one registered var observers = access.GetDataProviders <IMixedRealitySpatialAwarenessMeshObserver>(); // Loop through all known Meshes foreach (var observer in observers) { foreach (SpatialAwarenessMeshObject meshObject in observer.Meshes.Values) { this.scannedEnvironment.Add(meshObject.Renderer); } } #elif UNITY_ANDROID // Android ARkit PointcloudVisualizer[] visualizers = FindObjectsOfType <PointcloudVisualizer>(); foreach (PointcloudVisualizer visualizer in visualizers) { this.scannedEnvironment.Add(visualizer.gameObject.GetComponent <MeshRenderer>()); } #elif UNITY_EDITOR GameObject[] goItems = GameObject.FindObjectsOfType(typeof(GameObject)) as GameObject[]; //will return an array of all GameObjects in the scene foreach (GameObject goItem in goItems) { if (goItem.layer == LayerMask.NameToLayer(this.layerScanMesh)) { this.scannedEnvironment.Add(goItem.GetComponent <MeshRenderer>()); } } #endif #if DEBUG DebugUtilities.UniversalDebug(this.sourceName, "Meshes found: " + this.scannedEnvironment.Count); #endif }
private void SendData() { if (this.tcpSender != null) { List <byte[]> environmentData = ObjectManager.instance.EncodeEnvironmentMesh(out string currentMessage); if ((environmentData.Count > 0) && (!string.IsNullOrEmpty(currentMessage)) && (TCPSendComponent.lastMessage != currentMessage)) { TCPSendComponent.lastMessage = currentMessage; foreach (byte[] data in environmentData) { this.tcpSender.QueueUpData(data); } } else { #if DEBUG DebugUtilities.UniversalDebug(this.sourceName, "Mesh is already sent or no mesh is found."); #endif } } }
public void SendUI(byte[] data) { if (!string.IsNullOrEmpty(this.remoteIP)) // just in case { if ((this.udpSender == null) || (this.udpSender.remoteIP != this.remoteIP)) { this.udpSender = new UDPSend(this.remoteIP, this.remotePortOverride); this.udpSender.Connect(); } this.udpSender.QueueUpData(data); // if (!this.udpSender.success) { // #if DEBUGWARNING // DebugUtilities.UniversalWarning(this.sourceName, "Couldn't send data."); // #endif // } } else { #if DEBUGWARNING DebugUtilities.UniversalWarning(this.sourceName, "No server IP Found - enable Grasshopper UI Receiving Component"); #endif } }
// - IP address private void InterpreteIPAddress(string data) { string remoteIP = EncodeUtilities.InterpreteIPAddress(data); #if DEBUG DebugUtilities.UniversalDebug(this.sourceName, "Remote IP: " + remoteIP); #endif // TODO: Add ip integrity check // - UDP // TODO: Should not be stored in udp sender. UDPSendComponent udpSender = gameObject.GetComponent <UDPSendComponent>(); if (udpSender != null) { udpSender.remoteIP = remoteIP; UDPReceiveComponent.flagUICommunicationStarted = true; // Inform UI Manager. ParameterUIMenu.instance.OnValueChanged(); } // - TCP TCPSendComponent tcpSender = gameObject.GetComponent <TCPSendComponent>(); #if DEBUG DebugUtilities.UniversalDebug(this.sourceName, "Tcp sender found. Sending mesh to: " + remoteIP); #endif if (tcpSender != null) { tcpSender.UpdateIP(remoteIP); // byte[] environmentData = ObjectManager.instance.EnvironmentMesh(); // // Echo Environment Mesh // if (environmentData != null) { // tcpSender.remoteIP = remoteIP; // tcpSender.SendMesh(environmentData); // UDPReceiveComponent.flagEnvironmentSent = true; // } } }
//////////////////////////////////////////////////////////////////////// #if WINDOWS_UWP // Start a connection and send given byte array. private async void Send(byte[] sendBuffer) { this.flagSuccess = false; // Stop client if set previously. if (this.client != null) { this.client.Dispose(); this.client = null; // Good Practice? } try { // Open new one. this.client = new DatagramSocket(); // Write. using (var stream = await this.client.GetOutputStreamAsync(new HostName(this.remoteIP), this.remotePort.ToString())) { using (DataWriter writer = new DataWriter(stream)) { writer.WriteBytes(sendBuffer); await writer.StoreAsync(); } } // Close. this.client.Dispose(); this.client = null; // Good Practice? // Acknowledge. #if DEBUG DebugUtilities.UniversalDebug(this.sourceName, "Data Sent!", ref this.debugMessages); #endif this.flagSuccess = true; return; } catch (Exception exception) { // Exception. #if DEBUGWARNING DebugUtilities.UniversalWarning(this.sourceName, "Exception: " + exception.ToString(), ref this.debugMessages); #endif } }
// Start a connection and send given byte array. public async void Send(byte[] sendBuffer) { this.flagSuccess = false; try { if ((this.client != null) && this.flagConnected) { // Write. using (Stream outputStream = this.client.OutputStream.AsStreamForWrite()) { await outputStream.WriteAsync(sendBuffer, 0, sendBuffer.Length, this.sendCancellationTokenSource.Token); } // Acknowledge. #if DEBUG DebugUtilities.UniversalDebug(this.sourceName, "Data Sent!", ref this.debugMessages); #endif this.flagSuccess = true; } else { #if DEBUGWARNING DebugUtilities.UniversalWarning(this.sourceName, "Not connected", ref this.debugMessages); #endif this.flagConnected = false; } } catch (TaskCanceledException) { // timeout #if DEBUGWARNING DebugUtilities.UniversalWarning(this.sourceName, "Connection timed out!", ref this.debugMessages); #endif this.flagConnected = false; } catch (Exception exception) { // Exception. #if DEBUGWARNING DebugUtilities.UniversalWarning(this.sourceName, "Exception: " + exception.ToString(), ref this.debugMessages); #endif } }
public static void UniversalDebug(string source, string message, MessageType messageType = MessageType.Normal) { DebugUtilities.UniversalDebug("[" + DateTime.Now.ToString() + "]" + source + ": " + message, messageType); }
// Constantly check for new messages on given port. private void ReceiveData() { try { // Open. IPEndPoint anyIP = new IPEndPoint(IPAddress.Any, this.localPort); this.listener = new TcpListener(anyIP); this.listener.Start(); this.currentHistory = ""; // Infinite loop. while (true) { if (this.client == null) { #if DEBUG DebugUtilities.UniversalDebug(this.sourceName, "Listening for a Client!", ref this.debugMessages); #endif this.client = this.listener.AcceptTcpClient(); this.stream = this.client.GetStream(); } else { try { if (this.stream.DataAvailable) { OnClientFound(); #if DEBUG DebugUtilities.UniversalDebug(this.sourceName, "Reading Data: " + this.client.Available.ToString(), ref this.debugMessages); #endif } else { if (this.IsConnected) { #if DEBUGWARNING DebugUtilities.UniversalWarning(this.sourceName, "No Data Available!", ref this.debugMessages); #endif } else { #if DEBUGWARNING DebugUtilities.UniversalWarning(this.sourceName, "Client Disconnected", ref this.debugMessages); #endif this.stream.Close(); this.client.Close(); this.client = null; } } } catch (Exception e) { this.stream.Close(); this.client.Close(); this.client = null; } } } } catch (SocketException exception) { // SocketException. #if DEBUGWARNING DebugUtilities.UniversalWarning(this.sourceName, "SocketException: " + exception.ToString(), ref this.debugMessages); #endif } catch (Exception exception) { // Exception. #if DEBUGWARNING DebugUtilities.UniversalWarning(this.sourceName, "Exception: " + exception.ToString(), ref this.debugMessages); #endif } // TODO: Shouldn't it close in case of error? // finally { // this.Disconnect(); // } }
public static void UniversalDebug(string source, string message, ref List <string> log, MessageType messageType = MessageType.Normal) { message = "[" + DateTime.Now.ToString() + "]" + source + ": " + message; log.Add(message); DebugUtilities.UniversalDebug(message, messageType); }