2020-03-12 16:23:21 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Net.WebSockets;
|
|
|
|
|
using LeanCloud.Realtime.Protocol;
|
|
|
|
|
using LeanCloud.Storage;
|
|
|
|
|
using LeanCloud.Realtime.Internal.Router;
|
|
|
|
|
using LeanCloud.Common;
|
|
|
|
|
using Google.Protobuf;
|
|
|
|
|
|
|
|
|
|
namespace LeanCloud.Realtime.Internal.WebSocket {
|
2020-03-18 16:21:29 +08:00
|
|
|
|
internal class LCWebSocketConnection {
|
2020-03-12 16:23:21 +08:00
|
|
|
|
private const int KEEP_ALIVE_INTERVAL = 10;
|
2020-03-25 16:42:30 +08:00
|
|
|
|
// .net standard 2.0 好像在拼合 Frame 时有 bug,所以将这个值调整大一些
|
|
|
|
|
private const int RECV_BUFFER_SIZE = 1024 * 5;
|
2020-03-12 16:23:21 +08:00
|
|
|
|
|
|
|
|
|
private ClientWebSocket ws;
|
|
|
|
|
|
|
|
|
|
private volatile int requestI = 1;
|
|
|
|
|
|
|
|
|
|
private readonly object requestILock = new object();
|
|
|
|
|
|
2020-03-25 16:42:30 +08:00
|
|
|
|
private readonly Dictionary<int, TaskCompletionSource<GenericCommand>> responses;
|
2020-03-12 16:23:21 +08:00
|
|
|
|
|
2020-03-25 16:42:30 +08:00
|
|
|
|
private readonly string id;
|
|
|
|
|
|
|
|
|
|
internal LCRTMRouter Router {
|
|
|
|
|
get; private set;
|
|
|
|
|
}
|
2020-03-18 16:28:50 +08:00
|
|
|
|
|
2020-03-24 17:42:04 +08:00
|
|
|
|
internal Func<GenericCommand, Task> OnNotification {
|
2020-03-12 16:23:21 +08:00
|
|
|
|
get; set;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-25 16:42:30 +08:00
|
|
|
|
internal Action<int, string> OnClose {
|
|
|
|
|
get; set;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-18 16:28:50 +08:00
|
|
|
|
internal LCWebSocketConnection(string id) {
|
2020-03-25 16:42:30 +08:00
|
|
|
|
Router = new LCRTMRouter();
|
|
|
|
|
|
2020-03-18 16:28:50 +08:00
|
|
|
|
this.id = id;
|
2020-03-12 16:23:21 +08:00
|
|
|
|
responses = new Dictionary<int, TaskCompletionSource<GenericCommand>>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal async Task Connect() {
|
2020-03-25 16:42:30 +08:00
|
|
|
|
string rtmServer = await Router.GetServer();
|
2020-03-12 16:23:21 +08:00
|
|
|
|
|
|
|
|
|
ws = new ClientWebSocket();
|
|
|
|
|
ws.Options.AddSubProtocol("lc.protobuf2.3");
|
|
|
|
|
ws.Options.KeepAliveInterval = TimeSpan.FromSeconds(KEEP_ALIVE_INTERVAL);
|
|
|
|
|
await ws.ConnectAsync(new Uri(rtmServer), default);
|
|
|
|
|
_ = StartReceive();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal Task<GenericCommand> SendRequest(GenericCommand request) {
|
|
|
|
|
TaskCompletionSource<GenericCommand> tcs = new TaskCompletionSource<GenericCommand>();
|
|
|
|
|
request.I = RequestI;
|
|
|
|
|
responses.Add(request.I, tcs);
|
2020-03-30 14:45:41 +08:00
|
|
|
|
LCLogger.Debug($"{id} => {request.Cmd}/{request.Op}: {request}");
|
2020-03-12 16:23:21 +08:00
|
|
|
|
ArraySegment<byte> bytes = new ArraySegment<byte>(request.ToByteArray());
|
|
|
|
|
try {
|
|
|
|
|
ws.SendAsync(bytes, WebSocketMessageType.Binary, true, default);
|
2020-03-18 16:28:50 +08:00
|
|
|
|
} catch (Exception) {
|
2020-03-12 16:23:21 +08:00
|
|
|
|
// TODO 发送消息异常
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
return tcs.Task;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal async Task Close() {
|
|
|
|
|
await ws.CloseAsync(WebSocketCloseStatus.NormalClosure, "1", default);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task StartReceive() {
|
|
|
|
|
byte[] buffer = new byte[RECV_BUFFER_SIZE];
|
|
|
|
|
try {
|
|
|
|
|
while (ws.State == WebSocketState.Open) {
|
|
|
|
|
byte[] data = new byte[0];
|
|
|
|
|
WebSocketReceiveResult result;
|
|
|
|
|
do {
|
|
|
|
|
result = await ws.ReceiveAsync(new ArraySegment<byte>(buffer), default);
|
|
|
|
|
if (result.MessageType == WebSocketMessageType.Close) {
|
2020-03-25 16:42:30 +08:00
|
|
|
|
OnClose?.Invoke(-1, null);
|
2020-03-12 16:23:21 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// 拼合 WebSocket Frame
|
|
|
|
|
byte[] oldData = data;
|
2020-03-24 17:42:04 +08:00
|
|
|
|
data = new byte[oldData.Length + result.Count];
|
2020-03-12 16:23:21 +08:00
|
|
|
|
Array.Copy(oldData, data, oldData.Length);
|
|
|
|
|
Array.Copy(buffer, 0, data, oldData.Length, result.Count);
|
|
|
|
|
} while (!result.EndOfMessage);
|
|
|
|
|
try {
|
|
|
|
|
GenericCommand command = GenericCommand.Parser.ParseFrom(data);
|
2020-03-30 14:45:41 +08:00
|
|
|
|
LCLogger.Debug($"{id} <= {command.Cmd}/{command.Op}: {command}");
|
2020-03-24 17:42:04 +08:00
|
|
|
|
_ = HandleCommand(command);
|
2020-03-12 16:23:21 +08:00
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
// 解析消息错误
|
|
|
|
|
LCLogger.Error(e.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
2020-03-25 16:42:30 +08:00
|
|
|
|
// 连接断开
|
2020-03-12 16:23:21 +08:00
|
|
|
|
LCLogger.Error(e.Message);
|
2020-03-25 16:42:30 +08:00
|
|
|
|
await ws.CloseAsync(WebSocketCloseStatus.EndpointUnavailable, "read error", default);
|
2020-03-12 16:23:21 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-24 17:42:04 +08:00
|
|
|
|
private async Task HandleCommand(GenericCommand command) {
|
|
|
|
|
try {
|
|
|
|
|
if (command.HasI) {
|
|
|
|
|
// 应答
|
|
|
|
|
if (responses.TryGetValue(command.I, out TaskCompletionSource<GenericCommand> tcs)) {
|
|
|
|
|
if (command.HasErrorMessage) {
|
|
|
|
|
// 错误
|
|
|
|
|
ErrorCommand error = command.ErrorMessage;
|
|
|
|
|
int code = error.Code;
|
|
|
|
|
string detail = error.Detail;
|
2020-03-27 15:52:34 +08:00
|
|
|
|
// 包装成异常抛出
|
2020-03-24 17:42:04 +08:00
|
|
|
|
LCException exception = new LCException(code, detail);
|
|
|
|
|
tcs.SetException(exception);
|
|
|
|
|
} else {
|
|
|
|
|
tcs.SetResult(command);
|
|
|
|
|
}
|
2020-03-12 16:23:21 +08:00
|
|
|
|
}
|
2020-03-24 17:42:04 +08:00
|
|
|
|
} else {
|
|
|
|
|
// 通知
|
|
|
|
|
await OnNotification?.Invoke(command);
|
2020-03-12 16:23:21 +08:00
|
|
|
|
}
|
2020-03-24 17:42:04 +08:00
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
LCLogger.Error(e.Message);
|
2020-03-12 16:23:21 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int RequestI {
|
|
|
|
|
get {
|
|
|
|
|
lock (requestILock) {
|
|
|
|
|
return requestI++;
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|