// 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 } }
// 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 } }
// 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 } }
// 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; }
//////////////////////////////////////////////////////////////////////// // 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 } } }
// - 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 } }
// Check the queue and try send it. private 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.Enqueue(currentData); // } } } catch (Exception exception) { #if DEBUG DebugUtilities.UniversalDebug(this.sourceName, "Queue Exception: " + exception.ToString(), ref this.debugMessages); #endif this.flagSuccess = false; } }
// 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 } }
// 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); }
public static void UniversalDebug(string source, string message, MessageType messageType = MessageType.Normal) { DebugUtilities.UniversalDebug("[" + DateTime.Now.ToString() + "]" + source + ": " + message, messageType); }