public void OnReceive(PackageIn msg) { if (!m_bOpen) { return; } m_ReceiveBytes += msg.Length; }
public void Read(PackageIn packageIn) { if (null != readFunc) { readFunc.BeginPCall(); readFunc.PushObject(packageIn); readFunc.PCall(); readFunc.EndPCall(); } //LuaManager.instance.CallFunction("Network.OnRead", packageIn); }
public bool ReadMessage(int code, PackageIn pkg) { ICommand command = _GetCommand(code); ClearLock(); if (command != null) { Debug.Log("Socket Receive: [0x" + code.ToString() + "," + command.desc + "," + BitConverter.ToString(pkg.GetByteArray(0, pkg.length)) + "]"); command.ReadPackage(pkg); return(true); } return(false); }
public int GetLength(PackagePart packagePart) { int length = 0; if ((packagePart & PackagePart.In) > 0) { length += PackageIn.Sum(x => x.Length); } if ((packagePart & PackagePart.Out) > 0) { length += PackageOut.Sum(x => x.Length); } return(length); }
private void _SocketData(params object[] args) { PackageIn pkg = args[0] as PackageIn; ProtocolManager.instance.ReadMessage(pkg.code, pkg); }
private static bool _StreamToObject(PackageIn pkg, Type type, List <int> indexs, int depth, object entity, out object value) { value = null; if (type == typeof(bool)) { if (!pkg.CanPopByte()) { return(false); } value = pkg.PopByte() > 0 ? true : false; } else if (type == typeof(sbyte)) { if (!pkg.CanPopByte()) { return(false); } value = pkg.PopSByte(); } else if (type == typeof(byte)) { if (!pkg.CanPopByte()) { return(false); } value = pkg.PopByte(); } else if (type == typeof(short)) { if (!pkg.CanPopShort()) { return(false); } value = pkg.PopShort(); } else if (type == typeof(ushort)) { if (!pkg.CanPopShort()) { return(false); } value = pkg.PopUShort(); } else if (type == typeof(int)) { if (!pkg.CanPopInt()) { return(false); } value = pkg.PopInt(); } else if (type == typeof(uint)) { if (!pkg.CanPopInt()) { return(false); } value = pkg.PopUInt(); } else if (type == typeof(long)) { if (!pkg.CanPopLong()) { return(false); } value = pkg.PopLong(); } else if (type == typeof(ulong)) { if (!pkg.CanPopLong()) { return(false); } value = pkg.PopULong(); } else if (type == typeof(string)) { if (!pkg.CanPopUTF()) { return(false); } value = pkg.PopUTF(); } else { bool subComplete = true; object subValue = null; Type subType = type.GetElementType(); int count = 0; if (subType == null) { if (entity != null) { value = entity; FieldInfo[] fields = type.GetFields(); count = fields.Length; for (int i = indexs[depth]; i < count; i++) { indexs[depth] = i; subComplete = _StreamToObject(pkg, fields[i].FieldType, indexs, depth + 1, fields[i].GetValue(value), out subValue); if (subValue != null) { fields[i].SetValue(value, subValue); } if (!subComplete) { break; } } } else { value = System.Activator.CreateInstance(type); FieldInfo[] fields = type.GetFields(); count = fields.Length; indexs.Add(0); for (int i = 0; i < count; i++) { indexs[depth] = i; subComplete = _StreamToObject(pkg, fields[i].FieldType, indexs, depth + 1, null, out subValue); if (subValue != null) { fields[i].SetValue(value, subValue); } if (!subComplete) { break; } } } } else { if (entity != null) { Array array = entity as Array; count = array.Length; for (int i = indexs[depth]; i < count; i++) { indexs[depth] = i; subComplete = _StreamToObject(pkg, subType, indexs, depth + 1, array.GetValue(i), out subValue); array.SetValue(subValue, i); if (!subComplete) { break; } } value = array; } else { if (!pkg.CanPopShort()) { return(false); } count = pkg.PopShort(); Array array = Array.CreateInstance(subType, count); indexs.Add(0); for (int i = 0; i < count; i++) { indexs[depth] = i; subComplete = _StreamToObject(pkg, subType, indexs, depth + 1, null, out subValue); array.SetValue(subValue, i); if (!subComplete) { break; } } value = array; } } if (subComplete) { indexs.RemoveAt(indexs.Count - 1); } return(subComplete); } return(true); }
///======================强大的分割线======================== ///以下内容用于处理动态接收数据 /// <summary> /// 持续转换,即单次转换不一定转换完成 /// </summary> /// <param name="pkg">数据流</param> /// <param name="type">对象类型</param> /// <param name="indexs">已转换的数据层次及位置,不能为空</param> /// <param name="root">已转换的数据对象</param> /// <returns></returns> public static bool StreamToObject(PackageIn pkg, Type type, List <int> indexs, object root, out object value) { return(_StreamToObject(pkg, type, indexs, 0, root, out value)); }