@@ -46,6 +46,8 @@ public abstract class NodeBase : IDisposable, IAsyncDisposable {
46
46
47
47
public EndPoint LocalEndPoint => server ? . LocalEndPoint ?? throw new InvalidOperationException ( "not yet bound or already disposed" ) ;
48
48
49
+ private readonly ArrayBufferWriter < byte > responseBuffer = new ( initialCapacity : 1024 ) ; // TODO: define best initial capacity
50
+
49
51
protected NodeBase (
50
52
IAccessRule ? accessRule ,
51
53
ILogger ? logger
@@ -229,7 +231,6 @@ public async ValueTask AcceptSingleSessionAsync(
229
231
try {
230
232
await SendResponseAsync (
231
233
client ,
232
- Encoding ,
233
234
$ "# munin node at { HostName } ",
234
235
cancellationToken
235
236
) . ConfigureAwait ( false ) ;
@@ -550,7 +551,6 @@ CancellationToken cancellationToken
550
551
else {
551
552
return SendResponseAsync (
552
553
client ,
553
- Encoding ,
554
554
"# Unknown command. Try cap, list, nodes, config, fetch, version or quit" ,
555
555
cancellationToken
556
556
) ;
@@ -566,22 +566,19 @@ CancellationToken cancellationToken
566
566
"." ,
567
567
] ;
568
568
569
- private static ValueTask SendResponseAsync (
569
+ private ValueTask SendResponseAsync (
570
570
Socket client ,
571
- Encoding encoding ,
572
571
string responseLine ,
573
572
CancellationToken cancellationToken
574
573
)
575
574
=> SendResponseAsync (
576
575
client : client ,
577
- encoding : encoding ,
578
576
responseLines : Enumerable . Repeat ( responseLine , 1 ) ,
579
577
cancellationToken : cancellationToken
580
578
) ;
581
579
582
- private static async ValueTask SendResponseAsync (
580
+ private async ValueTask SendResponseAsync (
583
581
Socket client ,
584
- Encoding encoding ,
585
582
IEnumerable < string > responseLines ,
586
583
CancellationToken cancellationToken
587
584
)
@@ -591,21 +588,38 @@ CancellationToken cancellationToken
591
588
592
589
cancellationToken . ThrowIfCancellationRequested ( ) ;
593
590
594
- foreach ( var responseLine in responseLines ) {
595
- var resp = encoding . GetBytes ( responseLine ) ;
591
+ try {
592
+ foreach ( var responseLine in responseLines ) {
593
+ #if SYSTEM_TEXT_ENCODINGEXTENSIONS
594
+ _ = Encoding . GetBytes ( responseLine , responseBuffer ) ;
596
595
597
- await client . SendAsync (
598
- buffer : resp ,
599
- socketFlags : SocketFlags . None ,
600
- cancellationToken : cancellationToken
601
- ) . ConfigureAwait ( false ) ;
596
+ responseBuffer . Write ( EndOfLine . Span ) ;
597
+ #else
598
+ var totalByteCount = Encoding . GetByteCount ( responseLine ) + EndOfLine . Length ;
599
+ var buffer = responseBuffer . GetMemory ( totalByteCount ) ;
600
+ var bytesWritten = Encoding . GetBytes ( responseLine , buffer . Span ) ;
601
+
602
+ EndOfLine . CopyTo ( buffer [ bytesWritten ..] ) ;
603
+
604
+ bytesWritten += EndOfLine . Length ;
605
+
606
+ responseBuffer . Advance ( bytesWritten ) ;
607
+ #endif
608
+ }
602
609
603
610
await client . SendAsync (
604
- buffer : EndOfLine ,
611
+ buffer : responseBuffer . WrittenMemory ,
605
612
socketFlags : SocketFlags . None ,
606
613
cancellationToken : cancellationToken
607
614
) . ConfigureAwait ( false ) ;
608
615
}
616
+ finally {
617
+ #if SYSTEM_BUFFERS_ARRAYBUFFERWRITER_RESETWRITTENCOUNT
618
+ responseBuffer . ResetWrittenCount ( ) ;
619
+ #else
620
+ responseBuffer . Clear ( ) ;
621
+ #endif
622
+ }
609
623
}
610
624
611
625
private ValueTask ProcessCommandNodesAsync (
@@ -615,11 +629,10 @@ CancellationToken cancellationToken
615
629
{
616
630
return SendResponseAsync (
617
631
client : client ,
618
- encoding : Encoding ,
619
- responseLines : new [ ] {
632
+ responseLines : [
620
633
HostName ,
621
634
"." ,
622
- } ,
635
+ ] ,
623
636
cancellationToken : cancellationToken
624
637
) ;
625
638
}
@@ -631,7 +644,6 @@ CancellationToken cancellationToken
631
644
{
632
645
return SendResponseAsync (
633
646
client : client ,
634
- encoding : Encoding ,
635
647
responseLine : $ "munins node on { HostName } version: { NodeVersion } ",
636
648
cancellationToken : cancellationToken
637
649
) ;
@@ -650,7 +662,6 @@ CancellationToken cancellationToken
650
662
// XXX: ignores capability arguments
651
663
return SendResponseAsync (
652
664
client : client ,
653
- encoding : Encoding ,
654
665
responseLine : "cap" ,
655
666
cancellationToken : cancellationToken
656
667
) ;
@@ -669,7 +680,6 @@ CancellationToken cancellationToken
669
680
// XXX: ignore [node] arguments
670
681
return SendResponseAsync (
671
682
client : client ,
672
- encoding : Encoding ,
673
683
responseLine : string . Join ( " " , PluginProvider . Plugins . Select ( static plugin => plugin . Name ) ) ,
674
684
cancellationToken : cancellationToken
675
685
) ;
@@ -690,7 +700,6 @@ CancellationToken cancellationToken
690
700
if ( plugin is null ) {
691
701
await SendResponseAsync (
692
702
client ,
693
- Encoding ,
694
703
ResponseLinesUnknownService ,
695
704
cancellationToken
696
705
) . ConfigureAwait ( false ) ;
@@ -712,7 +721,6 @@ await SendResponseAsync(
712
721
713
722
await SendResponseAsync (
714
723
client : client ,
715
- encoding : Encoding ,
716
724
responseLines : responseLines ,
717
725
cancellationToken : cancellationToken
718
726
) . ConfigureAwait ( false ) ;
@@ -750,7 +758,6 @@ CancellationToken cancellationToken
750
758
if ( plugin is null ) {
751
759
return SendResponseAsync (
752
760
client ,
753
- Encoding ,
754
761
ResponseLinesUnknownService ,
755
762
cancellationToken
756
763
) ;
@@ -816,7 +823,6 @@ void AddFieldValueRange(string attr, PluginFieldNormalValueRange range)
816
823
817
824
return SendResponseAsync (
818
825
client : client ,
819
- encoding : Encoding ,
820
826
responseLines : responseLines ,
821
827
cancellationToken : cancellationToken
822
828
) ;
0 commit comments