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 {
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);
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<string> allowedIds = response.ConvMessage.AllowedPids.ToList();
List<ErrorCommand> 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<string> allowedIds = response.ConvMessage.AllowedPids.ToList();
List<ErrorCommand> 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);
}
/// <summary>
@ -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);
}
/// <summary>
@ -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);
}
/// <summary>
@ -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<string>(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<string>(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<string> 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<string, object> result = JsonConvert.DeserializeObject<Dictionary<string, object>>(jsonObj.Data);
if (result.TryGetValue(convId, out object obj)) {
@ -695,8 +695,8 @@ namespace LeanCloud.Realtime.Internal.Controller {
/// <returns></returns>
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);
}

View File

@ -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);
}
/// <summary>
@ -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);
}
/// <summary>
@ -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);
}
/// <summary>
@ -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

View File

@ -20,13 +20,16 @@ namespace LeanCloud.Realtime.Internal.Controller {
/// </summary>
/// <returns></returns>
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);
}
/// <summary>
@ -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 {
/// <returns></returns>
internal async Task Close() {
GenericCommand request = NewCommand(CommandType.Session, OpType.Close);
await Client.Connection.SendRequest(request);
await Connection.SendRequest(request);
Connection.UnRegister(Client);
}
/// <summary>
@ -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);
}

View File

@ -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 {
/// <summary>
@ -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);
}
/// <summary>
@ -306,15 +299,12 @@ namespace LeanCloud.Realtime {
/// <param name="force">是否强制登录</param>
/// <returns></returns>
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);
}
/// <summary>
@ -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);
}
}