コード例 #1
0
        public static object UnpackParam(Type type, byte[] buf)
        {
            var stream = MMStreamPool.GetTmp();//new MMStream(buf);

            stream.Reset(buf);
            var ret = doUnpack(stream, type);

            MMStreamPool.PutTmp(stream);
            return(ret);
        }
コード例 #2
0
        public static void PackParam(object obj, out byte[] buf)
        {
            var stream = MMStreamPool.Get(Header.MaxPacketSize);

            stream.WPos = 0;
            stream.RPos = 0;

            doPack(stream, obj);

            buf = new byte[stream.WPos];
            Buffer.BlockCopy(stream.Buf, 0, buf, 0, stream.WPos);
            MMStreamPool.Put(stream);
        }
コード例 #3
0
        /// <summary>
        /// 注册服务
        /// </summary>
        public override void Init()
        {
            var channelExtend = GBox.Make <IChannelManager>();

            channelExtend.Extend("cratos", (nsp) =>
            {
                var socketFactory = GBox.Make <ISocketManager>();
                var socket        = socketFactory.Create(nsp);
                return(new Channel.Channel(socket, new GaiaFragment()));
            });

            MMStreamPool.Init();

            //GBox.Singleton<Room>().Alias<IRoom>();
        }
コード例 #4
0
        public static bool Pack(object[] args, out byte[] msgBuf)
        {
            var stream = MMStreamPool.Get(Header.MaxPacketSize);

            stream.WPos = 0;
            stream.RPos = 0;

            for (var i = 0; i < args.Length; i++)
            {
                doPack(stream, args[i]);
            }

            msgBuf = new byte[stream.WPos];
            Buffer.BlockCopy(stream.Buf, 0, msgBuf, 0, stream.WPos);
            MMStreamPool.Put(stream);
            return(true);
        }
コード例 #5
0
        public static bool UnpackMethod(MethodInfo minfo, byte[] buf, out object[] args)
        {
            var argList = new List <object>();
            var stream  = MMStreamPool.GetTmp();// new MMStream(buf);

            stream.Reset(buf);
            var paramarr = minfo.GetParameters();

            for (var i = 0; i < paramarr.Length; i++)
            {
                var paraminfo = paramarr[i];
                argList.Add(doUnpack(stream, paraminfo.ParameterType));
            }
            args = argList.ToArray();
            MMStreamPool.PutTmp(stream);
            return(true);
        }
コード例 #6
0
        /// <summary>
        /// 参数解包
        /// </summary>
        /// <param name="buf">需要解包的流</param>
        /// <param name="args">解包后的参数表</param>
        /// <param name="protoBinaryReserved">是否进行proto二进制解决</param>
        /// <returns>是否成功解包</returns>
        public static bool ArgsUnpack(byte[] buf, out object[] args, bool protoBinaryReserved = false)
        {
            if (buf == null)
            {
                args = new object[] { };
                return(true);
            }

            var unPackSucceed = true;
            var stream        = MMStreamPool.GetTmp(); //new MMStream(buf);

            stream.Reset(buf);
            var argList = new List <object>();

            while (!stream.IsEOF())
            {
                var type = (ArgType)stream.ReadByte();
                switch (type)
                {
                case ArgType.typeInt8:
                case ArgType.typeUint8:
                    argList.Add(stream.ReadByte());
                    break;

                case ArgType.typeInt16:
                    argList.Add(stream.ReadInt16());
                    break;

                case ArgType.typeInt32:
                    argList.Add(stream.ReadInt32());
                    break;

                case ArgType.typeInt64:
                    argList.Add(stream.ReadInt64());
                    break;

                case ArgType.typeUint16:
                    argList.Add(stream.ReadUInt16());
                    break;

                case ArgType.typeUint32:
                    argList.Add(stream.ReadUInt32());
                    break;

                case ArgType.typeUint64:
                    argList.Add(stream.ReadUInt64());
                    break;

                case ArgType.typeBool:
                    argList.Add(stream.ReadBoolean());
                    break;

                case ArgType.typeString:
                    argList.Add(stream.ReadString());
                    break;

                case ArgType.typeBytes:
                    argList.Add(stream.ReadBytes());
                    break;

                case ArgType.typeFloat32:
                    argList.Add(stream.ReadFloat());
                    break;

                case ArgType.typeFloat64:
                    argList.Add(stream.ReadDouble());
                    break;

                case ArgType.typeProto:
                    //throw new NotSupportedException(
                    //    "not support proto param for rpc method");
                    object msg;
                    unPackSucceed = UnpackProto(stream, protoBinaryReserved, out msg);
                    argList.Add(msg);
                    break;
                }
            }

            args = argList.ToArray();
            MMStreamPool.PutTmp(stream);
            return(unPackSucceed);
        }
