Example #1
0
        private void HandleRPCMessage(RPCMessage rpcmsg)
        {
            Debuger.Log("Connection[{0}]-> {1}({2})", m_conn.id, rpcmsg.name, rpcmsg.args);

            var helper = m_rpc.GetMethodHelper(rpcmsg.name);

            if (helper != null)
            {
                object[] args     = rpcmsg.args;
                var      raw_args = rpcmsg.raw_args;

                var paramInfo = helper.method.GetParameters();

                if (raw_args.Count == paramInfo.Length)
                {
                    for (int i = 0; i < raw_args.Count; i++)
                    {
                        if (raw_args[i].type == RPCArgType.PBObject)
                        {
                            var    type = paramInfo[i].ParameterType;
                            object arg  = PBSerializer.NDeserialize(raw_args[i].raw_value, type);
                            args[i] = arg;
                        }
                    }

                    m_currInvokingName = rpcmsg.name;

                    try
                    {
                        helper.method.Invoke(helper.listener, BindingFlags.NonPublic, null, args, null);
                    }
                    catch (Exception e)
                    {
                        Debuger.LogError("RPC调用出错:{0}\n{1}", e.Message, e.StackTrace);
                    }

                    m_currInvokingName = null;
                }
                else
                {
                    Debuger.LogWarning("参数数量不一致!");
                }
            }
            else
            {
                Debuger.LogWarning("RPC不存在!");
            }
        }
Example #2
0
        public UDPSession()
        {
            codec  = new FrameCodec();
            typeDb = new PBTypeDB(Assembly.GetExecutingAssembly());
            cli    = new UDPClient();

            cli.OnConnect = peerId =>
            {
                Debug.Assert(this.peerId == 0);

                Connecting  = false;
                Connected   = true;
                this.peerId = peerId;

                OnConnect?.Invoke();
            };

            cli.OnDisconnect = peerId =>
            {
                Debug.Assert(this.peerId == peerId);


                Connecting  = false;
                Connected   = false;
                this.peerId = 0;

                OnDisconnect?.Invoke();
            };
            cli.OnReceive = (peerId, bytes, channelId) =>
            {
                Debug.Assert(this.peerId == peerId);

                codec.Feed(bytes);
            };

            codec.FrameCb = frame =>
            {
                try
                {
                    var msg = PBSerializer.Deserialize(typeDb, frame);
                    OnMsg?.Invoke(msg);
                }
                catch (Exception e)
                {
                    this.Fatal("{0}:{1}", peerId, e.ToString());
                }
            };
        }
Example #3
0
        public bool Open(string fullpath)
        {
            Debuger.Log("fullpath = " + fullpath);

            Byte[] bytes = FileUtils.ReadFile(fullpath);
            if (bytes != null && bytes.Length > 0)
            {
                m_content = PBSerializer.NDeserialize <NetDebugFileData>(bytes);
                return(ParserFile());
            }
            else
            {
                Debuger.LogError("File Is Not Exist, Or Open Wrong!");
                return(false);
            }
        }
Example #4
0
        public static void Init()
        {
            Debuger.Log("Path = " + Path);
            //加载配置

            var data = FileUtils.ReadFile(Path);

            if (data != null && data.Length > 0)
            {
                var cfg = PBSerializer.NDeserialize(data, typeof(AppConfig));
                if (cfg != null)
                {
                    m_value = cfg as AppConfig;
                }
            }
        }
Example #5
0
        private void OnReceive(byte[] bytes, int len)
        {
            NetMessage msg = new NetMessage();

            msg.Deserialize(bytes, len);

            if (msg.head.cmd == 0)
            {
                RPCMessage rpcmsg = PBSerializer.NDeserialize <RPCMessage>(msg.content);
                HandleRPCMessage(rpcmsg);
            }
            else
            {
                HandlePBMessage(msg);
            }
        }
Example #6
0
    //------------------------------------------------------------

    private void OnReceive(byte[] buffer, int size, IPEndPoint remotePoint)
    {
        FSPDataC2S data = PBSerializer.NDeserialize <FSPDataC2S>(buffer);

        FSPSession session = GetSession(data.sid);

        if (session == null)
        {
            //没有这个玩家,不理它的数据
            return;
        }


        session.EndPoint = remotePoint;
        session.Receive(data);
    }
