private void DoRecvMessage() { try { if (m_client.Available >= MessageHeadSize) { var buffer = new byte[MessageHeadSize]; var stream = m_client.GetStream(); if (stream.Read(buffer, 0, MessageHeadSize) == MessageHeadSize) { Marshal.Copy(buffer, 0, m_recvHeadBuffer, MessageHeadSize); m_recvHead = (MessageHead)Marshal.PtrToStructure(m_recvHeadBuffer, typeof(MessageHead)); KeyValuePair <UInt32, ConstructorInfo> ret; MessageLua ret_lua = null; if (m_messages.TryGetValue(m_recvHead.id, out ret) == false && m_luaMessages.TryGetValue(m_recvHead.id, out ret_lua) == false) { DispatchSocketEvent(Event.kErrorMessage, "no find message id:" + m_recvHead.id); return; } Message message = null; if (ret_lua != null) { if (ret_lua.SignId != m_recvHead.signatureId) { DispatchSocketEvent(Event.kErrorMessage, "signId check error,message id:" + m_recvHead.id); return; } message = new FastNet.LocalMessage(ret_lua.Create()); } else if (ret.Value != null) { if (ret.Key != m_recvHead.signatureId) { DispatchSocketEvent(Event.kErrorMessage, "signId check error,message id:" + m_recvHead.id); return; } message = ret.Value.Invoke(new object[] { }) as Message; } if (m_recvHead.size > 0) { buffer = new byte[m_recvHead.size]; if (stream.Read(buffer, 0, (int)m_recvHead.size) != m_recvHead.size) { DoClose("recv size error,message id:" + m_recvHead.id, true); return; } var memoryStream = new MemoryStream(buffer); var binaryReader = new BinaryReader(memoryStream); message.Deserialize(binaryReader); binaryReader.Close(); memoryStream.Close(); } if (message != null && message.ClsId != pkt.protocols.SocketHeartbeat.sClsId) { if (message.ClsId == pkt.protocols.RoomMessageRes.sClsId) { var roomMessageRes = (pkt.protocols.RoomMessageRes)message; KeyValuePair <UInt32, ConstructorInfo> ret_roomMessage; MessageLua ret_luaRoomMessage = null; if (false == m_messages.TryGetValue(roomMessageRes.clsId, out ret_roomMessage) && m_luaMessages.TryGetValue(roomMessageRes.clsId, out ret_luaRoomMessage) == false) { DispatchSocketEvent(Event.kErrorMessage, "no find room message id:" + roomMessageRes.clsId); } else { Message roomMessage = null; if (ret_lua != null) { if (ret_lua.SignId != m_recvHead.signatureId) { DispatchSocketEvent(Event.kErrorMessage, "signId check error,room message id:" + roomMessageRes.clsId); return; } roomMessage = new FastNet.LocalMessage(ret_luaRoomMessage.Create()); } else if (ret.Value != null) { if (ret.Key != m_recvHead.signatureId) { DispatchSocketEvent(Event.kErrorMessage, "signId check error,room message id:" + roomMessageRes.clsId); return; } roomMessage = ret_roomMessage.Value.Invoke(new object[] { }) as Message; } if (roomMessageRes.data.Length > 0) { byte[] byteArray = System.Text.Encoding.Default.GetBytes(roomMessageRes.data); var memoryStream = new MemoryStream(byteArray); var binaryReader = new BinaryReader(memoryStream); message.Deserialize(binaryReader); binaryReader.Close(); memoryStream.Close(); } lock (m_recvMessages) { var mi = new MessageInfo(); mi.message = roomMessage; mi.roleId = roomMessageRes.roleId; mi.roomId = roomMessageRes.roomId; m_recvMessages.Enqueue(mi); } } } else if (message.ClsId == pkt.protocols.SocketConnect.sClsId) { var socketConnect = (pkt.protocols.SocketConnect)message; m_serverId = socketConnect.serverId; m_sessionId = socketConnect.sessionId; lock (m_md5Values) { m_md5Values = socketConnect.md5Values; } DispatchSocketEvent(Event.kCheckUpdate, ""); } else { lock (m_recvMessages) { var mi = new MessageInfo(); mi.message = message; m_recvMessages.Enqueue(mi); } } } m_timerHeartbeat = 10000; } } } catch (Exception e) { DoClose(e.ToString(), true); } }
public XLua.LuaTable Create() { var t = _luaEnv.NewTable(); t.Set("ClsName", _clsName); t.Set("ClsId", _clsId); t.Set("SignId", _signId); t.Set(s_sSelf, this); object objDefVal = null; foreach (var v in _attrs) { if (v.objDefVal == null) { try { if (v.type.Equals(s_sString)) { v.objDefVal = v.defVal; } else if (v.type.Equals(s_sInt32)) { v.objDefVal = 0; v.objDefVal = Int32.Parse(v.defVal); } else if (v.type.Equals(s_sUint32)) { v.objDefVal = 0; v.objDefVal = UInt32.Parse(v.defVal); } else if (v.type.Equals(s_sEnum)) { v.objDefVal = false; v.objDefVal = Int32.Parse(v.defVal); } else if (v.type.Equals(s_sUint8)) { v.objDefVal = 0; v.objDefVal = Byte.Parse(v.defVal); } else if (v.type.Equals(s_sUint16)) { v.objDefVal = 0; v.objDefVal = UInt16.Parse(v.defVal); } else if (v.type.Equals(s_sUint64)) { v.objDefVal = 0; v.objDefVal = UInt64.Parse(v.defVal); } else if (v.type.Equals(s_sInt8)) { v.objDefVal = 0; v.objDefVal = SByte.Parse(v.defVal); } else if (v.type.Equals(s_sInt16)) { v.objDefVal = 0; v.objDefVal = Int16.Parse(v.defVal); } else if (v.type.Equals(s_sInt64)) { v.objDefVal = 0; v.objDefVal = Int64.Parse(v.defVal); } else if (v.type.Equals(s_sDouble)) { v.objDefVal = 0.0; v.objDefVal = Double.Parse(v.defVal); } else if (v.type.Equals(s_sFloat)) { v.objDefVal = 0.0; v.objDefVal = Single.Parse(v.defVal); } else if (v.type.Equals(s_sBool)) { v.objDefVal = false; v.objDefVal = Boolean.Parse(v.defVal); } else { var m = _luaEnv.Global.GetInPath <XLua.LuaTable>(v.type); MessageLua self = m.Get <String, MessageLua>(s_sSelf); objDefVal = self.Create(); } } catch (Exception e) { UnityEngine.Debug.LogException(e); } } if (v.objDefVal != null) { t.Set(v.name, v.objDefVal); } else if (objDefVal != null) { t.Set(v.name, objDefVal); } } return(t); }