下载进度

main
吴顺明 1 year ago
parent 99dc9e9593
commit 37f7bcf2c3

@ -29,17 +29,17 @@ namespace UMC.Host
protected abstract void WebSocket(UMC.Net.NetContext context);
public virtual void PrepareRespone(HttpMimeRequest httpMimeRequest)
public virtual void PrepareRespone(HttpMimeRequest request)
{
this.TimeOut = 300;
var context = new HttpMimeContext(httpMimeRequest, new HttpMimeResponse(this, httpMimeRequest));
var context = new HttpMimeContext(request, new HttpMimeResponse(this, request));
try
{
context.ProcessRequest();
if (httpMimeRequest.IsWebSocket)
if (request.IsWebSocket)
{
this.WebSocket(context);
}

@ -10,141 +10,7 @@ using UMC.Net;
namespace UMC.Host
{
abstract class WebSocket : UMC.Net.MimeRequest, IDisposable
{
public override bool IsWebSocket => true;
public abstract void Dispose();
}
class HttpsWebSocket : WebSocket
{
public byte[] buffer = System.Buffers.ArrayPool<byte>.Shared.Rent(0x600);
System.IO.Stream stream;
public HttpsWebSocket(HttpMime mime, System.IO.Stream stream)
{
mime1 = mime;
this.stream = stream;
WebSocketRead();
}
public override void Receive(byte[] buffer, int offset, int size)
{
stream.Write(buffer, offset, size);
}
HttpMime mime1;
async void WebSocketRead()
{
int size = 0;
try
{
while ((size = await stream.ReadAsync(buffer, 0, buffer.Length)) > 0)
{
mime1.Write(buffer, 0, size);
}
}
catch
{
this.Dispose();
return;
}
this.Dispose();
}
public override void Dispose()
{
mime1.Dispose();
stream.Close();
stream.Dispose();
if (buffer != null)
{
System.Buffers.ArrayPool<byte>.Shared.Return(buffer);
buffer = null;
}
}
protected override void Header(byte[] data, int offset, int size)
{
}
}
class HttpWebSocket : WebSocket
{
public byte[] buffer = System.Buffers.ArrayPool<byte>.Shared.Rent(0x600);
Socket socket;
public HttpWebSocket(HttpMime mime, Socket stream)
{
this.mime = mime;
this.socket = stream;
SocketAsyncEventArgs eventArgs = new SocketAsyncEventArgs();
eventArgs.SetBuffer(buffer);
eventArgs.Completed += Http;
if (!this.socket.ReceiveAsync(eventArgs))
{
ProcessReceive(eventArgs);
}
}
HttpMime mime;
private void Http(object sender, SocketAsyncEventArgs e)
{
switch (e.LastOperation)
{
case SocketAsyncOperation.Receive:
ProcessReceive(e);
break;
}
}
private void ProcessReceive(SocketAsyncEventArgs e)
{
if (e.BytesTransferred > 0 && e.SocketError == SocketError.Success)
{
try
{
this.mime.Write(buffer, e.Offset, e.BytesTransferred);
if (!socket.ReceiveAsync(e))
{
ProcessReceive(e);
}
}
catch
{
this.Dispose();
}
}
else
{
this.Dispose();
}
}
public override void Receive(byte[] buffer, int offset, int size)
{
socket.Send(buffer, offset, size, SocketFlags.None);
}
protected override void Header(byte[] data, int offset, int size)
{
}
public override void Dispose()
{
this.Dispose();
try
{
socket.Shutdown(SocketShutdown.Both);
}
catch
{
}
socket.Close();
System.Buffers.ArrayPool<byte>.Shared.Return(buffer);
buffer = null;
}
}
public class HttpMimeSocket : HttpMime
{
public override string Scheme => "http";

@ -1,218 +0,0 @@
using System;
using System.Buffers;
using System.IO;
using System.Net;
using System.Net.Security;
using System.Net.Sockets;
using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;
using System.Threading;
using System.Threading.Tasks;
using System.Threading.Tasks.Sources;
using UMC.Net;
namespace UMC.Host
{
class HttpMimeStream : System.IO.Stream, IValueTaskSource<int>
{
private ManualResetValueTaskSourceCore<int> _source = new ManualResetValueTaskSourceCore<int>();
#region 实现接口,告诉调用者,任务是否已经完成,以及是否有结果,是否有异常等
// 获取结果
public int GetResult(short token)
{
return _source.GetResult(token);
}
public ValueTaskSourceStatus GetStatus(short token)
{
return _source.GetStatus(token); ;
}
// 实现延续
public void OnCompleted(Action<object> continuation, object state, short token, ValueTaskSourceOnCompletedFlags flags)
{
_source.OnCompleted(continuation, state, token, flags);
}
#endregion
// 以及完成任务,并给出结果
public void SetResult(int result)
{
_source.SetResult(result);
}
short _token = 0;
// 要执行的任务出现异常
public void SetException(Exception exception)
{
_source.SetException(exception);
}
private int _disposed;
public override bool CanRead => true;
public override bool CanSeek => false;
public override bool CanWrite => true;
public override long Length
{
get
{
throw new NotSupportedException();
}
}
public override long Position
{
get
{
throw new NotSupportedException();
}
set
{
throw new NotSupportedException();
}
}
HttpMime _mime;
public HttpMimeStream(HttpMime mime)
{
this._mime = mime;
}
public override long Seek(long offset, SeekOrigin origin)
{
throw new NotSupportedException();
}
public override int Read(byte[] buffer, int offset, int count)
{
return this.ReadAsync(buffer, offset, count).Result;
}
public void AppendData(byte[] buffer, int offset, int size)
{
if (_buffers.IsEmpty == false)
{
if (_buffers.Length >= size)
{
buffer.AsMemory(offset, size).CopyTo(_buffer);
_source.SetResult(size);
}
else
{
buffer.AsMemory(offset, _buffers.Length).CopyTo(_buffer);
int len = size - _buffers.Length;
if (len + _bufferSize > _buffer.Length)
{
var _bs = new byte[len + _bufferSize + 200];
Array.Copy(_buffer, 0, _bs, 0, _bufferSize);
_buffer = _bs;
}
Array.Copy(buffer, offset + _buffer.Length, _buffer, _bufferSize, len);
_source.SetResult(_buffers.Length);
}
}
}
public override void Write(byte[] buffer, int offset, int count)
{
_mime.Write(buffer, offset, count);
}
protected override void Dispose(bool disposing)
{
if (Interlocked.Exchange(ref _disposed, 1) != 0)
{
return;
}
base.Dispose(disposing);
}
public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
Stream.ValidateBufferArguments(buffer, offset, count);
return ReadAsync(new Memory<byte>(buffer, offset, count), cancellationToken).AsTask();
}
byte[] _buffer = Array.Empty<byte>();
int _bufferSize = 0;
Memory<byte> _buffers;
//int _start = 0;
public override ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken)
{
if (_bufferSize == 0)
{
_token++;
_buffers = buffer;
_source.Reset();
return new ValueTask<int>(this, _token);
}
if (buffer.Length >= _bufferSize)
{
int len = _bufferSize;
_buffer.AsMemory(0, _bufferSize).CopyTo(buffer);
_bufferSize = 0;
return new ValueTask<int>(len);
}
else
{
_buffer.AsMemory(0, buffer.Length).CopyTo(buffer);
_bufferSize -= buffer.Length;
Array.Copy(_buffer, buffer.Length, _buffer, 0, _bufferSize);
return new ValueTask<int>(buffer.Length);
}
}
public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
Stream.ValidateBufferArguments(buffer, offset, count);
_mime.Write(buffer, offset, count);
return Task.CompletedTask;
//NetworkStream
}
public override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken)
{
byte[] array = ArrayPool<byte>.Shared.Rent(buffer.Length);
try
{
buffer.CopyTo(array);
_mime.Write(array, 0, buffer.Length);
return ValueTask.CompletedTask;
}
finally
{
ArrayPool<byte>.Shared.Return(array);
}
}
public override void Flush()
{
}
public override Task FlushAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
public override void SetLength(long value)
{
throw new NotSupportedException();
}
}
}