Example #7
0
        private void HandlePBMessage(ISession session, NetMessage msg)
        {
            var helper = m_listMsgListener[msg.head.cmd];

            if (helper != null)
            {
                object obj = PBSerializer.NDeserialize(msg.content, helper.TMsg);
                if (obj != null)
                {
                    helper.onMsg.DynamicInvoke(session, msg.head.index, obj);
                }
            }
            else
            {
                Debuger.LogWarning("未找到对应的监听者! cmd:{0}", msg.head.cmd);
            }
        }
Example #8
0
        public void Send <TMsg>(ISession session, uint index, uint cmd, TMsg msg)
        {
            Debuger.Log("index:{0}, cmd:{1}", index, cmd);

            NetMessage msgobj = new NetMessage();

            msgobj.head.index    = index;
            msgobj.head.cmd      = cmd;
            msgobj.head.uid      = session.uid;
            msgobj.content       = PBSerializer.NSerialize(msg);
            msgobj.head.dataSize = (ushort)msgobj.content.Length;

            byte[] tmp;
            int    len = msgobj.Serialize(out tmp);

            session.Send(tmp, len);
        }
Example #9
0
        //------------------------------------------------------------

        private void OnReceive(byte[] buffer, int size, IPEndPoint remotePoint)
        {
            FSPDataC2S data = PBSerializer.NDeserialize <FSPDataC2S>(buffer);

            FSPSession session = GetSession(data.sid);

            if (session == null)
            {
                Debuger.LogWarning(LOG_TAG_RECV, "DoReceive() 收到一个未知的SID = " + data.sid);
                //没有这个玩家,不理它的数据
                return;
            }
            this.Log("DoReceive() Receive Buffer, SID={0}, IP={1}, Size={2}", session.Id, remotePoint, buffer.Length);

            session.EndPoint = remotePoint;
            session.Receive(data);
        }
Example #10
0
        public void Invoke(int dst, string name, params object[] args)
        {
            Debuger.Log("->[{0}] {1}({2})", dst, name, args.ToListString());

            RPCMessage rpcmsg = new RPCMessage();

            rpcmsg.name = name;
            rpcmsg.args = args;

            IPCMessage msg = new IPCMessage();

            msg.src = m_id;
            msg.rpc = rpcmsg;

            byte[] temp = PBSerializer.NSerialize(msg);
            SendMessage(dst, temp, temp.Length);
        }
Example #11
0
        void OnReceive(byte[] buffer, int size, IPEndPoint remotePoint)
        {
            if (m_RecvListener != null)
            {
                FSPData_SC data = PBSerializer.Deserialize <FSPData_SC>(buffer);

                var frames = data.frames;
                for (int i = 0; i < frames.Count; i++)
                {
                    m_RecvListener(frames[i]);
                }
            }
            else
            {
                Debuger.LogWarning(LOG_TAG, "no receive listener!");
            }
        }
Example #12
0
        //------------------------------------------------------------

        private void OnReceive(byte[] buffer, int size, IPEndPoint remotePoint)
        {
            FSPDataC2S data = PBSerializer.NDeserialize <FSPDataC2S>(buffer);

            FSPSession session = GetSession(data.sid);

            if (session == null)
            {
                MyLogger.LogWarning(LOG_TAG_RECV, "DoReceive()", "unknown SID = " + data.sid);
                //player does not exist,reply nothing
                return;
            }
            this.Log("DoReceive() Receive Buffer, SID={0}, IP={1}, Size={2}", session.Id, remotePoint, buffer.Length);

            session.EndPoint = remotePoint;
            session.Receive(data);
        }
Example #13
0
        private void HandlePBMessage(ISession session, NetMessage msg)
        {
            Debuger.LogVerbose("msg.head:{0}", msg.head);

            var helper = m_listMsgListener[msg.head.cmd];

            if (helper != null)
            {
                object obj = null;

                try
                {
                    obj = PBSerializer.NDeserialize(msg.content, helper.TMsg);
                }
                catch (Exception e)
                {
                    Debuger.LogError("MsgName:{0}, msg.head:{0}", helper.TMsg.Name, msg.head);
                    Debuger.LogError("DeserializeError:" + e.Message);
                }

                if (obj != null)
                {
                    try
                    {
                        if (helper.onMsg0 != null)
                        {
                            helper.onMsg0.DynamicInvoke(session, msg.head.index, obj);
                        }
                        else if (helper.onMsg1 != null)
                        {
                            helper.onMsg1.DynamicInvoke(session, msg.head, obj);
                        }
                    }
                    catch (Exception e)
                    {
                        Debuger.LogError("MsgName:{0}, msg.head:{0}", helper.TMsg.Name, msg.head);
                        Debuger.LogError("BusinessError:" + e.Message + "\n" + e.StackTrace);
                    }
                }
            }
            else
            {
                Debuger.LogWarning("未找到对应的监听者! cmd:{0}", msg.head.cmd);
            }
        }
