Append client/server test app

pull/1/head
Ogoun 5 years ago
parent e5362f2cdf
commit 07685451fa

@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\ZeroLevel\ZeroLevel.csproj" />
</ItemGroup>
</Project>

@ -0,0 +1,117 @@
using System;
using System.Net;
using System.Threading;
using ZeroLevel;
namespace Client
{
class Program
{
static void Main(string[] args)
{
Log.AddConsoleLogger();
var ex = Bootstrap.CreateExchange();
var address = ReadIP();
var port = ReadPort();
var client = ex.GetConnection(new IPEndPoint(address, port));
Console.WriteLine("Esc - exit\r\nEnter - recreate connection\r\nSpace - send request");
long index = 0;
while (true)
{
if (Console.KeyAvailable)
{
switch (Console.ReadKey().Key)
{
case ConsoleKey.Escape:
client?.Dispose();
return;
case ConsoleKey.Enter:
address = ReadIP();
port = ReadPort();
try
{
if (client != null)
{
client.Dispose();
}
client = ex.GetConnection(new IPEndPoint(address, port));
}
catch (Exception exc)
{
Log.Error(exc, "Fault recreate connection");
}
break;
case ConsoleKey.Spacebar:
Log.Info("Send request");
if (client == null)
{
client = ex.GetConnection(new IPEndPoint(address, port));
if (client == null)
{
Log.Info("No connection");
}
continue;
}
if (false == client.Request<string, string>("time", "Time reqeust", s => { Log.Info($"Got time response '{s}'"); }))
{
Log.Warning("Send time request fault");
}
break;
}
}
Thread.Sleep(100);
index++;
if (index % 50 == 0)
{
if (client == null)
{
client = ex.GetConnection(new IPEndPoint(address, port));
if (client == null)
{
Log.Info("No connection");
}
continue;
}
if (false == client.Request<string, string>("whois", "Whois reqeust", s => { Log.Info($"Got whois response '{s}'"); }))
{
Log.Warning("Send whois request fault");
}
}
}
}
static IPAddress ReadIP()
{
IPAddress ip = null;
do
{
Console.WriteLine("IP>");
var address = Console.ReadLine();
if (IPAddress.TryParse(address, out ip) == false)
{
ip = null;
Console.WriteLine($"Incorrect ip address '{address}'");
}
} while (ip == null);
return ip;
}
static int ReadPort()
{
int port = -1;
do
{
Console.WriteLine("PORT>");
var port_line = Console.ReadLine();
if (int.TryParse(port_line, out port) == false || port <= 0)
{
port = -1;
Console.WriteLine($"Incorrect port '{port_line}'");
}
} while (port <= 0);
return port;
}
}
}

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<PublishProtocol>FileSystem</PublishProtocol>
<Configuration>Release</Configuration>
<Platform>Any CPU</Platform>
<TargetFramework>netcoreapp3.1</TargetFramework>
<PublishDir>bin\Release\netcoreapp3.1\publish\</PublishDir>
</PropertyGroup>
</Project>

