public void DataCallback(IAsyncResult ar) { Debug.Log("读取数据"); TcpClient tc = (TcpClient)ar.AsyncState; NetworkStream ns = tc.GetStream(); int bytesRead = ns.EndRead(ar); if (bytesRead == 0) { Debug.LogError("异常读取..."); return; } Utils.utils.Log(this.header); LuaTable result; using (MemoryStream m_stream = new MemoryStream()) { m_stream.Write(dataBuffer, 0, (int)m_stream.Length); LuaTable msgTable = SGK.LuaController.GetLuaState()?.NewTable(); object body = amf.Decode(m_stream); var header = LuaConvert.ConvertLuaTable(this.header); Utils.utils.Log("headr:", this.header, LuaConvert.ConvertObjects(header)); var bodyTable = LuaConvert.ConvertLuaTable(body, header); result = SGK.LuaController.GetLuaState().NewTable(); result.Set <int, LuaTable>(1, header); result.Set <int, LuaTable>(2, bodyTable); } if (result != null) { Utils.utils.Log("Header Add", LuaConvert.ConvertObjects(result)); this.cache.Add(result); } BegainRead(0); }
public bool Send(uint cmd, uint sn, params object[] obs) { if (!this.client.Connected) { return(false); } if (obs.Length == 0) { return(false); } byte[] sendBuffer; if (obs.Length == 1) { if (obs[0].GetType() == typeof(LuaTable)) { Utils.utils.Log("LuaTable:------>>>", LuaConvert.ConvertObjects(obs[0] as LuaTable)); object[] result = LuaConvert.ConvertObjects(obs[0] as LuaTable); //amf.Encode() Utils.utils.Log("result------->>>", result); using (MemoryStream stream = new MemoryStream()) { amf.Encode(stream, result); sendBuffer = stream.ToArray(); Utils.utils.Log(sendBuffer); } if (sendBuffer != null) { //header.Length = 12 + sendBuffer.Length; var header = new ClientHeader(); header.Length = (uint)(12 + sendBuffer.Length); header.Length = (uint)(12 + sendBuffer.Length); header.SN = sn; header.MessageID = cmd; var bsHeader = ConvertNetHeader(header); Utils.utils.Log("Header:", bsHeader); Utils.utils.Log("body:", sendBuffer); byte[] newBuffer = new byte[12 + sendBuffer.Length]; Array.Copy(bsHeader, 0, newBuffer, 0, bsHeader.Length); Array.Copy(sendBuffer, 0, newBuffer, 12, sendBuffer.Length); Utils.utils.Log("packBuffer:", newBuffer); try { this.client.GetStream().Write(newBuffer, 0, newBuffer.Length); return(true); } catch (Exception e) { this.client.Close(); Debug.Log(e.Message); } } } } object[] encodes = new object[obs.Length]; //amf.Encode() for (int i = 0; i < obs.Length; i++) { if (obs[i].GetType() == typeof(LuaTable)) { object[] result = LuaConvert.ConvertObjects(obs[0] as LuaTable); encodes[i] = result; } else { encodes[i] = obs[i]; } } using (MemoryStream stream = new MemoryStream()) { amf.Encode(stream, encodes); sendBuffer = stream.ToArray(); } try { var header = new ClientHeader(); header.Length = (uint)(12 + sendBuffer.Length); header.Length = (uint)(12 + sendBuffer.Length); header.SN = sn; header.MessageID = cmd; var bsHeader = ConvertNetHeader(header); Utils.utils.Log("Header:", bsHeader); Utils.utils.Log("body:", sendBuffer); byte[] newBuffer = new byte[bsHeader.Length + sendBuffer.Length]; Array.Copy(bsHeader, 0, newBuffer, 0, bsHeader.Length); Array.Copy(sendBuffer, 0, newBuffer, 12, sendBuffer.Length); Utils.utils.Log("packBuffer:", newBuffer); this.client.GetStream().Write(newBuffer, 0, newBuffer.Length); return(true); } catch (Exception e) { this.client.Close(); Debug.Log(e.Message); } //amf.Encode() return(false); }