private IEnumerator SendQueue() { isSendingQueue = true; while (sendQueue.Count > 0) { string message = sendQueue.Dequeue(); try { DreamLogs.Log("<color=blue>Sending data by queue: {0}</color>", message); byte[] data = Encoding.UTF8.GetBytes(message); sendClient.Send(data, data.Length, remoteEndPoint); } catch (Exception err) { Debug.LogWarning(err.ToString()); } yield return(new WaitForSeconds(0.05f)); } isSendingQueue = false; if (onCompleteQueue != null) { onCompleteQueue(); } }
private void SerializeMessage(string message) { try { string[] chain = message.Split(' '); string key = chain[0]; float value = 0; if (float.TryParse(chain[1], out value)) { Action <float> callback = null; if (floatSubscribers.TryGetValue(key, out callback)) { callback(value); } else { if (!logException.Contains(key)) { DreamLogs.LogWarning("<color=#FFFF00>There's no callback subscribed to '{0}'</color>", key); } } } else { Action <string> callback = null; if (stringSubscribers.TryGetValue(key, out callback)) { callback(chain[1]); } else { if (!logException.Contains(key)) { DreamLogs.LogWarning("<color=#FFFF00>There's no callback subscribed to '{0}'</color>", key); } } } if (!logException.Contains(key)) { DreamLogs.Log("<color=green>Receive data: {0}</color>", message); } } catch (Exception e) { DreamLogs.LogWarning("<color=#FFFF00>Cannot deserialize message '{0}', reason: {1}</color>", message, e.Message); } }
private void TryKillThread() { if (isInitialized) { receiveThread.Abort(); receiveThread = null; sendClient.Close(); sendClient = null; receiveClient.Close(); receiveClient = null; DreamLogs.Log("Thread killed: {0}", receiveThread); onCompleteQueue = null; isInitialized = false; } }
private void SendStringImmediatly(string message, int port) { try { DreamLogs.Log("<color=red>Sending data immediatly: {0}</color>", message); byte[] data = Encoding.UTF8.GetBytes(message); if (port > 0) { sendClient.Send(data, data.Length, new IPEndPoint(IPAddress.Parse(ip), port)); } else { sendClient.Send(data, data.Length, remoteEndPoint); } } catch (Exception err) { Debug.LogWarning(err.ToString()); } }