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 #35 from Job79/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
job79 authored Jun 30, 2020
2 parents 4d9b505 + 4fdcd22 commit 25df1f1
Show file tree
Hide file tree
Showing 19 changed files with 341 additions and 35 deletions.
4 changes: 4 additions & 0 deletions EasyTcp3/EasyTcp3.Actions/ActionMessage.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
namespace EasyTcp3.Actions
{
/// <summary>
/// Parameter of the Interceptor/filter functions
/// Represents received action
/// </summary>
public class ActionMessage : Message
{
/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion EasyTcp3/EasyTcp3.Actions/EasyTcp3.Actions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageVersion>2.3.1</PackageVersion>
<PackageVersion>2.4.0</PackageVersion>
<Title>EasyTcp.Actions</Title>
<Description>This package adds support to EasyTcp for triggering specific functions based on received data. See github for examples.</Description>
<Copyright>Job79</Copyright>
Expand Down
2 changes: 1 addition & 1 deletion EasyTcp3/EasyTcp3.Encryption/EasyTcp3.Encryption.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageVersion>2.2.0</PackageVersion>
<PackageVersion>2.2.1</PackageVersion>
<Title>EasyTcp.Encryption</Title>
<Description>This package adds ssl and encryption support to EasyTcp and EasyTcp.Actions</Description>
<Copyright>Job79</Copyright>
Expand Down
57 changes: 57 additions & 0 deletions EasyTcp3/EasyTcp3.Examples/LargeArray/LargeArrayExample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System;
using System.Threading.Tasks;
using EasyTcp3.Actions;
using EasyTcp3.Actions.ActionUtils;
using EasyTcp3.ClientUtils;
using EasyTcp3.ClientUtils.Async;
using EasyTcp3.Server.ServerUtils;

namespace EasyTcp3.Examples.LargeArray
{
/* Example usage of the SendLargeArray and ReceiveLargeArray functions
* Use these functions when sending large data (over 65.535 bytes)
* See stream examples for extreme large data
*/
public class LargeArrayExample
{
private const ushort Port = 6171;

public static void Run()
{
var server = new EasyTcpActionServer().Start(Port);
}

public static void Connect()
{
using var client = new EasyTcpClient();
if (!client.Connect("127.0.0.1", Port)) return;

// Trigger action
client.SendAction("LargeArray");

// Send large array
// Length of array is prefixed by default (int as byte[4])
client.SendLargeArray(new byte[1000000]);

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

/*
* Receive is only working in OnDataReceive or action methods
* This because the internal dataReceiver will read the stream when running
*/
[EasyTcpAction("LargeArray")]
public async Task LargeArrayReceive(Message message)
{
// Receive array with prefix
var largeArray = await message.ReceiveLargeArrayAsync();
Console.WriteLine($"Received {largeArray.Length} bytes");

// Receive array with known length
var largeArray2 = await message.ReceiveLargeArrayAsync(10000);
Console.WriteLine($"Received {largeArray2.Length} bytes");
}
}
}
3 changes: 2 additions & 1 deletion EasyTcp3/EasyTcp3.Examples/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using EasyTcp3.Examples.SpeedTest;

using EasyTcp3.Examples.SpeedTest;

namespace EasyTcp3.Examples
{
Expand Down
2 changes: 2 additions & 0 deletions EasyTcp3/EasyTcp3.Examples/Protocols/ProtocolsExample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public void Start()
{
/* Prefix length protocol, (Default when not specified)
* prefixes all data with its length. Length is a ushort as byte[2]
* Max data size is 65.535 bytes. See LargeArray or streams examples for large data
*
* Example message:
* data: "data"
Expand All @@ -48,6 +49,7 @@ public void Start()
/* None protocol,
* doesn't determine the end of a message
* Reads all available bytes into 1 byte[]
* Doesn't work with ReceiveStream/ReceiveLargeArray
*
* Example message:
* data: "data"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
using EasyTcp3.Actions.ActionUtils;
using EasyTcp3.ClientUtils;

namespace EasyTcp3.Examples.Files
namespace EasyTcp3.Examples.Streams
{
/* Example client that sends/receives a stream(file) over the network
* EasyTcp only supports sending streams with a known length
*/
public static class FileClient
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
using System.IO;
using System.Threading.Tasks;
using EasyTcp3.Actions;
using EasyTcp3.ClientUtils;
using EasyTcp3.ClientUtils.Async;
using EasyTcp3.Server.ServerUtils;

namespace EasyTcp3.Examples.Files
namespace EasyTcp3.Examples.Streams
{
/* Example server that sends/receives a stream(file) over the network
* EasyTcp only supports sending streams with a known length
*/
public class FileServer
{
Expand Down
63 changes: 63 additions & 0 deletions EasyTcp3/EasyTcp3.Examples/Streams/StreamExample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System;
using System.IO;
using System.Threading.Tasks;
using EasyTcp3.Actions;
using EasyTcp3.Actions.ActionUtils;
using EasyTcp3.ClientUtils;
using EasyTcp3.ClientUtils.Async;
using EasyTcp3.Server.ServerUtils;

namespace EasyTcp3.Examples.Streams
{
/* Example usage of the SendStream and ReceiveStream functions (Also see file server/client)
* Use these functions when sending extreme large data.
*/
public class StreamExample
{
private const ushort Port = 6179;
public static void Run()
{
var server = new EasyTcpActionServer().Start(Port);
}

public static void Connect()
{
using var client = new EasyTcpClient();
if(!client.Connect("127.0.0.1", Port)) return;

// Trigger action
client.SendAction("ReceiveStream");

// Send large array
// Length of stream is prefixed by default (long as byte[8])
var stream = new MemoryStream(new byte[100000]);
client.SendStream(stream);

// Send stream without length prefix
var stream2 = new MemoryStream(new byte[10000]);
client.SendStream(stream2, false);
Console.ReadLine();

// Writing / reading the base stream is also possible
var baseStream = client.Protocol.GetStream(client);
}

/*
* Receive is only working in OnDataReceive or action methods
* This because the internal dataReceiver will read the stream when running
*/
[EasyTcpAction("ReceiveStream")]
public async Task ReceiveStream(Message message)
{
// Receive stream with prefix
await using var stream = new MemoryStream();
await message.ReceiveStreamAsync(stream);
Console.WriteLine($"Received {stream.Length} bytes");

// Receive stream with known length
await using var stream2 = new MemoryStream();
await message.ReceiveStreamAsync(stream2, 10000);
Console.WriteLine($"Received {stream2.Length} bytes");
}
}
}
2 changes: 1 addition & 1 deletion EasyTcp3/EasyTcp3.Logging/EasyTcp3.Logging.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageVersion>1.0.0</PackageVersion>
<PackageVersion>1.1.0</PackageVersion>
<Title>EasyTcp.Logging</Title>
<Description>This package adds logging support to EasyTcp, EasyTcp.Actions and EasyTcp.Encryption</Description>
<Copyright>Job79</Copyright>
Expand Down
41 changes: 41 additions & 0 deletions EasyTcp3/EasyTcp3.Test/EasyTcp/Client/LargeArray/LargeArray.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System.Linq;
using System.Net;
using EasyTcp3.ClientUtils;
using EasyTcp3.ClientUtils.Async;
using EasyTcp3.Server;
using EasyTcp3.Server.ServerUtils;
using NUnit.Framework;

namespace EasyTcp3.Test.EasyTcp.Client.LargeArray
{
public class LargeArray
{
[Test]
public void TestLargeArray()
{
ushort port = TestHelper.GetPort();
using var server = new EasyTcpServer().Start(port);
server.OnDataReceive += async (sender, message) =>
{
var array = await message.ReceiveLargeArrayAsync();
message.Client.Send(array.Length);
await message.Client.SendLargeArrayAsync(array);
};

byte[] receivedArray = null;

using var client = new EasyTcpClient();
client.OnDataReceive += (sender, message) => receivedArray = message.ReceiveLargeArray();
Assert.IsTrue(client.Connect(IPAddress.Loopback, port));

client.Send("first message");

byte[] largeMessage = new byte[ushort.MaxValue * 10];
for (int i = 0; i < largeMessage.Length; i++) largeMessage[i] = 11;

client.SendLargeArray(largeMessage);
TestHelper.WaitWhileTrue(() => receivedArray == null);
Assert.IsTrue(receivedArray.SequenceEqual(largeMessage));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using EasyTcp3.Server.ServerUtils;
using NUnit.Framework;

namespace EasyTcp3.Test.EasyTcp.Client
namespace EasyTcp3.Test.EasyTcp.Client.Stream
{
/// <summary>
/// Tests for the Stream functions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
using EasyTcp3.Server.ServerUtils;
using NUnit.Framework;

namespace EasyTcp3.Test.EasyTcp.Client
namespace EasyTcp3.Test.EasyTcp.Client.Stream
{
/// <summary>
/// Tests for the StreamAsync functions
Expand Down
59 changes: 59 additions & 0 deletions EasyTcp3/EasyTcp3/ClientUtils/Async/LargeArrayAsyncUtil.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System;
using System.IO;
using System.Threading.Tasks;

namespace EasyTcp3.ClientUtils.Async
{
/// <summary>
/// Class with the SendLargeArrayAsync/ReceiveLargeArrayAsync functions
/// </summary>
public static class ArrayAsyncUtil
{
/// <summary>
/// Send array to the remote host
/// Host can only receive an array when not listening for incoming messages
/// </summary>
/// <param name="client"></param>
/// <param name="array"></param>
/// <param name="sendLengthPrefix">determines whether prefix with length of the data is send</param>
public static async Task SendLargeArrayAsync(this EasyTcpClient client, byte[] array, bool sendLengthPrefix = true)
{
if(client?.BaseSocket == null) throw new Exception("Client is not connected");

await using var networkStream = client.Protocol.GetStream(client);
if(sendLengthPrefix) await networkStream.WriteAsync(BitConverter.GetBytes(array.Length));
await networkStream.WriteAsync(array);
}

/// <summary>
/// Receive array from remote host
/// Use this method only when not listening for incoming messages (In the OnReceive event)
/// </summary>
/// <param name="message"></param>
/// <param name="count">length of data, use prefix when 0</param>
/// <param name="bufferSize"></param>
/// <exception cref="InvalidDataException">stream is not writable</exception>
public static async Task<byte[]> ReceiveLargeArrayAsync(this Message message, int count = 0, int bufferSize = 1024)
{
if(message?.Client?.BaseSocket == null) throw new Exception("Client is not connected");

await using var networkStream = message.Client.Protocol.GetStream(message.Client);

// Get length from stream
if (count == 0)
{
var length = new byte[4];
await networkStream.ReadAsync(length, 0, length.Length);
count = BitConverter.ToInt32(length);
}

var receivedArray = new byte[count];
int read, totalReceivedBytes = 0;

while (totalReceivedBytes < count &&
(read = await networkStream.ReadAsync(receivedArray, totalReceivedBytes, Math.Min(bufferSize, count - totalReceivedBytes))) > 0)
totalReceivedBytes += read;
return receivedArray;
}
}
}
Loading

0 comments on commit 25df1f1

Please sign in to comment.