@ -0,0 +1,50 @@
using System;
using ZeroLevel;
namespace Server
{
class Program
{
static void Main(string[] args)
{
Log.AddConsoleLogger();
var ex = Bootstrap.CreateExchange();
var port = ReadPort();
var server = ex.UseHost(port);
server.RegisterInbox<string, string>("time", (c, s) => { Log.Info($"Request time: [{s}]"); return DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss"); });
server.RegisterInbox<string, string>("whois", (c, s) => { Log.Info($"Request whois: [{s}]"); return $"[{Environment.MachineName}] {Environment.UserDomainName}\\{Environment.UserName}"; });
server.OnConnect += Server_OnConnect;
server.OnDisconnect += Server_OnDisconnect;
Console.ReadKey();
}
private static void Server_OnDisconnect(ZeroLevel.Network.ISocketClient obj)
{
Log.Info($"Client disconnected: {obj.Endpoint.Address}:{obj.Endpoint.Port}");
}
private static void Server_OnConnect(ZeroLevel.Network.IClient obj)
{
Log.Info($"Client connected: {obj.Endpoint.Address}:{obj.Endpoint.Port}");
}
static int ReadPort()
{
int port = -1;
do
{
Console.WriteLine("PORT>");
var port_line = Console.ReadLine();
if (int.TryParse(port_line, out port) == false || port <= 0)
{
port = -1;
Console.WriteLine($"Incorrect port '{port_line}'");
}
} while (port <= 0);
return port;
}
}
}

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<PublishProtocol>FileSystem</PublishProtocol>
<Configuration>Release</Configuration>
<Platform>Any CPU</Platform>
<TargetFramework>netcoreapp3.1</TargetFramework>
<PublishDir>bin\Release\netcoreapp3.1\publish\</PublishDir>
</PropertyGroup>
</Project>

@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\ZeroLevel\ZeroLevel.csproj" />
</ItemGroup>
</Project>

@ -51,6 +51,12 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FileTransferClient", "FileT
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FileTransferServer", "FileTransferTest\FileTransferServer\FileTransferServer.csproj", "{9BF859EE-EF90-4B5B-8576-E26770F2F792}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FileTransferServer", "FileTransferTest\FileTransferServer\FileTransferServer.csproj", "{9BF859EE-EF90-4B5B-8576-E26770F2F792}"
EndProject EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "ConnectionTest", "ConnectionTest", "{D5207A5A-2F27-4992-9BA5-0BDCFC59F133}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Client", "ConnectionTest\Client\Client.csproj", "{08CDD42E-E324-40A4-88C3-EDD0493AAF84}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Server", "ConnectionTest\Server\Server.csproj", "{3496A688-0749-48C2-BD60-ABB42A5C17C9}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
@ -241,6 +247,30 @@ Global
{9BF859EE-EF90-4B5B-8576-E26770F2F792}.Release|x64.Build.0 = Release|x64 {9BF859EE-EF90-4B5B-8576-E26770F2F792}.Release|x64.Build.0 = Release|x64
{9BF859EE-EF90-4B5B-8576-E26770F2F792}.Release|x86.ActiveCfg = Release|Any CPU {9BF859EE-EF90-4B5B-8576-E26770F2F792}.Release|x86.ActiveCfg = Release|Any CPU
{9BF859EE-EF90-4B5B-8576-E26770F2F792}.Release|x86.Build.0 = Release|Any CPU {9BF859EE-EF90-4B5B-8576-E26770F2F792}.Release|x86.Build.0 = Release|Any CPU
{08CDD42E-E324-40A4-88C3-EDD0493AAF84}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{08CDD42E-E324-40A4-88C3-EDD0493AAF84}.Debug|Any CPU.Build.0 = Debug|Any CPU
{08CDD42E-E324-40A4-88C3-EDD0493AAF84}.Debug|x64.ActiveCfg = Debug|Any CPU
{08CDD42E-E324-40A4-88C3-EDD0493AAF84}.Debug|x64.Build.0 = Debug|Any CPU
{08CDD42E-E324-40A4-88C3-EDD0493AAF84}.Debug|x86.ActiveCfg = Debug|Any CPU
{08CDD42E-E324-40A4-88C3-EDD0493AAF84}.Debug|x86.Build.0 = Debug|Any CPU
{08CDD42E-E324-40A4-88C3-EDD0493AAF84}.Release|Any CPU.ActiveCfg = Release|Any CPU
{08CDD42E-E324-40A4-88C3-EDD0493AAF84}.Release|Any CPU.Build.0 = Release|Any CPU
{08CDD42E-E324-40A4-88C3-EDD0493AAF84}.Release|x64.ActiveCfg = Release|Any CPU
{08CDD42E-E324-40A4-88C3-EDD0493AAF84}.Release|x64.Build.0 = Release|Any CPU
{08CDD42E-E324-40A4-88C3-EDD0493AAF84}.Release|x86.ActiveCfg = Release|Any CPU
{08CDD42E-E324-40A4-88C3-EDD0493AAF84}.Release|x86.Build.0 = Release|Any CPU
{3496A688-0749-48C2-BD60-ABB42A5C17C9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3496A688-0749-48C2-BD60-ABB42A5C17C9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3496A688-0749-48C2-BD60-ABB42A5C17C9}.Debug|x64.ActiveCfg = Debug|Any CPU
{3496A688-0749-48C2-BD60-ABB42A5C17C9}.Debug|x64.Build.0 = Debug|Any CPU
{3496A688-0749-48C2-BD60-ABB42A5C17C9}.Debug|x86.ActiveCfg = Debug|Any CPU
{3496A688-0749-48C2-BD60-ABB42A5C17C9}.Debug|x86.Build.0 = Debug|Any CPU
{3496A688-0749-48C2-BD60-ABB42A5C17C9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3496A688-0749-48C2-BD60-ABB42A5C17C9}.Release|Any CPU.Build.0 = Release|Any CPU
{3496A688-0749-48C2-BD60-ABB42A5C17C9}.Release|x64.ActiveCfg = Release|Any CPU
{3496A688-0749-48C2-BD60-ABB42A5C17C9}.Release|x64.Build.0 = Release|Any CPU
{3496A688-0749-48C2-BD60-ABB42A5C17C9}.Release|x86.ActiveCfg = Release|Any CPU
{3496A688-0749-48C2-BD60-ABB42A5C17C9}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE
@ -252,6 +282,8 @@ Global
{931DEA89-42D1-4C06-9CB8-A3A0412093D6} = {03ACF314-93FC-46FE-9FB8-3F46A01A5A15} {931DEA89-42D1-4C06-9CB8-A3A0412093D6} = {03ACF314-93FC-46FE-9FB8-3F46A01A5A15}
{F8B727E1-340D-4096-A784-E570AE13FABC} = {FC074553-5D9F-4DF1-9130-7092E37DE768} {F8B727E1-340D-4096-A784-E570AE13FABC} = {FC074553-5D9F-4DF1-9130-7092E37DE768}
{9BF859EE-EF90-4B5B-8576-E26770F2F792} = {FC074553-5D9F-4DF1-9130-7092E37DE768} {9BF859EE-EF90-4B5B-8576-E26770F2F792} = {FC074553-5D9F-4DF1-9130-7092E37DE768}
{08CDD42E-E324-40A4-88C3-EDD0493AAF84} = {D5207A5A-2F27-4992-9BA5-0BDCFC59F133}
{3496A688-0749-48C2-BD60-ABB42A5C17C9} = {D5207A5A-2F27-4992-9BA5-0BDCFC59F133}
EndGlobalSection EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {A65DB16F-877D-4586-A9F3-8BBBFBAF5CEB} SolutionGuid = {A65DB16F-877D-4586-A9F3-8BBBFBAF5CEB}

@ -111,7 +111,10 @@ namespace ZeroLevel.Network
try try
{ {
_connection_set_lock.EnterWriteLock(); _connection_set_lock.EnterWriteLock();
_connections[client.Endpoint].Dispose(); if (_connections.TryGetValue(client.Endpoint, out var ep))
{
ep.Dispose();
}
_connections.Remove(client.Endpoint); _connections.Remove(client.Endpoint);
} }
finally finally

@ -1,10 +1,8 @@
 using System;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Linq.Expressions; using System.Linq.Expressions;
using System.Reflection; using System.Reflection;
using System.Threading.Tasks;
using ZeroLevel.Network.SDL; using ZeroLevel.Network.SDL;
using ZeroLevel.Services.Invokation; using ZeroLevel.Services.Invokation;
using ZeroLevel.Services.Serialization; using ZeroLevel.Services.Serialization;
@ -100,38 +98,32 @@ namespace ZeroLevel.Network
}; };
} }
public void InvokeAsync(byte[] data, ISocketClient client) public void Invoke(byte[] data, ISocketClient client)
{ {
if (_typeResp == null) if (_typeResp == null)
{ {
if (_noArguments) if (_noArguments)
{ {
Task.Run(() => this._invoker.Invoke(this._instance, new object[] { client })); this._invoker.Invoke(this._instance, new object[] { client });
} }
else else
{
Task.Run(() =>
{ {
var incoming = (_typeReq == typeof(byte[])) ? data : MessageSerializer.DeserializeCompatible(_typeReq, data); var incoming = (_typeReq == typeof(byte[])) ? data : MessageSerializer.DeserializeCompatible(_typeReq, data);
this._invoker.Invoke(this._instance, new object[] { client, incoming }); this._invoker.Invoke(this._instance, new object[] { client, incoming });
});
} }
} }
} }
public void InvokeAsync(byte[] data, ISocketClient client, Action<object> callback) public void Invoke(byte[] data, ISocketClient client, Action<object> callback)
{ {
if (_typeReq == null) if (_typeReq == null)
{ {
Task.Run(() => { callback(this._invoker.Invoke(this._instance, new object[] { client })); }); callback(this._invoker.Invoke(this._instance, new object[] { client }));
} }
else else
{
Task.Run(() =>
{ {
var incoming = (_typeReq == typeof(byte[])) ? data : MessageSerializer.DeserializeCompatible(_typeReq, data); var incoming = (_typeReq == typeof(byte[])) ? data : MessageSerializer.DeserializeCompatible(_typeReq, data);
callback(this._invoker.Invoke(this._instance, new object[] { client, incoming })); callback(this._invoker.Invoke(this._instance, new object[] { client, incoming }));
});
} }
} }
@ -206,7 +198,7 @@ namespace ZeroLevel.Network
{ {
try try
{ {
invoker.InvokeAsync(frame.Payload, client); invoker.Invoke(frame.Payload, client);
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -228,7 +220,7 @@ namespace ZeroLevel.Network
MRInvoker invoker; MRInvoker invoker;
if (_requestors.TryGetValue(frame.Inbox, out invoker)) if (_requestors.TryGetValue(frame.Inbox, out invoker))
{ {
invoker.InvokeAsync(frame.Payload, client invoker.Invoke(frame.Payload, client
, result => , result =>
{ {
handler(identity, MessageSerializer.SerializeCompatible(result)); handler(identity, MessageSerializer.SerializeCompatible(result));

Loading…
Cancel
Save

Powered by TurnKey Linux.