コード例 #1
0
        private static void doPack(MMStream stream, object arg)
        {
            if (arg == null)
            {
                return;
            }
            //Debug.Log("doPack arg.GetType():" + arg.GetType() + " arg.GetType().IsValueType:" + arg.GetType().IsValueType + " arg.GetType().IsArray:" + arg.GetType().IsArray);
            if (arg is byte)
            {
                stream.WriteByte((byte)arg);
            }
            else if (arg is sbyte)
            {
                stream.WriteByte((byte)arg);
            }
            else if (arg is ushort)
            {
                stream.WriteUInt16((ushort)arg);
            }
            else if (arg is uint)
            {
                stream.WriteUInt32((uint)arg);
            }
            else if (arg is ulong)
            {
                stream.WriteUInt64((ulong)arg);
            }
            else if (arg is short)
            {
                stream.WriteInt16((short)arg);
            }
            else if (arg is int)
            {
                stream.WriteInt32((int)arg);
            }
            else if (arg is long)
            {
                stream.WriteInt64((long)arg);
            }
            else if (arg is float)
            {
                stream.WriteFloat((float)arg);
            }
            else if (arg is double)
            {
                stream.WriteDouble((double)arg);
            }
            else if (arg is string)
            {
                stream.WriteString((string)arg);
            }
            else if (arg.GetType() == typeof(byte[]))
            {
                stream.WriteBytes((byte[])arg);
            }
            else if (arg.GetType() == typeof(sbyte[]))
            {
                var buff = new byte[((sbyte[])arg).Length];
                Array.Copy((sbyte[])arg, buff, buff.Length);
                stream.WriteBytes(buff);
            }
            else if (arg is bool)
            {
                stream.WriteBool((bool)arg);
            }
            else if (arg is global::ProtoBuf.IExtensible)
            {
                //stream.WriteString(arg.GetType().FullName);

                // +2 是由于WriteBuf存在Length的长度
                var segment = new ArraySegment <byte>(stream.Buf, stream.WPos + 2, stream.Capicity - stream.WPos - 2);

                var pstream = new System.IO.MemoryStream(segment.Array, segment.Offset, segment.Count);
                ProtoBuf.Meta.RuntimeTypeModel.Default.Serialize(pstream, arg);
                stream.WriteUInt16((ushort)pstream.Position);
                stream.WPos += (int)pstream.Position;

                //var buf = new byte[stream.WPos];
                //Buffer.BlockCopy(stream.Buf, 0, buf, 0, stream.WPos);
                //stream.WriteBytes(buf);
            }
            else if (arg is IDictionary)
            {
                var dict = (arg as IDictionary);
                stream.WriteUInt16((ushort)dict.Count);
                var enumer = dict.GetEnumerator();
                while (enumer.MoveNext())
                {
                    var item      = enumer.Current;
                    var itemKey   = item.GetType().GetProperty("Key").GetValue(item, null);
                    var itemValue = item.GetType().GetProperty("Value").GetValue(item, null);

                    doPack(stream, itemKey);
                    doPack(stream, itemValue);
                }
                //var buf = new byte[stream.WPos];
                //Buffer.BlockCopy(stream.Buf, 0, buf, 0, stream.WPos);
                //stream.WriteBytes(buf);
            }
            else if (arg is IList)
            {
                var list = (arg as IList);
                stream.WriteUInt16((ushort)list.Count);
                for (var i = 0; i < list.Count; i++)
                {
                    var listitem = list[i];
                    doPack(stream, listitem);
                }

                //var buf = new byte[stream.WPos];
                //Buffer.BlockCopy(newstream.Buf, 0, buf, 0, newstream.WPos);
                //stream.WriteBytes(buf);
            }
            else if (arg.GetType().IsArray)
            {
                var fields = arg.GetType().GetFields();

                stream.WriteUInt16((ushort)fields.Length);

                for (var i = 0; i < fields.Length; i++)
                {
                    var field = fields[i];
                    //var index = arg.GetType().IsArray ? new object[] { i } : null;
                    doPack(stream, field.GetValue(arg));
                }
            }
            else if (arg.GetType().IsEnum)
            {
                var fields = arg.GetType().GetFields();

                var field = fields[0];
                //var index = arg.GetType().IsArray ? new object[] { i } : null;
                doPack(stream, field.GetValue(arg));
            }
            else if (arg.GetType().IsValueType || arg.GetType().IsClass)
            {
                //if (arg.GetType().IsArray)
                //{
                //    //array
                //}
                //else
                //{
                //    //struct
                //    var props = arg.GetType().GetProperties();

                //    for (var i = 0; i < props.Length; i++)
                //    {
                //        var prop = props[i];
                //        stream.WriteBytes(doPack(prop.GetValue(arg, null)));
                //    }
                //}
                var fields = arg.GetType().GetFields();

                for (var i = 0; i < fields.Length; i++)
                {
                    var field = fields[i];
                    //var index = arg.GetType().IsArray ? new object[] { i } : null;
                    doPack(stream, field.GetValue(arg));
                }

                //var buf = new byte[newstream.WPos];
                //Buffer.BlockCopy(newstream.Buf, 0, buf, 0, newstream.WPos);
                //stream.WriteBytes(buf);
            }
            else
            {
                throw new NotSupportedException("Unknow rpc method param type: [" + arg.GetType().Name + "]");
            }
        }