Skip to content

Commit

Permalink
Adding cancellation tokens to events. Switching params to interfaces …
Browse files Browse the repository at this point in the history
…and specific classes. Updated dependencies.
  • Loading branch information
LiveOrDevTrying committed May 13, 2023
1 parent 73cceba commit 08aa0a3
Show file tree
Hide file tree
Showing 30 changed files with 615 additions and 704 deletions.
9 changes: 6 additions & 3 deletions Tcp.NET.Client/Handlers/TcpClientHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ protected override TcpConnectionClientEventArgs CreateConnectionEventArgs(TcpCon
return new TcpConnectionClientEventArgs
{
Connection = args.Connection,
ConnectionEventType = args.ConnectionEventType
ConnectionEventType = args.ConnectionEventType,
CancellationToken = args.CancellationToken
};
}

Expand All @@ -37,7 +38,8 @@ protected override TcpErrorClientEventArgs CreateErrorEventArgs(TcpErrorEventArg
{
Connection = args.Connection,
Exception = args.Exception,
Message = args.Message
Message = args.Message,
CancellationToken = args.CancellationToken
};
}

Expand All @@ -48,7 +50,8 @@ protected override TcpMessageClientEventArgs CreateMessageEventArgs(TcpMessageEv
Bytes = args.Bytes,
Connection = args.Connection,
Message = args.Message,
MessageEventType = args.MessageEventType
MessageEventType = args.MessageEventType,
CancellationToken = args.CancellationToken
};
}
}
Expand Down
92 changes: 35 additions & 57 deletions Tcp.NET.Client/Handlers/TcpClientHandlerBase.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using PHS.Networking.Enums;
using PHS.Networking.Services;
using PHS.Networking.Handlers;
using PHS.Networking.Utilities;
using System;
using System.IO;
Expand All @@ -16,21 +16,18 @@
namespace Tcp.NET.Client.Handlers
{
public abstract class TcpClientHandlerBase<T, U, V, W, Y> :
CoreNetworkingGeneric<T, U, V, W, Y>,
ICoreNetworkingGeneric<T, U, V, Y>
HandlerClientBase<T, U, V, W, Y>
where T : TcpConnectionEventArgs<Y>
where U : TcpMessageEventArgs<Y>
where V : TcpErrorEventArgs<Y>
where W : ParamsTcpClient
where Y : ConnectionTcp
{
protected Y _connection;

public TcpClientHandlerBase(W parameters) : base(parameters)
{
}

public virtual async Task<bool> ConnectAsync(CancellationToken cancellationToken = default)
public override async Task<bool> ConnectAsync(CancellationToken cancellationToken = default)
{
try
{
Expand All @@ -56,6 +53,7 @@ public virtual async Task<bool> ConnectAsync(CancellationToken cancellationToken
{
Connection = _connection,
ConnectionEventType = ConnectionEventType.Connected,
CancellationToken = cancellationToken
}));

_ = Task.Run(async () => { await ReceiveAsync(cancellationToken).ConfigureAwait(false); }, cancellationToken).ConfigureAwait(false);
Expand All @@ -75,15 +73,16 @@ public virtual async Task<bool> ConnectAsync(CancellationToken cancellationToken
{
Exception = ex,
Connection = _connection,
Message = ex.Message
Message = ex.Message,
CancellationToken = cancellationToken
}));

await DisconnectAsync(cancellationToken);
await DisconnectAsync(cancellationToken).ConfigureAwait(false);
}

return false;
}
public virtual async Task<bool> DisconnectAsync(CancellationToken cancellationToken = default)
public override async Task<bool> DisconnectAsync(CancellationToken cancellationToken = default)
{
try
{
Expand All @@ -93,23 +92,16 @@ public virtual async Task<bool> DisconnectAsync(CancellationToken cancellationTo
{
if (_parameters.UseDisconnectBytes)
{
await SendAsync(_parameters.DisconnectBytes, cancellationToken);
await SendAsync(_parameters.DisconnectBytes, cancellationToken).ConfigureAwait(false);
}

if (_connection != null && _connection.TcpClient != null)
{
_connection.TcpClient.Close();
}

if (_connection != null && _connection.TcpClient != null)
{
_connection.TcpClient.Dispose();
}
_connection?.Dispose();

FireEvent(this, CreateConnectionEventArgs(new TcpConnectionEventArgs<Y>
{
ConnectionEventType = ConnectionEventType.Disconnect,
Connection = _connection
Connection = _connection,
CancellationToken = cancellationToken
}));
}

Expand All @@ -124,14 +116,15 @@ public virtual async Task<bool> DisconnectAsync(CancellationToken cancellationTo
{
Connection = _connection,
Exception = ex,
Message = ex.Message
Message = ex.Message,
CancellationToken = cancellationToken
}));
}

return false;
}

