mirror of
https://github.com/esiur/esiur-dotnet.git
synced 2025-05-06 11:32:59 +00:00
ASPNet Support
This commit is contained in:
parent
7d9751501e
commit
5745887b48
@ -1,48 +1,75 @@
|
|||||||
|
using Esiur.ASPNet;
|
||||||
|
using Esiur.Core;
|
||||||
|
using Esiur.Net.IIP;
|
||||||
|
using Esiur.Net.Sockets;
|
||||||
|
using Esiur.Resource;
|
||||||
|
using Esiur.Stores;
|
||||||
|
using Microsoft.AspNetCore.Hosting.Server;
|
||||||
|
using System.Net.WebSockets;
|
||||||
|
using System.Reflection;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
var builder = WebApplication.CreateBuilder(args);
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
|
|
||||||
// Add services to the container.
|
// Add services to the container.
|
||||||
|
|
||||||
builder.Services.AddControllers();
|
//builder.Services.AddControllers();
|
||||||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
||||||
builder.Services.AddEndpointsApiExplorer();
|
//builder.Services.AddEndpointsApiExplorer();
|
||||||
builder.Services.AddSwaggerGen();
|
//builder.Services.AddSwaggerGen();
|
||||||
|
|
||||||
|
builder.WebHost.UseUrls("http://localhost:8080");
|
||||||
|
|
||||||
var app = builder.Build();
|
var app = builder.Build();
|
||||||
|
|
||||||
// Configure the HTTP request pipeline.
|
var webSocketOptions = new WebSocketOptions()
|
||||||
if (app.Environment.IsDevelopment())
|
|
||||||
{
|
{
|
||||||
app.UseSwagger();
|
KeepAliveInterval = TimeSpan.FromSeconds(120),
|
||||||
app.UseSwaggerUI();
|
};
|
||||||
}
|
|
||||||
|
|
||||||
app.UseHttpsRedirection();
|
app.UseWebSockets(webSocketOptions);
|
||||||
|
|
||||||
app.UseAuthorization();
|
|
||||||
|
|
||||||
app.MapControllers();
|
// Configure the HTTP request pipeline.
|
||||||
|
//if (app.Environment.IsDevelopment())
|
||||||
|
//{
|
||||||
|
// app.UseSwagger();
|
||||||
|
// app.UseSwaggerUI();
|
||||||
|
//}
|
||||||
|
|
||||||
app.UseWebSockets();
|
//app.UseHttpsRedirection();
|
||||||
|
|
||||||
|
//app.UseAuthorization();
|
||||||
|
|
||||||
|
//app.MapControllers();
|
||||||
|
|
||||||
|
|
||||||
|
await Warehouse.Put("sys", new MemoryStore());
|
||||||
|
await Warehouse.Put("sys/service", new MyResource());
|
||||||
|
var server = await Warehouse.Put("sys/server", new DistributedServer());
|
||||||
|
|
||||||
|
await Warehouse.Open();
|
||||||
|
|
||||||
app.Use(async (context, next) =>
|
app.Use(async (context, next) =>
|
||||||
{
|
{
|
||||||
if (context.Request.Path == "/esiur")
|
var buffer = new ArraySegment<byte>(new byte[10240]);
|
||||||
|
|
||||||
|
if (context.WebSockets.IsWebSocketRequest)
|
||||||
{
|
{
|
||||||
if (context.WebSockets.IsWebSocketRequest)
|
var webSocket = await context.WebSockets.AcceptWebSocketAsync("iip");
|
||||||
{
|
var socket = new FrameworkWebSocket(webSocket);
|
||||||
using var webSocket = await context.WebSockets.AcceptWebSocketAsync();
|
var iipConnection = new DistributedConnection();
|
||||||
}
|
server.Add(iipConnection);
|
||||||
else
|
iipConnection.Assign(socket);
|
||||||
{
|
socket.Begin();
|
||||||
context.Response.StatusCode = StatusCodes.Status400BadRequest;
|
|
||||||
}
|
while (webSocket.State == WebSocketState.Open) ;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
await next(context);
|
await next(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
app.Run();
|
await app.RunAsync();
|
||||||
|
@ -1595,7 +1595,7 @@ public partial class DistributedConnection : NetworkConnection, IStore
|
|||||||
{
|
{
|
||||||
var os = RuntimeInformation.FrameworkDescription;
|
var os = RuntimeInformation.FrameworkDescription;
|
||||||
if (UseWebSocket || RuntimeInformation.OSDescription == "Browser")
|
if (UseWebSocket || RuntimeInformation.OSDescription == "Browser")
|
||||||
socket = new ClientWSocket();
|
socket = new FrameworkWebSocket();
|
||||||
else
|
else
|
||||||
socket = new TCPSocket();
|
socket = new TCPSocket();
|
||||||
}
|
}
|
||||||
|
@ -11,13 +11,15 @@ using System.Drawing;
|
|||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using System.Net.Sockets;
|
using System.Net.Sockets;
|
||||||
|
using Microsoft.CodeAnalysis;
|
||||||
|
|
||||||
namespace Esiur.Net.Sockets
|
namespace Esiur.Net.Sockets
|
||||||
{
|
{
|
||||||
public class ClientWSocket : ISocket
|
public class FrameworkWebSocket : ISocket
|
||||||
{
|
{
|
||||||
|
bool began;
|
||||||
|
|
||||||
ClientWebSocket sock;
|
WebSocket sock;
|
||||||
|
|
||||||
NetworkBuffer receiveNetworkBuffer = new NetworkBuffer();
|
NetworkBuffer receiveNetworkBuffer = new NetworkBuffer();
|
||||||
NetworkBuffer sendNetworkBuffer = new NetworkBuffer();
|
NetworkBuffer sendNetworkBuffer = new NetworkBuffer();
|
||||||
@ -38,16 +40,34 @@ namespace Esiur.Net.Sockets
|
|||||||
public IPEndPoint RemoteEndPoint { get; } = new IPEndPoint(IPAddress.Any, 0);
|
public IPEndPoint RemoteEndPoint { get; } = new IPEndPoint(IPAddress.Any, 0);
|
||||||
|
|
||||||
|
|
||||||
public SocketState State { get; internal set; } = SocketState.Closed;
|
public SocketState State => sock == null ? SocketState.Closed : sock.State switch
|
||||||
|
{
|
||||||
|
WebSocketState.Aborted => SocketState.Closed,
|
||||||
|
WebSocketState.Closed => SocketState.Closed,
|
||||||
|
WebSocketState.Connecting => SocketState.Connecting,
|
||||||
|
WebSocketState.Open => SocketState.Established,
|
||||||
|
WebSocketState.CloseReceived => SocketState.Closed,
|
||||||
|
WebSocketState.CloseSent => SocketState.Closed,
|
||||||
|
WebSocketState.None => SocketState.Initial,
|
||||||
|
_ => SocketState.Initial
|
||||||
|
};
|
||||||
|
|
||||||
public INetworkReceiver<ISocket> Receiver { get; set; }
|
public INetworkReceiver<ISocket> Receiver { get; set; }
|
||||||
|
|
||||||
public ClientWSocket()
|
public FrameworkWebSocket()
|
||||||
{
|
{
|
||||||
websocketReceiveBufferSegment = new ArraySegment<byte>(websocketReceiveBuffer);
|
websocketReceiveBufferSegment = new ArraySegment<byte>(websocketReceiveBuffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public FrameworkWebSocket(WebSocket webSocket)
|
||||||
|
{
|
||||||
|
websocketReceiveBufferSegment = new ArraySegment<byte>(websocketReceiveBuffer);
|
||||||
|
sock = webSocket;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public void Send(byte[] message)
|
public void Send(byte[] message)
|
||||||
{
|
{
|
||||||
|
|
||||||
@ -88,7 +108,7 @@ namespace Esiur.Net.Sockets
|
|||||||
|
|
||||||
public void Close()
|
public void Close()
|
||||||
{
|
{
|
||||||
sock.CloseAsync(WebSocketCloseStatus.NormalClosure, "", new System.Threading.CancellationToken());
|
sock?.CloseAsync(WebSocketCloseStatus.NormalClosure, "", new System.Threading.CancellationToken());
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool Secure { get; set; }
|
public bool Secure { get; set; }
|
||||||
@ -97,23 +117,36 @@ namespace Esiur.Net.Sockets
|
|||||||
{
|
{
|
||||||
var url = new Uri($"{(Secure ? "wss" : "ws")}://{hostname}:{port}");
|
var url = new Uri($"{(Secure ? "wss" : "ws")}://{hostname}:{port}");
|
||||||
|
|
||||||
sock = new ClientWebSocket();
|
var ws = new ClientWebSocket();
|
||||||
await sock.ConnectAsync(url, new CancellationToken());
|
sock = ws;
|
||||||
|
|
||||||
State = SocketState.Established;
|
await ws.ConnectAsync(url, new CancellationToken());
|
||||||
|
|
||||||
sock.ReceiveAsync(websocketReceiveBufferSegment, new CancellationToken())
|
|
||||||
|
sock.ReceiveAsync(websocketReceiveBufferSegment, CancellationToken.None)
|
||||||
.ContinueWith(NetworkReceive);
|
.ContinueWith(NetworkReceive);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public bool Begin()
|
public bool Begin()
|
||||||
{
|
{
|
||||||
|
|
||||||
|
// Socket destroyed
|
||||||
|
if (sock == null)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (began)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
began = true;
|
||||||
|
|
||||||
|
sock.ReceiveAsync(websocketReceiveBufferSegment, CancellationToken.None)
|
||||||
|
.ContinueWith(NetworkReceive);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool Trigger(ResourceTrigger trigger)
|
public bool Trigger(ResourceTrigger trigger)
|
||||||
@ -187,14 +220,14 @@ namespace Esiur.Net.Sockets
|
|||||||
|
|
||||||
public AsyncReply<bool> BeginAsync()
|
public AsyncReply<bool> BeginAsync()
|
||||||
{
|
{
|
||||||
return new AsyncReply<bool>(true);
|
return new AsyncReply<bool>(Begin());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private void NetworkReceive(Task<WebSocketReceiveResult> task)
|
private void NetworkReceive(Task<WebSocketReceiveResult> task)
|
||||||
{
|
{
|
||||||
|
|
||||||
if (sock.State == WebSocketState.Closed)
|
if (sock.State == WebSocketState.Closed || sock.State == WebSocketState.Aborted)
|
||||||
{
|
{
|
||||||
Receiver?.NetworkClose(this);
|
Receiver?.NetworkClose(this);
|
||||||
return;
|
return;
|
||||||
@ -207,7 +240,7 @@ namespace Esiur.Net.Sockets
|
|||||||
|
|
||||||
Receiver?.NetworkReceive(this, receiveNetworkBuffer);
|
Receiver?.NetworkReceive(this, receiveNetworkBuffer);
|
||||||
|
|
||||||
sock.ReceiveAsync(websocketReceiveBufferSegment, new CancellationToken())
|
sock.ReceiveAsync(websocketReceiveBufferSegment, CancellationToken.None)
|
||||||
.ContinueWith(NetworkReceive);
|
.ContinueWith(NetworkReceive);
|
||||||
|
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user