Skip to main content
fixed spell, added how to run in background
Source Link
Dr.YSG
  • 179
  • 1
  • 8

Well, I stumped our local unity expert also, but we did a debug session in my office and aferafter about an hour, we found out it is not the code. The Unity IDE will report console messages even focus is off the window. But our test program was an external exe (Blaster.exe) which is a console program. So it looked like the messages were arriving and the update was not running. That was not the case, it was just that the IDE lost focus. I cleaned up my code to use a Synchrhonized Queue, so now all should be very thread safe.

BTW: The problem with IDE stopping running when it loses focus can be solved by:

    Application.runInBackground = true;

...

Well, I stumped our local unity expert also, but we did a debug session in my office and afer about an hour, we found out it is not the code. The Unity IDE will report console messages even focus is off the window. But our test program was an external exe (Blaster.exe) which is a console program. So it looked like the messages were arriving and the update was not running. That was not the case, it was just that the IDE lost focus. I cleaned up my code to use a Synchrhonized Queue, so now all should be very thread safe.

Well, I stumped our local unity expert also, but we did a debug session in my office and after about an hour, we found out it is not the code. The Unity IDE will report console messages even focus is off the window. But our test program was an external exe (Blaster.exe) which is a console program. So it looked like the messages were arriving and the update was not running. That was not the case, it was just that the IDE lost focus. I cleaned up my code to use a Synchrhonized Queue, so now all should be very thread safe.

BTW: The problem with IDE stopping running when it loses focus can be solved by:

    Application.runInBackground = true;

...

Source Link
Dr.YSG
  • 179
  • 1
  • 8

Well, I stumped our local unity expert also, but we did a debug session in my office and afer about an hour, we found out it is not the code. The Unity IDE will report console messages even focus is off the window. But our test program was an external exe (Blaster.exe) which is a console program. So it looked like the messages were arriving and the update was not running. That was not the case, it was just that the IDE lost focus. I cleaned up my code to use a Synchrhonized Queue, so now all should be very thread safe.

using UnityEngine;
using System.Collections;
using System.Net.Sockets;
using System;
using System.Text;
using System.Net;
using System.Threading;

public class CommScript : MonoBehaviour
{
    UdpClient client;
    IPEndPoint endPoint;
    public int port = 7777;
    public string hostName = "localhost";
    public GameObject car;
    public int stepNum;
    Thread listener;
    Queue pQueue = Queue.Synchronized(new Queue()); // holds the packet queue

    void Start()
    {
        endPoint = new IPEndPoint(Dns.GetHostAddresses(hostName)[0], port);
        client = new UdpClient(endPoint);
        Tools.LogDebug(gameObject.name, "Listening for Data...");
        listener = new Thread(new ThreadStart(translater));
        listener.IsBackground = true;
        listener.Start();
    }
    void Update()
    {
        lock (pQueue.SyncRoot)
        {
            if (pQueue.Count > 0)
            {
                Packet p = (Packet)pQueue.Dequeue();
                stepNum = p.step;
                car.GetComponent<CarMover>().moveMe(p);
            }
        }
    }

    void OnApplicationQuit()
    {
        client.Close();
    }

    void translater()
    {
        Byte[] data = new byte[0];
        while (true)
        {
            try
            {
                data = client.Receive(ref endPoint);
            }
            catch (Exception err)
            {
                Tools.LogDebugThread("Comm.translater", "recieve data error " + err, -1, -1);
                client.Close();
                return;
            }
            string json = Encoding.ASCII.GetString(data);
            pQueue.Enqueue(Packet.Parse(json));
        }
    }
}