-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatchWorker.cs
More file actions
232 lines (180 loc) · 6.52 KB
/
Copy pathMatchWorker.cs
File metadata and controls
232 lines (180 loc) · 6.52 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
using CloudStructures;
using Microsoft.Extensions.Options;
using StackExchange.Redis;
using System.Collections.Concurrent;
using System.Text.Json;
using Common;
using CloudStructures.Structures;
using System.Threading.Tasks.Dataflow;
public interface IMatchWoker : IDisposable
{
public ErrorCode AddUser(string userID);
public ErrorCode RemoveUser(string userID);
public (bool, MatchingServerInfo?) GetCompleteMatching(string userID);
}
public class MatchWoker : IMatchWoker
{
ILogger<MatchWoker> _logger = null!;
System.Threading.Thread? _reqWorker = null;
ConcurrentList<string> _reqList = new();
Int64 _matchID = 0;
System.Threading.Thread? _completeWorker = null;
ConcurrentDictionary<string, MatchingServerInfo> _completeDic = new();
RedisConnection _redisUserStateConnection = null!;
RedisConnection _redisCompleteConnection = null!;
RedisList<CompleteMatchingData> _redisCompleteList;
RedisConnection _redisMatchConnection = null!;
RedisList<MatchingData> _redisMatchList;
string _redisAddress = "";
string _userStateKey = "_state";
public MatchWoker(IOptions<MatchingConfig> matchingConfig, ILogger<MatchWoker> logger)
{
_logger = logger;
_redisAddress = matchingConfig.Value.RedisAddress;
TimeSpan timeout = new(0, 0, 1);
var config = new RedisConfig("default", _redisAddress);
_redisUserStateConnection = new RedisConnection(config);
_redisCompleteConnection = new RedisConnection(config);
RedisKey redisCompleteKey = new(matchingConfig.Value.SubKey);
_redisCompleteList = new(_redisCompleteConnection, redisCompleteKey, timeout);
_redisMatchConnection = new RedisConnection(config);
RedisKey redisMatchKey = new(matchingConfig.Value.PubKey);
_redisMatchList = new(_redisMatchConnection, redisMatchKey, timeout);
_reqWorker = new System.Threading.Thread(this.RunMatchingList);
_reqWorker.Start();
_completeWorker = new System.Threading.Thread(this.CompleteMatchingList);
_completeWorker.Start();
}
public ErrorCode AddUser(string userID)
{
string key = userID + _userStateKey;
RedisString<string> str = new(_redisUserStateConnection, key, TimeSpan.MaxValue);
var result = str.GetAsync().Result;
if (result.HasValue == false)
{
return ErrorCode.MATCHING_SERVER_ERROR;
}
if (result.Value != UserState.LOBBY)
{
return ErrorCode.MATCHING_ALEARY_MATCHED;
}
_ = str.SetAsync(UserState.JOIN_MATCh).Result;
_reqList.Add(userID);
return ErrorCode.NONE;
}
public ErrorCode RemoveUser(string userID)
{
string key = userID + _userStateKey;
RedisString<string> str = new(_redisUserStateConnection, key, TimeSpan.MaxValue);
var getResult = str.GetAsync().Result;
if (getResult.HasValue == false)
{
return ErrorCode.MATCHING_SERVER_ERROR;
}
if (getResult.Value == UserState.JOIN_MATCh)
{
bool isSuccess = _reqList.Remove(userID);
if (isSuccess == false)
{
return ErrorCode.MATCHING_ALREADY_COMPLETE;
}
var setResult = str.SetAsync(UserState.LOBBY).Result;
return ErrorCode.NONE;
}
else if (getResult.Value == UserState.COMPLETE_MATCH)
{
return ErrorCode.MATCHING_ALREADY_COMPLETE;
}
return ErrorCode.MATCHING_NOT_FOUND_USER;
}
public (bool, MatchingServerInfo?) GetCompleteMatching(string userID)
{
if (_completeDic.TryGetValue(userID, out MatchingServerInfo? data))
{
_completeDic.Remove(userID, out _);
return (true, data);
}
return (false, null);
}
void RunMatchingList()
{
while (true)
{
try
{
if (_reqList.Count < 2)
{
System.Threading.Thread.Sleep(250);
continue;
}
if (_reqList.TryGetFirstAndDelete(out var user1) == false)
{
continue;
}
if (_reqList.TryGetFirstAndDelete(out var user2) == false)
{
_reqList.Add(user1);
continue;
}
{
string key = user1 + _userStateKey;
RedisString<string> redisString = new(_redisUserStateConnection, key, TimeSpan.FromSeconds(10));
var value = redisString.SetAsync(UserState.COMPLETE_MATCH).Result;
}
{
string key = user2 + _userStateKey;
RedisString<string> redisString = new(_redisUserStateConnection, key, TimeSpan.FromSeconds(10));
var value = redisString.SetAsync(UserState.COMPLETE_MATCH).Result;
}
Int64 id = Interlocked.Increment(ref _matchID);
MatchingData matchingData = new()
{
MatchID = id,
FirstUserID = user1,
SecondUserID = user2
};
var result = _redisMatchList.LeftPushAsync(matchingData).Result;
}
catch (Exception e)
{
_logger.LogError(e, "RunMatchingList Error");
}
}
}
void CompleteMatchingList()
{
while (true)
{
try
{
var result = _redisCompleteList.RightPopAsync().Result;
if (result.HasValue == false)
{
continue;
}
var data = result.Value;
if (data == null)
return;
if (_completeDic.ContainsKey(data.FirstUserID) || _completeDic.ContainsKey(data.SecondUserID))
{
return;
}
_completeDic.TryAdd(data.FirstUserID, data.ServerInfo);
_completeDic.TryAdd(data.SecondUserID, data.ServerInfo);
}
catch (Exception e)
{
_logger.LogError(e, "CompleteMatchingList Error");
}
}
}
public void Dispose()
{
}
}
public class MatchingConfig
{
public string RedisAddress { get; set; } = null!;
public string PubKey { get; set; } = null!;
public string SubKey { get; set; } = null!;
}