Skip to content

Commit

Permalink
Merge branch 'master' into NETStandard2.0
Browse files Browse the repository at this point in the history
# Conflicts:
#	Tcp.NET.Client/Handlers/TcpClientHandlerBase.cs
#	Tcp.NET.Server/Handlers/TcpHandlerServerBase.cs
  • Loading branch information
LiveOrDevTrying committed Jul 10, 2023
2 parents e07f9e1 + 6da5517 commit 0ee211a
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 36 deletions.
71 changes: 54 additions & 17 deletions Tcp.NET.Client/Handlers/TcpClientHandlerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public override async Task<bool> ConnectAsync(CancellationToken cancellationToke
}
else
{
await CreateNonSSLConnectionAsync(cancellationToken).ConfigureAwait(false);
await CreateConnectionAsync(cancellationToken).ConfigureAwait(false);
}

if (_connection != null && _connection.TcpClient.Connected && !cancellationToken.IsCancellationRequested)
Expand Down 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).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).ConfigureAwait(false);
}

FireEvent(this, CreateMessageEventArgs(new TcpMessageEventArgs<Y>
{
Expand All @@ -153,9 +162,9 @@ public override async Task<bool> SendAsync(string message, CancellationToken can
Bytes = bytes,
CancellationToken = cancellationToken
}));

return true;
}

return true;
}
catch (Exception ex)
{
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).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).ConfigureAwait(false);
}

FireEvent(this, CreateMessageEventArgs(new TcpMessageEventArgs<Y>
{
Expand All @@ -194,9 +212,9 @@ public override async Task<bool> SendAsync(byte[] message, CancellationToken can
Bytes = bytes,
CancellationToken = cancellationToken
}));

return true;
}

return true;
}
catch (Exception ex)
{
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 byte[_connection.TcpClient.Available];
var result = _connection.TcpClient.Client.Receive(buffer);
await _connection.MemoryStream.WriteAsync(buffer, 0, 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).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 @@ -314,7 +349,7 @@ protected virtual async Task ReceiveAsync(CancellationToken cancellationToken)
await DisconnectAsync(cancellationToken).ConfigureAwait(false);
}

protected virtual async Task CreateNonSSLConnectionAsync(CancellationToken cancellationToken)
protected virtual async Task CreateConnectionAsync(CancellationToken cancellationToken)
{
// Establish the remote endpoint for the socket.
_connection?.Dispose();
Expand Down Expand Up @@ -354,8 +389,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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
66 changes: 51 additions & 15 deletions Tcp.NET.Server/Handlers/TcpHandlerServerBase.cs
Original file line number Diff line number Diff line change
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 @@ -178,7 +177,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 @@ -242,17 +243,34 @@ protected virtual async Task ReceiveAsync(Z connection, CancellationToken cancel
{
try
{
if (connection.TcpClient.Available > 0)
if (connection.SslStream != null)
{
var buffer = new byte[connection.TcpClient.Available];
var result = connection.TcpClient.Client.Receive(buffer, SocketFlags.None);
await connection.MemoryStream.WriteAsync(buffer, 0, buffer.Length, 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).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 @@ -332,7 +350,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);
connection.TcpClient.Client.Send(bytes, 0, bytes.Length, SocketFlags.None, out var error);

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).ConfigureAwait(false);
}

FireEvent(this, CreateMessageEventArgs(new TcpMessageServerBaseEventArgs<Z>
{
Expand All @@ -342,9 +369,9 @@ public override async Task<bool> SendAsync(string message, Z connection, Cancell
Bytes = bytes,
CancellationToken = cancellationToken
}));

return true;
}

return true;
}
catch (Exception ex)
{
Expand Down Expand Up @@ -372,7 +399,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);
connection.TcpClient.Client.Send(bytes, 0, bytes.Length, SocketFlags.None, out var error);

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).ConfigureAwait(false);
}

FireEvent(this, CreateMessageEventArgs(new TcpMessageServerBaseEventArgs<Z>
{
Expand All @@ -382,9 +418,9 @@ public override async Task<bool> SendAsync(byte[] message, Z connection, Cancell
Bytes = bytes,
CancellationToken = cancellationToken
}));

return true;
}

return true;
}
catch (Exception ex)
{
Expand Down
2 changes: 1 addition & 1 deletion TestApps/Tcp.NET.TestApps.Client/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ private static void OnMessageEvent(object sender, TcpMessageClientEventArgs args
case MessageEventType.Sent:
break;
case MessageEventType.Receive:
//Console.WriteLine(args.Message + " : " + +_clients.Values.Where(x => x.IsRunning).Count());
Console.WriteLine(args.Message + " : " + +_clients.Values.Where(x => x.IsRunning).Count());
break;
default:
break;
Expand Down

0 comments on commit 0ee211a

Please sign in to comment.