Exemple #1
0
        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);
        }
Exemple #2
0
        public LuaTable Read()
        {
            if (client.Connected)
            {
                NetworkStream stream = client.GetStream();
                //
                System.IO.BinaryReader reader = new BinaryReader(stream);
                byte[] headerBuffer           = new byte[12];
                reader.Read(headerBuffer, 0, headerBuffer.Length);
                ClientHeader header     = ConvertHeader(headerBuffer);
                UInt32       length     = 12;
                UInt32       bodyLength = header.Length - length;

                byte[] bodyBytes = new byte[bodyLength];
                using (MemoryStream m_stream = new MemoryStream())
                {
                    m_stream.Write(bodyBytes, 0, (int)m_stream.Length);
                    LuaTable msgTable = LuaController.Instance.LuaEnv.NewTable();
                    object   body     = amf.Decode(m_stream);
                    return(LuaConvert.ConvertLuaTable(body));
                }
            }
            return(null);
        }