Skip to content

Commit 6fdb3b5

Browse files
committed
修复了漫游系统在断开的时候无法销毁Terminus的问题。相关Issue #104
1 parent 7058d05 commit 6fdb3b5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+629
-1360
lines changed

‎Fantasy.Packages/Fantasy.Net/Runtime/Core/Network/Roaming/Component/RoamingComponent.cs‎

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ private async FTask DisposeAsync()
4747

4848
foreach (var (_, sessionRoamingComponent) in _sessionRoamingComponents)
4949
{
50-
await sessionRoamingComponent.UnLink();
50+
await sessionRoamingComponent.UnLinkAll();
51+
sessionRoamingComponent.Dispose();
5152
}
5253

5354
_sessionRoamingComponents.Clear();
@@ -156,20 +157,30 @@ public async FTask Remove(long roamingId, int roamingType, int delayRemove = 0)
156157
return;
157158
}
158159

159-
taskId = _timerSchedulerNet.OnceTimer(delayRemove, () =>
160-
{ InnerRemove(roamingId, roamingType).Coroutine(); });
160+
taskId = _timerSchedulerNet.OnceTimer(delayRemove, () =>
161+
{
162+
InnerRemove(roamingId, roamingType).Coroutine();
163+
});
161164
_delayRemoveTaskId.Add(roamingId, taskId);
162165
}
163166

164167
private async FTask InnerRemove(long roamingId, int roamingType)
165168
{
166-
if (!_sessionRoamingComponents.Remove(roamingId, out var sessionRoamingComponent))
169+
if (!_sessionRoamingComponents.TryGetValue(roamingId, out var sessionRoamingComponent))
167170
{
168171
return;
169172
}
170173

171-
await sessionRoamingComponent.UnLink(roamingType);
172-
sessionRoamingComponent.Dispose();
174+
if (roamingType == 0)
175+
{
176+
await sessionRoamingComponent.UnLinkAll();
177+
sessionRoamingComponent.Dispose();
178+
}
179+
else
180+
{
181+
await sessionRoamingComponent.UnLink(roamingType, true);
182+
}
183+
173184
_delayRemoveTaskId.Remove(roamingId);
174185
}
175186
}

‎Fantasy.Packages/Fantasy.Net/Runtime/Core/Network/Roaming/Component/SessionRoamingComponent.cs‎

Lines changed: 111 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
#if FANTASY_NET
2-
using System;
3-
using System.Collections.Generic;
42
using Fantasy.Async;
53
using Fantasy.Entitas;
6-
using Fantasy.Entitas.Interface;
74
using Fantasy.InnerMessage;
85
using Fantasy.Network.Interface;
96
using Fantasy.PacketParser.Interface;
107
using Fantasy.Platform.Net;
118
using Fantasy.Scheduler;
129
using Fantasy.Timer;
10+
// ReSharper disable CheckNamespace
1311
#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type.
1412
#pragma warning disable CS8601 // Possible null reference assignment.
1513
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring as nullable.
@@ -18,33 +16,19 @@
1816

1917
namespace Fantasy.Network.Roaming;
2018

21-
internal sealed class SessionRoamingComponentDestroySystem : DestroySystem<SessionRoamingComponent>
22-
{
23-
protected override void Destroy(SessionRoamingComponent self)
24-
{
25-
self.RoamingLock.Dispose();
26-
self.RoamingMessageLock.Dispose();
27-
28-
self.RoamingLock = null;
29-
self.RoamingMessageLock = null;
30-
self.TimerComponent = null;
31-
self.NetworkMessagingComponent = null;
32-
self.MessageDispatcherComponent = null;
33-
}
34-
}
35-
3619
/// <summary>
3720
/// Session的漫游组件。
3821
/// 用于关联对应的Session的功能。
3922
/// 但这个组件并不会挂载到这个Session下。
4023
/// </summary>
4124
public sealed class SessionRoamingComponent : Entity
4225
{
43-
internal CoroutineLock RoamingLock;
44-
internal CoroutineLock RoamingMessageLock;
45-
internal TimerComponent TimerComponent;
46-
internal NetworkMessagingComponent NetworkMessagingComponent;
47-
internal MessageDispatcherComponent MessageDispatcherComponent;
26+
private CoroutineLock? _roamingLock;
27+
private CoroutineLock? _roamingMessageLock;
28+
private TimerComponent _timerComponent;
29+
private NetworkMessagingComponent _networkMessagingComponent;
30+
private MessageDispatcherComponent _messageDispatcherComponent;
31+
4832
/// <summary>
4933
/// 漫游的列表。
5034
/// </summary>
@@ -55,13 +39,41 @@ internal void Initialize(Session session)
5539
session.SessionRoamingComponent = this;
5640

5741
var scene = session.Scene;
58-
TimerComponent = scene.TimerComponent;
59-
NetworkMessagingComponent = scene.NetworkMessagingComponent;
60-
MessageDispatcherComponent = scene.MessageDispatcherComponent;
61-
RoamingLock = scene.CoroutineLockComponent.Create(this.GetType().TypeHandle.Value.ToInt64());
62-
RoamingMessageLock = scene.CoroutineLockComponent.Create(this.GetType().TypeHandle.Value.ToInt64());
42+
_timerComponent = scene.TimerComponent;
43+
_networkMessagingComponent = scene.NetworkMessagingComponent;
44+
_messageDispatcherComponent = scene.MessageDispatcherComponent;
45+
_roamingLock = scene.CoroutineLockComponent.Create(this.GetType().TypeHandle.Value.ToInt64());
46+
_roamingMessageLock = scene.CoroutineLockComponent.Create(this.GetType().TypeHandle.Value.ToInt64());
6347
}
64-
48+
49+
/// <summary>
50+
/// 销毁方法
51+
/// </summary>
52+
public override void Dispose()
53+
{
54+
if (IsDisposed)
55+
{
56+
return;
57+
}
58+
59+
if (_roamingLock != null)
60+
{
61+
_roamingLock.Dispose();
62+
_roamingLock = null;
63+
}
64+
65+
if (_roamingMessageLock != null)
66+
{
67+
_roamingMessageLock.Dispose();
68+
_roamingMessageLock = null;
69+
}
70+
71+
_timerComponent = null;
72+
_networkMessagingComponent = null;
73+
_messageDispatcherComponent = null;
74+
base.Dispose();
75+
}
76+
6577
#region Get
6678

