diff --git a/AxibugEmuOnline.Client/Assets/Script/AppMain/Common/ProtoBufHelper.cs b/AxibugEmuOnline.Client/Assets/Script/AppMain/Common/ProtoBufHelper.cs index 4ff0eab0..59952fa3 100644 --- a/AxibugEmuOnline.Client/Assets/Script/AppMain/Common/ProtoBufHelper.cs +++ b/AxibugEmuOnline.Client/Assets/Script/AppMain/Common/ProtoBufHelper.cs @@ -1,21 +1,33 @@ -using Google.Protobuf; +using AxibugEmuOnline.Client.Network; +using Google.Protobuf; using System; namespace AxibugEmuOnline.Client.Common { public static class ProtoBufHelper { + private static ProtobufferMsgPool _msgPool = new ProtobufferMsgPool(); public static byte[] Serizlize(IMessage msg) { return msg.ToByteArray(); } - public static T DeSerizlize(byte[] bytes) + public static T DeSerizlizeFromPool(byte[] bytes) { var msgType = typeof(T); - object msg = Activator.CreateInstance(msgType); + object msg = _msgPool.Get(msgType); ((IMessage)msg).MergeFrom(bytes); return (T)msg; } + public static IMessage DeSerizlizeFromPool(byte[] bytes, Type protoType) + { + var msgType = protoType; + object msg = _msgPool.Get(msgType); + ((IMessage)msg).MergeFrom(bytes); + return (IMessage)msg; + } + public static void ReleaseToPool(IMessage msg) + { + _msgPool.Release(msg); + } } - } diff --git a/AxibugEmuOnline.Client/Assets/Script/AppMain/Emulator/EssgeeEmulator/UEssgeeInterface/UEGSaveByteConvert.cs b/AxibugEmuOnline.Client/Assets/Script/AppMain/Emulator/EssgeeEmulator/UEssgeeInterface/UEGSaveByteConvert.cs index 441a3192..3bc1ca2b 100644 --- a/AxibugEmuOnline.Client/Assets/Script/AppMain/Emulator/EssgeeEmulator/UEssgeeInterface/UEGSaveByteConvert.cs +++ b/AxibugEmuOnline.Client/Assets/Script/AppMain/Emulator/EssgeeEmulator/UEssgeeInterface/UEGSaveByteConvert.cs @@ -2,12 +2,17 @@ using System; using AxibugProtobuf; using AxibugEmuOnline.Client.Common; +/// +/// ProtoBuff¼浵 +/// public class UEGSaveByteConvert : IAxiEssgssStatusBytesCover { public AxiEssgssStatusData ToAxiEssgssStatusData(byte[] byteArray) { - pb_AxiEssgssStatusData pbdata = ProtoBufHelper.DeSerizlize(byteArray); - return pbAxiEssgssStatusDataToSrcData(pbdata); + pb_AxiEssgssStatusData dataFromPool = ProtoBufHelper.DeSerizlizeFromPool(byteArray); + AxiEssgssStatusData data = pbAxiEssgssStatusDataToSrcData(dataFromPool); + ProtoBufHelper.ReleaseToPool(dataFromPool); + return data; } public byte[] ToByteArray(AxiEssgssStatusData data) { diff --git a/AxibugEmuOnline.Client/Assets/Script/AppMain/Manager/AppChat.cs b/AxibugEmuOnline.Client/Assets/Script/AppMain/Manager/AppChat.cs index 029d76f7..935cd0f2 100644 --- a/AxibugEmuOnline.Client/Assets/Script/AppMain/Manager/AppChat.cs +++ b/AxibugEmuOnline.Client/Assets/Script/AppMain/Manager/AppChat.cs @@ -10,7 +10,7 @@ namespace AxibugEmuOnline.Client.Manager { public AppChat() { - NetMsg.Instance.RegNetMsgEvent((int)CommandID.CmdChatmsg, RecvChatMsg); + NetMsg.Instance.RegNetMsgEvent((int)CommandID.CmdChatmsg, RecvChatMsg); } public void SendChatMsg(string ChatMsg) @@ -22,9 +22,8 @@ namespace AxibugEmuOnline.Client.Manager App.network.SendToServer((int)CommandID.CmdChatmsg, ProtoBufHelper.Serizlize(msg)); } - public void RecvChatMsg(byte[] reqData) + public void RecvChatMsg(Protobuf_ChatMsg_RESP msg) { - Protobuf_ChatMsg_RESP msg = ProtoBufHelper.DeSerizlize(reqData); Eventer.Instance.PostEvent(EEvent.OnChatMsg, msg.NickName, msg.ChatMsg); } } diff --git a/AxibugEmuOnline.Client/Assets/Script/AppMain/Manager/AppLogin.cs b/AxibugEmuOnline.Client/Assets/Script/AppMain/Manager/AppLogin.cs index 11b34204..64f22168 100644 --- a/AxibugEmuOnline.Client/Assets/Script/AppMain/Manager/AppLogin.cs +++ b/AxibugEmuOnline.Client/Assets/Script/AppMain/Manager/AppLogin.cs @@ -14,7 +14,7 @@ namespace AxibugEmuOnline.Client.Manager public AppLogin() { - NetMsg.Instance.RegNetMsgEvent((int)CommandID.CmdLogin, RecvLoginMsg); + NetMsg.Instance.RegNetMsgEvent((int)CommandID.CmdLogin, RecvLoginMsg); } public void Login() @@ -67,9 +67,8 @@ namespace AxibugEmuOnline.Client.Manager App.network.SendToServer((int)CommandID.CmdLogin, ProtoBufHelper.Serizlize(msg)); } - public void RecvLoginMsg(byte[] reqData) + public void RecvLoginMsg(Protobuf_Login_RESP msg) { - Protobuf_Login_RESP msg = ProtoBufHelper.DeSerizlize(reqData); if (msg.Status == LoginResultStatus.Ok) { App.log.Info("登录成功"); diff --git a/AxibugEmuOnline.Client/Assets/Script/AppMain/Manager/AppRoom.cs b/AxibugEmuOnline.Client/Assets/Script/AppMain/Manager/AppRoom.cs index a2cd39d9..0d805251 100644 --- a/AxibugEmuOnline.Client/Assets/Script/AppMain/Manager/AppRoom.cs +++ b/AxibugEmuOnline.Client/Assets/Script/AppMain/Manager/AppRoom.cs @@ -41,17 +41,17 @@ namespace AxibugEmuOnline.Client.Manager Protobuf_Screnn_Frame _Protobuf_Screnn_Frame = new Protobuf_Screnn_Frame(); public AppRoom() { - NetMsg.Instance.RegNetMsgEvent((int)CommandID.CmdRoomList, RecvGetRoomList); - NetMsg.Instance.RegNetMsgEvent((int)CommandID.CmdRoomListUpdate, RecvGetRoomListUpdate); - NetMsg.Instance.RegNetMsgEvent((int)CommandID.CmdRoomGetScreen, RecvRoomGetScreen); - NetMsg.Instance.RegNetMsgEvent((int)CommandID.CmdRoomCreate, RecvCreateRoom); - NetMsg.Instance.RegNetMsgEvent((int)CommandID.CmdRoomJoin, RecvJoinRoom); - NetMsg.Instance.RegNetMsgEvent((int)CommandID.CmdRoomLeave, RecvLeavnRoom); - NetMsg.Instance.RegNetMsgEvent((int)CommandID.CmdRoomMyRoomStateChanged, RecvRoomMyRoomStateChange); - NetMsg.Instance.RegNetMsgEvent((int)CommandID.CmdRoomWaitStep, RecvRoom_WaitStep); - NetMsg.Instance.RegNetMsgEvent((int)CommandID.CmdRoomHostPlayerUpdateStateRaw, RecvHostPlayer_UpdateStateRaw); - NetMsg.Instance.RegNetMsgEvent((int)CommandID.CmdRoomSynPlayerInput, RecvHostSyn_RoomFrameAllInputData); - NetMsg.Instance.RegNetMsgEvent((int)CommandID.CmdScreen, OnScreen); + NetMsg.Instance.RegNetMsgEvent((int)CommandID.CmdRoomList, RecvGetRoomList); + NetMsg.Instance.RegNetMsgEvent((int)CommandID.CmdRoomListUpdate, RecvGetRoomListUpdate); + NetMsg.Instance.RegNetMsgEvent((int)CommandID.CmdRoomGetScreen, RecvRoomGetScreen); + NetMsg.Instance.RegNetMsgEvent((int)CommandID.CmdRoomCreate, RecvCreateRoom); + NetMsg.Instance.RegNetMsgEvent((int)CommandID.CmdRoomJoin, RecvJoinRoom); + NetMsg.Instance.RegNetMsgEvent((int)CommandID.CmdRoomLeave, RecvLeavnRoom); + NetMsg.Instance.RegNetMsgEvent((int)CommandID.CmdRoomMyRoomStateChanged, RecvRoomMyRoomStateChange); + NetMsg.Instance.RegNetMsgEvent((int)CommandID.CmdRoomWaitStep, RecvRoom_WaitStep); + NetMsg.Instance.RegNetMsgEvent((int)CommandID.CmdRoomHostPlayerUpdateStateRaw, RecvHostPlayer_UpdateStateRaw); + NetMsg.Instance.RegNetMsgEvent((int)CommandID.CmdRoomSynPlayerInput, RecvHostSyn_RoomFrameAllInputData); + NetMsg.Instance.RegNetMsgEvent((int)CommandID.CmdScreen, OnScreen); } #region 房间列表管理 @@ -153,10 +153,9 @@ namespace AxibugEmuOnline.Client.Manager /// 获取所有房间列表 /// /// - void RecvGetRoomList(byte[] reqData) + void RecvGetRoomList(Protobuf_Room_List_RESP msg) { App.log.Info("取得完整房间列表"); - Protobuf_Room_List_RESP msg = ProtoBufHelper.DeSerizlize(reqData); dictRoomListID2Info.Clear(); for (int i = 0; i < msg.RoomMiniInfoList.Count; i++) AddOrUpdateRoomList(msg.RoomMiniInfoList[i]); @@ -167,10 +166,9 @@ namespace AxibugEmuOnline.Client.Manager /// 获取单个列表更新 /// /// - void RecvGetRoomListUpdate(byte[] reqData) + void RecvGetRoomListUpdate(Protobuf_Room_Update_RESP msg) { App.log.Debug("单个房间状态更新"); - Protobuf_Room_Update_RESP msg = ProtoBufHelper.DeSerizlize(reqData); if (msg.UpdateType == 0) { if (AddOrUpdateRoomList(msg.RoomMiniInfo)) @@ -203,10 +201,9 @@ namespace AxibugEmuOnline.Client.Manager /// 获取单个房间画面 /// /// - void RecvRoomGetScreen(byte[] reqData) + void RecvRoomGetScreen(Protobuf_Room_Get_Screen_RESP msg) { App.log.Debug("单个房间状态更新"); - Protobuf_Room_Get_Screen_RESP msg = ProtoBufHelper.DeSerizlize(reqData); //解压 byte[] data = Helper.DecompressByteArray(msg.RawBitmap.ToArray()); Eventer.Instance.PostEvent(EEvent.OnRoomGetRoomScreen, msg.RoomID, data); @@ -230,10 +227,9 @@ namespace AxibugEmuOnline.Client.Manager /// 创建房间成功 /// /// - void RecvCreateRoom(byte[] reqData) + void RecvCreateRoom(Protobuf_Room_Create_RESP msg) { App.log.Debug("创建房间成功"); - Protobuf_Room_Create_RESP msg = ProtoBufHelper.DeSerizlize(reqData); mineRoomMiniInfo = msg.RoomMiniInfo; InitRePlay(); Eventer.Instance.PostEvent(EEvent.OnMineRoomCreated); @@ -258,10 +254,9 @@ namespace AxibugEmuOnline.Client.Manager /// 加入房间成功 /// /// - void RecvJoinRoom(byte[] reqData) + void RecvJoinRoom(Protobuf_Room_Join_RESP msg) { App.log.Debug("加入房间成功"); - Protobuf_Room_Join_RESP msg = ProtoBufHelper.DeSerizlize(reqData); mineRoomMiniInfo = msg.RoomMiniInfo; InitRePlay(); { @@ -287,25 +282,22 @@ namespace AxibugEmuOnline.Client.Manager /// 离开房间成功 /// /// - void RecvLeavnRoom(byte[] reqData) + void RecvLeavnRoom(Protobuf_Room_Leave_RESP msg) { App.log.Debug("离开房间成功"); - Protobuf_Room_Leave_RESP msg = ProtoBufHelper.DeSerizlize(reqData); ReleaseRePlay(); mineRoomMiniInfo = null; Eventer.Instance.PostEvent(EEvent.OnMineLeavnRoom); OverlayManager.PopTip($"你已经离开房间"); } - void RecvRoomMyRoomStateChange(byte[] reqData) + void RecvRoomMyRoomStateChange(Protobuf_Room_MyRoom_State_Change msg) { if (mineRoomMiniInfo == null) { App.log.Error("RecvRoomMyRoomStateChange 时 mineRoomMiniInfo 为空"); return; } - - Protobuf_Room_MyRoom_State_Change msg = ProtoBufHelper.DeSerizlize(reqData); long[] oldRoomPlayer = GetRoom4PlayerUIDs(); Protobuf_Room_GamePlaySlot[] oldslotArr = GetRoom4GameSlotMiniInfos(); mineRoomMiniInfo = msg.RoomMiniInfo; @@ -464,9 +456,8 @@ namespace AxibugEmuOnline.Client.Manager App.network.SendToServer((int)CommandID.CmdRoomHostPlayerUpdateStateRaw, ProtoBufHelper.Serizlize(msg)); } - void RecvRoom_WaitStep(byte[] reqData) + void RecvRoom_WaitStep(Protobuf_Room_WaitStep_RESP msg) { - Protobuf_Room_WaitStep_RESP msg = ProtoBufHelper.DeSerizlize(reqData); if (WaitStep != msg.WaitStep) { WaitStep = msg.WaitStep; @@ -481,10 +472,9 @@ namespace AxibugEmuOnline.Client.Manager } } - void RecvHostPlayer_UpdateStateRaw(byte[] reqData) + void RecvHostPlayer_UpdateStateRaw(Protobuf_Room_HostPlayer_UpdateStateRaw_RESP msg) { - Protobuf_Room_HostPlayer_UpdateStateRaw_RESP msg = ProtoBufHelper.DeSerizlize(reqData); - App.log.Info($"鸡翅孙当上报成功"); + App.log.Info($"即时存档上报成功"); } /// @@ -515,9 +505,8 @@ namespace AxibugEmuOnline.Client.Manager } ulong TestAllData = 0; - void RecvHostSyn_RoomFrameAllInputData(byte[] reqData) + void RecvHostSyn_RoomFrameAllInputData(Protobuf_Room_Syn_RoomFrameAllInputData msg) { - Protobuf_Room_Syn_RoomFrameAllInputData msg = ProtoBufHelper.DeSerizlize(reqData); if (TestAllData != msg.InputData) { TestAllData = msg.InputData; @@ -535,9 +524,8 @@ namespace AxibugEmuOnline.Client.Manager App.network.SendToServer((int)CommandID.CmdScreen, ProtoBufHelper.Serizlize(_Protobuf_Screnn_Frame)); } - public void OnScreen(byte[] reqData) + public void OnScreen(Protobuf_Screnn_Frame msg) { - Protobuf_Screnn_Frame msg = ProtoBufHelper.DeSerizlize(reqData); //解压 byte[] data = Helper.DecompressByteArray(msg.RawBitmap.ToArray()); } diff --git a/AxibugEmuOnline.Client/Assets/Script/AppMain/Manager/AppShare.cs b/AxibugEmuOnline.Client/Assets/Script/AppMain/Manager/AppShare.cs index 0d2d05c1..0a1facea 100644 --- a/AxibugEmuOnline.Client/Assets/Script/AppMain/Manager/AppShare.cs +++ b/AxibugEmuOnline.Client/Assets/Script/AppMain/Manager/AppShare.cs @@ -11,8 +11,8 @@ namespace AxibugEmuOnline.Client.Manager { public AppShare() { - NetMsg.Instance.RegNetMsgEvent((int)CommandID.CmdGameMark, RecvGameStar); - NetMsg.Instance.RegNetMsgEvent((int)CommandID.CmdGamescreenImgUpload, RecvGamescreenImgUpload); + NetMsg.Instance.RegNetMsgEvent((int)CommandID.CmdGameMark, RecvGameStar); + NetMsg.Instance.RegNetMsgEvent((int)CommandID.CmdGamescreenImgUpload, RecvGamescreenImgUpload); } /// @@ -35,9 +35,8 @@ namespace AxibugEmuOnline.Client.Manager /// 收藏 /// /// - void RecvGameStar(byte[] reqData) + void RecvGameStar(Protobuf_Game_Mark_RESP msg) { - Protobuf_Game_Mark_RESP msg = ProtoBufHelper.DeSerizlize(reqData); Eventer.Instance.PostEvent(EEvent.OnRomStarStateChanged, msg.RomID, msg.IsStar == 1); } @@ -63,7 +62,7 @@ namespace AxibugEmuOnline.Client.Manager App.network.SendToServer((int)CommandID.CmdGamescreenImgUpload, ProtoBufHelper.Serizlize(req)); } - private void RecvGamescreenImgUpload(byte[] data) + private void RecvGamescreenImgUpload(Protobuf_GameScreen_Img_Upload_RESP msg) { OverlayManager.PopTip("封面图上传成功"); } diff --git a/AxibugEmuOnline.Client/Assets/Script/AppMain/Manager/SaveSlotManager/SavCloudApi.cs b/AxibugEmuOnline.Client/Assets/Script/AppMain/Manager/SaveSlotManager/SavCloudApi.cs index 8821f113..66445fd8 100644 --- a/AxibugEmuOnline.Client/Assets/Script/AppMain/Manager/SaveSlotManager/SavCloudApi.cs +++ b/AxibugEmuOnline.Client/Assets/Script/AppMain/Manager/SaveSlotManager/SavCloudApi.cs @@ -20,9 +20,9 @@ namespace AxibugEmuOnline.Client public SavCloudApi() { - NetMsg.Instance.RegNetMsgEvent((int)CommandID.CmdGamesavGetGameSavList, RecvGetGameSavList); - NetMsg.Instance.RegNetMsgEvent((int)CommandID.CmdGamesavDelGameSav, RecvDelGameSavList); - NetMsg.Instance.RegNetMsgEvent((int)CommandID.CmdGamesavUploadGameSav, RecvUpLoadGameSav); + NetMsg.Instance.RegNetMsgEvent((int)CommandID.CmdGamesavGetGameSavList, RecvGetGameSavList); + NetMsg.Instance.RegNetMsgEvent((int)CommandID.CmdGamesavDelGameSav, RecvDelGameSavList); + NetMsg.Instance.RegNetMsgEvent((int)CommandID.CmdGamesavUploadGameSav, RecvUpLoadGameSav); } private HashSet m_fetchingRomIDs = new HashSet(); @@ -42,10 +42,8 @@ namespace AxibugEmuOnline.Client App.network.SendToServer((int)CommandID.CmdGamesavGetGameSavList, ProtoBufHelper.Serizlize(req)); } - void RecvGetGameSavList(byte[] reqData) + void RecvGetGameSavList(Protobuf_Mine_GetGameSavList_RESP msg) { - Protobuf_Mine_GetGameSavList_RESP msg = ProtoBufHelper.DeSerizlize(reqData); - if (m_fetchingRomIDs.Remove(msg.RomID)) return; Protobuf_Mine_GameSavInfo[] savArr = new Protobuf_Mine_GameSavInfo[4]; @@ -73,9 +71,8 @@ namespace AxibugEmuOnline.Client App.network.SendToServer((int)CommandID.CmdGamesavDelGameSav, ProtoBufHelper.Serizlize(req)); } - void RecvDelGameSavList(byte[] reqData) + void RecvDelGameSavList(Protobuf_Mine_DelGameSav_RESP msg) { - Protobuf_Mine_DelGameSav_RESP msg = ProtoBufHelper.DeSerizlize(reqData); Eventer.Instance.PostEvent(EEvent.OnNetGameSavDeleted, msg.RomID, msg.SavDataIdx); } @@ -108,9 +105,8 @@ namespace AxibugEmuOnline.Client App.network.SendToServer((int)CommandID.CmdGamesavUploadGameSav, ProtoBufHelper.Serizlize(req)); } - void RecvUpLoadGameSav(byte[] reqData) + void RecvUpLoadGameSav(Protobuf_Mine_UpLoadGameSav_RESP msg) { - Protobuf_Mine_UpLoadGameSav_RESP msg = ProtoBufHelper.DeSerizlize(reqData); OnUploadedSavData?.Invoke(msg.RomID, msg.SavDataIdx, msg.UploadSevInfo); } diff --git a/AxibugEmuOnline.Client/Assets/Script/AppMain/Manager/UserDataManager.cs b/AxibugEmuOnline.Client/Assets/Script/AppMain/Manager/UserDataManager.cs index f6bd247f..0bb2af69 100644 --- a/AxibugEmuOnline.Client/Assets/Script/AppMain/Manager/UserDataManager.cs +++ b/AxibugEmuOnline.Client/Assets/Script/AppMain/Manager/UserDataManager.cs @@ -29,13 +29,13 @@ namespace AxibugEmuOnline.Client.Manager App.network.OnReConnected += OnReConnected; //网络事件注册 - NetMsg.Instance.RegNetMsgEvent((int)CommandID.CmdUserOnlinelist, RecvUserOnlinelist); - NetMsg.Instance.RegNetMsgEvent((int)CommandID.CmdUserJoin, RecvCmdUserJoin); - NetMsg.Instance.RegNetMsgEvent((int)CommandID.CmdUserLeave, RecvGetUserLeave); + NetMsg.Instance.RegNetMsgEvent((int)CommandID.CmdUserOnlinelist, RecvUserOnlinelist); + NetMsg.Instance.RegNetMsgEvent((int)CommandID.CmdUserJoin, RecvCmdUserJoin); + NetMsg.Instance.RegNetMsgEvent((int)CommandID.CmdUserLeave, RecvGetUserLeave); - NetMsg.Instance.RegNetMsgEvent((int)CommandID.CmdModifyNickName, RecvModifyNickName); - NetMsg.Instance.RegNetMsgEvent((int)CommandID.CmdUpdateSelfUserInfo, RecvUpdateSelfUserInfo); - NetMsg.Instance.RegNetMsgEvent((int)CommandID.CmdUpdateOtherUserInfo, RecvUpdateOtherUserInfo); + NetMsg.Instance.RegNetMsgEvent((int)CommandID.CmdModifyNickName, RecvModifyNickName); + NetMsg.Instance.RegNetMsgEvent((int)CommandID.CmdUpdateSelfUserInfo, RecvUpdateSelfUserInfo); + NetMsg.Instance.RegNetMsgEvent((int)CommandID.CmdUpdateOtherUserInfo, RecvUpdateOtherUserInfo); } public MainUserDataBase userdata { get; private set; } = new MainUserDataBase(); @@ -153,9 +153,8 @@ namespace AxibugEmuOnline.Client.Manager App.network.SendToServer((int)CommandID.CmdUserOnlinelist, ProtoBufHelper.Serizlize(msg)); } - public void RecvUserOnlinelist(byte[] reqData) + public void RecvUserOnlinelist(Protobuf_UserList_RESP msg) { - Protobuf_UserList_RESP msg = ProtoBufHelper.DeSerizlize(reqData); DictUID2User.Clear(); for (int i = 0; i < msg.UserList.Count; i++) { @@ -165,9 +164,8 @@ namespace AxibugEmuOnline.Client.Manager } Eventer.Instance.PostEvent(EEvent.OnUserListAllUpdate); } - public void RecvCmdUserJoin(byte[] reqData) + public void RecvCmdUserJoin(Protobuf_UserJoin_RESP msg) { - Protobuf_UserJoin_RESP msg = ProtoBufHelper.DeSerizlize(reqData); bool isNewUser; UpdateOrAddUser(msg.UserInfo, out isNewUser); if (isNewUser) @@ -177,9 +175,8 @@ namespace AxibugEmuOnline.Client.Manager } } - public void RecvGetUserLeave(byte[] reqData) + public void RecvGetUserLeave(Protobuf_UserLeave_RESP msg) { - Protobuf_UserLeave_RESP msg = ProtoBufHelper.DeSerizlize(reqData); RemoveUser(msg.UID); } @@ -196,28 +193,23 @@ namespace AxibugEmuOnline.Client.Manager App.network.SendToServer((int)CommandID.CmdModifyNickName, ProtoBufHelper.Serizlize(msg)); } - void RecvModifyNickName(byte[] reqData) + void RecvModifyNickName(Protobuf_Modify_NickName_RESP msg) { - Protobuf_Modify_NickName_RESP msg = ProtoBufHelper.DeSerizlize(reqData); } - private void RecvUpdateSelfUserInfo(byte[] reqData) + private void RecvUpdateSelfUserInfo(Protobuf_Update_UserInfo_RESP msg) { - Protobuf_Update_UserInfo_RESP msg = ProtoBufHelper.DeSerizlize(reqData); userdata.NickName = msg.UserInfo.NickName; Eventer.Instance.PostEvent(EEvent.OnSelfInfoUpdate); } - private void RecvUpdateOtherUserInfo(byte[] reqData) + private void RecvUpdateOtherUserInfo(Protobuf_Update_OtherUserInfo_RESP msg) { - Protobuf_Update_OtherUserInfo_RESP msg = ProtoBufHelper.DeSerizlize(reqData); UserDataBase userdata = GetUserByUid(msg.UID); if (userdata == null) return; userdata.NickName = msg.UserInfo.NickName; - App.roomMgr.ChangeCurrRoomPlayerName(msg.UID); - Eventer.Instance.PostEvent(EEvent.OnOtherUserInfoUpdate, msg.UID); } diff --git a/AxibugEmuOnline.Client/Assets/Script/AppMain/MonoCom/TickLoop.cs b/AxibugEmuOnline.Client/Assets/Script/AppMain/MonoCom/TickLoop.cs index 858e1ad7..b2722b4b 100644 --- a/AxibugEmuOnline.Client/Assets/Script/AppMain/MonoCom/TickLoop.cs +++ b/AxibugEmuOnline.Client/Assets/Script/AppMain/MonoCom/TickLoop.cs @@ -26,8 +26,8 @@ namespace AxibugEmuOnline.Client public const int NetAveDelayCount = 3; private void Awake() { - NetMsg.Instance.RegNetMsgEvent((int)CommandID.CmdPing, OnCmdPing); - NetMsg.Instance.RegNetMsgEvent((int)CommandID.CmdPong, OnCmdPong); + NetMsg.Instance.RegNetMsgEvent((int)CommandID.CmdPing, OnCmdPing); + NetMsg.Instance.RegNetMsgEvent((int)CommandID.CmdPong, OnCmdPong); LoopAction_3s += Ping; @@ -97,10 +97,9 @@ namespace AxibugEmuOnline.Client } - public void OnCmdPing(byte[] reqData) + public void OnCmdPing(Protobuf_Ping msg) { //App.log.Debug($"OnCmdPing"); - Protobuf_Ping msg = ProtoBufHelper.DeSerizlize(reqData); Protobuf_Pong resp = new Protobuf_Pong() { Seed = msg.Seed, @@ -108,10 +107,9 @@ namespace AxibugEmuOnline.Client App.network.SendToServer((int)CommandID.CmdPong, ProtoBufHelper.Serizlize(resp)); } - public void OnCmdPong(byte[] reqData) + public void OnCmdPong(Protobuf_Pong msg) { //App.log.Debug($"OnCmdPong"); - Protobuf_Pong msg = ProtoBufHelper.DeSerizlize(reqData); if (LastPingSeed == msg.Seed) { diff --git a/AxibugEmuOnline.Client/Assets/Script/AppMain/Network/NetMsg.cs b/AxibugEmuOnline.Client/Assets/Script/AppMain/Network/NetMsg.cs index dd75c8d0..196fca38 100644 --- a/AxibugEmuOnline.Client/Assets/Script/AppMain/Network/NetMsg.cs +++ b/AxibugEmuOnline.Client/Assets/Script/AppMain/Network/NetMsg.cs @@ -1,9 +1,10 @@ using AxibugEmuOnline.Client.ClientCore; -using AxibugEmuOnline.Client.Event; using AxibugProtobuf; +using Google.Protobuf; using System; using System.Collections.Generic; -using UnityEngine; +using System.Linq; +using AxibugEmuOnline.Client.Common; namespace AxibugEmuOnline.Client.Network { @@ -24,11 +25,28 @@ namespace AxibugEmuOnline.Client.Network private NetMsg() { } + private static Dictionary cmd2MsgTypeDict = new Dictionary(); + private static Type GetTypeByCmd(int cmd, List delegates) + { + if (cmd2MsgTypeDict.TryGetValue(cmd, out var type)) return type; + var paramters = delegates.First().Method.GetParameters(); + if (paramters.Length != 0) + { + var protoType = paramters[0].ParameterType; + if (!protoType.IsInterface && !protoType.IsAbstract) + { + cmd2MsgTypeDict[cmd] = protoType; + return protoType; + } + } + + return null; + } #region RegisterMsgEvent - public void RegNetMsgEvent(int cmd, Action callback) + public void RegNetMsgEvent(int cmd, Action callback) where T : IMessage { InterRegNetMsgEvent(cmd, callback); } @@ -99,10 +117,10 @@ namespace AxibugEmuOnline.Client.Network void PostNetEventFromNet(Action act) { try - { + { act?.Invoke(); } - catch(Exception ex) + catch (Exception ex) { App.log.Error(ex.ToString()); } @@ -171,7 +189,7 @@ namespace AxibugEmuOnline.Client.Network default: break; } - OverlayManager.PopTip("错误:"+ errMsg); + OverlayManager.PopTip("错误:" + errMsg); App.log.Error("错误:" + errMsg); } @@ -182,11 +200,16 @@ namespace AxibugEmuOnline.Client.Network List eventList = GetNetEventDicList(cmd); if (eventList != null) { + Type protoType = GetTypeByCmd(cmd, eventList); foreach (Delegate callback in eventList) { try { - ((Action)callback)(arg); + IMessage protobufMsg = ProtoBufHelper.DeSerizlizeFromPool(arg, protoType); + //((Action)callback)(protobufMsg); + callback.DynamicInvoke(protobufMsg); + ProtoBufHelper.ReleaseToPool(protobufMsg); + //((Action)callback)(arg); } catch (Exception e) { diff --git a/AxibugEmuOnline.Client/Assets/Script/AppMain/Network/ProtobufferMsgPool.cs b/AxibugEmuOnline.Client/Assets/Script/AppMain/Network/ProtobufferMsgPool.cs new file mode 100644 index 00000000..6075fc72 --- /dev/null +++ b/AxibugEmuOnline.Client/Assets/Script/AppMain/Network/ProtobufferMsgPool.cs @@ -0,0 +1,58 @@ +using AxibugEmuOnline.Client.ClientCore; +using Google.Protobuf; +using System; +using System.Collections.Generic; + +namespace AxibugEmuOnline.Client.Network +{ + public class ProtobufferMsgPool + { + Dictionary> _pool = new Dictionary>(); + + public IMessage Get(Type msgType) + { + if (!_pool.TryGetValue(msgType, out var queue)) + { + queue = new Queue(); + _pool[msgType] = queue; + } + + if (queue.Count == 0) + { + var msgIns = Activator.CreateInstance(msgType) as IMessage; + return msgIns; + } + else + { + var msgIns = queue.Dequeue(); + msgIns.Reset(); + + return msgIns as IMessage; + } + } + + public void Release(IMessage msg) + { + if (msg is IResetable resetableMsg) + { + var msgType = msg.GetType(); + if (!_pool.TryGetValue(msgType, out var queue)) + { + queue = new Queue(); + _pool[msgType] = queue; + } + + queue.Enqueue(resetableMsg); + } + else + { + App.log.Error("[NET]" + msg.GetType() + "}未生成Resetable代码"); + } + } + } + + public interface IResetable + { + void Reset(); + } +} \ No newline at end of file diff --git a/AxibugEmuOnline.Client/Assets/Script/AppMain/Network/ProtobufferMsgPool.cs.meta b/AxibugEmuOnline.Client/Assets/Script/AppMain/Network/ProtobufferMsgPool.cs.meta new file mode 100644 index 00000000..6a20a849 --- /dev/null +++ b/AxibugEmuOnline.Client/Assets/Script/AppMain/Network/ProtobufferMsgPool.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: 5d5918c81333c984c8912ed3212e960e \ No newline at end of file diff --git a/AxibugEmuOnline.Client/Assets/Script/AppMain/Network/ProtobufferMsgPool.g.cs b/AxibugEmuOnline.Client/Assets/Script/AppMain/Network/ProtobufferMsgPool.g.cs new file mode 100644 index 00000000..e9775440 --- /dev/null +++ b/AxibugEmuOnline.Client/Assets/Script/AppMain/Network/ProtobufferMsgPool.g.cs @@ -0,0 +1,746 @@ +using AxibugEmuOnline.Client.Network; + +namespace AxibugProtobuf +{ + public sealed partial class Protobuf_ChatMsg : IResetable + { + public void Reset() + { + ChatMsg = string.Empty; + } + } +} + + +namespace AxibugProtobuf +{ + public sealed partial class Protobuf_ChatMsg_RESP : IResetable + { + public void Reset() + { + NickName = string.Empty; + ChatMsg = string.Empty; + Date = default; + } + } +} + + +namespace AxibugProtobuf +{ + public sealed partial class Protobuf_Ping : IResetable + { + public void Reset() + { + Seed = default; + } + } +} + + +namespace AxibugProtobuf +{ + public sealed partial class Protobuf_Pong : IResetable + { + public void Reset() + { + Seed = default; + } + } +} + + +namespace AxibugProtobuf +{ + public sealed partial class Protobuf_Login : IResetable + { + public void Reset() + { + LoginType = default; + DeviceType = default; + DeviceStr = string.Empty; + Account = string.Empty; + Password = string.Empty; + } + } +} + + +namespace AxibugProtobuf +{ + public sealed partial class Protobuf_Login_RESP : IResetable + { + public void Reset() + { + NickName = string.Empty; + Token = string.Empty; + LastLoginDate = string.Empty; + RegDate = string.Empty; + Status = default; + UID = default; + } + } +} + + +namespace AxibugProtobuf +{ + public sealed partial class Protobuf_Token_Struct : IResetable + { + public void Reset() + { + UID = default; + TokenGenDate = default; + Seed = default; + } + } +} + + +namespace AxibugProtobuf +{ + public sealed partial class Protobuf_UserList : IResetable + { + public void Reset() + { + } + } +} + + +namespace AxibugProtobuf +{ + public sealed partial class Protobuf_UserList_RESP : IResetable + { + public void Reset() + { + UserCount = default; + UserList?.Clear(); + } + } +} + + +namespace AxibugProtobuf +{ + public sealed partial class Protobuf_UserJoin_RESP : IResetable + { + public void Reset() + { + UserInfo?.Reset(); + } + } +} + + +namespace AxibugProtobuf +{ + public sealed partial class Protobuf_UserLeave_RESP : IResetable + { + public void Reset() + { + UID = default; + } + } +} + + +namespace AxibugProtobuf +{ + public sealed partial class Protobuf_UserState_RESP : IResetable + { + public void Reset() + { + UID = default; + State = default; + } + } +} + + +namespace AxibugProtobuf +{ + public sealed partial class UserMiniInfo : IResetable + { + public void Reset() + { + UID = default; + NickName = string.Empty; + DeviceType = default; + } + } +} + + +namespace AxibugProtobuf +{ + public sealed partial class Protobuf_Modify_NickName : IResetable + { + public void Reset() + { + NickName = string.Empty; + } + } +} + + +namespace AxibugProtobuf +{ + public sealed partial class Protobuf_Modify_NickName_RESP : IResetable + { + public void Reset() + { + } + } +} + + +namespace AxibugProtobuf +{ + public sealed partial class Protobuf_Update_UserInfo_RESP : IResetable + { + public void Reset() + { + UserInfo?.Reset(); + } + } +} + + +namespace AxibugProtobuf +{ + public sealed partial class Protobuf_Update_OtherUserInfo_RESP : IResetable + { + public void Reset() + { + UID = default; + UserInfo?.Reset(); + } + } +} + + +namespace AxibugProtobuf +{ + public sealed partial class Protobuf_Room_List : IResetable + { + public void Reset() + { + } + } +} + + +namespace AxibugProtobuf +{ + public sealed partial class Protobuf_Room_List_RESP : IResetable + { + public void Reset() + { + RoomMiniInfoList?.Clear(); + } + } +} + + +namespace AxibugProtobuf +{ + public sealed partial class Protobuf_Room_MiniInfo : IResetable + { + public void Reset() + { + RoomID = default; + GameRomID = default; + GameRomHash = string.Empty; + GamePlatformType = default; + HostPlayerUID = default; + GameState = default; + ObsUserCount = default; + ScreenProviderUID = default; + GamePlaySlotList?.Clear(); + } + } +} + + +namespace AxibugProtobuf +{ + public sealed partial class Protobuf_Room_GamePlaySlot : IResetable + { + public void Reset() + { + PlayerUID = default; + PlayerNickName = string.Empty; + DeviceType = default; + PlayerLocalJoyIdx = default; + PlayerLocalGamePadType = default; + } + } +} + + +namespace AxibugProtobuf +{ + public sealed partial class Protobuf_Room_Update_RESP : IResetable + { + public void Reset() + { + UpdateType = default; + RoomMiniInfo?.Reset(); + } + } +} + + +namespace AxibugProtobuf +{ + public sealed partial class Protobuf_Screnn_Frame : IResetable + { + public void Reset() + { + RoomID = default; + FrameID = default; + RawBitmap = Google.Protobuf.ByteString.Empty; + } + } +} + + +namespace AxibugProtobuf +{ + public sealed partial class Protobuf_Room_SinglePlayerInputData : IResetable + { + public void Reset() + { + FrameID = default; + InputData = default; + } + } +} + + +namespace AxibugProtobuf +{ + public sealed partial class Protobuf_Room_Syn_RoomFrameAllInputData : IResetable + { + public void Reset() + { + FrameID = default; + InputData = default; + ServerFrameID = default; + ServerForwardCount = default; + } + } +} + + +namespace AxibugProtobuf +{ + public sealed partial class Protobuf_Room_Create : IResetable + { + public void Reset() + { + GameRomID = default; + GameRomHash = string.Empty; + } + } +} + + +namespace AxibugProtobuf +{ + public sealed partial class Protobuf_Room_Create_RESP : IResetable + { + public void Reset() + { + RoomMiniInfo?.Reset(); + } + } +} + + +namespace AxibugProtobuf +{ + public sealed partial class Protobuf_Room_Join : IResetable + { + public void Reset() + { + RoomID = default; + } + } +} + + +namespace AxibugProtobuf +{ + public sealed partial class Protobuf_Room_Join_RESP : IResetable + { + public void Reset() + { + RoomMiniInfo?.Reset(); + } + } +} + + +namespace AxibugProtobuf +{ + public sealed partial class Protobuf_Room_Leave : IResetable + { + public void Reset() + { + RoomID = default; + } + } +} + + +namespace AxibugProtobuf +{ + public sealed partial class Protobuf_Room_Leave_RESP : IResetable + { + public void Reset() + { + RoomID = default; + } + } +} + + +namespace AxibugProtobuf +{ + public sealed partial class Protobuf_Room_MyRoom_State_Change : IResetable + { + public void Reset() + { + RoomMiniInfo?.Reset(); + } + } +} + + +namespace AxibugProtobuf +{ + public sealed partial class Protobuf_Room_Change_PlaySlotWithJoy : IResetable + { + public void Reset() + { + SlotWithJoy?.Clear(); + } + } +} + + +namespace AxibugProtobuf +{ + public sealed partial class Protobuf_PlaySlotIdxWithJoyIdx : IResetable + { + public void Reset() + { + PlayerSlotIdx = default; + PlayerLocalJoyIdx = default; + PlayerLocalGamePadType = default; + } + } +} + + +namespace AxibugProtobuf +{ + public sealed partial class Protobuf_Room_Change_PlaySlotWithJoy_RESP : IResetable + { + public void Reset() + { + } + } +} + + +namespace AxibugProtobuf +{ + public sealed partial class Protobuf_Room_WaitStep_RESP : IResetable + { + public void Reset() + { + WaitStep = default; + LoadStateRaw = Google.Protobuf.ByteString.Empty; + } + } +} + + +namespace AxibugProtobuf +{ + public sealed partial class Protobuf_Room_HostPlayer_UpdateStateRaw : IResetable + { + public void Reset() + { + LoadStateRaw = Google.Protobuf.ByteString.Empty; + } + } +} + + +namespace AxibugProtobuf +{ + public sealed partial class Protobuf_Room_HostPlayer_UpdateStateRaw_RESP : IResetable + { + public void Reset() + { + } + } +} + + +namespace AxibugProtobuf +{ + public sealed partial class Protobuf_Room_Player_Ready : IResetable + { + public void Reset() + { + PushFrameNeedTimeUs = default; + LoadStateNeedTimeUs = default; + VideoFrameShowNeedTimeUs = default; + AudioFramePlayNeedTimeUs = default; + } + } +} + + +namespace AxibugProtobuf +{ + public sealed partial class Protobuf_Room_Get_Screen : IResetable + { + public void Reset() + { + RoomID = default; + } + } +} + + +namespace AxibugProtobuf +{ + public sealed partial class Protobuf_Room_Get_Screen_RESP : IResetable + { + public void Reset() + { + RoomID = default; + FrameID = default; + RawBitmap = Google.Protobuf.ByteString.Empty; + } + } +} + + +namespace AxibugProtobuf +{ + public sealed partial class Protobuf_Game_Mark : IResetable + { + public void Reset() + { + RomID = default; + Motion = default; + } + } +} + + +namespace AxibugProtobuf +{ + public sealed partial class Protobuf_Game_Mark_RESP : IResetable + { + public void Reset() + { + RomID = default; + IsStar = default; + Stars = default; + } + } +} + + +namespace AxibugProtobuf +{ + public sealed partial class Protobuf_Mine_GetGameSavList : IResetable + { + public void Reset() + { + RomID = default; + } + } +} + + +namespace AxibugProtobuf +{ + public sealed partial class Protobuf_Mine_GetGameSavList_RESP : IResetable + { + public void Reset() + { + RomID = default; + SavDataList?.Clear(); + } + } +} + + +namespace AxibugProtobuf +{ + public sealed partial class Protobuf_Mine_GameSavInfo : IResetable + { + public void Reset() + { + BHadSaveData = default; + SavID = default; + Uid = default; + SavDataIdx = default; + RomID = default; + GamePlatformType = default; + SavDate = string.Empty; + SavName = string.Empty; + Note = string.Empty; + SavImgUrl = string.Empty; + SavUrl = string.Empty; + Sequence = default; + } + } +} + + +namespace AxibugProtobuf +{ + public sealed partial class Protobuf_Mine_DelGameSav : IResetable + { + public void Reset() + { + RomID = default; + SavDataIdx = default; + } + } +} + + +namespace AxibugProtobuf +{ + public sealed partial class Protobuf_Mine_DelGameSav_RESP : IResetable + { + public void Reset() + { + RomID = default; + SavDataIdx = default; + } + } +} + + +namespace AxibugProtobuf +{ + public sealed partial class Protobuf_Mine_UpLoadGameSav : IResetable + { + public void Reset() + { + RomID = default; + SavDataIdx = default; + Name = string.Empty; + Note = string.Empty; + SavImg = Google.Protobuf.ByteString.Empty; + StateRaw = Google.Protobuf.ByteString.Empty; + Sequence = default; + } + } +} + + +namespace AxibugProtobuf +{ + public sealed partial class Protobuf_Mine_UpLoadGameSav_RESP : IResetable + { + public void Reset() + { + RomID = default; + SavDataIdx = default; + UploadSevInfo?.Reset(); + } + } +} + + +namespace AxibugProtobuf +{ + public sealed partial class Protobuf_GameScreen_Img_Upload : IResetable + { + public void Reset() + { + RomID = default; + SavImg = Google.Protobuf.ByteString.Empty; + } + } +} + + +namespace AxibugProtobuf +{ + public sealed partial class Protobuf_GameScreen_Img_Upload_RESP : IResetable + { + public void Reset() + { + } + } +} + + +namespace AxibugProtobuf +{ + public sealed partial class pb_AxiEssgssStatusData : IResetable + { + public void Reset() + { + MemberData?.Clear(); + Array2DMemberData?.Clear(); + ClassData?.Clear(); + } + } +} + + +namespace AxibugProtobuf +{ + public sealed partial class pb_AxiEssgssStatusData_ByteData : IResetable + { + public void Reset() + { + KeyName = string.Empty; + Raw = Google.Protobuf.ByteString.Empty; + } + } +} + + +namespace AxibugProtobuf +{ + public sealed partial class pb_AxiEssgssStatusData_2DArray : IResetable + { + public void Reset() + { + KeyName = string.Empty; + Rows = default; + Cols = default; + Raw = Google.Protobuf.ByteString.Empty; + } + } +} + + +namespace AxibugProtobuf +{ + public sealed partial class pb_AxiEssgssStatusData_ClassData : IResetable + { + public void Reset() + { + KeyName = string.Empty; + ClassData?.Reset(); + } + } +} + diff --git a/AxibugEmuOnline.Client/Assets/Script/AppMain/Network/ProtobufferMsgPool.g.cs.meta b/AxibugEmuOnline.Client/Assets/Script/AppMain/Network/ProtobufferMsgPool.g.cs.meta new file mode 100644 index 00000000..cc11c896 --- /dev/null +++ b/AxibugEmuOnline.Client/Assets/Script/AppMain/Network/ProtobufferMsgPool.g.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: 0416c776519c0aa4db70cc28e0ef9945 \ No newline at end of file