private void Run() { Queue <MsgToSend> sending = new Queue <MsgToSend>(); while (running) { while (sendQueue.Count == 0) { Thread.Sleep(1); } while (sendQueue.Count > 0) { lock (lockobj) { sending.Enqueue(sendQueue.Dequeue()); } } while (sending.Count > 0) { MsgToSend msg = sending.Dequeue(); msg.Serialize(); byte[] buff = msg.raw; try { stream.Write(buff, 0, buff.Length); Debug.Log("socket write length: " + buff.Length); } catch (Exception e) { socketMgr.ReportError(client, "send error: " + e.Message + e.StackTrace); running = false; break; } } } }
private void Run() { byte[] head = new byte[MsgRec.HEAD_SIZE]; while (running) { MsgRec rec = null; int received = 0; try { received = stream.Read(head, 0, head.Length); // Debug.Log(received); } catch (Exception e) { socketMgr.ReportError(client, "receive head error: " + e.ToString()); running = false; break; } if (received != head.Length) { socketMgr.ReportError(client, "receive head size error(maybe stream end):" + received + "-" + head.Length); running = false; break; } rec = MsgRec.Create(head); if (rec.bodyLength > 0) { byte[] buff = new byte[rec.bodyLength]; try { Debug.Log("socket begin receive body length: " + buff.Length); received = stream.Read(buff, 0, buff.Length); // Debug.Log("socket receive body length: " + buff.Length); } catch (Exception e) { socketMgr.ReportError(client, "receive content error: " + e.Message + e.StackTrace); running = false; break; } if (received != buff.Length) { socketMgr.ReportError(client, "content receive size error(maybe stream end):" + received + "/" + buff.Length); running = false; break; } rec.content = buff; rec.Deserialize(); lock (lockobj) { recQueue.Enqueue(rec); } } else { lock (lockobj) { recQueue.Enqueue(rec); } } } }