public virtual async Task<bool> SendAsync(string message, CancellationToken cancellationToken = default)
public override async Task<bool> SendAsync(string message, CancellationToken cancellationToken = default)
{
try
{
Expand All @@ -148,7 +141,8 @@ public virtual async Task<bool> SendAsync(string message, CancellationToken canc
MessageEventType = MessageEventType.Sent,
Connection = _connection,
Message = message,
Bytes = bytes
Bytes = bytes,
CancellationToken = cancellationToken
}));

return true;
Expand All @@ -160,15 +154,16 @@ public virtual async Task<bool> SendAsync(string message, CancellationToken canc
{
Connection = _connection,
Exception = ex,
Message = ex.Message
Message = ex.Message,
CancellationToken = cancellationToken
}));

await DisconnectAsync(cancellationToken);
await DisconnectAsync(cancellationToken).ConfigureAwait(false);
}

return false;
}
public virtual async Task<bool> SendAsync(byte[] message, CancellationToken cancellationToken = default)
public override async Task<bool> SendAsync(byte[] message, CancellationToken cancellationToken = default)
{
try
{
Expand All @@ -185,7 +180,8 @@ public virtual async Task<bool> SendAsync(byte[] message, CancellationToken canc
MessageEventType = MessageEventType.Sent,
Connection = _connection,
Message = null,
Bytes = bytes
Bytes = bytes,
CancellationToken = cancellationToken
}));

return true;
Expand All @@ -197,10 +193,11 @@ public virtual async Task<bool> SendAsync(byte[] message, CancellationToken canc
{
Connection = _connection,
Exception = ex,
Message = ex.Message
Message = ex.Message,
CancellationToken = cancellationToken
}));

await DisconnectAsync(cancellationToken);
await DisconnectAsync(cancellationToken).ConfigureAwait(false);
}

return false;
Expand All @@ -225,7 +222,7 @@ protected virtual async Task ReceiveAsync(CancellationToken cancellationToken)

var buffer = new ArraySegment<byte>(new byte[_connection.TcpClient.Available]);
var result = await _connection.TcpClient.Client.ReceiveAsync(buffer, SocketFlags.None, cancellationToken).ConfigureAwait(false);
await ms.WriteAsync(buffer.Array, buffer.Offset, result, cancellationToken).ConfigureAwait(false);
await ms.WriteAsync(buffer.Array.AsMemory(buffer.Offset, result), cancellationToken).ConfigureAwait(false);

endOfMessage = Statics.ByteArrayContainsSequence(ms.ToArray(), _parameters.EndOfLineBytes);
}
Expand All @@ -239,20 +236,13 @@ protected virtual async Task ReceiveAsync(CancellationToken cancellationToken)
{
if (_parameters.UseDisconnectBytes && Statics.ByteArrayEquals(parts[i], _parameters.DisconnectBytes))
{
if (_connection != null && _connection.TcpClient != null)
{
_connection.TcpClient.Close();
}

if (_connection != null && _connection.TcpClient != null)
{
_connection.TcpClient.Dispose();
}
_connection?.Dispose();

FireEvent(this, CreateConnectionEventArgs(new TcpConnectionEventArgs<Y>
{
ConnectionEventType = ConnectionEventType.Disconnect,
Connection = _connection
Connection = _connection,
CancellationToken = cancellationToken
}));

_connection = null;
Expand All @@ -269,9 +259,9 @@ protected virtual async Task ReceiveAsync(CancellationToken cancellationToken)
MessageEventType = MessageEventType.Receive,
Connection = _connection,
Message = !_parameters.OnlyEmitBytes ? Encoding.UTF8.GetString(parts[i]) : null,
Bytes = parts[i]
Bytes = parts[i],
CancellationToken = cancellationToken
}));

}
}
}
Expand All @@ -284,11 +274,12 @@ protected virtual async Task ReceiveAsync(CancellationToken cancellationToken)
{
Connection = _connection,
Exception = ex,
Message = ex.Message
Message = ex.Message,
CancellationToken = cancellationToken
}));
}

await DisconnectAsync(cancellationToken);
await DisconnectAsync(cancellationToken).ConfigureAwait(false);
}

