// 异步收包线程. private void waitSocket() { Debug.Log("receiver"); int len; while (true) { if (!clientSocket.Poll(-1, SelectMode.SelectRead)) { Debug.Log("poll error"); Close(); } len = clientSocket.Receive(buf.GetRaw(), 0, HEAD_SIZE, SocketFlags.None); if (len < 0) { Debug.Log("length error"); Close(); return; } Debug.Assert(len == HEAD_SIZE); int payload_length = buf.GetShort(0); //Debug.Log ("payload_length" + payload_length); Debug.Assert(payload_length < BUFF_SIZE); int want = payload_length; int lenAll = 0; buf.Clear(); SocketError socketError; while (want > 0) { len = clientSocket.Receive(buf.GetRaw(), lenAll, want, SocketFlags.None, out socketError); want -= len; lenAll += len; } // Debug.Log("receive length:" + lenAll+ " payload_length:" // + payload_length); Debug.Assert(payload_length == lenAll); var ms_byte = new byte[payload_length]; Array.Copy(buf.GetRaw(), 0, ms_byte, 0, payload_length); var ms2 = new MemoryStream(ms_byte, 0, payload_length); int cmd = ms2.ReadByte(); string eventName = EventName.GetEventName(cmd); // ShowBytes("bytes:"+eventName+":", buf.GetRaw(), payload_length); NetEvent.FireOut(eventName, new object[] { ms2 }); /* * var protoPacket = ProtoBuf.Serializer.Deserialize<packet>(ms2); * Debug.Log("receive packet.cmd:"+protoPacket.cmd); * var eventname = MessageMap.GetEventName(protoPacket.cmd); * Event.FireOut(eventname, new object[]{protoPacket.payload}); */ } }