Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into NET6.0
Browse files Browse the repository at this point in the history
  • Loading branch information
LiveOrDevTrying committed Jul 4, 2023
2 parents 9e21b9a + 753b6db commit e96784d
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 25 deletions.
59 changes: 48 additions & 11 deletions Tcp.NET.Client/Handlers/TcpClientHandlerBase.cs
Expand Up @@ -143,7 +143,16 @@ public override async Task<bool> SendAsync(string message, CancellationToken can
!string.IsNullOrWhiteSpace(message))
{
var bytes = Statics.ByteArrayAppend(Encoding.UTF8.GetBytes($"{message}"), _parameters.EndOfLineBytes);
await _connection.TcpClient.Client.SendAsync(new ArraySegment<byte>(bytes), SocketFlags.None, cancellationToken).ConfigureAwait(false);

if (_connection.SslStream != null)
{
await _connection.SslStream.WriteAsync(bytes, cancellationToken).ConfigureAwait(false);
await _connection.SslStream.FlushAsync(cancellationToken).ConfigureAwait(false);
}
else
{
await _connection.TcpClient.Client.SendAsync(new ArraySegment<byte>(bytes), SocketFlags.None, cancellationToken).ConfigureAwait(false);
}

FireEvent(this, CreateMessageEventArgs(new TcpMessageEventArgs<Y>
{
Expand Down Expand Up @@ -184,7 +193,16 @@ public override async Task<bool> SendAsync(byte[] message, CancellationToken can
message.Where(x => x != 0).Any())
{
var bytes = Statics.ByteArrayAppend(message, _parameters.EndOfLineBytes);
await _connection.TcpClient.Client.SendAsync(new ArraySegment<byte>(bytes), SocketFlags.None, cancellationToken).ConfigureAwait(false);

if (_connection.SslStream != null)
{
await _connection.SslStream.WriteAsync(bytes, cancellationToken).ConfigureAwait(false);
await _connection.SslStream.FlushAsync(cancellationToken).ConfigureAwait(false);
}
else
{
await _connection.TcpClient.Client.SendAsync(new ArraySegment<byte>(bytes), SocketFlags.None, cancellationToken).ConfigureAwait(false);
}

FireEvent(this, CreateMessageEventArgs(new TcpMessageEventArgs<Y>
{
Expand Down Expand Up @@ -224,17 +242,34 @@ protected virtual async Task ReceiveAsync(CancellationToken cancellationToken)
{
try
{
if (_connection.TcpClient.Available > 0)
if (_connection.SslStream != null)
{
var buffer = new ArraySegment<byte>(new byte[_connection.TcpClient.Available]);
var result = await _connection.TcpClient.Client.ReceiveAsync(buffer, SocketFlags.None, cancellationToken).ConfigureAwait(false);
await _connection.MemoryStream.WriteAsync(buffer.Array.AsMemory(buffer.Offset, result), cancellationToken).ConfigureAwait(false);

_connection.EndOfLine = Statics.ByteArrayContainsSequence(_connection.MemoryStream.ToArray(), _parameters.EndOfLineBytes) > -1;
var bytesRead = 0;
if ((bytesRead = _connection.SslStream.Read(_connection.ReadBuffer, 0, _connection.ReadBuffer.Length)) > 0)
{
await _connection.MemoryStream.WriteAsync(_connection.ReadBuffer.AsMemory(0, bytesRead), cancellationToken).ConfigureAwait(false);
_connection.EndOfLine = Statics.ByteArrayContainsSequence(_connection.MemoryStream.ToArray(), _parameters.EndOfLineBytes) > -1;
_connection.ReadBuffer = new byte[4096];
}
else
{
await Task.Delay(1, cancellationToken).ConfigureAwait(false);
}
}
else
{
await Task.Delay(1, cancellationToken).ConfigureAwait(false);
if (_connection.TcpClient.Available > 0)
{
var buffer = new ArraySegment<byte>(new byte[_connection.TcpClient.Available]);
var result = await _connection.TcpClient.Client.ReceiveAsync(buffer, SocketFlags.None, cancellationToken).ConfigureAwait(false);
await _connection.MemoryStream.WriteAsync(buffer.Array.AsMemory(buffer.Offset, result), cancellationToken).ConfigureAwait(false);

_connection.EndOfLine = Statics.ByteArrayContainsSequence(_connection.MemoryStream.ToArray(), _parameters.EndOfLineBytes) > -1;
}
else
{
await Task.Delay(1, cancellationToken).ConfigureAwait(false);
}
}
}
catch { }
Expand Down Expand Up @@ -357,8 +392,10 @@ protected virtual async Task CreateSSLConnectionAsync(CancellationToken cancella
{
_connection = CreateConnection(new ConnectionTcp
{
TcpClient = client
});
TcpClient = client,
SslStream = sslStream,
ReadBuffer = new byte[4096]
});
}
else
{
Expand Down
20 changes: 20 additions & 0 deletions Tcp.NET.Core/Models/ConnectionTcp.cs
@@ -1,5 +1,6 @@
using PHS.Networking.Models;
using System.IO;
using System.Net.Security;
using System.Net.Sockets;

namespace Tcp.NET.Core.Models
Expand All @@ -10,6 +11,8 @@ public class ConnectionTcp : IConnection
public MemoryStream MemoryStream { get; set; }
public bool Disposed { get; set; }
public bool EndOfLine { get; set; }
public SslStream SslStream { get; set; }
public byte[] ReadBuffer { get; set; }

public ConnectionTcp()
{
Expand All @@ -18,6 +21,23 @@ public ConnectionTcp()

public virtual void Dispose()
{
if (SslStream != null)
{
try
{
SslStream.Close();
}
catch { }

try
{
SslStream.Dispose();
}
catch { }

SslStream = null;
}

try
{
TcpClient?.GetStream().Close();
Expand Down
4 changes: 3 additions & 1 deletion Tcp.NET.Server/Handlers/TcpHandlerServer.cs
Expand Up @@ -26,7 +26,9 @@ protected override ConnectionTcpServer CreateConnection(ConnectionTcpServer conn
return new ConnectionTcpServer
{
TcpClient = connection.TcpClient,
ConnectionId = Guid.NewGuid().ToString()
ConnectionId = Guid.NewGuid().ToString(),
SslStream = connection.SslStream,
ReadBuffer = connection.ReadBuffer
};
}
protected override TcpConnectionServerEventArgs CreateConnectionEventArgs(ConnectionEventArgs<ConnectionTcpServer> args)
Expand Down
5 changes: 3 additions & 2 deletions Tcp.NET.Server/Handlers/TcpHandlerServerAuth.cs
Expand Up @@ -3,7 +3,6 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Tcp.NET.Core.Models;
using Tcp.NET.Server.Events.Args;
using Tcp.NET.Server.Models;

Expand Down Expand Up @@ -43,7 +42,9 @@ protected override IdentityTcpServer<T> CreateConnection(ConnectionTcpServer con
return new IdentityTcpServer<T>
{
TcpClient = connection.TcpClient,
ConnectionId = Guid.NewGuid().ToString()
ConnectionId = Guid.NewGuid().ToString(),
SslStream = connection.SslStream,
ReadBuffer = connection.ReadBuffer
};
}
protected override TcpConnectionServerAuthEventArgs<T> CreateConnectionEventArgs(ConnectionEventArgs<IdentityTcpServer<T>> args)
Expand Down
58 changes: 47 additions & 11 deletions Tcp.NET.Server/Handlers/TcpHandlerServerBase.cs
Expand Up @@ -12,7 +12,6 @@
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Tcp.NET.Core.Models;
using Tcp.NET.Server.Events.Args;
using Tcp.NET.Server.Models;
using System.Linq;
Expand Down Expand Up @@ -181,7 +180,9 @@ protected virtual async Task ListenForConnectionsSSLAsync(CancellationToken canc
{
var connection = CreateConnection(new ConnectionTcpServer
{
TcpClient = client
TcpClient = client,
SslStream = sslStream,
ReadBuffer = new byte[4096]
});

if (_parameters.PingIntervalSec > 0)
Expand Down Expand Up @@ -245,17 +246,34 @@ protected virtual async Task ReceiveAsync(Z connection, CancellationToken cancel
{
try
{
if (connection.TcpClient.Available > 0)
if (connection.SslStream != null)
{
var buffer = new ArraySegment<byte>(new byte[connection.TcpClient.Available]);
var result = await connection.TcpClient.Client.ReceiveAsync(buffer, SocketFlags.None, cancellationToken).ConfigureAwait(false);
await connection.MemoryStream.WriteAsync(buffer.Array.AsMemory(buffer.Offset, result), cancellationToken).ConfigureAwait(false);

connection.EndOfLine = Statics.ByteArrayContainsSequence(connection.MemoryStream.ToArray(), _parameters.EndOfLineBytes) > -1;
var bytesRead = 0;
if ((bytesRead = connection.SslStream.Read(connection.ReadBuffer, 0, connection.ReadBuffer.Length)) > 0)
{
await connection.MemoryStream.WriteAsync(connection.ReadBuffer.AsMemory(0, bytesRead), cancellationToken).ConfigureAwait(false);
connection.EndOfLine = Statics.ByteArrayContainsSequence(connection.MemoryStream.ToArray(), _parameters.EndOfLineBytes) > -1;
connection.ReadBuffer = new byte[4096];
}
else
{
await Task.Delay(1, cancellationToken).ConfigureAwait(false);
}
}
else
{
await Task.Delay(1, cancellationToken).ConfigureAwait(false);
if (connection.TcpClient.Available > 0)
{
var buffer = new ArraySegment<byte>(new byte[connection.TcpClient.Available]);
var result = await connection.TcpClient.Client.ReceiveAsync(buffer, SocketFlags.None, cancellationToken).ConfigureAwait(false);
await connection.MemoryStream.WriteAsync(buffer.Array.AsMemory(buffer.Offset, result), cancellationToken).ConfigureAwait(false);

connection.EndOfLine = Statics.ByteArrayContainsSequence(connection.MemoryStream.ToArray(), _parameters.EndOfLineBytes) > -1;
}
else
{
await Task.Delay(1, cancellationToken).ConfigureAwait(false);
}
}
}
catch { }
Expand Down Expand Up @@ -335,7 +353,16 @@ public override async Task<bool> SendAsync(string message, Z connection, Cancell
!string.IsNullOrWhiteSpace(message))
{
var bytes = Statics.ByteArrayAppend(Encoding.UTF8.GetBytes(message), _parameters.EndOfLineBytes);
await connection.TcpClient.Client.SendAsync(new ArraySegment<byte>(bytes), SocketFlags.None, cancellationToken).ConfigureAwait(false);

if (connection.SslStream != null)
{
await connection.SslStream.WriteAsync(bytes, cancellationToken).ConfigureAwait(false);
await connection.SslStream.FlushAsync(cancellationToken).ConfigureAwait(false);
}
else
{
await connection.TcpClient.Client.SendAsync(new ArraySegment<byte>(bytes), SocketFlags.None, cancellationToken).ConfigureAwait(false);
}

FireEvent(this, CreateMessageEventArgs(new TcpMessageServerBaseEventArgs<Z>
{
Expand Down Expand Up @@ -375,7 +402,16 @@ public override async Task<bool> SendAsync(byte[] message, Z connection, Cancell
message.Where(x => x != 0).Any())
{
var bytes = Statics.ByteArrayAppend(message, _parameters.EndOfLineBytes);
await connection.TcpClient.Client.SendAsync(new ArraySegment<byte>(bytes), SocketFlags.None, cancellationToken).ConfigureAwait(false);

if (connection != null)
{
await connection.SslStream.WriteAsync(bytes, cancellationToken).ConfigureAwait(false);
await connection.SslStream.FlushAsync(cancellationToken).ConfigureAwait(false);
}
else
{
await connection.TcpClient.Client.SendAsync(new ArraySegment<byte>(bytes), SocketFlags.None, cancellationToken).ConfigureAwait(false);
}

FireEvent(this, CreateMessageEventArgs(new TcpMessageServerBaseEventArgs<Z>
{
Expand Down

0 comments on commit e96784d

Please sign in to comment.