private void Thread_Read(object main) { byte[] data1 = new byte[2]; byte[] data2 = new byte[2]; TcpHandle tcp = main as TcpHandle; try { while (tcp.m_TcpClient.Connected) { MsgPacket msg = new MsgPacket(); NetworkStream stream = m_TcpClient.GetStream(); Read(ref stream, data1); msg.msgLength = BitConverter.ToUInt16(data1, 0); Read(ref stream, data2); msg.msgIndex = BitConverter.ToUInt16(data2, 0); if (msg.msgLength != 0) { msg.data = new byte[msg.msgLength]; Read(ref stream, msg.data); } tcp.PushRecvMsgPacker(msg); } } catch (Exception err) { // 这里应该是半路突然被XX掉了的 // 所以暂时先直接放弃收到的消息,反正发不出去 Console.WriteLine(err.Message); } finally { tcp.gameState.SetState(ELoginState.Login_ERR); } }
private void PushRecvMsgPacker(MsgPacket msg) { lock (m_Queue) m_Queue.Enqueue(msg); }