Example #14
0
        private void HandlePBMessage(NetMessage msg)
        {
            if (msg.head.index == 0)
            {
                var helper = m_listNtfListener[msg.head.cmd];
                if (helper != null)
                {
                    object obj = PBSerializer.NDeserialize(msg.content, helper.TMsg);
                    if (obj != null)
                    {
                        helper.onMsg.DynamicInvoke(obj);
                    }
                    else
                    {
                        Debuger.LogError("协议格式错误! cmd:{0}", msg.head.cmd);
                    }
                }
                else
                {
                    Debuger.LogError("未找到对应的监听者! cmd:{0}", msg.head.cmd);
                }
            }
            else
            {
                var helper = m_listRspListener[msg.head.index];
                if (helper != null)
                {
                    m_listRspListener.Remove(msg.head.index);

                    object obj = PBSerializer.NDeserialize(msg.content, helper.TMsg);
                    if (obj != null)
                    {
                        helper.onMsg.DynamicInvoke(obj);
                    }
                    else
                    {
                        Debuger.LogError("协议格式错误! cmd:{0}, index:{0}", msg.head.cmd, msg.head.index);
                    }
                }
                else
                {
                    Debuger.LogError("未找到对应的监听者! cmd:{0}, index:{0}", msg.head.cmd, msg.head.index);
                }
            }
        }
Example #15
0
        public void Send <TReq, TRsp>(uint cmd, TReq req, Action <TRsp> onRsp, float timeout = 30,
                                      Action <NetErrorCode> onErr = null)
        {
            NetMessage msg = new NetMessage();

            msg.head.index    = MessageIndexGenerator.NewIndex();
            msg.head.cmd      = cmd;
            msg.head.uid      = m_uid;
            msg.content       = PBSerializer.NSerialize(req);
            msg.head.dataSize = (ushort)msg.content.Length;

            byte[] temp;
            int    len = msg.Serialize(out temp);

            m_conn.Send(temp, len);

            AddListener(cmd, typeof(TRsp), onRsp, msg.head.index, timeout, onErr);
        }
Example #16
0
        public void Invoke(ISession session, string name, params object[] args)
        {
            Debuger.LogVerbose("->Session[{0}] {1}({2})", session.Id, name, args.ToListString());

            RPCMessage rpcmsg = new RPCMessage();

            rpcmsg.name = name;
            rpcmsg.args = args;
            byte[] buffer = PBSerializer.NSerialize(rpcmsg);

            NetMessage msg = new NetMessage();

            msg.head          = new ProtocolHead();
            msg.head.dataSize = (uint)buffer.Length;
            msg.content       = buffer;

            session.Send(msg);
        }
Example #17
0
        public void Send <TReq>(uint cmd, TReq req)
        {
            Debuger.Log("cmd:{0}", cmd);


            NetMessage msg = new NetMessage();

            msg.head.index    = 0;
            msg.head.cmd      = cmd;
            msg.head.uid      = m_uid;
            msg.content       = PBSerializer.NSerialize(req);
            msg.head.dataSize = (ushort)msg.content.Length;

            byte[] temp;
            int    len = msg.Serialize(out temp);

            m_conn.Send(temp, len);
        }
Example #18
0
        public static void Init()
        {
            if (!File.Exists(Path))
            {
                Save();
                Debuger.Log("AppConfig First Create", "Init() Path = " + Path);
            }

            byte[] data = FileUtils.ReadFile(Path);
            if (data != null && data.Length > 0)
            {
                AppConfig cfg = (AppConfig)PBSerializer.NDeserialize(data, typeof(AppConfig));
                if (cfg != null)
                {
                    m_Value = cfg;
                    Debuger.Log("AppConfig Load", "Init() Path = " + Path);
                }
            }
        }
