mirror of
https://github.com/Stone-Red-Code/HyperbolicDownloader.git
synced 2026-09-09 07:46:26 +02:00
Make sure NetworkClient can properly handle concurrent requests.
This commit is contained in:
@@ -86,7 +86,7 @@ internal class NetworkClient(FilesManager filesManager)
|
|||||||
tcpListener.Start();
|
tcpListener.Start();
|
||||||
IsListening = true;
|
IsListening = true;
|
||||||
|
|
||||||
_ = new TaskFactory().StartNew(async () =>
|
_ = new TaskFactory().StartNew(() =>
|
||||||
{
|
{
|
||||||
while (IsListening)
|
while (IsListening)
|
||||||
{
|
{
|
||||||
@@ -94,10 +94,36 @@ internal class NetworkClient(FilesManager filesManager)
|
|||||||
{
|
{
|
||||||
ApiManager.SendNotificationMessageNewLine("Listening for connections...", NotificationMessageType.Debug);
|
ApiManager.SendNotificationMessageNewLine("Listening for connections...", NotificationMessageType.Debug);
|
||||||
|
|
||||||
TcpClient client = tcpListener.AcceptTcpClient();
|
using TcpClient client = tcpListener.AcceptTcpClient();
|
||||||
|
|
||||||
ApiManager.SendNotificationMessageNewLine($"{(client.Client.RemoteEndPoint as IPEndPoint)?.Address} > Connected", NotificationMessageType.Debug);
|
ApiManager.SendNotificationMessageNewLine($"{(client.Client.RemoteEndPoint as IPEndPoint)?.Address} > Connected", NotificationMessageType.Debug);
|
||||||
|
|
||||||
|
_ = Task.Run(() => HandleRequest(client));
|
||||||
|
}
|
||||||
|
catch (SocketException ex)
|
||||||
|
{
|
||||||
|
ApiManager.SendNotificationMessageNewLine(ex.Message, NotificationMessageType.Log);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
tcpListener.Stop();
|
||||||
|
}, TaskCreationOptions.LongRunning);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void StopListening()
|
||||||
|
{
|
||||||
|
tcpListener?.Stop();
|
||||||
|
IsListening = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void ListenTo<T>(string eventName, EventHandler<MessageRecivedEventArgs<T>> eventHandler)
|
||||||
|
{
|
||||||
|
events.Add(eventName, (typeof(T), eventHandler));
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task HandleRequest(TcpClient client)
|
||||||
|
{
|
||||||
|
ApiManager.SendNotificationMessageNewLine($"{(client.Client.RemoteEndPoint as IPEndPoint)?.Address} > Handling request", NotificationMessageType.Debug);
|
||||||
|
|
||||||
NetworkStream nwStream = client.GetStream();
|
NetworkStream nwStream = client.GetStream();
|
||||||
byte[] buffer = new byte[client.ReceiveBufferSize];
|
byte[] buffer = new byte[client.ReceiveBufferSize];
|
||||||
|
|
||||||
@@ -115,13 +141,13 @@ internal class NetworkClient(FilesManager filesManager)
|
|||||||
{
|
{
|
||||||
ApiManager.SendNotificationMessageNewLine($"{(client.Client.RemoteEndPoint as IPEndPoint)?.Address} > Download request", NotificationMessageType.Debug);
|
ApiManager.SendNotificationMessageNewLine($"{(client.Client.RemoteEndPoint as IPEndPoint)?.Address} > Download request", NotificationMessageType.Debug);
|
||||||
_ = Upload(client, dataReceived[8..]);
|
_ = Upload(client, dataReceived[8..]);
|
||||||
continue;
|
return;
|
||||||
}
|
}
|
||||||
if (string.IsNullOrWhiteSpace(dataReceived))
|
if (string.IsNullOrWhiteSpace(dataReceived))
|
||||||
{
|
{
|
||||||
ApiManager.SendNotificationMessageNewLine($"{(client.Client.RemoteEndPoint as IPEndPoint)?.Address} > Disconnected", NotificationMessageType.Debug);
|
ApiManager.SendNotificationMessageNewLine($"{(client.Client.RemoteEndPoint as IPEndPoint)?.Address} > Disconnected", NotificationMessageType.Debug);
|
||||||
client.Close();
|
client.Close();
|
||||||
continue;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
ApiManager.SendNotificationMessageNewLine($"{(client.Client.RemoteEndPoint as IPEndPoint)?.Address} > Deserializing data", NotificationMessageType.Debug);
|
ApiManager.SendNotificationMessageNewLine($"{(client.Client.RemoteEndPoint as IPEndPoint)?.Address} > Deserializing data", NotificationMessageType.Debug);
|
||||||
@@ -151,30 +177,7 @@ internal class NetworkClient(FilesManager filesManager)
|
|||||||
ApiManager.SendNotificationMessageNewLine($"{(client.Client.RemoteEndPoint as IPEndPoint)?.Address} > Event not found", NotificationMessageType.Debug);
|
ApiManager.SendNotificationMessageNewLine($"{(client.Client.RemoteEndPoint as IPEndPoint)?.Address} > Event not found", NotificationMessageType.Debug);
|
||||||
}
|
}
|
||||||
|
|
||||||
ApiManager.SendNotificationMessageNewLine($"{(client.Client.RemoteEndPoint as IPEndPoint)?.Address} > Closing connection", NotificationMessageType.Debug);
|
ApiManager.SendNotificationMessageNewLine($"{(client.Client.RemoteEndPoint as IPEndPoint)?.Address} > End of request", NotificationMessageType.Debug);
|
||||||
|
|
||||||
client.Close();
|
|
||||||
|
|
||||||
ApiManager.SendNotificationMessageNewLine($"{(client.Client.RemoteEndPoint as IPEndPoint)?.Address} > Disconnected", NotificationMessageType.Debug);
|
|
||||||
}
|
|
||||||
catch (SocketException ex)
|
|
||||||
{
|
|
||||||
ApiManager.SendNotificationMessageNewLine(ex.Message, NotificationMessageType.Log);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
tcpListener.Stop();
|
|
||||||
}, TaskCreationOptions.LongRunning);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void StopListening()
|
|
||||||
{
|
|
||||||
tcpListener?.Stop();
|
|
||||||
IsListening = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void ListenTo<T>(string eventName, EventHandler<MessageRecivedEventArgs<T>> eventHandler)
|
|
||||||
{
|
|
||||||
events.Add(eventName, (typeof(T), eventHandler));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task Upload(TcpClient client, string hash)
|
private async Task Upload(TcpClient client, string hash)
|
||||||
|
|||||||
Reference in New Issue
Block a user