protected virtual async Task CreateNonSSLConnectionAsync(CancellationToken cancellationToken)
Expand Down Expand Up @@ -342,18 +333,5 @@ protected virtual async Task CreateSSLConnectionAsync(CancellationToken cancella
protected abstract T CreateConnectionEventArgs(TcpConnectionEventArgs<Y> args);
protected abstract U CreateMessageEventArgs(TcpMessageEventArgs<Y> args);
protected abstract V CreateErrorEventArgs(TcpErrorEventArgs<Y> args);

public override void Dispose()
{
DisconnectAsync().Wait();
}

public Y Connection
{
get
{
return _connection;
}
}
}
}
1 change: 1 addition & 0 deletions Tcp.NET.Client/ITcpNETClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ public interface ITcpNETClient :
TcpErrorClientEventArgs,
ConnectionTcp>
{
bool IsRunning { get; }
}
}
19 changes: 19 additions & 0 deletions Tcp.NET.Client/Models/IParamsTcpClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using PHS.Networking.Models;

namespace Tcp.NET.Client.Models
{
public interface IParamsTcpClient : IParams
{
byte[] DisconnectBytes { get; }
byte[] EndOfLineBytes { get; }
string Host { get; }
bool IsSSL { get; }
bool OnlyEmitBytes { get; }
byte[] PingBytes { get; }
byte[] PongBytes { get; }
int Port { get; }
byte[] Token { get; }
bool UseDisconnectBytes { get; }
bool UsePingPong { get; }
}
}
59 changes: 2 additions & 57 deletions Tcp.NET.Client/Models/ParamsTcpClient.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
using PHS.Networking.Models;
using PHS.Networking.Utilities;
using PHS.Networking.Utilities;
using System;
using System.Linq;
using System.Text;

namespace Tcp.NET.Client.Models
{
public class ParamsTcpClient : Params
public class ParamsTcpClient : IParamsTcpClient
{
public string Host { get; protected set; }
public int Port { get; protected set; }
Expand Down Expand Up @@ -63,59 +61,6 @@ public ParamsTcpClient(string host, int port, string endOfLineCharacters, string
Token = Encoding.UTF8.GetBytes(token);
}

if (UseDisconnectBytes && (DisconnectBytes == null || Statics.ByteArrayEquals(DisconnectBytes, Array.Empty<byte>())))
{
DisconnectBytes = new byte[] { 3 };
}
}
public ParamsTcpClient(string host, int port, byte[] endOfLineBytes, string token = null, bool isSSL = true, bool onlyEmitBytes = true, bool usePingPong = true, byte[] pingBytes = null, byte[] pongBytes = null, bool useDisconnectBytes = true, byte[] disconnectBytes = null)
{
if (string.IsNullOrWhiteSpace(host))
{
throw new ArgumentException("Host is not valid");
}

if (port <= 0)
{
throw new ArgumentException("Port is not valid");
}

if (endOfLineBytes == null || endOfLineBytes.Length <= 0 || Statics.ByteArrayEquals(endOfLineBytes, Array.Empty<byte>()))
{
throw new ArgumentException("End of Line Characters are not valid");
}

if (token != null && string.IsNullOrWhiteSpace(token))
{
throw new ArgumentException("Token is not valid");
}

if (usePingPong && (pingBytes == null || pingBytes.Length <= 0 || Statics.ByteArrayEquals(pingBytes, Array.Empty<byte>())))
{
pingBytes = Encoding.UTF8.GetBytes("ping");
}

if (usePingPong && (pongBytes == null || pongBytes.Length <= 0 || Statics.ByteArrayEquals(pongBytes, Array.Empty<byte>())))
{
pongBytes = Encoding.UTF8.GetBytes("pong");
}

Host = host;
Port = port;
EndOfLineBytes = endOfLineBytes;
UsePingPong = usePingPong;
PingBytes = pingBytes;
PongBytes = pongBytes;
IsSSL = isSSL;
OnlyEmitBytes = onlyEmitBytes;
UseDisconnectBytes = useDisconnectBytes;
DisconnectBytes = disconnectBytes;

if (!string.IsNullOrWhiteSpace(token))
{
Token = Encoding.UTF8.GetBytes(token);
}

if (UseDisconnectBytes && (DisconnectBytes == null || Statics.ByteArrayEquals(DisconnectBytes, Array.Empty<byte>())))
{
DisconnectBytes = new byte[] { 3 };
Expand Down

0 comments on commit 08aa0a3

Please sign in to comment.