Esempio n. 1
0
        // 处理消息
        public static void ProcessMessage(byte[] Buffer)
        {
            var nbs = new NetBitStream();

            nbs.BeginRead(Buffer);
            EProtocolId id = (EProtocolId)nbs.ReadProtocolHeader();

            //Debug($"处理协议->  {id} -> {(int)id} ......");

            if (EventHandler != null)
            {
                EventHandler(Buffer);
                return;
            }

            if (!DictProtocolEvent.TryGetValue(id, out var protocolevent))
            {
                Debug(string.Format("未注册协议->  {0} -> {1} ......", id, (int)id));
                return;
            }
            try
            {
                //处理
                protocolevent.ExecuteProtocolEvent(Buffer);
            }
            catch (Exception ex)
            {
                Error(string.Format("处理协议->  {0} -> {1}出错 ex:{2}", id, (int)id, ex.Message));
            }
        }
        private static void GateEventHandler(byte[] buffer)
        {
            var nbs = new NetBitStream();

            nbs.BeginRead(buffer);
            EProtocolId id     = (EProtocolId)nbs.ReadProtocolHeader();
            var         objMsg = ProtocolDump.Dump(id, buffer);

            if (objMsg == null)
            {
                loger.Error($"错误协议!{id}");
                return;
            }
            if (ClientDispatcher.DictProtocolEvent.TryGetValue(id, out var protocolevent))
            {
                protocolevent.ExecuteProtocolEvent(buffer);
                return;
            }

            if ((int)id < 100)
            {
                return;
            }
            long puid = ((ProtocolMsgBase)objMsg).Puid;

            if (puid == 0 && ((ProtocolMsgBase)objMsg).RspPuids.Count == 0)
            {
                loger.Error($"发送客户端消息错误,无发送目标!");
                return;
            }
            List <long> rspId = new List <long>();

            if (puid != 0)
            {
                rspId.Add(puid);
            }
            if (((ProtocolMsgBase)objMsg).RspPuids.Count > 0)
            {
                rspId.AddRange(((ProtocolMsgBase)objMsg).RspPuids);
            }
            SendMsg(objMsg, rspId);
        }
        private static void GameEventHandler(byte[] buffer)
        {
            var nbs = new NetBitStream();

            nbs.BeginRead(buffer);
            EProtocolId id     = (EProtocolId)nbs.ReadProtocolHeader();
            var         objMsg = ProtocolDump.Dump(id, buffer);

            if (objMsg == null)
            {
                loger.Error($"错误协议!{id}");
                return;
            }

            if (id == EProtocolId.S2C_SERVER_CONNECT)
            {
                var rsp = new C2S_Server_Connect()
                {
                    SessionType = (short)BaseServerInfo.SessionType
                };
                Dispatcher.SendByServerID(((S2C_Server_Connect)objMsg).ServerID, rsp);
                return;
            }
            if ((int)id < 100)
            {
                return;
            }
            if (!ClientDispatcher.DictProtocolEvent.TryGetValue(id, out var protocolevent))
            {
                loger.Debug(string.Format("未注册协议->  {0} -> {1} ......", id, (int)id));
                return;
            }
            try
            {
                //处理
                protocolevent.ExecuteProtocolEvent(buffer);
            }
            catch (Exception ex)
            {
                loger.Error(string.Format("处理协议->  {0} -> {1}出错 ex:{2}", id, (int)id, ex.Message));
            }
        }
Esempio n. 4
0
        static void Main(string[] args)
        {
            ushort id      = 10;
            byte   n       = 0;
            bool   b       = false;
            int    vint    = 100;
            uint   vint2   = 999;
            short  vshort  = 10;
            ushort vshort2 = 101;
            float  number  = 0.8f;
            string data    = "我是中国人";

            NetBitStream stream = new NetBitStream();

            stream.BeginWrite(id);
            stream.WriteByte(n);
            stream.WriteBool(b);
            stream.WriteInt(vint);
            stream.WriteUInt(vint2);
            stream.WriteShort(vshort);
            stream.WriteUShort(vshort2);
            stream.WriteFloat(number);
            stream.WriteString(data);
            stream.EncodeHeader();

            NetPacket packet = new NetPacket();

            packet.CopyBytes(stream);


            NetBitStream stream2 = new NetBitStream();

            stream2.BeginRead(packet, out id);
            stream2.ReadByte(out n);
            stream2.ReadBool(out b);
            stream2.ReadInt(out vint);
            stream2.ReadUInt(out vint2);
            stream2.ReadShort(out vshort);
            stream2.ReadUShort(out vshort2);
            stream2.ReadFloat(out number);
            stream2.ReadString(out data);

            System.Console.WriteLine(id);
            System.Console.WriteLine(n);
            System.Console.WriteLine(b);
            System.Console.WriteLine(vint);
            System.Console.WriteLine(vint2);
            System.Console.WriteLine(vshort);
            System.Console.WriteLine(vshort2);
            System.Console.WriteLine(number);
            System.Console.WriteLine(data);

            NetStructManager.TestStruct cif;
            cif.header = 10;
            cif.msgid  = 5;
            cif.m      = 0.9f;
            cif.n      = 10;
            cif.str    = "hello";


            byte[] bs = NetStructManager.getBytes(cif);
            NetStructManager.EncoderHeader(ref bs);

            NetStructManager.TestStruct cif2;
            System.Type type = typeof(NetStructManager.TestStruct);

            cif2 = (NetStructManager.TestStruct)NetStructManager.fromBytes(bs, type);

            System.Console.WriteLine(":" + bs.Length);
            System.Console.WriteLine(":" + cif2.header);
            System.Console.WriteLine(cif2.msgid);
            System.Console.WriteLine(cif2.m);
            System.Console.WriteLine(cif2.n);
            System.Console.WriteLine(cif2.str);


            ChatClient client = new ChatClient();

            client.Start();
            client.Update();
        }