Example #19
0
        public void Invoke(string name, params object[] args)
        {
            Debuger.LogVerbose("->Connection[{0}] {1}({2})", m_conn.Id, name, args);

            RPCMessage rpcmsg = new RPCMessage();

            rpcmsg.name = name;
            rpcmsg.args = args;
            byte[] buffer = PBSerializer.NSerialize(rpcmsg);

            NetMessage msg = new NetMessage();

            msg.head          = new ProtocolHead();
            msg.head.token    = m_token;
            msg.head.dataSize = (uint)buffer.Length;
            msg.content       = buffer;

            m_conn.Send(msg);
        }
Example #20
0
        public void ReturnError(params object[] args)
        {
            var name = "On" + m_currInvokingName + "Error";


            RPCMessage rpcmsg = new RPCMessage();

            rpcmsg.name = name;
            rpcmsg.args = args;
            byte[] buffer = PBSerializer.NSerialize(rpcmsg);

            NetMessage msg = new NetMessage();

            msg.head          = new ProtocolHead();
            msg.head.dataSize = (uint)buffer.Length;
            msg.content       = buffer;

            m_currInvokingSession.Send(msg);
        }
Example #21
0
        /// <summary>
        /// start server
        /// </summary>
        public void StartServer()
        {
            FSPServer.Instance.Start(0);

            //customized game params
            //for example game map ID, random seed, game mode etc.
            GameParam gameParam = new GameParam();

            byte[] customGameParam = PBSerializer.NSerialize(gameParam);

            //send customized params to game room
            //those params would send to players when game starts
            FSPServer.Instance.Room.SetCustomGameParam(customGameParam);
            FSPServer.Instance.SetServerTimeout(0);

            string ipport = GetRoomIP() + ":" + GetRoomPort();

            onStartServer.Invoke(ipport);
        }
Example #22
0
        public void ReturnError(string errinfo, int errcode = 1)
        {
            var name = "On" + m_currInvokingName + "Error";

            Debuger.LogWarning("->[{0}] {1}({2},{3})", m_currInvokingSrc, name, errinfo, errcode);

            RPCMessage rpcmsg = new RPCMessage();

            rpcmsg.name = name;
            rpcmsg.args = new object[] { errinfo, errcode };

            IPCMessage msg = new IPCMessage();

            msg.src = m_id;
            msg.rpc = rpcmsg;

            byte[] temp = PBSerializer.NSerialize(msg);
            SendMessage(m_currInvokingSrc, temp, temp.Length);
        }
Example #23
0
        /// <summary>
        /// 启动服务器
        /// </summary>
        public void StartServer()
        {
            FSPServer.Instance.Start(0);

            //自定义的游戏参数
            //比如游戏的地图ID,随机种子,游戏模式等等
            GameParam gameParam = new GameParam();

            byte[] customGameParam = PBSerializer.NSerialize(gameParam);

            //将自定义游戏参数传给房间
            //以便于由房间通知玩家游戏开始时,能够将该参数转发给所有玩家
            FSPServer.Instance.Room.SetCustomGameParam(customGameParam);
            FSPServer.Instance.SetServerTimeout(0);

            string ipport = GetRoomIP() + ":" + GetRoomPort();

            onStartServer.Invoke(ipport);
        }
Example #24
0
        void Call_NotifyGameStart()
        {
            Debuger.Log(LOG_TAG, "Call_NotifyGameStart()");

            var players = m_data.players;

            FSPGameStartParam param = new FSPGameStartParam();

            param.fspParam = FSPServer.Instance.GetParam();

            foreach (var player in players)
            {
                param.players.Add(player);
            }
            param.customGameParam = m_customGameParam;

            FSPServer.Instance.StartGame();
            FSPServer.Instance.Game.onPlayerExit += _OnPlayerExit;
            FSPServer.Instance.Game.onGameEnd    += _OnGameEnd;

            for (int i = 0; i < players.Count; i++)
            {
                var player = players[i];

                param.fspParam.sid = player.sid;

                //将玩家加入到FSPServer中
                FSPServer.Instance.Game.AddPlayer(player.id, player.sid);

                if (m_mapUserId2Address.ContainsKey(player.userId))
                {
                    IPEndPoint address = m_mapUserId2Address[player.userId];

                    byte[] buff = PBSerializer.Serialize(param);
                    RPC(address, RoomRPC.RPC_NotifyGameStart, buff);
                }
                else
                {
                    Debuger.LogError(LOG_TAG, "User {0} does not have net address", player.userId);
                }
            }
        }
