Skip to content
This repository has been archived by the owner on Aug 15, 2023. It is now read-only.

Commit

Permalink
Merge pull request #38 from Job79/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
job79 authored Jul 4, 2020
2 parents 0172c48 + 80c249a commit ab24d0c
Show file tree
Hide file tree
Showing 43 changed files with 431 additions and 418 deletions.
6 changes: 4 additions & 2 deletions EasyTcp3/EasyTcp3.Actions/ActionUtils/EasyTcpActionFilter.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System;

namespace EasyTcp3.Actions.ActionUtils
{
/// <summary>
/// Filter attribute for EasyTcpActions
/// </summary>
public interface IEasyTcpActionFilter
public abstract class EasyTcpActionFilter : Attribute
{
/// <summary>
/// Determines whether client has access to an action
Expand All @@ -12,6 +14,6 @@ public interface IEasyTcpActionFilter
/// <param name="sender">EasyTcpActionServer/EasyTcpActionClient as sender</param>
/// <param name="message"></param>
/// <returns></returns>
public bool HasAccess(object sender, ActionMessage message);
public abstract bool HasAccess(object sender, ActionMessage message);
}
}
4 changes: 2 additions & 2 deletions EasyTcp3/EasyTcp3.Actions/ActionsCore/Action.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class Action
/// <summary>
/// List with EasyTcpAction filters
/// </summary>
public List<IEasyTcpActionFilter> Filters;
public List<EasyTcpActionFilter> Filters;

/// <summary>
/// Instance of EasyTcpActionDelegate*
Expand All @@ -50,7 +50,7 @@ public Action(MethodInfo method, Dictionary<Type, object> classInstances)
if (classInstance == null) EasyTcpAction = Delegate.CreateDelegate(methodType, method);
EasyTcpAction = Delegate.CreateDelegate(methodType, classInstance, method);

var filters = method.GetCustomAttributes().OfType<IEasyTcpActionFilter>().ToList();
var filters = method.GetCustomAttributes().OfType<EasyTcpActionFilter>().ToList();
if (filters.Any()) Filters = filters;
}