@ -1,14 +1,25 @@
using System.Threading.Tasks;
using System;
using System.Buffers;
using System.IO;
using System.Net;
using System.Net.Security;
using System.Net.Sockets;
using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Threading.Tasks.Sources;
using UMC.Net;
using System.Net.WebSockets;
namespace UMC.Host
{
class HttpWebSocket2
abstract class WebSocket : UMC.Net.MimeRequest, IDisposable
{
public override bool IsWebSocket => true;
public abstract void Dispose();
internal static string GetSecWebSocketAcceptString(string secWebSocketKey)
{
@ -79,7 +90,7 @@ namespace UMC.Host
writer.Write($"Sec-WebSocket-Accept: {secWebSocketAcceptString}\r\n");
writer.Write($"Sec-WebSocket-Protocol: mqtt\r\n");
writer.Write("Server: UMC.Proxy\r\n\r\n");
writer.Write("Server: Apiumc\r\n\r\n");
writer.Flush();
writer.Dispose();
@ -97,5 +108,83 @@ namespace UMC.Host
}
class HttpWebSocket : WebSocket
{
public byte[] buffer = System.Buffers.ArrayPool<byte>.Shared.Rent(0x600);
Socket socket;
public HttpWebSocket(HttpMime mime, Socket stream)
{
this.mime = mime;
this.socket = stream;
SocketAsyncEventArgs eventArgs = new SocketAsyncEventArgs();
eventArgs.SetBuffer(buffer);
eventArgs.Completed += Http;
if (!this.socket.ReceiveAsync(eventArgs))
{
ProcessReceive(eventArgs);
}
}
HttpMime mime;
private void Http(object sender, SocketAsyncEventArgs e)
{
switch (e.LastOperation)
{
case SocketAsyncOperation.Receive:
ProcessReceive(e);
break;
}
}
private void ProcessReceive(SocketAsyncEventArgs e)
{
if (e.BytesTransferred > 0 && e.SocketError == SocketError.Success)
{
try
{
this.mime.Write(buffer, e.Offset, e.BytesTransferred);
if (!socket.ReceiveAsync(e))
{
ProcessReceive(e);
}
}
catch
{
this.Dispose();
}
}
else
{
this.Dispose();
}
}
public override void Receive(byte[] buffer, int offset, int size)
{
socket.Send(buffer, offset, size, SocketFlags.None);
}
protected override void Header(byte[] data, int offset, int size)
{
}
public override void Dispose()
{
this.Dispose();
try
{
socket.Shutdown(SocketShutdown.Both);
}
catch
{
}
socket.Close();
System.Buffers.ArrayPool<byte>.Shared.Return(buffer);
buffer = null;
}
}
}

@ -1,6 +1,5 @@
using System.Net.WebSockets;
namespace UMC.Host;
namespace UMC.Host;
using System;
using System.Collections.Specialized;
using System.IO;

@ -0,0 +1,64 @@
using System.Threading.Tasks;
using System;
using System.IO;
using System.Text;
using UMC.Net;
using System.Net.WebSockets;
namespace UMC.Host
{
class HttpsWebSocket : WebSocket
{
public byte[] buffer = System.Buffers.ArrayPool<byte>.Shared.Rent(0x600);
System.IO.Stream stream;
public HttpsWebSocket(HttpMime mime, System.IO.Stream stream)
{
mime1 = mime;
this.stream = stream;
WebSocketRead();
}
public override void Receive(byte[] buffer, int offset, int size)
{
stream.Write(buffer, offset, size);
}
HttpMime mime1;
async void WebSocketRead()
{
int size = 0;
try
{
while ((size = await stream.ReadAsync(buffer, 0, buffer.Length)) > 0)
{
mime1.Write(buffer, 0, size);
}
}
catch
{
this.Dispose();
return;
}
this.Dispose();
}
public override void Dispose()
{
mime1.Dispose();
stream.Close();
stream.Dispose();
if (buffer != null)
{
System.Buffers.ArrayPool<byte>.Shared.Return(buffer);
buffer = null;
}
}
protected override void Header(byte[] data, int offset, int size)
{
}
}
}

@ -21,9 +21,6 @@ namespace UMC.Host
public static void Main(string[] args)
{
// var ipHost = Dns.GetHostAddresses("blj.sau.edu.cn");
AppDomain.CurrentDomain.UnhandledException += UnhandledException;
var variable = System.Environment.GetEnvironmentVariable("UMC");
@ -112,7 +109,7 @@ namespace UMC.Host
public static bool IsRun()
{
using (var pipeClient = new NamedPipeClientStream(".", $"APIUMC", PipeDirection.InOut))
using (var pipeClient = new NamedPipeClientStream(".", "APIUMC", PipeDirection.InOut))
{
try
{
@ -257,6 +254,34 @@ namespace UMC.Host
Console.WriteLine("Apiumc网关服务未开启请开启。");
}
break;
case "restart":
if (IsRun())
{
Write(new string[] { "stop" });
Thread.Sleep(5000);
Excel("main");
}
else
{
Excel("main");
}
if (OperatingSystem.IsWindows())
{
Console.WriteLine();
Console.Write("特别注意:");
Console.ForegroundColor = ConsoleColor.DarkRed;
Console.WriteLine("需要退出程序时,请用[exit]指令退出当前程序");
Console.WriteLine();
Console.ResetColor();
}
else
{
IsDo = false;
}
Write("info");
break;
case "start":
if (IsRun() == false)
{
@ -399,7 +424,6 @@ namespace UMC.Host
size += l;
if (b.Length == 0)
{
Console.WriteLine("下载完成");
Console.WriteLine();
stream.Flush();
@ -417,8 +441,9 @@ namespace UMC.Host
Utility.Move(UMC.Data.Utility.MapPath("/cavif"), file);
}
}
catch//(Exception ex)
catch
{
Console.SetCursorPosition(0, Console.CursorTop);
Console.WriteLine($"图片组件下载失败,影响图片功能");
}
File.Delete(downloadFile);
@ -426,8 +451,8 @@ namespace UMC.Host
}
else
{
Console.Write($"正在下载图片组件{size * 100 / count:0}%");
Console.SetCursorPosition(0, Console.CursorTop);
Console.Write($"正在下载图片组件{size * 100 / count:0}%");
stream.Write(b, c, l);
}
@ -487,8 +512,7 @@ namespace UMC.Host
APIProxy.Version = p?.InformationalVersion ?? "1.0.0";
Register();
// File.WriteAllText(UMC.Data.Utility.MapPath($".lock"), $"{{\"Id\":\"{System.Diagnostics.Process.GetCurrentProcess().Id}\"}}");
// UMC.Data.HotCache.Namespace(typeof(UMC.Proxy.Entities.Cookie).Namespace);
UMC.Proxy.DataFactory.Instance();
UMC.Data.DataFactory.Instance();

@ -31,7 +31,7 @@
var qvalue = UMC.query(location.search.substring(1));
UMC.UI.On("User", function () {
if (qvalue.oauth_callback) {
location.href = v.oauth_callback;
location.href = qvalue.oauth_callback;
} else {
location.reload(true);
}
@ -55,7 +55,6 @@
'code': info.code,
'appid': corpId,
'transfer': qvalue.transfer
});
}
});

Loading…
Cancel
Save