-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTaskFluxClientFactory.cs
125 lines (105 loc) · 4.16 KB
/
TaskFluxClientFactory.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
using System.Net;
using System.Net.Sockets;
using TaskFlux.Network;
using TaskFlux.Network.Authorization;
using TaskFlux.Network.Packets;
using TaskFlux.Transport.Tcp.Client.Exceptions;
namespace TaskFlux.Transport.Tcp.Client;
public class TaskFluxClientFactory
{
private readonly EndPoint[] _endPoints;
/// <summary>
/// Id текущего лидера кластера.
/// Равен индексу в массиве <see cref="_endPoints"/>.
/// Если лидер не известен, то равен <c>null</c>
/// </summary>
internal int? LeaderId { get; set; }
public TaskFluxClientFactory(EndPoint[] endPoints)
{
ArgumentNullException.ThrowIfNull(endPoints);
if (endPoints.Length == 0)
{
throw new ArgumentOutOfRangeException(nameof(endPoints), endPoints, "Массив адресов серверов пуст");
}
_endPoints = endPoints;
}
public async Task<ITaskFluxClient> ConnectAsync(CancellationToken token)
{
var client = await ConnectCoreAsync(token);
return new ReconnectingTaskFluxClientDecorator(client, this);
}
internal async Task<TaskFluxClient> ConnectCoreAsync(CancellationToken token)
{
if (LeaderId is { } savedLeaderId)
{
var stream = await EstablishConnectionToHostAsync(_endPoints[savedLeaderId], token);
if (stream is not null)
{
return new TaskFluxClient(stream);
}
}
foreach (var endPoint in _endPoints)
{
var stream = await EstablishConnectionToHostAsync(endPoint, token);
if (stream is not null)
{
return new TaskFluxClient(stream);
}
}
throw new ClusterUnavailableException();
}
private static async Task<NetworkStream?> EstablishConnectionToHostAsync(EndPoint endPoint, CancellationToken token)
{
var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
// 1. Устанавливаем TCP соединение
await socket.ConnectAsync(endPoint, token);
var stream = new NetworkStream(socket);
// 2. Авторизуемся
await AuthorizeAsync(stream, token);
// 3. Бутстрапимся
await BootstrapAsync(stream, token);
// 4. Готово
return stream;
}
catch (Exception) // Так плохо, но легче
{
socket.Dispose();
}
return null;
}
private static async Task BootstrapAsync(NetworkStream stream, CancellationToken token)
{
var version = Constants.ClientVersion;
var request = new BootstrapRequestPacket(version.Major, version.Minor, version.Build);
await request.SerializeAsync(stream, token);
var response = await Packet.DeserializeAsync(stream, token);
Helpers.CheckNotErrorResponse(response);
if (response.Type != PacketType.BootstrapResponse)
{
throw new UnexpectedPacketException(PacketType.BootstrapResponse, response.Type);
}
var bootstrapResponse = (BootstrapResponsePacket)response;
if (!bootstrapResponse.Success)
{
throw new BootstrapException(bootstrapResponse.Reason);
}
}
private static async Task AuthorizeAsync(NetworkStream stream, CancellationToken token)
{
var authRequestPacket = new AuthorizationRequestPacket(new NoneAuthorizationMethod());
await authRequestPacket.SerializeAsync(stream, token);
var responsePacket = await Packet.DeserializeAsync(stream, token);
Helpers.CheckNotErrorResponse(responsePacket);
if (responsePacket.Type != PacketType.AuthorizationResponse)
{
throw new UnexpectedPacketException(PacketType.AuthorizationResponse, responsePacket.Type);
}
var authResponsePacket = (AuthorizationResponsePacket)responsePacket;
if (!authResponsePacket.Success)
{
throw new AuthorizationException(authResponsePacket.ErrorReason);
}
}
}