コード例 #7
0
        /// <summary>
        /// 参数打包
        /// </summary>
        /// <param name="args">参数名</param>
        /// <param name="msgBuf">需要返回的流</param>
        /// <returns>是否打包成功</returns>
        public static bool ArgsPack(object[] args, out byte[] msgBuf)
        {
            var stream = MMStreamPool.Get();

            stream.WPos = 0;
            stream.RPos = 0;

            try
            {
                foreach (var arg in args)
                {
                    if (arg is byte)
                    {
                        stream.WriteByte((byte)ArgType.typeUint8);
                        stream.WriteByte((byte)arg);
                    }
                    else if (arg is ushort)
                    {
                        stream.WriteByte((byte)ArgType.typeUint16);
                        stream.WriteUInt16((ushort)arg);
                    }
                    else if (arg is uint)
                    {
                        stream.WriteByte((byte)ArgType.typeUint32);
                        stream.WriteUInt32((uint)arg);
                    }
                    else if (arg is ulong)
                    {
                        stream.WriteByte((byte)ArgType.typeUint64);
                        stream.WriteUInt64((ulong)arg);
                    }
                    else if (arg is short)
                    {
                        stream.WriteByte((byte)ArgType.typeInt16);
                        stream.WriteInt16((short)arg);
                    }
                    else if (arg is int)
                    {
                        stream.WriteByte((byte)ArgType.typeInt32);
                        stream.WriteInt32((int)arg);
                    }
                    else if (arg is long)
                    {
                        stream.WriteByte((byte)ArgType.typeInt64);
                        stream.WriteInt64((long)arg);
                    }
                    else if (arg is float)
                    {
                        stream.WriteByte((byte)ArgType.typeFloat32);
                        stream.WriteFloat((float)arg);
                    }
                    else if (arg is double)
                    {
                        stream.WriteByte((byte)ArgType.typeFloat64);
                        stream.WriteDouble((double)arg);
                    }
                    else if (arg is string)
                    {
                        stream.WriteByte((byte)ArgType.typeString);
                        stream.WriteString((string)arg);
                    }
                    else if (arg.GetType() == typeof(byte[]))
                    {
                        stream.WriteByte((byte)ArgType.typeBytes);
                        stream.WriteBytes((byte[])arg);
                    }
                    else if (arg is bool)
                    {
                        stream.WriteByte((byte)ArgType.typeBool);
                        stream.WriteBool((bool)arg);
                    }
                    else
                    {
                        //throw new NotSupportedException(
                        //    "Unknow rpc method param type: [" + arg.GetType().Name + "]");
                        //var msgInfo = MsgService.Instance.GetMsgByName(arg.GetType().Name);
                        //if (msgInfo == null)
                        //{
                        //    throw new NotSupportedException(
                        //        "Unknow msg info , msg type is [" + arg.GetType().Name + "]");
                        //}

                        stream.WriteByte((byte)ArgType.typeProto);
                        //stream.WriteUInt16((ushort)msgInfo.Id);
                        stream.WriteString(arg.GetType().FullName);

                        // +2 是由于WriteBuf存在Length的长度
                        var segment = new ArraySegment <byte>(stream.Buf, stream.WPos + 2, stream.Capicity - stream.WPos - 2);
                        //PacketPolicy.Packed(ref segment, msgInfo, arg);
                        //stream.WriteUInt16((ushort)segment.Count);
                        //stream.WPos += segment.Count;

                        var pstream = new System.IO.MemoryStream(segment.Array, segment.Offset, segment.Count);
                        ProtoBuf.Meta.RuntimeTypeModel.Default.Serialize(pstream, arg);
                        stream.WriteUInt16((ushort)segment.Count);
                        stream.WPos += segment.Count;
                    }
                }
            }
            catch (Exception)
            {
                msgBuf = null;
                return(false);
            }

            if (stream.WPos > 0)
            {
                msgBuf = new byte[stream.WPos];
                Buffer.BlockCopy(stream.Buf, 0, msgBuf, 0, stream.WPos);
            }
            else
            {
                msgBuf = null;
            }
            MMStreamPool.Put(stream);

            return(true);
        }