Skip to content

Commit 50a4d99

Browse files
committed
http server update
1 parent fb0316f commit 50a4d99

File tree

11 files changed

+396
-135
lines changed

11 files changed

+396
-135
lines changed

.dockerignore

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
**/.classpath
2+
**/.dockerignore
3+
**/.env
4+
**/.git
5+
**/.gitignore
6+
**/.project
7+
**/.settings
8+
**/.toolstarget
9+
**/.vs
10+
**/.vscode
11+
**/*.*proj.user
12+
**/*.dbmdl
13+
**/*.jfm
14+
**/azds.yaml
15+
**/bin
16+
**/charts
17+
**/docker-compose*
18+
**/Dockerfile*
19+
**/node_modules
20+
**/npm-debug.log
21+
**/obj
22+
**/secrets.dev.yaml
23+
**/values.dev.yaml
24+
LICENSE
25+
README.md

RelayServerTest.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,19 @@ EndProject
88
Global
99
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1010
Debug|Any CPU = Debug|Any CPU
11+
Debug|x86 = Debug|x86
1112
Release|Any CPU = Release|Any CPU
13+
Release|x86 = Release|x86
1214
EndGlobalSection
1315
GlobalSection(ProjectConfigurationPlatforms) = postSolution
1416
{5CBE7822-5903-4733-8DDE-67E9A5A85F0C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
1517
{5CBE7822-5903-4733-8DDE-67E9A5A85F0C}.Debug|Any CPU.Build.0 = Debug|Any CPU
18+
{5CBE7822-5903-4733-8DDE-67E9A5A85F0C}.Debug|x86.ActiveCfg = Debug|x86
19+
{5CBE7822-5903-4733-8DDE-67E9A5A85F0C}.Debug|x86.Build.0 = Debug|x86
1620
{5CBE7822-5903-4733-8DDE-67E9A5A85F0C}.Release|Any CPU.ActiveCfg = Release|Any CPU
1721
{5CBE7822-5903-4733-8DDE-67E9A5A85F0C}.Release|Any CPU.Build.0 = Release|Any CPU
22+
{5CBE7822-5903-4733-8DDE-67E9A5A85F0C}.Release|x86.ActiveCfg = Release|x86
23+
{5CBE7822-5903-4733-8DDE-67E9A5A85F0C}.Release|x86.Build.0 = Release|x86
1824
EndGlobalSection
1925
GlobalSection(SolutionProperties) = preSolution
2026
HideSolutionNode = FALSE

RelayServerTest/HttpSimple/Alternative/HttpNativeServer.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,7 @@ internal class HttpNativeServer
2424
string p = "<html><body><body style=\"background-color:black;\"><font color=\"white\"><pre style=\"font-size: large; color: white;\">{0}</pre>";
2525
string page = "<script>setTimeout(function(){ location.reload();},1000);</script></body></html>";
2626

27-
PerformanceCounter cpuCounter;
28-
PerformanceCounter ramCounter;
29-
30-
PerformanceCounter total_cpu; //= new PerformanceCounter("Process", "% Processor Time", "_Total");
31-
PerformanceCounter process_cpu; //= new PerformanceCounter("Process", "% Processor Time", Process.GetCurrentProcess().ProcessName);
32-
27+
3328
DateTime LastUpdate = DateTime.Now.AddSeconds(-2);
3429
private TcpStatistics TcpGeneralStats;
3530
private UdpStatistics udpGeneralStats;

RelayServerTest/HttpSimple/HttpHeaderUtil.cs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Drawing;
34
using System.Linq;
45
using System.Reflection.PortableExecutable;
56
using System.Text;
@@ -10,12 +11,13 @@ namespace RelayServer.HttpSimple
1011
internal static class HttpHeaderUtil
1112
{
1213
public const string MainPageHeader = @"HTTP/1.1 200 OK
13-
Content-Length: 777
1414
Content-Type: text/html
1515
Server: Microsoft-HTTPAPI/2.0
1616
Access-Control-Allow-Origin: *
1717
Connection: keep-alive
1818
Date: Fri, 27 Jan 2023 18:06:10 GMT
19+
Content-Length: 777
20+
1921
2022
";
2123
private const string GenericHeader1 = @"HTTP/1.1 200 OK
@@ -30,17 +32,29 @@ internal static class HttpHeaderUtil
3032
";
3133
static byte[] GenericHeader1Bytes = Encoding.ASCII.GetBytes(GenericHeader1);
3234
static byte[] GenericHeader2Bytes = Encoding.ASCII.GetBytes(GenericHeader2);
33-
static byte[] contentLenghtBytes= new byte[4];
35+
static byte[] contentLenghtBytes= new byte[3];
3436
static byte[] headerBytes;
3537
static HttpHeaderUtil()
3638
{
3739
headerBytes = GenericHeader1Bytes.Concat(contentLenghtBytes).Concat(GenericHeader2Bytes).ToArray();
3840
}
41+
3942
public static byte[] GetASCIIHeader(int contentLenght)
4043
{
41-
var contentLenghtBytes=Encoding.ASCII.GetBytes(contentLenght.ToString());
42-
Buffer.BlockCopy(contentLenghtBytes, 0, headerBytes, 33, contentLenghtBytes.Length);
44+
var contentLenghtBytes = Encoding.ASCII.GetBytes(contentLenght.ToString());
4345
return GenericHeader1Bytes.Concat(contentLenghtBytes).Concat(GenericHeader2Bytes).ToArray();
4446
}
47+
48+
public static void GetASCIIHeader(Stream stream, int contentLenght)
49+
{
50+
stream.Write(GenericHeader1Bytes, 0, GenericHeader1Bytes.Length);
51+
52+
var contentLenghtBytes = Encoding.ASCII.GetBytes(contentLenght.ToString());
53+
stream.Write(contentLenghtBytes, 0, contentLenghtBytes.Length);
54+
55+
stream.Write(GenericHeader2Bytes, 0, GenericHeader2Bytes.Length);
56+
57+
58+
}
4559
}
4660
}

