Example #1
0
    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();
        }
    }
Example #2
0
 public static void Remove(string key, Action <string> callback)
 {
     if (instance.stringSubscribers.ContainsKey(key))
     {
         instance.stringSubscribers.Remove(key);
     }
     else
     {
         DreamLogs.LogWarning("<color=#FFFF00>There's no callback subscribed to '{0}'</color>", key);
     }
 }
Example #3
0
    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);
        }
    }
Example #4
0
    private void ReceiveDataListener()
    {
        while (true)
        {
            try
            {
                byte[] data = receiveClient.Receive(ref receiveEndPoint);
                string text = Encoding.UTF8.GetString(data);

                SerializeMessage(text);
            }
            catch (Exception ex)
            {
                DreamLogs.LogWarning(ex.ToString());
            }
        }
    }
Example #5
0
    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;
        }
    }
Example #6
0
    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());
        }
    }