Beispiel #1
0
        private void DoReceiveInMain()
        {
            recvBuffQueue.Switch();
            while (!recvBuffQueue.Empty())
            {
                var recvBufferRaw = recvBuffQueue.Pop();
                int ret           = kcp.Input(recvBufferRaw, recvBufferRaw.Length);

                if (ret < 0)
                {
                    Debuger.LogError("收到不正确的KCP包, Ret:{0}", ret);
                    return;
                }

                needKcpUpdateFlag = true;
                for (int size = kcp.PeekSize(); size > 0; size = kcp.PeekSize())
                {
                    var recvBuffer = new byte[size];
                    if (kcp.Recv(recvBuffer) > 0)
                    {
                        lastRecvTimestamp = (uint)TimeUtility.GetTotalMillisecondsSince1970();
                        var data = ProtoBuffUtility.Deserialize <FSPDataSToC>(recvBuffer);
                        recvListener?.Invoke(data.frame);
                    }
                }
            }
        }
        private void DoReceiveInMain()
        {
            recvBufQueue.Switch();
            while (!recvBufQueue.Empty())
            {
                var recvBufferRaw = recvBufQueue.Pop();
                int ret           = kcp.Input(recvBufferRaw, recvBufferRaw.Length);
                if (ret < 0)
                {
                    Debuger.LogError("收到不正确的KCP包!Ret:{0}", ret);
                    return;
                }

                needKcpUpdateFlag = true;

                for (int size = kcp.PeekSize(); size > 0; size = kcp.PeekSize())
                {
                    var recvBuffer = new byte[size];
                    if (kcp.Recv(recvBuffer) > 0)
                    {
                        if (listener != null)
                        {
                            FSPDataCToS data = ProtoBuffUtility.Deserialize <FSPDataCToS>(recvBuffer);
                            listener(data);
                        }
                        else
                        {
                            Debuger.LogError("找不到接收者!");
                        }
                    }
                }
            }
        }
        public void OnReceive(ISession session, byte[] bytes, int len)
        {
            NetMessage msg = new NetMessage();

            msg.Deserialize(bytes, len);
            if (session.IsAuth())
            {
                if (msg.Head.cmd == 0) //RPC
                {
                    RPCMessage rpcMessage = ProtoBuffUtility.Deserialize <RPCMessage>(msg.content);
                    HandleRPCMessage(session, rpcMessage);
                }
                else //Proto
                {
                    HandlePrptoMessage(session, msg);
                }
            }
            else
            {
                if (msg.Head.cmd == authCmd)
                {
                    HandlePrptoMessage(session, msg);
                }
                else
                {
                    Debuger.LogWarning("收到未授权的消息! cmd:{0}", msg.Head.cmd);
                }
            }
        }
        //--------------------------------------------------------------------------


        //--------------------------------------------------------------------------

        private void HandlePrptoMessage(NetMessage message)
        {
            if (message.Head.index == 0) //监听命令
            {
                if (ntfHelpers.ContainsKey(message.Head.cmd))
                {
                    var helper = ntfHelpers[message.Head.cmd];
                    if (helper != null)
                    {
                        object obj = ProtoBuffUtility.Deserialize(helper.typeMsg, message.content);
                        if (obj != null)
                        {
                            helper.successHandle.DynamicInvoke(obj);
                        }
                        else
                        {
                            Debuger.LogError("反序列化失败! cmd:{0}", message.Head.cmd);
                        }
                    }
                    else
                    {
                        Debuger.LogError("未找到对应的监听者! cmd:{0}", message.Head.cmd);
                    }
                }
                else
                {
                    Debuger.LogError("未找到对应的监听者! cmd:{0}", message.Head.cmd);
                }
            }
            else //一问一答
            {
                if (rspHelpers.ContainsKey(message.Head.index))
                {
                    var helper = rspHelpers[message.Head.index];
                    if (helper != null)
                    {
                        rspHelpers.Remove(message.Head.index);
                        object obj = ProtoBuffUtility.Deserialize(helper.typeMsg, message.content);
                        if (obj != null)
                        {
                            helper.successHandle.DynamicInvoke(obj);
                        }
                        else
                        {
                            Debuger.LogError("反序列化失败! cmd:{0}", message.Head.cmd);
                        }
                    }
                    else
                    {
                        Debuger.LogError("未找到对应的监听者! cmd:{0}", message.Head.cmd);
                    }
                }
                else
                {
                    Debuger.LogError("未找到对应的监听者! cmd:{0}", message.Head.cmd);
                }
            }
        }
        private void DoReceiveInMain()
        {
            lock (recvBufferQueue)
            {
                if (recvBufferQueue.Count > 0)
                {
                    byte[] buffer = recvBufferQueue.Dequeue();

                    IPCMessage msg = ProtoBuffUtility.Deserialize<IPCMessage>(buffer);
                    HandleMessage(msg);
                }
            }
        }
        private void OnReceive(byte[] bytes, int len)
        {
            NetMessage msg = new NetMessage();

            msg.Deserialize(bytes, len);
            if (msg.Head.cmd == 0)
            {
                RPCMessage rpcmsg = ProtoBuffUtility.Deserialize <RPCMessage>(msg.content);
                HandleRPCMessage(rpcmsg);
            }
            else
            {
                HandlePrptoMessage(msg);
            }
        }
        private void HandleRPCMessage(ISession session, RPCMessage rpcMessage)
        {
            RPCMethodHelper helper = rpcManager.GetMethodHelper(rpcMessage.name);

            if (helper != null)
            {
                object[] args      = new object[rpcMessage.rawargs.Count + 1];
                var      rawargs   = rpcMessage.rawargs;
                var      paramInfo = helper.method.GetParameters();
                args[0] = session;
                if (args.Length == paramInfo.Length)
                {
                    for (int i = 0; i < rawargs.Count; i++)
                    {
                        if (rawargs[i].type == enRPCArgType.PBObject)
                        {
                            args[i + 1] = ProtoBuffUtility.Deserialize(paramInfo[i + 1].ParameterType, rawargs[i].rawValue);
                        }
                        else
                        {
                            args[i + 1] = rawargs[i].value;
                        }
                    }

                    currInvokingName    = rpcMessage.name;
                    currInvokingSession = session;

                    try
                    {
                        helper.method.Invoke(helper.listener, BindingFlags.NonPublic, null, args, null);
                    }
                    catch (Exception e)
                    {
                        Debuger.LogError("RPC调用出错:{0} : {1}\n{2}", rpcMessage.name, e.Message, e.StackTrace);
                    }
                    currInvokingName    = null;
                    currInvokingSession = null;
                }
                else
                {
                    Debuger.LogWarning("参数数量不一致!{0}", rpcMessage.name);
                }
            }
            else
            {
                Debuger.LogWarning("RPC不存在!{0}", rpcMessage.name);
            }
        }
 private void HandleMessage(IPCMessage msg)
 {
     RPCMessage rpcmsg = msg.rpcMessage;
     var helper = rpcManager.GetMethodHelper(rpcmsg.name);
     if (helper != null)
     {
         object[] args  = new object[rpcmsg.args.Length +1];
         List<RPCArg> rawargs = rpcmsg.rawargs;
         ParameterInfo[] paramInfo = helper.method.GetParameters();
         if (args.Length == paramInfo.Length)
         {
             for (int i = 0; i < rawargs.Count; i++)
             {
                 if (rawargs[i].type == enRPCArgType.PBObject)
                 {
                     args[i + 1] = ProtoBuffUtility.Deserialize(paramInfo[i + 1].ParameterType, rawargs[i].rawValue);
                 }
                 else
                 {
                     args[i + 1] = rawargs[i].value;
                 }
             }
             args[0] = msg.srcID;
             currInvokingName = rpcmsg.name;
             currInvokingSrc = msg.srcID;
             try
             {
                 helper.method.Invoke(helper.listener, BindingFlags.NonPublic, null, args, null);
             }
             catch (Exception e)
             {
                 Debuger.LogError("RPC调用出错:{0}\n{1}", e.Message, e.StackTrace);
             }
             currInvokingName = "";
             currInvokingSrc = 0;
         }
         else
         {
             Debuger.LogWarning("参数数量不一致!");
         }
     }
     else
     {
         Debuger.LogWarning("RPC不存在!");
     }
 }
        private void HandleRPCMessage(RPCMessage rpcmsg)
        {
            Debuger.Log("Connection[{0}]-> {1}({2})", connection.ID, rpcmsg.name, rpcmsg.args);
            var helper = rpcManager.GetMethodHelper(rpcmsg.name);

            if (helper != null)
            {
                object[] args      = rpcmsg.args;
                var      rawargs   = rpcmsg.rawargs;
                var      paramInfo = helper.method.GetParameters();

                if (rawargs.Count == paramInfo.Length)
                {
                    for (int i = 0; i < rawargs.Count; i++)
                    {
                        if (rawargs[i].type == enRPCArgType.PBObject)
                        {
                            var    type = paramInfo[i].ParameterType;
                            object arg  = ProtoBuffUtility.Deserialize(type, rawargs[i].rawValue);
                            args[i] = arg;
                        }
                    }
                    currInvokingName = rpcmsg.name;
                    try
                    {
                        helper.method.Invoke(helper.listener, BindingFlags.NonPublic, null, args, null);
                    }
                    catch (Exception e)
                    {
                        Debuger.LogError("RPC调用出错:{0}: {1}\n{2}", rpcmsg.name, e.Message, e.StackTrace);
                    }
                    currInvokingName = null;
                }
                else
                {
                    Debuger.LogWarning("参数数量不一致!{0}", rpcmsg.name);
                }
            }
            else
            {
                Debuger.LogWarning("RPC不存在!{0} ", rpcmsg.name);
            }
        }
        //----------------------------------------------------------------------------



        //----------------------------------------------------------------------------

        private void HandlePrptoMessage(ISession session, NetMessage msg)
        {
            if (listenerHelpers.ContainsKey(msg.Head.cmd))
            {
                var helper = listenerHelpers[msg.Head.cmd];
                if (helper != null)
                {
                    object obj = ProtoBuffUtility.Deserialize(helper.typeMsg, msg.content);
                    if (obj != null)
                    {
                        helper.handleMsg.DynamicInvoke(session, msg.Head.index, obj);
                    }
                }
                else
                {
                    Debuger.LogWarning("未找到对应的监听者! cmd:{0}", msg.Head.cmd);
                }
            }
            else
            {
                Debuger.LogWarning("未找到对应的监听者! cmd:{0}", msg.Head.cmd);
            }
        }