Expand Down
2 changes: 1 addition & 1 deletion EasyTcp3/EasyTcp3.Actions/EasyTcp3.Actions.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<TargetFramework>netstandard2.0</TargetFramework>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageVersion>2.4.0</PackageVersion>
<Title>EasyTcp.Actions</Title>
Expand Down
2 changes: 1 addition & 1 deletion EasyTcp3/EasyTcp3.Actions/EasyTcpActionClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public EasyTcpActionClient(IEasyTcpProtocol protocol = null, Assembly assembly =
string nameSpace = null) : base(protocol)
{
AddActions(assembly ?? Assembly.GetCallingAssembly(), nameSpace);
OnDataReceive += async (sender, message) =>
OnDataReceiveAsync += async (sender, message) =>
{
try { await Actions.ExecuteAction(Interceptor, FireOnUnknownAction, sender, message); }
catch (Exception ex) { FireOnError(ex); }
Expand Down
2 changes: 1 addition & 1 deletion EasyTcp3/EasyTcp3.Actions/EasyTcpActionServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public EasyTcpActionServer(IEasyTcpProtocol protocol = null, Assembly assembly =
: base(protocol)
{
AddActions(assembly ?? Assembly.GetCallingAssembly(), nameSpace);
OnDataReceive += async (sender, message) =>
OnDataReceiveAsync += async (sender, message) =>
{
try { await Actions.ExecuteAction(Interceptor, FireOnUnknownAction, sender, message); }
catch (Exception ex) { FireOnError(ex); }
Expand Down
4 changes: 2 additions & 2 deletions EasyTcp3/EasyTcp3.Encryption/EasyTcp3.Encryption.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<TargetFramework>netstandard2.0</TargetFramework>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageVersion>2.2.1</PackageVersion>
<Title>EasyTcp.Encryption</Title>
Expand All @@ -17,7 +17,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="EasyEncrypt" Version="2.2.0.1" />
<PackageReference Include="EasyEncrypt" Version="2.2.1" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Threading.Tasks;
using EasyEncrypt2;
using EasyTcp3.Protocols;
using EasyTcp3.Protocols.Tcp;
Expand Down Expand Up @@ -84,19 +85,19 @@ public override byte[] CreateMessage(params byte[][] data)
/// <param name="data"></param>
/// <param name="receivedBytes">ignored</param>
/// <param name="client"></param>
public override void DataReceive(byte[] data, int receivedBytes, EasyTcpClient client)
public override async Task DataReceive(byte[] data, int receivedBytes, EasyTcpClient client)
{
if (!(ReceivingLength = !ReceivingLength))
{
BufferSize = BitConverter.ToUInt16(client.Buffer, 0);
BufferSize = BitConverter.ToUInt16(data, 0);
if (BufferSize == 0) client.Dispose();
}
else
{
BufferSize = 2;
try
{
client.DataReceiveHandler(new Message(client.Buffer, client).Decrypt(Encrypter));
await client.DataReceiveHandler(new Message(data, client).Decrypt(Encrypter));
}
catch { OnDecryptionError(client); }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Net.Security;
using System.Net.Sockets;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;
using EasyTcp3.Protocols;
using EasyTcp3.Server;

Expand All @@ -13,6 +14,8 @@ namespace EasyTcp3.Encryption.Protocols.Tcp.Ssl
/// </summary>
public abstract class DefaultSslProtocol : IEasyTcpProtocol
{
public byte[] ReceiveBuffer;

/// <summary>
/// Instance of SslStream,
/// null for base protocol of server
Expand Down Expand Up @@ -80,8 +83,15 @@ public virtual Socket GetSocket(AddressFamily addressFamily) =>
/// <param name="server"></param>
public virtual void StartAcceptingClients(EasyTcpServer server)
{
server.BaseSocket.Listen(5000);
server.BaseSocket.BeginAccept(OnConnectCallback, server);
if (server.AcceptArgs == null)
{
server.BaseSocket.Listen(50000);
server.AcceptArgs = new SocketAsyncEventArgs {UserToken = server};
server.AcceptArgs.Completed += (_, ar) => OnConnectCallback(ar);
}

server.AcceptArgs.AcceptSocket = null;
if (!server.BaseSocket.AcceptAsync(server.AcceptArgs)) OnConnectCallback(server.AcceptArgs);
}

/// <summary>
Expand All @@ -92,8 +102,10 @@ public virtual void EnsureDataReceiverIsRunning(EasyTcpClient client)
{
if (IsListening) return;
IsListening = true;
((DefaultSslProtocol) client.Protocol).SslStream.BeginRead(client.Buffer = new byte[BufferSize], 0,
client.Buffer.Length, OnReceiveCallback, client);

var protocol = (DefaultSslProtocol)client.Protocol;
((DefaultSslProtocol) client.Protocol).SslStream.BeginRead(protocol.ReceiveBuffer = new byte[BufferSize], 0,
protocol.ReceiveBuffer.Length, OnReceiveCallback, client);
}

/// <summary>
Expand All @@ -114,7 +126,7 @@ public virtual void SendMessage(EasyTcpClient client, byte[] message)
if (client?.BaseSocket == null || !client.BaseSocket.Connected)
throw new Exception("Could not send data: Client not connected or null");

client.FireOnDataSend(new Message(message, client));
client.FireOnDataSend(message, client);
SslStream.BeginWrite(message, 0, message.Length, ar =>
{
var stream = ar.AsyncState as SslStream;
Expand Down Expand Up @@ -192,7 +204,7 @@ public virtual bool OnConnectServer(EasyTcpClient client)
/// <param name="data">received data, has size of clients buffer</param>
/// <param name="receivedBytes">amount of received bytes</param>
/// <param name="client"></param>
public abstract void DataReceive(byte[] data, int receivedBytes, EasyTcpClient client);
public abstract Task DataReceive(byte[] data, int receivedBytes, EasyTcpClient client);

/*
* Internal methods
Expand Down Expand Up @@ -231,29 +243,30 @@ protected virtual void HandleDisconnect(EasyTcpClient client)
/// Fired when new client connects
/// </summary>
/// <param name="ar"></param>
protected virtual void OnConnectCallback(IAsyncResult ar)
protected virtual void OnConnectCallback(SocketAsyncEventArgs ar)
{
var server = ar.AsyncState as EasyTcpServer;
var server = ar.UserToken as EasyTcpServer;
if (server?.BaseSocket == null || !server.IsRunning) return;

try
{
var client = new EasyTcpClient(server.BaseSocket.EndAccept(ar),
var client = new EasyTcpClient(ar.AcceptSocket,
(IEasyTcpProtocol) server.Protocol.Clone())
{
Serialize = server.Serialize,
Deserialize = server.Deserialize
};
client.OnDataReceive += (_, message) => server.FireOnDataReceive(message);
client.OnDataReceiveAsync += async (_, message) => await server.FireOnDataReceive(message);
client.OnDataSend += (_, message) => server.FireOnDataSend(message);
client.OnDisconnect += (_, c) => server.FireOnDisconnect(c);
client.OnError += (_, exception) => server.FireOnError(exception);
server.BaseSocket.BeginAccept(OnConnectCallback, server);

StartAcceptingClients(server);

if (!client.Protocol.OnConnectServer(client)) return;
server.FireOnConnect(client);
if (client.BaseSocket != null) //Check if user aborted OnConnect with Client.Dispose()
lock (server.UnsafeConnectedClients)
if (client.BaseSocket != null) // Check if user aborted OnConnect with Client.Dispose()
lock (server.UnsafeConnectedClients)
server.UnsafeConnectedClients.Add(client);
}
catch (Exception ex)
Expand All @@ -267,7 +280,7 @@ protected virtual void OnConnectCallback(IAsyncResult ar)
/// Fired when new data is received
/// </summary>
/// <param name="ar"></param>
protected virtual void OnReceiveCallback(IAsyncResult ar)
protected virtual async void OnReceiveCallback(IAsyncResult ar)
{
var client = ar.AsyncState as EasyTcpClient;
if (client == null) return;
Expand All @@ -278,7 +291,9 @@ protected virtual void OnReceiveCallback(IAsyncResult ar)
int receivedBytes = SslStream.EndRead(ar);
if (receivedBytes != 0)
{
DataReceive(client.Buffer, receivedBytes, client);
var protocol = (DefaultSslProtocol)client.Protocol;
await DataReceive(protocol.ReceiveBuffer, receivedBytes, client);

if (client.BaseSocket == null)
HandleDisconnect(client); // Check if client is disposed by DataReceive
else EnsureDataReceiverIsRunning(client);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;

namespace EasyTcp3.Encryption.Protocols.Tcp.Ssl
{
Expand Down Expand Up @@ -157,7 +158,7 @@ public override object Clone()
/// <param name="data">received data, has size of clients buffer</param>
/// <param name="receivedBytes">amount of received bytes</param>
/// <param name="client"></param>
public override void DataReceive(byte[] data, int receivedBytes, EasyTcpClient client)
public override async Task DataReceive(byte[] data, int receivedBytes, EasyTcpClient client)
{
byte receivedByte = data[0]; // Size of buffer is always 1
ReceivedBytes.Add(receivedByte);
Expand All @@ -173,7 +174,7 @@ public override void DataReceive(byte[] data, int receivedBytes, EasyTcpClient c
byte[] receivedData = AutoRemoveDelimiter
? ReceivedBytes.Take(receivedBytesLength).ToArray() // Remove delimiter from message
: ReceivedBytes.ToArray();
client.DataReceiveHandler(new Message(receivedData, client));
await client.DataReceiveHandler(new Message(receivedData, client));
ReceivedBytes.Clear();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;

namespace EasyTcp3.Encryption.Protocols.Tcp.Ssl
{
Expand Down Expand Up @@ -82,11 +83,11 @@ public override object Clone()
/// <param name="data">received data, has size of clients buffer</param>
/// <param name="receivedBytes">amount of received bytes</param>
/// <param name="client"></param>
public override void DataReceive(byte[] data, int receivedBytes, EasyTcpClient client)
public override async Task DataReceive(byte[] data, int receivedBytes, EasyTcpClient client)
{
byte[] receivedData = new byte[receivedBytes];
Buffer.BlockCopy(data,0,receivedData,0,receivedBytes);
client.DataReceiveHandler(new Message(receivedData, client));
await client.DataReceiveHandler(new Message(receivedData, client));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;

namespace EasyTcp3.Encryption.Protocols.Tcp.Ssl
{
Expand Down Expand Up @@ -83,17 +84,17 @@ public override object Clone()
/// <param name="data"></param>
/// <param name="receivedBytes">ignored</param>
/// <param name="client"></param>
public override void DataReceive(byte[] data, int receivedBytes, EasyTcpClient client)
public override async Task DataReceive(byte[] data, int receivedBytes, EasyTcpClient client)
{
if (!(ReceivingLength = !ReceivingLength))
{
BufferSize = BitConverter.ToUInt16(client.Buffer, 0);
BufferSize = BitConverter.ToUInt16(data, 0);
if (BufferSize == 0) client.Dispose();
}
else
{
BufferSize = 2;
client.DataReceiveHandler(new Message(client.Buffer, client));
await client.DataReceiveHandler(new Message(data, client));
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions EasyTcp3/EasyTcp3.Examples/Actions/AuthorizationExample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public static void Connect()
* example filter attribute for authorization
*/
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
public class EasyTcpAuthorization : Attribute, IEasyTcpActionFilter
public class EasyTcpAuthorization : EasyTcpActionFilter
{
private readonly UserRole[] _allowedRoles;

Expand All @@ -83,7 +83,7 @@ public class EasyTcpAuthorization : Attribute, IEasyTcpActionFilter
/// <param name="sender"></param>
/// <param name="message"></param>
/// <returns></returns>
public bool HasAccess(object sender, ActionMessage message)
public override bool HasAccess(object sender, ActionMessage message)
{
var hasRole = message.Client.Session.TryGetValue("UserRole", out object userRole);
if (!hasRole) return false;
Expand Down
4 changes: 2 additions & 2 deletions EasyTcp3/EasyTcp3.Examples/LargeArray/LargeArrayExample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static void Connect()
client.SendLargeArray(new byte[1000000]);

// Send large array without length prefix
client.SendLargeArray(new byte[10000], false);
client.SendLargeArray(new byte[10000], sendLengthPrefix: false);
Console.ReadLine();
}

Expand All @@ -50,7 +50,7 @@ public async Task LargeArrayReceive(Message message)
Console.WriteLine($"Received {largeArray.Length} bytes");

// Receive array with known length
var largeArray2 = await message.ReceiveLargeArrayAsync(10000);
var largeArray2 = await message.ReceiveLargeArrayAsync(count: 10000);
Console.WriteLine($"Received {largeArray2.Length} bytes");
}
}
Expand Down
2 changes: 1 addition & 1 deletion EasyTcp3/EasyTcp3.Examples/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Program
static void Main()
{
// Run test methods here
MultiThreadedActionSpeedTest.Run();
ThroughputTest.Run();
}
}
}
Loading

0 comments on commit ab24d0c

Please sign in to comment.