This repository has been archived by the owner on Aug 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from Job79/develop
Develop
- Loading branch information
Showing
19 changed files
with
341 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
EasyTcp3/EasyTcp3.Examples/LargeArray/LargeArrayExample.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 1 addition & 3 deletions
4
...cp3/EasyTcp3.Examples/Files/FileServer.cs → ...3/EasyTcp3.Examples/Streams/FileServer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
EasyTcp3/EasyTcp3.Test/EasyTcp/Client/LargeArray/LargeArray.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
EasyTcp3/EasyTcp3/ClientUtils/Async/LargeArrayAsyncUtil.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
Oops, something went wrong.