6779
/// <summary>
@@ -75,6 +87,23 @@ public bool TryGetRoaming(int roamingType, out Roaming roaming)
7587
return _roaming.TryGetValue(roamingType, out roaming);
7688
}
7789

90+
#endregion
91+
92+
#region Remove
93+
94+
internal void Remove(int roamingType, bool isDisponse)
95+
{
96+
if (!_roaming.Remove(roamingType, out var roaming))
97+
{
98+
return;
99+
}
100+
101+
if (isDisponse)
102+
{
103+
roaming.Dispose();
104+
}
105+
}
106+
78107
#endregion
79108

80109
#region Link
@@ -125,7 +154,7 @@ public async FTask<uint> Link(long targetSceneAddress, long forwardSessionAddres
125154
roaming.ForwardSessionAddress = forwardSessionAddress;
126155
roaming.SessionRoamingComponent = this;
127156
roaming.RoamingType = roamingType;
128-
roaming.RoamingLock = RoamingLock;
157+
roaming.RoamingLock = _roamingLock;
129158
_roaming.Add(roamingType, roaming);
130159
return 0;
131160
}
@@ -136,51 +165,58 @@ public async FTask<uint> Link(long targetSceneAddress, long forwardSessionAddres
136165

137166
/// <summary>
138167
/// 断开当前的所有漫游关系。
139-
/// <param name="removeRoamingType">要移除的RoamingType,默认不设置是移除所有漫游。</param>
140168
/// </summary>
141-
public async FTask UnLink(int removeRoamingType = 0)
169+
public async FTask UnLinkAll()
142170
{
143-
switch (removeRoamingType)
171+
foreach (var (roamingType,roaming) in _roaming)
144172
{
145-
case 0:
173+
try
146174
{
147-
foreach (var (roamingType,roaming) in _roaming)
148-
{
149-
try
150-
{
151-
var errorCode = await roaming.Disconnect();
175+
var errorCode = await roaming.Disconnect();
152176

153-
if (errorCode != 0)
154-
{
155-
Log.Warning($"roaming roamingId:{Id} roamingType:{roamingType} disconnect errorCode:{errorCode}");
156-
}
157-
}
158-
finally
159-
{
160-
roaming.Dispose();
161-
}
177+
if (errorCode != 0)
178+
{
179+
Log.Warning($"roaming roamingId:{Id} roamingType:{roamingType} disconnect errorCode:{errorCode}");
162180
}
163-
164-
_roaming.Clear();
165-
return;
166181
}
167-
default:
182+
finally
168183
{
169-
if (!_roaming.Remove(removeRoamingType, out var roaming))
170-
{
171-
return;
172-
}
184+
roaming.Dispose();
185+
}
186+
}
187+
188+
_roaming.Clear();
189+
}
173190

174-
var errorCode = await roaming.Disconnect();
191+
/// <summary>
192+
/// 断开当前的漫游关系。
193+
/// <param name="removeRoamingType">要移除的RoamingType,默认不设置是移除所有漫游。</param>
194+
/// <param name="isDisponse">如果当前没有任何连接的Roaming就移除这个组件</param>
195+
/// </summary>
196+
public async FTask UnLink(int removeRoamingType, bool isDisponse)
197+
{
198+
if (removeRoamingType == 0)
199+
{
200+
throw new ArgumentException("removeRoamingType cannot be 0. Use UnLinkAll() to remove all roaming connections.", nameof(removeRoamingType));
201+
}
175202

176-
if (errorCode != 0)
177-
{
178-
Log.Warning($"roaming roamingId:{Id} roamingType:{removeRoamingType} disconnect errorCode:{errorCode}");
179-
}
203+
if (!_roaming.Remove(removeRoamingType, out var roaming))
204+
{
205+
return;
206+
}
180207

181-
roaming.Dispose();
182-
return;
183-
}
208+
var errorCode = await roaming.Disconnect();
209+
210+
if (errorCode != 0)
211+
{
212+
Log.Warning($"roaming roamingId:{Id} roamingType:{removeRoamingType} disconnect errorCode:{errorCode}");
213+
}
214+
215+
roaming.Dispose();
216+
217+
if(isDisponse && _roaming.Count == 0)
218+
{
219+
Dispose();
184220
}
185221
}
186222

@@ -227,7 +263,7 @@ public async FTask<IResponse> Call<T>(int roamingType, T message) where T : IAdd
227263
{
228264
if (!_roaming.TryGetValue(roamingType, out var roaming))
229265
{
230-
return MessageDispatcherComponent.CreateResponse(message.OpCode(), InnerErrorCode.ErrNotFoundRoaming);
266+
return _messageDispatcherComponent.CreateResponse(message.OpCode(), InnerErrorCode.ErrNotFoundRoaming);
231267
}
232268

233269
var failCount = 0;
@@ -236,7 +272,7 @@ public async FTask<IResponse> Call<T>(int roamingType, T message) where T : IAdd
236272

237273
IResponse iRouteResponse = null;
238274

239-
using (await RoamingMessageLock.Wait(roaming.RoamingType, "RoamingComponent Call MemoryStream"))
275+
using (await _roamingMessageLock!.Wait(roaming.RoamingType, "RoamingComponent Call MemoryStream"))
240276
{
241277
while (!IsDisposed)
242278
{
@@ -247,10 +283,10 @@ public async FTask<IResponse> Call<T>(int roamingType, T message) where T : IAdd
247283

248284
if (address == 0)
249285
{
250-
return MessageDispatcherComponent.CreateResponse(message.OpCode(), InnerErrorCode.ErrNotFoundRoaming);
286+
return _messageDispatcherComponent.CreateResponse(message.OpCode(), InnerErrorCode.ErrNotFoundRoaming);
251287
}
252288

253-
iRouteResponse = await NetworkMessagingComponent.Call(address, message);
289+
iRouteResponse = await _networkMessagingComponent.Call(address, message);
254290

255291
if (runtimeId != RuntimeId)
256292
{
@@ -273,7 +309,7 @@ public async FTask<IResponse> Call<T>(int roamingType, T message) where T : IAdd
273309
return iRouteResponse;
274310
}
275311

276-
await TimerComponent.Net.WaitAsync(100);
312+
await _timerComponent.Net.WaitAsync(100);
277313

278314
if (runtimeId != RuntimeId)
279315
{
@@ -307,14 +343,14 @@ internal async FTask<IResponse> Call(int roamingType, Type requestType, APackInf
307343
{
308344
if (IsDisposed)
309345
{
310-
return MessageDispatcherComponent.CreateResponse(packInfo.ProtocolCode, InnerErrorCode.ErrNotFoundRoaming);
346+
return _messageDispatcherComponent.CreateResponse(packInfo.ProtocolCode, InnerErrorCode.ErrNotFoundRoaming);
311347
}
312348

313349
packInfo.IsDisposed = true;
314350

315351
if (!_roaming.TryGetValue(roamingType, out var roaming))
316352
{
317-
return MessageDispatcherComponent.CreateResponse(packInfo.ProtocolCode, InnerErrorCode.ErrNotFoundRoaming);
353+
return _messageDispatcherComponent.CreateResponse(packInfo.ProtocolCode, InnerErrorCode.ErrNotFoundRoaming);
318354
}
319355

320356
var failCount = 0;
@@ -324,7 +360,7 @@ internal async FTask<IResponse> Call(int roamingType, Type requestType, APackInf
324360

325361
try
326362
{
327-
using (await RoamingMessageLock.Wait(roamingType, "RoamingComponent Call MemoryStream"))
363+
using (await _roamingMessageLock!.Wait(roamingType, "RoamingComponent Call MemoryStream"))
328364
{
329365
while (!IsDisposed)
330366
{
@@ -335,10 +371,10 @@ internal async FTask<IResponse> Call(int roamingType, Type requestType, APackInf
335371

336372
if (address == 0)
337373
{
338-
return MessageDispatcherComponent.CreateResponse(packInfo.ProtocolCode, InnerErrorCode.ErrNotFoundRoaming);
374+
return _messageDispatcherComponent.CreateResponse(packInfo.ProtocolCode, InnerErrorCode.ErrNotFoundRoaming);
339375
}
340376

341-
iRouteResponse = await NetworkMessagingComponent.Call(address, requestType, packInfo);
377+
iRouteResponse = await _networkMessagingComponent.Call(address, requestType, packInfo);
342378

343379
if (runtimeId != RuntimeId)
344380
{
@@ -361,7 +397,7 @@ internal async FTask<IResponse> Call(int roamingType, Type requestType, APackInf
361397
return iRouteResponse;
362398
}
363399

364-
await TimerComponent.Net.WaitAsync(100);
400+
await _timerComponent.Net.WaitAsync(100);
365401

366402
if (runtimeId != RuntimeId)
367403
{

0 commit comments

Comments
 (0)