1- using NetworkLibrary . Components . Statistics ;
1+ using NetworkLibrary . Components ;
2+ using NetworkLibrary . Components . Statistics ;
23using NetworkLibrary . TCP . Base ;
34using NetworkLibrary . Utils ;
45using Protobuff . P2P ;
@@ -30,20 +31,58 @@ internal class SimpleHttpServer
3031
3132 private SecureProtoRelayServer server ;
3233 private AsyncTcpServer httpMiniServer ;
33- private SharerdMemoryStreamPool streamPool = new SharerdMemoryStreamPool ( ) ;
34- public SimpleHttpServer ( SecureProtoRelayServer s , int porthttp )
34+ private SharerdMemoryStreamPool streamPool = new SharerdMemoryStreamPool ( ) ;
35+ byte [ ] cachedSendArray = new byte [ 500 ] ;
36+
37+ public SimpleHttpServer ( SecureProtoRelayServer s , int porthttp )
3538 {
3639 server = s ;
3740
3841 var mainPageHeaderBytes = Encoding . ASCII . GetBytes ( HttpHeaderUtil . MainPageHeader ) ;
39- var mainPageBodybytes = Encoding . UTF8 . GetBytes ( PageResources . TextVisualizePage ) ;
42+ var mainPageBodybytes = Encoding . ASCII . GetBytes ( PageResources . TextVisualizePage ) ;
4043 mainPageBytes = mainPageHeaderBytes . Concat ( mainPageBodybytes ) . ToArray ( ) ;
41-
44+
4245 httpMiniServer = new AsyncTcpServer ( porthttp ) ;
43- httpMiniServer . GatherConfig = ScatterGatherConfig . UseQueue ;
46+ httpMiniServer . GatherConfig = ScatterGatherConfig . UseBuffer ;
4447 httpMiniServer . MaxIndexedMemoryPerClient = 128000 ;
4548 httpMiniServer . OnBytesReceived += ServerBytesReceived ;
4649 httpMiniServer . StartServer ( ) ;
50+
51+ }
52+
53+ [ ThreadStatic ]
54+ static PooledMemoryStream stream ;
55+
56+ int len1 = textPagePart1 . Length ;
57+ int len2 = textPagePart2 . Length ;
58+ byte [ ] textPagePart1Bytes = Encoding . ASCII . GetBytes ( textPagePart1 ) ;
59+ byte [ ] textPagePart2Bytes = Encoding . ASCII . GetBytes ( textPagePart2 ) ;
60+
61+ PooledMemoryStream GetTextResponse ( string data )
62+ {
63+ int bodyLen = data . Length + len1 + len2 ;
64+
65+ if ( stream == null )
66+ {
67+ stream = new PooledMemoryStream ( ) ;
68+ // ensure capcacity;
69+ stream . Position = 5000 ;
70+ }
71+ // write header
72+ stream . Position = 0 ;
73+ HttpHeaderUtil . GetASCIIHeader ( stream , bodyLen ) ;
74+
75+ // write static part 1 of the body
76+ stream . Write ( textPagePart1Bytes ) ;
77+
78+ // write dynamic part of body
79+ Encoding . ASCII . GetBytes ( data , 0 , data . Length , stream . GetBuffer ( ) , ( int ) stream . Position ) ;
80+ stream . Position += data . Length ;
81+
82+ // write static part 2 of body
83+ stream . Write ( textPagePart2Bytes ) ;
84+
85+ return stream ;
4786 }
4887 private void ServerBytesReceived ( in Guid guid , byte [ ] bytes , int offset , int count )
4988 {
@@ -65,13 +104,9 @@ private void ServerBytesReceived(in Guid guid, byte[] bytes, int offset, int cou
65104 TcpGeneralStats . ToString ( ) + "\n \n " +
66105 udpGeneralStats . ToString ( ) ;
67106
68- string page = textPagePart1 + data + textPagePart2 ;
69- byte [ ] body = Encoding . UTF8 . GetBytes ( page ) ;
70- byte [ ] header = HttpHeaderUtil . GetASCIIHeader ( body . Length ) ;
71-
72- httpMiniServer . SendBytesToClient ( in guid , header ) ;
73- httpMiniServer . SendBytesToClient ( in guid , body ) ;
74107
108+ var stream = GetTextResponse ( data ) ;
109+ httpMiniServer . SendBytesToClient ( in guid , stream . GetBuffer ( ) , 0 , ( int ) stream . Position ) ;
75110 }
76111 } catch { }
77112
0 commit comments