示例#1
0
        /// <summary>
        /// 发送数据流
        /// </summary>
        //[ThreadStatic]
        //private static MMStream sentStream;

        /// <summary>
        /// 接收数据流
        /// </summary>
        //[ThreadStatic]
        //private static MMStream receiveStream;

        /// <summary>
        /// 发送数据流
        /// </summary>
        //private static MMStream SentStream
        //{
        //    get { return sentStream ?? (sentStream = new MMStream()); }
        //}

        /// <summary>
        /// 接收数据流
        /// </summary>
        //private static MMStream ReceiveStream
        //{
        //    get { return receiveStream ?? (receiveStream = new MMStream()); }
        //}

        /// <summary>
        /// 打包
        /// </summary>
        /// <param name="packet">需要发送的数据包</param>
        /// <param name="next">投递到下一个处理器</param>
        /// <returns>打包的数据</returns>
        public static ArraySegment <byte> Packed(SentPackage packet, Func <SentPackage, ArraySegment <byte> > next)
        {
            if (packet.MsgInfo == null || packet.MsgInfo.MsgType != MsgTypes.Binary)
            {
                return(next(packet));
            }

            byte[] buff = null;
            MsgHelper.PackParam(packet.Packet, out buff);

            Array.Copy(buff, 0, packet.SentBuffer.Array, packet.SentBuffer.Offset, buff.Length);
            //SentStream.Reset(packet.SentBuffer.Array, packet.SentBuffer.Offset, packet.SentBuffer.Count);
            //((IMsg)packet.Packet).Marshal(SentStream);
            return(new ArraySegment <byte>(packet.SentBuffer.Array, packet.SentBuffer.Offset, buff.Length));
        }
示例#2
0
        private void OnRpc(RpcMsg msg)
        {
            var methodname = msg.methodeName;
            //for (var k = 0; k < args.Length; k++)
            //{
            //    name += "_" + args[k].GetType().Name;
            //}
            //fire(msg.methodeName, args);
            Dictionary <object, MethodInfo> rpcinfo = new Dictionary <object, MethodInfo>();

            if (rpcHandlerDict.TryGetValue(methodname, out rpcinfo))
            {
                object[] args = null;
                var      iter = rpcinfo.GetEnumerator();
                bool     flag = true;

                while (iter.MoveNext())
                {
                    var info = iter.Current;
                    if (flag)
                    {
                        flag = false;
                        try
                        {
                            if (!MsgHelper.UnpackMethod(info.Value, msg.data, out args))
                            {
                                throw new Exception("Parameter cannot be unpacked for rpc method " + methodname);
                            }
                        }
                        catch (Exception e)
                        {
                            throw new Exception("OnRpc UnpackMethod Execption for method name " + methodname + " exception:" + e);
                        }
                    }
                    info.Value.Invoke(info.Key, args);
                }

                return;
            }

            if (!entityDict.ContainsKey(msg.EntityID))
            {
                throw new Exception("entity id->" + msg.EntityID + " not exists!");
            }

            entityDict[msg.EntityID].OnRpc(methodname, msg.data);
        }
示例#3
0
        public void Rpc(string methodname, params object[] args)
        {
            byte[] bytes;
            if (!MsgHelper.Pack(args, out bytes))
            {
                throw new Exception("Parameter cannot be pack for rpc call method:" + methodname);
            }

            RpcMsg req = new RpcMsg();

            //req.rpcType = rpctype;
            req.methodeName = methodname;
            req.data        = bytes;

            try
            {
                Send(req);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
示例#4
0
        /// <summary>
        /// 解包
        /// </summary>
        /// <param name="packet">接受数据包</param>
        /// <param name="next">投递到下一个处理器</param>
        /// <returns>解包的对象</returns>
        public static object Unpacked(ReceivePackage packet, Func <ReceivePackage, object> next)
        {
            if (packet.MsgInfo == null || packet.MsgInfo.MsgType != MsgTypes.Binary)
            {
                return(next(packet));
            }

            //ReceiveStream.Reset(packet.Body.Array, packet.Body.Offset, packet.Body.Count);

            var buff = new byte[packet.Body.Count];

            Array.Copy(packet.Body.Array, packet.Body.Offset, buff, 0, packet.Body.Count);

            var msg = MsgHelper.UnpackParam(packet.MsgInfo.ProtocolType, buff); // Generate(packet.MsgInfo) as IMsg;

            //if (msg == null)
            //{
            //    throw new NotSupportedException("Can not generate protocol [" + packet.MsgInfo.Name + "]");
            //}

            //msg.UnMarshal(ReceiveStream);
            return(msg);
        }
示例#5
0
        internal void OnRpc(string methodname, byte[] data)
        {
            Dictionary <object, MethodInfo> rpcinfo = new Dictionary <object, MethodInfo>();

            if (rpcHandlerDict.TryGetValue(methodname, out rpcinfo))
            {
                object[] args = null;
                var      iter = rpcinfo.GetEnumerator();
                bool     flag = true;

                while (iter.MoveNext())
                {
                    var info = iter.Current;
                    if (flag)
                    {
                        flag = false;
                        try
                        {
                            if (!MsgHelper.UnpackMethod(info.Value, data, out args))
                            {
                                throw new Exception("Parameter cannot be unpacked for rpc method " + methodname + " in entity id->" + id);
                            }
                        }
                        catch (Exception e)
                        {
                            throw new Exception("OnRpc UnpackMethod Execption for method name " + methodname + " in entity id->" + id + ". exception:" + e);
                        }
                    }
                    info.Value.Invoke(info.Key, args);
                }
            }
            else
            {
                throw new Exception("rpc method name->" + methodname + " not exists in entity id->" + id);
            }
        }