From 772a00f52019585b4202296ec8f1e7956d288897 Mon Sep 17 00:00:00 2001 From: oneRain Date: Tue, 23 Jun 2020 16:03:24 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E7=A7=BB=E9=99=A4=20Client=20?= =?UTF-8?q?=E5=AF=B9=20Connection=20=E7=9A=84=E4=BE=9D=E8=B5=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Internal/Controller/LCIMController.cs | 2 +- .../Controller/LCIMConversationController.cs | 32 +++++++++---------- .../Controller/LCIMMessageController.cs | 12 +++---- .../Controller/LCIMSessionController.cs | 12 ++++--- Realtime/Realtime/LCIMClient.cs | 17 +--------- 5 files changed, 32 insertions(+), 43 deletions(-) diff --git a/Realtime/Realtime/Internal/Controller/LCIMController.cs b/Realtime/Realtime/Internal/Controller/LCIMController.cs index 78f134d..1a6d933 100644 --- a/Realtime/Realtime/Internal/Controller/LCIMController.cs +++ b/Realtime/Realtime/Internal/Controller/LCIMController.cs @@ -15,7 +15,7 @@ namespace LeanCloud.Realtime.Internal.Controller { protected LCConnection Connection { get { - return Client.Connection; + return LCRealtime.GetConnection(LCApplication.AppId); } } diff --git a/Realtime/Realtime/Internal/Controller/LCIMConversationController.cs b/Realtime/Realtime/Internal/Controller/LCIMConversationController.cs index 7a484bd..b2f288f 100644 --- a/Realtime/Realtime/Internal/Controller/LCIMConversationController.cs +++ b/Realtime/Realtime/Internal/Controller/LCIMConversationController.cs @@ -125,7 +125,7 @@ namespace LeanCloud.Realtime.Internal.Controller { }; GenericCommand request = NewCommand(CommandType.Conv, OpType.Update); request.ConvMessage = conv; - GenericCommand response = await Client.Connection.SendRequest(request); + GenericCommand response = await Connection.SendRequest(request); JsonObjectMessage attr = response.ConvMessage.AttrModified; // 更新自定义属性 if (attr != null) { @@ -159,7 +159,7 @@ namespace LeanCloud.Realtime.Internal.Controller { } GenericCommand request = NewCommand(CommandType.Conv, OpType.Add); request.ConvMessage = conv; - GenericCommand response = await Client.Connection.SendRequest(request); + GenericCommand response = await Connection.SendRequest(request); List allowedIds = response.ConvMessage.AllowedPids.ToList(); List errors = response.ConvMessage.FailedPids.ToList(); return NewPartiallySuccessResult(allowedIds, errors); @@ -189,7 +189,7 @@ namespace LeanCloud.Realtime.Internal.Controller { } GenericCommand request = NewCommand(CommandType.Conv, OpType.Remove); request.ConvMessage = conv; - GenericCommand response = await Client.Connection.SendRequest(request); + GenericCommand response = await Connection.SendRequest(request); List allowedIds = response.ConvMessage.AllowedPids.ToList(); List errors = response.ConvMessage.FailedPids.ToList(); return NewPartiallySuccessResult(allowedIds, errors); @@ -206,7 +206,7 @@ namespace LeanCloud.Realtime.Internal.Controller { }; GenericCommand request = NewCommand(CommandType.Conv, OpType.Mute); request.ConvMessage = conv; - await Client.Connection.SendRequest(request); + await Connection.SendRequest(request); } /// @@ -220,7 +220,7 @@ namespace LeanCloud.Realtime.Internal.Controller { }; GenericCommand request = NewCommand(CommandType.Conv, OpType.Unmute); request.ConvMessage = conv; - await Client.Connection.SendRequest(request); + await Connection.SendRequest(request); } /// @@ -240,7 +240,7 @@ namespace LeanCloud.Realtime.Internal.Controller { conv.M.AddRange(clientIds); GenericCommand request = NewCommand(CommandType.Conv, OpType.AddShutup); request.ConvMessage = conv; - GenericCommand response = await Client.Connection.SendRequest(request); + GenericCommand response = await Connection.SendRequest(request); return NewPartiallySuccessResult(response.ConvMessage.AllowedPids, response.ConvMessage.FailedPids); } @@ -258,7 +258,7 @@ namespace LeanCloud.Realtime.Internal.Controller { conv.M.AddRange(clientIds); GenericCommand request = NewCommand(CommandType.Conv, OpType.RemoveShutup); request.ConvMessage = conv; - GenericCommand response = await Client.Connection.SendRequest(request); + GenericCommand response = await Connection.SendRequest(request); return NewPartiallySuccessResult(response.ConvMessage.AllowedPids, response.ConvMessage.FailedPids); } @@ -285,7 +285,7 @@ namespace LeanCloud.Realtime.Internal.Controller { } GenericCommand request = NewCommand(CommandType.Blacklist, OpType.Block); request.BlacklistMessage = blacklist; - GenericCommand response = await Client.Connection.SendRequest(request); + GenericCommand response = await Connection.SendRequest(request); return NewPartiallySuccessResult(response.BlacklistMessage.AllowedPids, response.BlacklistMessage.FailedPids); } @@ -312,7 +312,7 @@ namespace LeanCloud.Realtime.Internal.Controller { } GenericCommand request = NewCommand(CommandType.Blacklist, OpType.Unblock); request.BlacklistMessage = blacklist; - GenericCommand response = await Client.Connection.SendRequest(request); + GenericCommand response = await Connection.SendRequest(request); return NewPartiallySuccessResult(response.BlacklistMessage.AllowedPids, response.BlacklistMessage.FailedPids); } @@ -336,7 +336,7 @@ namespace LeanCloud.Realtime.Internal.Controller { }; GenericCommand request = NewCommand(CommandType.Conv, OpType.MemberInfoUpdate); request.ConvMessage = conv; - GenericCommand response = await Client.Connection.SendRequest(request); + GenericCommand response = await Connection.SendRequest(request); } /// @@ -386,7 +386,7 @@ namespace LeanCloud.Realtime.Internal.Controller { } GenericCommand request = NewCommand(CommandType.Conv, OpType.QueryShutup); request.ConvMessage = conv; - GenericCommand response = await Client.Connection.SendRequest(request); + GenericCommand response = await Connection.SendRequest(request); return new LCIMPageResult { Results = new ReadOnlyCollection(response.ConvMessage.M), Next = response.ConvMessage.Next @@ -412,7 +412,7 @@ namespace LeanCloud.Realtime.Internal.Controller { } GenericCommand request = NewCommand(CommandType.Blacklist, OpType.Query); request.BlacklistMessage = black; - GenericCommand response = await Client.Connection.SendRequest(request); + GenericCommand response = await Connection.SendRequest(request); return new LCIMPageResult { Results = new ReadOnlyCollection(response.BlacklistMessage.BlockedPids), Next = response.BlacklistMessage.Next @@ -525,7 +525,7 @@ namespace LeanCloud.Realtime.Internal.Controller { }; GenericCommand request = NewCommand(CommandType.Conv, OpType.Members); request.ConvMessage = conv; - GenericCommand response = await Client.Connection.SendRequest(request); + GenericCommand response = await Connection.SendRequest(request); ReadOnlyCollection members = response.ConvMessage.M .ToList().AsReadOnly(); return members; @@ -541,7 +541,7 @@ namespace LeanCloud.Realtime.Internal.Controller { conv.Cids.Add(convId); GenericCommand request = NewCommand(CommandType.Conv, OpType.IsMember); request.ConvMessage = conv; - GenericCommand response = await Client.Connection.SendRequest(request); + GenericCommand response = await Connection.SendRequest(request); JsonObjectMessage jsonObj = response.ConvMessage.Results; Dictionary result = JsonConvert.DeserializeObject>(jsonObj.Data); if (result.TryGetValue(convId, out object obj)) { @@ -695,8 +695,8 @@ namespace LeanCloud.Realtime.Internal.Controller { /// private async Task OnLeft(ConvCommand convMessage) { LCIMConversation conversation = await Client.GetOrQueryConversation(convMessage.Cid); - // TODO 从内存中清除对话 - + // 从内存中清除对话 + Client.ConversationDict.Remove(conversation.Id); Client.OnKicked?.Invoke(conversation, convMessage.InitBy); } diff --git a/Realtime/Realtime/Internal/Controller/LCIMMessageController.cs b/Realtime/Realtime/Internal/Controller/LCIMMessageController.cs index bfa0099..cec51cc 100644 --- a/Realtime/Realtime/Internal/Controller/LCIMMessageController.cs +++ b/Realtime/Realtime/Internal/Controller/LCIMMessageController.cs @@ -65,7 +65,7 @@ namespace LeanCloud.Realtime.Internal.Controller { if (command.Priority > 0) { command.Priority = (int)options.Priority; } - GenericCommand response = await Client.Connection.SendRequest(command); + GenericCommand response = await Connection.SendRequest(command); // 消息发送应答 AckCommand ack = response.AckMessage; message.Id = ack.Uid; @@ -94,7 +94,7 @@ namespace LeanCloud.Realtime.Internal.Controller { patch.Patches.Add(item); GenericCommand request = NewCommand(CommandType.Patch, OpType.Modify); request.PatchMessage = patch; - await Client.Connection.SendRequest(request); + await Connection.SendRequest(request); } /// @@ -130,7 +130,7 @@ namespace LeanCloud.Realtime.Internal.Controller { patch.Patches.Add(item); GenericCommand request = NewCommand(CommandType.Patch, OpType.Modify); request.PatchMessage = patch; - GenericCommand response = await Client.Connection.SendRequest(request); + GenericCommand response = await Connection.SendRequest(request); } /// @@ -170,7 +170,7 @@ namespace LeanCloud.Realtime.Internal.Controller { } GenericCommand request = NewCommand(CommandType.Logs, OpType.Open); request.LogsMessage = logs; - GenericCommand response = await Client.Connection.SendRequest(request); + GenericCommand response = await Connection.SendRequest(request); // 反序列化聊天记录 return response.LogsMessage.Logs.Select(item => { LCIMMessage message; @@ -211,7 +211,7 @@ namespace LeanCloud.Realtime.Internal.Controller { }; GenericCommand command = NewCommand(CommandType.Ack); command.AckMessage = ack; - await Client.Connection.SendCommand(command); + await Connection.SendCommand(command); } /// @@ -231,7 +231,7 @@ namespace LeanCloud.Realtime.Internal.Controller { read.Convs.Add(tuple); GenericCommand command = NewCommand(CommandType.Read); command.ReadMessage = read; - await Client.Connection.SendCommand(command); + await Connection.SendCommand(command); } #endregion diff --git a/Realtime/Realtime/Internal/Controller/LCIMSessionController.cs b/Realtime/Realtime/Internal/Controller/LCIMSessionController.cs index 73bce54..81e1861 100644 --- a/Realtime/Realtime/Internal/Controller/LCIMSessionController.cs +++ b/Realtime/Realtime/Internal/Controller/LCIMSessionController.cs @@ -20,13 +20,16 @@ namespace LeanCloud.Realtime.Internal.Controller { /// /// internal async Task Open(bool force) { + await Connection.Connect(); + SessionCommand session = await NewSessionCommand(); session.R = !force; session.ConfigBitmap = 0x2B; GenericCommand request = NewCommand(CommandType.Session, OpType.Open); request.SessionMessage = session; - GenericCommand response = await Client.Connection.SendRequest(request); + GenericCommand response = await Connection.SendRequest(request); UpdateSession(response.SessionMessage); + Connection.Register(Client); } /// @@ -38,7 +41,7 @@ namespace LeanCloud.Realtime.Internal.Controller { session.R = true; GenericCommand request = NewCommand(CommandType.Session, OpType.Open); request.SessionMessage = session; - GenericCommand response = await Client.Connection.SendRequest(request); + GenericCommand response = await Connection.SendRequest(request); if (response.Op == OpType.Opened) { UpdateSession(response.SessionMessage); } else if (response.Op == OpType.Closed) { @@ -52,7 +55,8 @@ namespace LeanCloud.Realtime.Internal.Controller { /// internal async Task Close() { GenericCommand request = NewCommand(CommandType.Session, OpType.Close); - await Client.Connection.SendRequest(request); + await Connection.SendRequest(request); + Connection.UnRegister(Client); } /// @@ -72,7 +76,7 @@ namespace LeanCloud.Realtime.Internal.Controller { SessionCommand session = await NewSessionCommand(); GenericCommand request = NewCommand(CommandType.Session, OpType.Refresh); request.SessionMessage = session; - GenericCommand response = await Client.Connection.SendRequest(request); + GenericCommand response = await Connection.SendRequest(request); UpdateSession(response.SessionMessage); } diff --git a/Realtime/Realtime/LCIMClient.cs b/Realtime/Realtime/LCIMClient.cs index e4c3acb..6402076 100644 --- a/Realtime/Realtime/LCIMClient.cs +++ b/Realtime/Realtime/LCIMClient.cs @@ -6,7 +6,6 @@ using System.Collections.ObjectModel; using LeanCloud.Storage; using LeanCloud.Realtime.Internal.Protocol; using LeanCloud.Realtime.Internal.Controller; -using LeanCloud.Realtime.Internal.Connection; namespace LeanCloud.Realtime { /// @@ -233,10 +232,6 @@ namespace LeanCloud.Realtime { get; private set; } - internal LCConnection Connection { - get; set; - } - internal LCIMSessionController SessionController { get; private set; } @@ -296,8 +291,6 @@ namespace LeanCloud.Realtime { ConversationController = new LCIMConversationController(this); MessageController = new LCIMMessageController(this); GoAwayController = new LCIMGoAwayController(this); - - Connection = LCRealtime.GetConnection(LCApplication.AppId); } /// @@ -306,15 +299,12 @@ namespace LeanCloud.Realtime { /// 是否强制登录 /// public async Task Open(bool force = true) { - await Connection.Connect(); try { // 打开 Session await SessionController.Open(force); - Connection.Register(this); } catch (Exception e) { LCLogger.Error(e); // 如果 session 阶段异常,则关闭连接 - Connection.UnRegister(this); throw e; } } @@ -326,7 +316,6 @@ namespace LeanCloud.Realtime { public async Task Close() { // 关闭 session await SessionController.Close(); - Connection.UnRegister(this); } /// @@ -449,9 +438,6 @@ namespace LeanCloud.Realtime { #endregion internal void HandleNotification(GenericCommand notification) { - if (notification.PeerId != Id) { - return; - } switch (notification.Cmd) { case CommandType.Session: SessionController.HandleNotification(notification); @@ -485,8 +471,7 @@ namespace LeanCloud.Realtime { OnResume?.Invoke(); } catch (Exception e) { LCLogger.Error(e); - Connection.UnRegister(this); - // TODO 告知 + // 重连成功,但 session/open 失败 OnClose?.Invoke(0, string.Empty); } }