-
Notifications
You must be signed in to change notification settings - Fork 582
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support UDS(Unix Domain Socket) - Server/Client #135
- Loading branch information
Showing
13 changed files
with
761 additions
and
5 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
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,170 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Net.Sockets; | ||
using System.Threading; | ||
using NetCoreServer; | ||
using NDesk.Options; | ||
|
||
namespace UdsEchoClient | ||
{ | ||
class EchoClient : UdsClient | ||
{ | ||
public EchoClient(string path, int messages) : base(path) | ||
{ | ||
_messages = messages; | ||
} | ||
|
||
protected override void OnConnected() | ||
{ | ||
for (long i = _messages; i > 0; i--) | ||
SendMessage(); | ||
} | ||
|
||
protected override void OnSent(long sent, long pending) | ||
{ | ||
_sent += sent; | ||
} | ||
|
||
protected override void OnReceived(byte[] buffer, long offset, long size) | ||
{ | ||
_received += size; | ||
while (_received >= Program.MessageToSend.Length) | ||
{ | ||
SendMessage(); | ||
_received -= Program.MessageToSend.Length; | ||
} | ||
|
||
Program.TimestampStop = DateTime.UtcNow; | ||
Program.TotalBytes += size; | ||
} | ||
|
||
protected override void OnError(SocketError error) | ||
{ | ||
Console.WriteLine($"Client caught an error with code {error}"); | ||
Program.TotalErrors++; | ||
} | ||
|
||
private void SendMessage() | ||
{ | ||
SendAsync(Program.MessageToSend); | ||
} | ||
|
||
private long _sent; | ||
private long _received; | ||
private long _messages; | ||
} | ||
|
||
class Program | ||
{ | ||
public static byte[] MessageToSend; | ||
public static DateTime TimestampStart = DateTime.UtcNow; | ||
public static DateTime TimestampStop = DateTime.UtcNow; | ||
public static long TotalErrors; | ||
public static long TotalBytes; | ||
public static long TotalMessages; | ||
|
||
static void Main(string[] args) | ||
{ | ||
bool help = false; | ||
string path = Path.Combine(Path.GetTempPath(), "echo.sock"); | ||
int clients = 100; | ||
int messages = 1000; | ||
int size = 32; | ||
int seconds = 10; | ||
|
||
var options = new OptionSet() | ||
{ | ||
{ "h|?|help", v => help = v != null }, | ||
{ "p|path=", v => path = v }, | ||
{ "c|clients=", v => clients = int.Parse(v) }, | ||
{ "m|messages=", v => messages = int.Parse(v) }, | ||
{ "s|size=", v => size = int.Parse(v) }, | ||
{ "z|seconds=", v => seconds = int.Parse(v) } | ||
}; | ||
|
||
try | ||
{ | ||
options.Parse(args); | ||
} | ||
catch (OptionException e) | ||
{ | ||
Console.Write("Command line error: "); | ||
Console.WriteLine(e.Message); | ||
Console.WriteLine("Try `--help' to get usage information."); | ||
return; | ||
} | ||
|
||
if (help) | ||
{ | ||
Console.WriteLine("Usage:"); | ||
options.WriteOptionDescriptions(Console.Out); | ||
return; | ||
} | ||
|
||
Console.WriteLine($"Server Unix Domain Socket path: {path}"); | ||
Console.WriteLine($"Working clients: {clients}"); | ||
Console.WriteLine($"Working messages: {messages}"); | ||
Console.WriteLine($"Message size: {size}"); | ||
Console.WriteLine($"Seconds to benchmarking: {seconds}"); | ||
|
||
Console.WriteLine(); | ||
|
||
// Prepare a message to send | ||
MessageToSend = new byte[size]; | ||
|
||
// Create echo clients | ||
var echoClients = new List<EchoClient>(); | ||
for (int i = 0; i < clients; i++) | ||
{ | ||
var client = new EchoClient(path, messages); | ||
echoClients.Add(client); | ||
} | ||
|
||
TimestampStart = DateTime.UtcNow; | ||
|
||
// Connect clients | ||
Console.Write("Clients connecting..."); | ||
foreach (var client in echoClients) | ||
client.ConnectAsync(); | ||
Console.WriteLine("Done!"); | ||
foreach (var client in echoClients) | ||
while (!client.IsConnected) | ||
Thread.Yield(); | ||
Console.WriteLine("All clients connected!"); | ||
|
||
// Wait for benchmarking | ||
Console.Write("Benchmarking..."); | ||
Thread.Sleep(seconds * 1000); | ||
Console.WriteLine("Done!"); | ||
|
||
// Disconnect clients | ||
Console.Write("Clients disconnecting..."); | ||
foreach (var client in echoClients) | ||
client.Disconnect(); | ||
Console.WriteLine("Done!"); | ||
foreach (var client in echoClients) | ||
while (client.IsConnected) | ||
Thread.Yield(); | ||
Console.WriteLine("All clients disconnected!"); | ||
|
||
Console.WriteLine(); | ||
|
||
Console.WriteLine($"Errors: {TotalErrors}"); | ||
|
||
Console.WriteLine(); | ||
|
||
TotalMessages = TotalBytes / size; | ||
|
||
Console.WriteLine($"Total time: {Utilities.GenerateTimePeriod((TimestampStop - TimestampStart).TotalMilliseconds)}"); | ||
Console.WriteLine($"Total data: {Utilities.GenerateDataSize(TotalBytes)}"); | ||
Console.WriteLine($"Total messages: {TotalMessages}"); | ||
Console.WriteLine($"Data throughput: {Utilities.GenerateDataSize((long)(TotalBytes / (TimestampStop - TimestampStart).TotalSeconds))}/s"); | ||
if (TotalMessages > 0) | ||
{ | ||
Console.WriteLine($"Message latency: {Utilities.GenerateTimePeriod((TimestampStop - TimestampStart).TotalMilliseconds / TotalMessages)}"); | ||
Console.WriteLine($"Message throughput: {(long)(TotalMessages / (TimestampStop - TimestampStart).TotalSeconds)} msg/s"); | ||
} | ||
} | ||
} | ||
} |
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,16 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net6.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="NDesk.Options.Core" Version="1.2.5" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\source\NetCoreServer\NetCoreServer.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
Oops, something went wrong.