- added about window

- discord: async queue
This commit is contained in:
slothdpal
2025-09-19 20:18:33 +03:00
parent 41d8a0a3e2
commit 9989c4d58c
9 changed files with 2188 additions and 92 deletions

View File

@@ -1,104 +1,101 @@
using System;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Discord.Webhook
{
public class DiscordWebhook
{
/// <summary>
/// Webhook url
/// </summary>
public string Url { get; set; }
private void AddField(MemoryStream stream, string bound, string cDisposition, string cType, byte[] data)
{
string prefix = stream.Length > 0 ? "\r\n--" : "--";
string fBegin = $"{prefix}{bound}\r\n";
private UInt64 queueNum = 0;
private readonly ConcurrentQueue<(UInt64 num, DiscordMessage message, FileInfo[] files)> _queue = new ConcurrentQueue<(UInt64 num, DiscordMessage, FileInfo[])>();
private readonly SemaphoreSlim _semaphore = new SemaphoreSlim(1, 1);
private bool _isProcessing;
byte[] fBeginBuffer = Utils.Encode(fBegin);
byte[] cDispositionBuffer = Utils.Encode(cDisposition);
byte[] cTypeBuffer = Utils.Encode(cType);
stream.Write(fBeginBuffer, 0, fBeginBuffer.Length);
stream.Write(cDispositionBuffer, 0, cDispositionBuffer.Length);
stream.Write(cTypeBuffer, 0, cTypeBuffer.Length);
stream.Write(data, 0, data.Length);
}
private void SetJsonPayload(MemoryStream stream, string bound, string json)
{
string cDisposition = "Content-Disposition: form-data; name=\"payload_json\"\r\n";
string cType = "Content-Type: application/octet-stream\r\n\r\n";
AddField(stream, bound, cDisposition, cType, Utils.Encode(json));
}
private void SetFile(MemoryStream stream, string bound, int index, FileInfo file)
{
string cDisposition = $"Content-Disposition: form-data; name=\"file_{index}\"; filename=\"{file.Name}\"\r\n";
string cType = "Content-Type: application/octet-stream\r\n\r\n";
AddField(stream, bound, cDisposition, cType, File.ReadAllBytes(file.FullName));
}
/// <summary>
/// Send webhook message
/// </summary>
public void Send(DiscordMessage message, params FileInfo[] files)
public async Task SendAsync(DiscordMessage message, params FileInfo[] files)
{
if (string.IsNullOrEmpty(Url))
throw new ArgumentNullException("Invalid Webhook URL.");
string bound = "------------------------" + DateTime.Now.Ticks.ToString("x");
WebClient webhookRequest = new WebClient();
webhookRequest.Headers.Add("Content-Type", "multipart/form-data; boundary=" + bound);
MemoryStream stream = new MemoryStream();
for (int i = 0; i < files.Length; i++)
SetFile(stream, bound, i, files[i]);
string boundary = "------------------------" + DateTime.Now.Ticks.ToString("x");
string json = message.ToString();
SetJsonPayload(stream, bound, json);
byte[] bodyEnd = Utils.Encode($"\r\n--{bound}--");
stream.Write(bodyEnd, 0, bodyEnd.Length);
//byte[] beginBodyBuffer = Encoding.UTF8.GetBytes("--" + bound + "\r\n");
//stream.Write(beginBodyBuffer, 0, beginBodyBuffer.Length);
//bool flag = file != null && file.Exists;
//if (flag)
//{
// string fileBody = "Content-Disposition: form-data; name=\"file\"; filename=\"" + file.Name + "\"\r\nContent-Type: application/octet-stream\r\n\r\n";
// byte[] fileBodyBuffer = Encoding.UTF8.GetBytes(fileBody);
// stream.Write(fileBodyBuffer, 0, fileBodyBuffer.Length);
// byte[] fileBuffer = File.ReadAllBytes(file.FullName);
// stream.Write(fileBuffer, 0, fileBuffer.Length);
// string fileBodyEnd = "\r\n--" + bound + "\r\n";
// byte[] fileBodyEndBuffer = Encoding.UTF8.GetBytes(fileBodyEnd);
// stream.Write(fileBodyEndBuffer, 0, fileBodyEndBuffer.Length);
//}
//string jsonBody = string.Concat(new string[]
//{
// "Content-Disposition: form-data; name=\"payload_json\"\r\nContent-Type: application/json\r\n\r\n",
// string.Format("{0}\r\n", message),
// "--",
// bound,
// "--"
//});
//byte[] jsonBodyBuffer = Encoding.UTF8.GetBytes(jsonBody);
//stream.Write(jsonBodyBuffer, 0, jsonBodyBuffer.Length);
try
using (var client = new HttpClient() { Timeout = TimeSpan.FromSeconds(30) })
using (var content = new MultipartFormDataContent(boundary))
{
Uri uri = new Uri(this.Url);
webhookRequest.UploadData(uri, stream.ToArray());
}
catch (WebException ex)
{
throw new WebException(Utils.Decode(ex.Response.GetResponseStream()));
}
// Добавляем JSON payload
var jsonContent = new StringContent(message.ToString(), Encoding.UTF8, "application/json");
jsonContent.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("form-data")
{
Name = "\"payload_json\""
};
content.Add(jsonContent);
stream.Dispose();
// Добавляем файлы
for (int i = 0; i < files.Length; i++)
{
if (files[i].Exists)
{
var fileContent = new ByteArrayContent(File.ReadAllBytes(files[i].FullName));
fileContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream");
fileContent.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("form-data")
{
Name = $"\"file_{i}\"",
FileName = $"\"{files[i].Name}\""
};
content.Add(fileContent);
}
}
try
{
var response = await client.PostAsync(Url, content);
response.EnsureSuccessStatusCode();
}
catch (HttpRequestException ex)
{
Debug.WriteLine($"Discord webhook request failed: {ex.Message}");
}
}
}
private async Task ProcessQueueAsync()
{
while (_queue.TryDequeue(out var item))
{
await _semaphore.WaitAsync();
try
{
Debug.WriteLine($"Processing message {item.num}. Queue size: {_queue.Count}");
await SendAsync(item.message, item.files);
Debug.WriteLine($"Message {item.num} sent.");
}
finally
{
_semaphore.Release();
}
Task.Delay(500).Wait();
}
_isProcessing = false;
}
public void Send(DiscordMessage message, params FileInfo[] files)
{
_queue.Enqueue((queueNum++, message, files));
Debug.WriteLine($"Message {queueNum-1} added. Queue size: {_queue.Count}");
if (_isProcessing)
{
Debug.WriteLine("Already processing queue.");
return;
}
Debug.WriteLine("Run ProcessQueueAsync");
_isProcessing = true;
Task.Run(ProcessQueueAsync);
}
}
}
}