// NOT on main thread protected void AcceptCallback(IAsyncResult ar) { int threadId = Thread.CurrentThread.ManagedThreadId; Debug.Log("Accept thread id: " + threadId); TcpListener listener = (TcpListener)ar.AsyncState; TcpClient client = listener.EndAcceptTcpClient(ar); Debug.Log("thread id " + threadId + " accepted client " + client.Client.RemoteEndPoint); Debug.Log("thread id " + threadId + " beginning read from client " + client.Client.RemoteEndPoint); SCL_ClientSocketHandler clientHandler = new SCL_ClientSocketHandler(client, this.clientSocketHandlerDelegate, this.separatorString, this.encoding); Thread clientThread = new Thread(new ThreadStart(clientHandler.Run)); this.clientHandlerThreads.Add(new SCL_SocketClientThreadHolder(clientThread, clientHandler)); clientThread.Start(); Debug.Log("Client thread started"); if (this.ClientCount < this.maxClients) { Debug.Log("client handler threads less than max clients. Listening again"); this.ListenForConnection(); } else { Debug.Log(String.Format("Max number of clients reached ({0}), stopping listening", this.maxClients)); this.StopListeningForConnections(); } }
// this delegate method will be called on another thread, so use locks for synchronization public void ClientSocketHandlerDidReadMessage(SCL_ClientSocketHandler handler, string message) { // message = message.Trim(new char[] {'[', ']'}); // int startIndex = message.LastIndexOf('{'); // int endIndex = message.LastIndexOf('}'); // message = message.Substring(startIndex, endIndex + 1); // int y = 0; // int x = 0; // string[] strs = message.Split(','); // for(int i = 0; i < strs.Length; i++) { // string temp = Regex.Match(strs[i], @"\d+").Value; // if(strs[i].Contains("X")) { // x = Int32.Parse(temp); // }else { // y = Int32.Parse(temp); // } // } // character.SetData (x, y); // Debug.Log(str); // StepData data = JsonUtility.FromJson<StepData>(message); Debug.Log(message); new Thread(() => { Thread.CurrentThread.IsBackground = true; int startIndex = message.LastIndexOf('{'); int endIndex = message.LastIndexOf('}'); message = message.Substring(startIndex, endIndex + 1); int y = 0; int x = 0; string[] strs = message.Split(','); for (int i = 0; i < strs.Length; i++) { string temp = Regex.Match(strs[i], @"\d+").Value; if (strs[i].Contains("X")) { x = Int32.Parse(temp); } else { y = Int32.Parse(temp); } } character.SetData(x, y); }).Start(); // StartCoroutine(ProcessData(message)); // Debug.Log(data.posY); }
// this delegate method will be called on another thread, so use locks for synchronization public void msgParse(SCL_ClientSocketHandler handler, string message) { string[] pieces = message.Split('+'); if (pieces.Length > maxObjects) { Debug.Log("Too many arguments to msgParse, terminating."); return; } for (int i = 0; i < pieces.Length; i++) { execCmd(pieces[i]); } }
// this delegate method will be called on another thread, so use locks for synchronization public void ClientSocketHandlerDidReadMessage(SCL_ClientSocketHandler handler, string message) { string [] pieces = message.Split(','); if (pieces.Length == 3) { try { float x = float.Parse(pieces[0], CultureInfo.InvariantCulture); float y = float.Parse(pieces[1], CultureInfo.InvariantCulture); float z = float.Parse(pieces[2], CultureInfo.InvariantCulture); Debug.Log(String.Format("x: {0}, y: {1}, z: {2}", x, y, z)); this.SetPositionValue(x, y, z); } catch (FormatException) { Debug.Log("Invalid number format: " + message); } } else { Debug.Log("Length is not three: " + message); } }
public SCL_SocketClientThreadHolder(Thread thread, SCL_ClientSocketHandler handler) { this.thread = thread; this.handler = handler; }
public void ClientSocketHandlerDidReadMessage(SCL_ClientSocketHandler handler, string message) { UnityMainThreadDispatcher.Instance().Enqueue(ThisWillBeExecutedOnTheMainThread(message)); }