chore: 移除 Client 对 Connection 的依赖

oneRain 2020-06-23 16:03:24 +08:00
parent 2c919d4344
commit 772a00f520
5 changed files with 32 additions and 43 deletions

View File

@ -15,7 +15,7 @@ namespace LeanCloud.Realtime.Internal.Controller {
protected LCConnection Connection { protected LCConnection Connection {
get { get {
return Client.Connection; return LCRealtime.GetConnection(LCApplication.AppId);
} }
} }

View File

@ -125,7 +125,7 @@ namespace LeanCloud.Realtime.Internal.Controller {
}; };
GenericCommand request = NewCommand(CommandType.Conv, OpType.Update); GenericCommand request = NewCommand(CommandType.Conv, OpType.Update);
request.ConvMessage = conv; request.ConvMessage = conv;
GenericCommand response = await Client.Connection.SendRequest(request); GenericCommand response = await Connection.SendRequest(request);
JsonObjectMessage attr = response.ConvMessage.AttrModified; JsonObjectMessage attr = response.ConvMessage.AttrModified;
// 更新自定义属性 // 更新自定义属性
if (attr != null) { if (attr != null) {
@ -159,7 +159,7 @@ namespace LeanCloud.Realtime.Internal.Controller {
} }
GenericCommand request = NewCommand(CommandType.Conv, OpType.Add); GenericCommand request = NewCommand(CommandType.Conv, OpType.Add);
request.ConvMessage = conv; request.ConvMessage = conv;
GenericCommand response = await Client.Connection.SendRequest(request); GenericCommand response = await Connection.SendRequest(request);
List<string> allowedIds = response.ConvMessage.AllowedPids.ToList(); List<string> allowedIds = response.ConvMessage.AllowedPids.ToList();
List<ErrorCommand> errors = response.ConvMessage.FailedPids.ToList(); List<ErrorCommand> errors = response.ConvMessage.FailedPids.ToList();
return NewPartiallySuccessResult(allowedIds, errors); return NewPartiallySuccessResult(allowedIds, errors);
@ -189,7 +189,7 @@ namespace LeanCloud.Realtime.Internal.Controller {
} }
GenericCommand request = NewCommand(CommandType.Conv, OpType.Remove); GenericCommand request = NewCommand(CommandType.Conv, OpType.Remove);
request.ConvMessage = conv; request.ConvMessage = conv;
GenericCommand response = await Client.Connection.SendRequest(request); GenericCommand response = await Connection.SendRequest(request);
List<string> allowedIds = response.ConvMessage.AllowedPids.ToList(); List<string> allowedIds = response.ConvMessage.AllowedPids.ToList();
List<ErrorCommand> errors = response.ConvMessage.FailedPids.ToList(); List<ErrorCommand> errors = response.ConvMessage.FailedPids.ToList();
return NewPartiallySuccessResult(allowedIds, errors); return NewPartiallySuccessResult(allowedIds, errors);
@ -206,7 +206,7 @@ namespace LeanCloud.Realtime.Internal.Controller {
}; };
GenericCommand request = NewCommand(CommandType.Conv, OpType.Mute); GenericCommand request = NewCommand(CommandType.Conv, OpType.Mute);
request.ConvMessage = conv; request.ConvMessage = conv;
await Client.Connection.SendRequest(request); await Connection.SendRequest(request);
} }
/// <summary> /// <summary>
@ -220,7 +220,7 @@ namespace LeanCloud.Realtime.Internal.Controller {
}; };
GenericCommand request = NewCommand(CommandType.Conv, OpType.Unmute); GenericCommand request = NewCommand(CommandType.Conv, OpType.Unmute);
request.ConvMessage = conv; request.ConvMessage = conv;
await Client.Connection.SendRequest(request); await Connection.SendRequest(request);
} }
/// <summary> /// <summary>
@ -240,7 +240,7 @@ namespace LeanCloud.Realtime.Internal.Controller {
conv.M.AddRange(clientIds); conv.M.AddRange(clientIds);
GenericCommand request = NewCommand(CommandType.Conv, OpType.AddShutup); GenericCommand request = NewCommand(CommandType.Conv, OpType.AddShutup);
request.ConvMessage = conv; request.ConvMessage = conv;
GenericCommand response = await Client.Connection.SendRequest(request); GenericCommand response = await Connection.SendRequest(request);
return NewPartiallySuccessResult(response.ConvMessage.AllowedPids, response.ConvMessage.FailedPids); return NewPartiallySuccessResult(response.ConvMessage.AllowedPids, response.ConvMessage.FailedPids);
} }
@ -258,7 +258,7 @@ namespace LeanCloud.Realtime.Internal.Controller {
conv.M.AddRange(clientIds); conv.M.AddRange(clientIds);
GenericCommand request = NewCommand(CommandType.Conv, OpType.RemoveShutup); GenericCommand request = NewCommand(CommandType.Conv, OpType.RemoveShutup);
request.ConvMessage = conv; request.ConvMessage = conv;
GenericCommand response = await Client.Connection.SendRequest(request); GenericCommand response = await Connection.SendRequest(request);
return NewPartiallySuccessResult(response.ConvMessage.AllowedPids, response.ConvMessage.FailedPids); return NewPartiallySuccessResult(response.ConvMessage.AllowedPids, response.ConvMessage.FailedPids);
} }
@ -285,7 +285,7 @@ namespace LeanCloud.Realtime.Internal.Controller {
} }
GenericCommand request = NewCommand(CommandType.Blacklist, OpType.Block); GenericCommand request = NewCommand(CommandType.Blacklist, OpType.Block);
request.BlacklistMessage = blacklist; request.BlacklistMessage = blacklist;
GenericCommand response = await Client.Connection.SendRequest(request); GenericCommand response = await Connection.SendRequest(request);
return NewPartiallySuccessResult(response.BlacklistMessage.AllowedPids, response.BlacklistMessage.FailedPids); return NewPartiallySuccessResult(response.BlacklistMessage.AllowedPids, response.BlacklistMessage.FailedPids);
} }
@ -312,7 +312,7 @@ namespace LeanCloud.Realtime.Internal.Controller {
} }
GenericCommand request = NewCommand(CommandType.Blacklist, OpType.Unblock); GenericCommand request = NewCommand(CommandType.Blacklist, OpType.Unblock);
request.BlacklistMessage = blacklist; request.BlacklistMessage = blacklist;
GenericCommand response = await Client.Connection.SendRequest(request); GenericCommand response = await Connection.SendRequest(request);
return NewPartiallySuccessResult(response.BlacklistMessage.AllowedPids, response.BlacklistMessage.FailedPids); return NewPartiallySuccessResult(response.BlacklistMessage.AllowedPids, response.BlacklistMessage.FailedPids);
} }
@ -336,7 +336,7 @@ namespace LeanCloud.Realtime.Internal.Controller {
}; };
GenericCommand request = NewCommand(CommandType.Conv, OpType.MemberInfoUpdate); GenericCommand request = NewCommand(CommandType.Conv, OpType.MemberInfoUpdate);
request.ConvMessage = conv; request.ConvMessage = conv;
GenericCommand response = await Client.Connection.SendRequest(request); GenericCommand response = await Connection.SendRequest(request);
} }
/// <summary> /// <summary>
@ -386,7 +386,7 @@ namespace LeanCloud.Realtime.Internal.Controller {
} }
GenericCommand request = NewCommand(CommandType.Conv, OpType.QueryShutup); GenericCommand request = NewCommand(CommandType.Conv, OpType.QueryShutup);
request.ConvMessage = conv; request.ConvMessage = conv;
GenericCommand response = await Client.Connection.SendRequest(request); GenericCommand response = await Connection.SendRequest(request);
return new LCIMPageResult { return new LCIMPageResult {
Results = new ReadOnlyCollection<string>(response.ConvMessage.M), Results = new ReadOnlyCollection<string>(response.ConvMessage.M),
Next = response.ConvMessage.Next Next = response.ConvMessage.Next
@ -412,7 +412,7 @@ namespace LeanCloud.Realtime.Internal.Controller {
} }
GenericCommand request = NewCommand(CommandType.Blacklist, OpType.Query); GenericCommand request = NewCommand(CommandType.Blacklist, OpType.Query);
request.BlacklistMessage = black; request.BlacklistMessage = black;
GenericCommand response = await Client.Connection.SendRequest(request); GenericCommand response = await Connection.SendRequest(request);
return new LCIMPageResult { return new LCIMPageResult {
Results = new ReadOnlyCollection<string>(response.BlacklistMessage.BlockedPids), Results = new ReadOnlyCollection<string>(response.BlacklistMessage.BlockedPids),
Next = response.BlacklistMessage.Next Next = response.BlacklistMessage.Next
@ -525,7 +525,7 @@ namespace LeanCloud.Realtime.Internal.Controller {
}; };
GenericCommand request = NewCommand(CommandType.Conv, OpType.Members); GenericCommand request = NewCommand(CommandType.Conv, OpType.Members);
request.ConvMessage = conv; request.ConvMessage = conv;
GenericCommand response = await Client.Connection.SendRequest(request); GenericCommand response = await Connection.SendRequest(request);
ReadOnlyCollection<string> members = response.ConvMessage.M ReadOnlyCollection<string> members = response.ConvMessage.M
.ToList().AsReadOnly(); .ToList().AsReadOnly();
return members; return members;
@ -541,7 +541,7 @@ namespace LeanCloud.Realtime.Internal.Controller {
conv.Cids.Add(convId); conv.Cids.Add(convId);
GenericCommand request = NewCommand(CommandType.Conv, OpType.IsMember); GenericCommand request = NewCommand(CommandType.Conv, OpType.IsMember);
request.ConvMessage = conv; request.ConvMessage = conv;
GenericCommand response = await Client.Connection.SendRequest(request); GenericCommand response = await Connection.SendRequest(request);
JsonObjectMessage jsonObj = response.ConvMessage.Results; JsonObjectMessage jsonObj = response.ConvMessage.Results;
Dictionary<string, object> result = JsonConvert.DeserializeObject<Dictionary<string, object>>(jsonObj.Data); Dictionary<string, object> result = JsonConvert.DeserializeObject<Dictionary<string, object>>(jsonObj.Data);
if (result.TryGetValue(convId, out object obj)) { if (result.TryGetValue(convId, out object obj)) {
@ -695,8 +695,8 @@ namespace LeanCloud.Realtime.Internal.Controller {
/// <returns></returns> /// <returns></returns>
private async Task OnLeft(ConvCommand convMessage) { private async Task OnLeft(ConvCommand convMessage) {
LCIMConversation conversation = await Client.GetOrQueryConversation(convMessage.Cid); LCIMConversation conversation = await Client.GetOrQueryConversation(convMessage.Cid);
// TODO 从内存中清除对话 // 从内存中清除对话
Client.ConversationDict.Remove(conversation.Id);
Client.OnKicked?.Invoke(conversation, convMessage.InitBy); Client.OnKicked?.Invoke(conversation, convMessage.InitBy);
} }

View File

@ -65,7 +65,7 @@ namespace LeanCloud.Realtime.Internal.Controller {
if (command.Priority > 0) { if (command.Priority > 0) {
command.Priority = (int)options.Priority; command.Priority = (int)options.Priority;
} }
GenericCommand response = await Client.Connection.SendRequest(command); GenericCommand response = await Connection.SendRequest(command);
// 消息发送应答 // 消息发送应答
AckCommand ack = response.AckMessage; AckCommand ack = response.AckMessage;
message.Id = ack.Uid; message.Id = ack.Uid;
@ -94,7 +94,7 @@ namespace LeanCloud.Realtime.Internal.Controller {
patch.Patches.Add(item); patch.Patches.Add(item);
GenericCommand request = NewCommand(CommandType.Patch, OpType.Modify); GenericCommand request = NewCommand(CommandType.Patch, OpType.Modify);
request.PatchMessage = patch; request.PatchMessage = patch;
await Client.Connection.SendRequest(request); await Connection.SendRequest(request);
} }
/// <summary> /// <summary>
@ -130,7 +130,7 @@ namespace LeanCloud.Realtime.Internal.Controller {
patch.Patches.Add(item); patch.Patches.Add(item);
GenericCommand request = NewCommand(CommandType.Patch, OpType.Modify); GenericCommand request = NewCommand(CommandType.Patch, OpType.Modify);
request.PatchMessage = patch; request.PatchMessage = patch;
GenericCommand response = await Client.Connection.SendRequest(request); GenericCommand response = await Connection.SendRequest(request);
} }
/// <summary> /// <summary>
@ -170,7 +170,7 @@ namespace LeanCloud.Realtime.Internal.Controller {
} }
GenericCommand request = NewCommand(CommandType.Logs, OpType.Open); GenericCommand request = NewCommand(CommandType.Logs, OpType.Open);
request.LogsMessage = logs; request.LogsMessage = logs;
GenericCommand response = await Client.Connection.SendRequest(request); GenericCommand response = await Connection.SendRequest(request);
// 反序列化聊天记录 // 反序列化聊天记录
return response.LogsMessage.Logs.Select(item => { return response.LogsMessage.Logs.Select(item => {
LCIMMessage message; LCIMMessage message;
@ -211,7 +211,7 @@ namespace LeanCloud.Realtime.Internal.Controller {
}; };
GenericCommand command = NewCommand(CommandType.Ack); GenericCommand command = NewCommand(CommandType.Ack);
command.AckMessage = ack; command.AckMessage = ack;
await Client.Connection.SendCommand(command); await Connection.SendCommand(command);
} }
/// <summary> /// <summary>
@ -231,7 +231,7 @@ namespace LeanCloud.Realtime.Internal.Controller {
read.Convs.Add(tuple); read.Convs.Add(tuple);
GenericCommand command = NewCommand(CommandType.Read); GenericCommand command = NewCommand(CommandType.Read);
command.ReadMessage = read; command.ReadMessage = read;
await Client.Connection.SendCommand(command); await Connection.SendCommand(command);
} }
#endregion #endregion

View File

@ -20,13 +20,16 @@ namespace LeanCloud.Realtime.Internal.Controller {
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
internal async Task Open(bool force) { internal async Task Open(bool force) {
await Connection.Connect();
SessionCommand session = await NewSessionCommand(); SessionCommand session = await NewSessionCommand();
session.R = !force; session.R = !force;
session.ConfigBitmap = 0x2B; session.ConfigBitmap = 0x2B;
GenericCommand request = NewCommand(CommandType.Session, OpType.Open); GenericCommand request = NewCommand(CommandType.Session, OpType.Open);
request.SessionMessage = session; request.SessionMessage = session;
GenericCommand response = await Client.Connection.SendRequest(request); GenericCommand response = await Connection.SendRequest(request);
UpdateSession(response.SessionMessage); UpdateSession(response.SessionMessage);
Connection.Register(Client);
} }
/// <summary> /// <summary>
@ -38,7 +41,7 @@ namespace LeanCloud.Realtime.Internal.Controller {
session.R = true; session.R = true;
GenericCommand request = NewCommand(CommandType.Session, OpType.Open); GenericCommand request = NewCommand(CommandType.Session, OpType.Open);
request.SessionMessage = session; request.SessionMessage = session;
GenericCommand response = await Client.Connection.SendRequest(request); GenericCommand response = await Connection.SendRequest(request);
if (response.Op == OpType.Opened) { if (response.Op == OpType.Opened) {
UpdateSession(response.SessionMessage); UpdateSession(response.SessionMessage);
} else if (response.Op == OpType.Closed) { } else if (response.Op == OpType.Closed) {
@ -52,7 +55,8 @@ namespace LeanCloud.Realtime.Internal.Controller {
/// <returns></returns> /// <returns></returns>
internal async Task Close() { internal async Task Close() {
GenericCommand request = NewCommand(CommandType.Session, OpType.Close); GenericCommand request = NewCommand(CommandType.Session, OpType.Close);
await Client.Connection.SendRequest(request); await Connection.SendRequest(request);
Connection.UnRegister(Client);
} }
/// <summary> /// <summary>
@ -72,7 +76,7 @@ namespace LeanCloud.Realtime.Internal.Controller {
SessionCommand session = await NewSessionCommand(); SessionCommand session = await NewSessionCommand();
GenericCommand request = NewCommand(CommandType.Session, OpType.Refresh); GenericCommand request = NewCommand(CommandType.Session, OpType.Refresh);
request.SessionMessage = session; request.SessionMessage = session;
GenericCommand response = await Client.Connection.SendRequest(request); GenericCommand response = await Connection.SendRequest(request);
UpdateSession(response.SessionMessage); UpdateSession(response.SessionMessage);
} }

View File

@ -6,7 +6,6 @@ using System.Collections.ObjectModel;
using LeanCloud.Storage; using LeanCloud.Storage;
using LeanCloud.Realtime.Internal.Protocol; using LeanCloud.Realtime.Internal.Protocol;
using LeanCloud.Realtime.Internal.Controller; using LeanCloud.Realtime.Internal.Controller;
using LeanCloud.Realtime.Internal.Connection;
namespace LeanCloud.Realtime { namespace LeanCloud.Realtime {
/// <summary> /// <summary>
@ -233,10 +232,6 @@ namespace LeanCloud.Realtime {
get; private set; get; private set;
} }
internal LCConnection Connection {
get; set;
}
internal LCIMSessionController SessionController { internal LCIMSessionController SessionController {
get; private set; get; private set;
} }
@ -296,8 +291,6 @@ namespace LeanCloud.Realtime {
ConversationController = new LCIMConversationController(this); ConversationController = new LCIMConversationController(this);
MessageController = new LCIMMessageController(this); MessageController = new LCIMMessageController(this);
GoAwayController = new LCIMGoAwayController(this); GoAwayController = new LCIMGoAwayController(this);
Connection = LCRealtime.GetConnection(LCApplication.AppId);
} }
/// <summary> /// <summary>
@ -306,15 +299,12 @@ namespace LeanCloud.Realtime {
/// <param name="force">是否强制登录</param> /// <param name="force">是否强制登录</param>
/// <returns></returns> /// <returns></returns>
public async Task Open(bool force = true) { public async Task Open(bool force = true) {
await Connection.Connect();
try { try {
// 打开 Session // 打开 Session
await SessionController.Open(force); await SessionController.Open(force);
Connection.Register(this);
} catch (Exception e) { } catch (Exception e) {
LCLogger.Error(e); LCLogger.Error(e);
// 如果 session 阶段异常,则关闭连接 // 如果 session 阶段异常,则关闭连接
Connection.UnRegister(this);
throw e; throw e;
} }
} }
@ -326,7 +316,6 @@ namespace LeanCloud.Realtime {
public async Task Close() { public async Task Close() {
// 关闭 session // 关闭 session
await SessionController.Close(); await SessionController.Close();
Connection.UnRegister(this);
} }
/// <summary> /// <summary>
@ -449,9 +438,6 @@ namespace LeanCloud.Realtime {
#endregion #endregion
internal void HandleNotification(GenericCommand notification) { internal void HandleNotification(GenericCommand notification) {
if (notification.PeerId != Id) {
return;
}
switch (notification.Cmd) { switch (notification.Cmd) {
case CommandType.Session: case CommandType.Session:
SessionController.HandleNotification(notification); SessionController.HandleNotification(notification);
@ -485,8 +471,7 @@ namespace LeanCloud.Realtime {
OnResume?.Invoke(); OnResume?.Invoke();
} catch (Exception e) { } catch (Exception e) {
LCLogger.Error(e); LCLogger.Error(e);
Connection.UnRegister(this); // 重连成功,但 session/open 失败
// TODO 告知
OnClose?.Invoke(0, string.Empty); OnClose?.Invoke(0, string.Empty);
} }
} }