-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttpserver.cs
More file actions
144 lines (129 loc) · 4.69 KB
/
Copy pathhttpserver.cs
File metadata and controls
144 lines (129 loc) · 4.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.ResponseCompression;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Linq;
using System.Collections.Generic;
using System.IO.Compression;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace AllPet.http.server
{
public class httpserver
{
public httpserver()
{
onHttpEvents = new System.Collections.Concurrent.ConcurrentDictionary<string, IController>();
}
private IWebHost host;
public System.Collections.Concurrent.ConcurrentDictionary<string, IController> onHttpEvents;
deleProcessHttp onHttp404;
public void Start(int port, int portForHttps = 0, string pfxpath = null, string password = null)
{
host = new WebHostBuilder().UseKestrel((options) =>
{
options.Listen(IPAddress.Any, port, listenOptions =>
{
});
if (portForHttps != 0)
{
options.Listen(IPAddress.Any, portForHttps, listenOptions =>
{
//if (!string.IsNullOrEmpty(sslCert))
//if (useHttps)
listenOptions.UseHttps(pfxpath, password);
//sslCert, password);
});
}
}).Configure(app =>
{
app.UseWebSockets();
app.UseResponseCompression();
app.Run(ProcessAsync);
}).ConfigureServices(services =>
{
services.AddResponseCompression(options =>
{
options.EnableForHttps = false;
options.Providers.Add<GzipCompressionProvider>();
options.MimeTypes = ResponseCompressionDefaults.MimeTypes.Concat(new[] { "application/json" });
});
services.Configure<GzipCompressionProviderOptions>(options =>
{
options.Level = CompressionLevel.Fastest;
});
}).Build();
host.Start();
}
public void AddJsonRPC(string path, string method, JSONRPCController.ActionRPC action)
{
if (onHttpEvents.ContainsKey(path) == false)
{
onHttpEvents[path] = new JSONRPCController();
}
var jsonc = onHttpEvents[path] as JSONRPCController;
jsonc.AddAction(method, action);
}
public void SetJsonRPCFail(string path, JSONRPCController.ActionRPCFail action)
{
if (onHttpEvents.ContainsKey(path) == false)
{
onHttpEvents[path] = new JSONRPCController();
}
var jsonc = onHttpEvents[path] as JSONRPCController;
jsonc.SetFailAction(action);
}
public void SetHttpAction(string path, deleProcessHttp httpaction)
{
onHttpEvents[path] = new ActionController(httpaction);
}
public void SetWebsocketAction(string path, deleWebSocketCreator websocketaction)
{
onHttpEvents[path] = new WebSocketController(websocketaction);
}
public void SetHttpController(string path, IController controller)
{
onHttpEvents[path] = controller;
}
public void SetFailAction(deleProcessHttp httpaction)
{
onHttp404 = httpaction;
}
public delegate Task deleProcessHttp(HttpContext context);
public interface IWebSocketPeer
{
Task OnConnect();
Task OnRecv(System.IO.MemoryStream stream, int count);
Task OnDisConnect();
}
public delegate IWebSocketPeer deleWebSocketCreator(System.Net.WebSockets.WebSocket websocket);
//public enum WebsocketEventType
//{
// Connect,
// Disconnect,
// Recieve,
//}
//public delegate Task onProcessWebsocket(WebsocketEventType type, System.Net.WebSockets.WebSocket context, byte[] message = null);
private async Task ProcessAsync(HttpContext context)
{
try
{
var path = context.Request.Path.Value;
if (onHttpEvents.TryGetValue(path.ToLower(), out IController controller))
{
await controller.ProcessAsync(context);
}
else
{
await onHttp404(context);
}
}
catch
{
}
}
}
}