Skip to content

Commit

Permalink
fix(lib): network error
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaojiuwo1993 committed Nov 3, 2023
1 parent b255ac4 commit 8887cf1
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 20 deletions.
4 changes: 4 additions & 0 deletions src/Libraries/Core/src/Network/Http/Server/HttpConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ public HttpConnection(HttpListenerContext context, IConnectionManager manager)
public void OnReceived(IHttpRequest request)
{
OnReceive(request);
if (Context.Request.Headers["Connection"] == "close")
{
Context.Response.Close();
}
}

public void Send(string response)
Expand Down
24 changes: 11 additions & 13 deletions src/Libraries/Core/src/Network/Http/Server/HttpConnectionManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Concurrent;
using System.Net;
using System.Threading.Tasks;
using UniSpy.Server.Core.Abstraction.Interface;
Expand All @@ -23,19 +24,16 @@ public void Start()
{
while (true)
{
try
{
var context = Listener.GetContext();
var raw = context.Request;
var request = new HttpRequest(raw);
var conn = new HttpConnection(context, this);
OnInitialization(conn);
conn.OnReceived(request);
}
catch (Exception ex)
{
LogWriter.LogError(ex);
}
var context = Listener.GetContext();
Task.Run(() =>
{

var conn = new HttpConnection(context, this);
OnInitialization(conn);
var raw = context.Request;
var request = new HttpRequest(raw);
conn.OnReceived(request);
});
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/Libraries/Core/src/Network/Tcp/Server/TcpConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public TcpConnection(TcpClient client, IConnectionManager manager)
}
public void Disconnect()
{
OnDisconnected();
Client.Client.Disconnect(true);
OnDisconnected();
}

public void Send(string response) => Send(UniSpyEncoding.GetBytes(response));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,11 @@ public class TcpConnectionManager : IConnectionManager, IDisposable
{
public event OnConnectingEventHandler OnInitialization;
public TcpListener Listener;
public ConcurrentDictionary<IPEndPoint, IConnection> ConnectionPool = new();

public TcpConnectionManager(IPEndPoint endPoint)
{
Listener = new TcpListener(endPoint);
}


public void Start()
{
Listener.Start();
Expand Down
1 change: 1 addition & 0 deletions src/Libraries/Core/src/Network/Udp/Server/UdpConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class UdpConnection : IUdpConnection
public NetworkConnectionType ConnectionType { get; } = NetworkConnectionType.Udp;

public event OnReceivedEventHandler OnReceive;

public UdpConnection(IPEndPoint endPoint, IConnectionManager manager)
{
RemoteIPEndPoint = endPoint;
Expand Down
17 changes: 14 additions & 3 deletions src/Libraries/Core/src/Network/Udp/Server/UdpConnectionManager.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections.Concurrent;
using System;
using System.Net;
using System.Net.Sockets;
Expand All @@ -11,6 +12,7 @@ public class UdpConnectionManager : IConnectionManager, IDisposable
{
public event OnConnectingEventHandler OnInitialization;
public UdpClient Listener { get; private set; }
public ConcurrentDictionary<IPEndPoint, UdpConnection> Pool = new();
public UdpConnectionManager(IPEndPoint endPoint)
{
Listener = new UdpClient(endPoint);
Expand All @@ -24,9 +26,18 @@ public void Start()
{
var clientEndPoint = new IPEndPoint(IPAddress.Any, 0);
var data = Listener.Receive(ref clientEndPoint);
var conn = new UdpConnection(clientEndPoint, this);
// OnInitialization?.Invoke(conn);
Task.Run(() => conn.OnReceived(data));

Task.Run(() =>
{
UdpConnection conn;
if (!Pool.TryGetValue(clientEndPoint, out conn))
{
conn = new UdpConnection(clientEndPoint, this);
Pool.TryAdd(clientEndPoint, conn);
}
OnInitialization(conn);
conn.OnReceived(data);
});
}
});
}
Expand Down
20 changes: 20 additions & 0 deletions src/Servers/WebServer/test/Auth/RSATest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Security.Cryptography;
using Xunit;

namespace UniSpy.Server.WebServer.Test.Auth
{
public class RSATest
{
[Fact]
public void KeyGen()
{
using (var rsa = new RSACryptoServiceProvider(1024))
{
var privateKey = rsa.ExportParameters(true);

// The public key is used for verifying the signature
var publicKey = rsa.ExportParameters(false);
}
}
}
}

0 comments on commit 8887cf1

Please sign in to comment.