11#if FANTASY_NET
2- using System ;
3- using System . Collections . Generic ;
42using Fantasy . Async ;
53using Fantasy . Entitas ;
6- using Fantasy . Entitas . Interface ;
74using Fantasy . InnerMessage ;
85using Fantasy . Network . Interface ;
96using Fantasy . PacketParser . Interface ;
107using Fantasy . Platform . Net ;
118using Fantasy . Scheduler ;
129using 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.
1816
1917namespace 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>
4124public 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