RelayServerTest/HttpSimple/SimpleHttpServer.cs

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using NetworkLibrary.Components.Statistics;
1+
using NetworkLibrary.Components;
2+
using NetworkLibrary.Components.Statistics;
23
using NetworkLibrary.TCP.Base;
34
using NetworkLibrary.Utils;
45
using 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

RelayServerTest/PerformanceStatistics.cs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,34 @@ internal class PerformanceStatistics
1111
{
1212
private static Process process;
1313
private static TimeSpan lastProctime;
14-
private static DateTime lastRequestTimeCpu;
15-
private static DateTime lastRequestTimeMemory;
14+
private static long lastRequestTimeCpu;
15+
private static long lastRequestTimeMemory;
1616
private static string lastCpuUsage;
1717
private static string lastMemoryUsage;
18-
18+
static Stopwatch sw = new Stopwatch();
19+
static double ProcCount;
1920
static PerformanceStatistics()
2021
{
2122
process = Process.GetCurrentProcess();
2223
lastProctime=process.TotalProcessorTime;
23-
lastRequestTimeCpu = DateTime.Now;
24-
lastRequestTimeMemory = DateTime.Now;
24+
lastRequestTimeCpu = 0;
25+
lastRequestTimeMemory = 0;
26+
ProcCount = Convert.ToDouble(Environment.ProcessorCount);
27+
sw.Start();
28+
2529
}
2630
public static string GetCpuUsage()
2731
{
28-
if((DateTime.Now - lastRequestTimeCpu).TotalMilliseconds < 500)
32+
33+
long elapsed = sw.ElapsedMilliseconds;
34+
long deltaTms = elapsed - lastRequestTimeCpu;
35+
if(deltaTms < 900)
2936
{
3037
return lastCpuUsage;
3138
}
3239
var currentProcessorTime = process.TotalProcessorTime;
33-
var currentTimeStamp = DateTime.Now;
34-
double CPUUsage = (currentProcessorTime.TotalMilliseconds - lastProctime.TotalMilliseconds) / currentTimeStamp.Subtract(lastRequestTimeCpu).TotalMilliseconds / Convert.ToDouble(Environment.ProcessorCount);
35-
lastRequestTimeCpu = currentTimeStamp;
40+
double CPUUsage = (currentProcessorTime.TotalMilliseconds - lastProctime.TotalMilliseconds) / deltaTms / ProcCount;
41+
lastRequestTimeCpu = elapsed;
3642
lastProctime = currentProcessorTime;
3743
lastCpuUsage = (CPUUsage * 100).ToString("N3") + "%";
3844
return lastCpuUsage;
@@ -41,12 +47,13 @@ public static string GetCpuUsage()
4147

4248
public static string GetMemoryUsage()
4349
{
44-
if ((DateTime.Now - lastRequestTimeMemory).TotalMilliseconds < 500)
50+
long elapsed = sw.ElapsedMilliseconds;
51+
long deltaTms = elapsed - lastRequestTimeMemory;
52+
if (deltaTms < 900)
4553
{
4654
return lastMemoryUsage;
4755
}
48-
lastRequestTimeMemory=DateTime.Now;
49-
//process = Process.GetCurrentProcess();
56+
lastRequestTimeMemory=elapsed;
5057
process.Refresh();
5158
const double f = 1024.0 * 1024.0;
5259
lastMemoryUsage = (process.WorkingSet64 / f).ToString("N3") + "MB";

0 commit comments

Comments
 (0)