void SendThreadProc() { var stream = m_TcpClient.GetStream(); while (!m_ExitRequested) { var collaborationData = new ARCollaborationData(); int queueSize = 0; lock (m_CollaborationDataSendQueue) { if (m_CollaborationDataSendQueue.Count > 0) { collaborationData = m_CollaborationDataSendQueue.Dequeue(); } queueSize = m_CollaborationDataSendQueue.Count; } if (collaborationData.valid) { using (collaborationData) { SendData(stream, collaborationData.bytes); } } if (queueSize == 0) { // If there's nothing else in the queue at the moment, // then go to sleep for a bit. // Otherwise, immediately try to send the next one. Thread.Sleep(1); } } }
void SendThreadProc() { var stream = m_TcpClient.GetStream(); while (!m_ExitRequested) { var collaborationData = new ARCollaborationData(); int queueSize = 0; lock (m_CollaborationDataSendQueue) { if (m_CollaborationDataSendQueue.Count > 0) { collaborationData = m_CollaborationDataSendQueue.Dequeue(); } queueSize = m_CollaborationDataSendQueue.Count; } if (collaborationData.valid) { // Serialize the collaboration data to a byte array SerializedARCollaborationData serializedData; using (collaborationData) { // ARCollaborationData can be diposed after being serialized to bytes. serializedData = collaborationData.ToSerialized(); } using (serializedData) { // Get the raw data as a NativeSlice var collaborationBytes = serializedData.bytes; // Construct the message header var header = new MessageHeader { messageSize = collaborationBytes.Length, messageType = MessageType.CollaborationData }; // Send the header followed by the ARCollaborationData bytes m_WriteBuffer.Send(stream, header); m_WriteBuffer.Send(stream, collaborationBytes); Logger.Log($"Sent {collaborationBytes.Length} bytes of collaboration data."); } } if (queueSize == 0) { // If there's nothing else in the queue at the moment, // then go to sleep for a bit. // Otherwise, immediately try to send the next one. Thread.Sleep(1); } } }
void Update() { var subsystem = GetSubsystem(); if (subsystem == null) { return; } // Check for new collaboration data while (subsystem.collaborationDataCount > 0) { using (var collaborationData = subsystem.DequeueCollaborationData()) { CollaborationNetworkingIndicator.NotifyHasCollaborationData(); if (m_MCSession.ConnectedPeerCount == 0) { continue; } using (var serializedData = collaborationData.ToSerialized()) using (var data = NSData.CreateWithBytesNoCopy(serializedData.bytes)) { m_MCSession.SendToAllPeers(data, collaborationData.priority == ARCollaborationDataPriority.Critical ? MCSessionSendDataMode.Reliable : MCSessionSendDataMode.Unreliable); CollaborationNetworkingIndicator.NotifyOutgoingDataSent(); } } } // Check for incoming data while (m_MCSession.ReceivedDataQueueSize > 0) { CollaborationNetworkingIndicator.NotifyIncomingDataReceived(); using (var data = m_MCSession.DequeueReceivedData()) using (var collaborationData = new ARCollaborationData(data.Bytes)) { if (collaborationData.valid) { subsystem.UpdateWithCollaborationData(collaborationData); } } } }
unsafe void ReadThreadProc() { var stream = m_TcpClient.GetStream(); while (!m_ExitRequested) { if (stream.DataAvailable) { var lengthBytes = ReadBytes(stream, sizeof(int)); int expectedLength = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(lengthBytes, 0)); if (expectedLength <= 0) { Logger.Log($"Warning: received data of length {expectedLength}. Ignoring."); } else { // Read incomming stream into byte arrary. var collaborationBytes = ReadBytes(stream, expectedLength); var collaborationData = new ARCollaborationData(collaborationBytes); if (collaborationData.valid) { lock (m_CollaborationDataReadQueue) { m_CollaborationDataReadQueue.Enqueue(collaborationData); } } else { Logger.Log($"Received {expectedLength} bytes from remote host, but the collaboration data was not valid."); } } } Thread.Sleep(1); } }
void Update() { var subsystem = GetSubsystem(); if (subsystem == null) { return; } // Check for new collaboration data while (subsystem.collaborationDataCount > 0) { using (var collaborationData = subsystem.DequeueCollaborationData()) { CollaborationNetworkingIndicator.NotifyHasCollaborationData(); if (m_MCSession.ConnectedPeerCount == 0) { continue; } using (var serializedData = collaborationData.ToSerialized()) using (var data = NSData.CreateWithBytesNoCopy(serializedData.bytes)) { m_MCSession.SendToAllPeers(data, collaborationData.priority == ARCollaborationDataPriority.Critical ? MCSessionSendDataMode.Reliable : MCSessionSendDataMode.Unreliable); CollaborationNetworkingIndicator.NotifyOutgoingDataSent(); // Only log 'critical' data as 'optional' data tends to come every frame if (collaborationData.priority == ARCollaborationDataPriority.Critical) { Logger.Log($"Sent {data.Length} bytes of collaboration data."); } } } } // Check for incoming data while (m_MCSession.ReceivedDataQueueSize > 0) { CollaborationNetworkingIndicator.NotifyIncomingDataReceived(); using (var data = m_MCSession.DequeueReceivedData()) using (var collaborationData = new ARCollaborationData(data.Bytes)) { if (collaborationData.valid) { subsystem.UpdateWithCollaborationData(collaborationData); if (collaborationData.priority == ARCollaborationDataPriority.Critical) { Logger.Log($"Received {data.Bytes.Length} bytes of collaboration data."); } } else { Logger.Log($"Received {data.Bytes.Length} bytes from remote, but the collaboration data was not valid."); } } } }