Example #25
0
        void _RPC_UpdateRoomInfo(IPEndPoint remote, byte[] bytes)
        {
            FSPRoomData data = PBSerializer.Deserialize <FSPRoomData>(bytes);

            m_listPlayerInfo = data.players;

            m_isInRoom = false;
            m_isReady  = false;

            for (int i = 0; i < m_listPlayerInfo.Count; ++i)
            {
                if (m_listPlayerInfo[i].userId == m_mainUserId)
                {
                    m_isInRoom = true;
                    m_isReady  = m_listPlayerInfo[i].isReady;
                }
            }

            EventManager.Instance.SendEvent("OnRoomUpdate");
        }
Example #26
0
        public void Return(params object[] args)
        {
            var name = "On" + m_currInvokingName;

            Debuger.Log("->[{0}] {1}({2})", m_currInvokingSrc, name, args.ToListString());

            RPCMessage rpcmsg = new RPCMessage();

            rpcmsg.name = name;
            rpcmsg.args = args;

            IPCMessage msg = new IPCMessage();

            msg.src = m_id;
            msg.rpc = rpcmsg;


            byte[] temp = PBSerializer.NSerialize(msg);
            SendMessage(m_currInvokingSrc, temp, temp.Length);
        }
Example #27
0
    public void StartHost()
    {
        if (!HasHost())
        {
            FSPServer.Instance.Start(0);
            FSPServer.Instance.SetServerTimeout(0);
            //将自定义游戏参数传给房间
            //以便于由房间通知玩家游戏开始时,能够将该参数转发给所有玩家
            byte[] customGameParam = PBSerializer.Serialize(new GamePlay.GameParam());
            FSPServer.Instance.Room.SetCustomGameParam(customGameParam);

            Debuger.Log("HostModule", string.Format("start host: {0}-{1}", ip, port));

            EventManager.Instance.SendEvent <string, int>("OnStartHost", ip, port);
            EventManager.Instance.SendEvent("OnHostChanged");
        }
        else
        {
            Debuger.Log("HostModule", "The host is alreay started");
        }
    }
Example #28
0
        public void Return(params object[] args)
        {
            if (m_conn != null)
            {
                var name = "On" + m_currInvokingName;
                Debuger.Log("->Connection[{0}] {1}({2})", m_conn.Id, name, args);

                RPCMessage rpcmsg = new RPCMessage();
                rpcmsg.name = name;
                rpcmsg.args = args;
                byte[] buffer = PBSerializer.NSerialize(rpcmsg);

                NetMessage msg = new NetMessage();
                msg.head          = new ProtocolHead();
                msg.head.token    = m_token;
                msg.head.dataSize = (uint)buffer.Length;
                msg.content       = buffer;

                m_conn.Send(msg);
            }
        }
Example #29
0
        public void Invoke(ISession[] listSession, string name, params object[] args)
        {
            Debuger.Log("->Session<Cnt={0}> {1}({2})", listSession.Length, name, args.ToListString());

            RPCMessage rpcmsg = new RPCMessage();

            rpcmsg.name = name;
            rpcmsg.args = args;
            byte[] buffer = PBSerializer.NSerialize(rpcmsg);

            NetMessage msg = new NetMessage();

            msg.head          = new ProtocolHead();
            msg.head.dataSize = (uint)buffer.Length;
            msg.content       = buffer;

            for (int i = 0; i < listSession.Length; i++)
            {
                listSession[i].Send(msg);
            }
        }
Example #30
0
        internal static void ReturnErrorMessage(Socket socket, IPEndPoint remoteEndPoint, NetErrorCode errcode, string errinfo = "")
        {
            NetErrorMessage msg = new NetErrorMessage();

            msg.code = (int)errcode;
            msg.info = errinfo;

            NetMessage msgobj = new NetMessage();

            msgobj.head.index    = 0;
            msgobj.head.cmd      = 0;
            msgobj.head.token    = 0;
            msgobj.head.sid      = 0;
            msgobj.content       = PBSerializer.NSerialize(msg);
            msgobj.head.dataSize = (uint)msgobj.content.Length;

            NetBuffer bufferSend = new NetBuffer(new byte[msgobj.Length]);

            msgobj.Serialize(bufferSend);
            socket.SendTo(bufferSend.GetBytes(), remoteEndPoint);
        }