77using System . Collections . Generic ;
88using System . Linq ;
99using System . Threading . Tasks ;
10- using DotNetty . Transport . Channels . Groups ;
1110using NosCore . Networking . SessionGroup ;
1211using NosCore . Packets . Interfaces ;
1312
@@ -34,9 +33,9 @@ public static Task SendPacketAsync(this IBroadcastable channelGroup, IPacket pac
3433 /// </summary>
3534 /// <param name="channelGroup">The broadcastable group to send the packet to.</param>
3635 /// <param name="packet">The packet to send.</param>
37- /// <param name="matcher">The channel matcher to filter recipients.</param>
36+ /// <param name="matcher">The session matcher to filter recipients.</param>
3837 /// <returns>A task representing the asynchronous send operation.</returns>
39- public static Task SendPacketAsync ( this IBroadcastable channelGroup , IPacket packet , IChannelMatcher matcher )
38+ public static Task SendPacketAsync ( this IBroadcastable channelGroup , IPacket packet , ISessionMatcher matcher )
4039 {
4140 return channelGroup . SendPacketsAsync ( new [ ] { packet } , matcher ) ;
4241 }
@@ -46,29 +45,39 @@ public static Task SendPacketAsync(this IBroadcastable channelGroup, IPacket pac
4645 /// </summary>
4746 /// <param name="channelGroup">The broadcastable group to send the packets to.</param>
4847 /// <param name="packets">The collection of packets to send.</param>
49- /// <param name="matcher">The optional channel matcher to filter recipients.</param>
48+ /// <param name="matcher">The optional session matcher to filter recipients.</param>
5049 /// <returns>A task representing the asynchronous send operation.</returns>
5150 public static async Task SendPacketsAsync ( this IBroadcastable channelGroup , IEnumerable < IPacket > packets ,
52- IChannelMatcher ? matcher )
51+ ISessionMatcher ? matcher )
5352 {
5453 var packetDefinitions = ( packets as IPacket [ ] ?? packets ) . Where ( c => c != null ) . ToArray ( ) ;
55- if ( packetDefinitions . Any ( ) )
54+ if ( packetDefinitions . Length == 0 )
5655 {
57- Parallel . ForEach ( packets , packet => channelGroup . LastPackets . Enqueue ( packet ) ) ;
58- Parallel . For ( 0 , channelGroup . LastPackets . Count - channelGroup . MaxPacketsBuffer , ( _ , __ ) => channelGroup . LastPackets . TryDequeue ( out var ___ ) ) ;
59- if ( channelGroup . Sessions == null ! )
60- {
61- return ;
62- }
56+ return ;
57+ }
58+
59+ foreach ( var packet in packetDefinitions )
60+ {
61+ channelGroup . LastPackets . Enqueue ( packet ) ;
62+ }
63+
64+ while ( channelGroup . LastPackets . Count > channelGroup . MaxPacketsBuffer )
65+ {
66+ channelGroup . LastPackets . TryDequeue ( out _ ) ;
67+ }
6368
64- if ( matcher == null )
65- {
66- await channelGroup . Sessions . Broadcast ( packetDefinitions ) . ConfigureAwait ( false ) ;
67- }
68- else
69- {
70- await channelGroup . Sessions . Broadcast ( packetDefinitions , matcher ) . ConfigureAwait ( false ) ;
71- }
69+ if ( channelGroup . Sessions == null ! )
70+ {
71+ return ;
72+ }
73+
74+ if ( matcher == null )
75+ {
76+ await channelGroup . Sessions . Broadcast ( packetDefinitions ) . ConfigureAwait ( false ) ;
77+ }
78+ else
79+ {
80+ await channelGroup . Sessions . Broadcast ( packetDefinitions , matcher ) . ConfigureAwait ( false ) ;
7281 }
7382 }
7483
0 commit comments