diff --git a/ConnectionTest/Client/Client.csproj b/ConnectionTest/Client/Client.csproj new file mode 100644 index 0000000..f90f0c0 --- /dev/null +++ b/ConnectionTest/Client/Client.csproj @@ -0,0 +1,12 @@ + + + + Exe + netcoreapp3.1 + + + + + + + diff --git a/ConnectionTest/Client/Program.cs b/ConnectionTest/Client/Program.cs new file mode 100644 index 0000000..274d434 --- /dev/null +++ b/ConnectionTest/Client/Program.cs @@ -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("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("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; + } + } +} diff --git a/ConnectionTest/Client/Properties/PublishProfiles/FolderProfile.pubxml b/ConnectionTest/Client/Properties/PublishProfiles/FolderProfile.pubxml new file mode 100644 index 0000000..ccb19b1 --- /dev/null +++ b/ConnectionTest/Client/Properties/PublishProfiles/FolderProfile.pubxml @@ -0,0 +1,13 @@ + + + + + FileSystem + Release + Any CPU + netcoreapp3.1 + bin\Release\netcoreapp3.1\publish\ + + \ No newline at end of file diff --git a/ConnectionTest/Server/Program.cs b/ConnectionTest/Server/Program.cs new file mode 100644 index 0000000..571db6d --- /dev/null +++ b/ConnectionTest/Server/Program.cs @@ -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("time", (c, s) => { Log.Info($"Request time: [{s}]"); return DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss"); }); + server.RegisterInbox("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; + } + } +} diff --git a/ConnectionTest/Server/Properties/PublishProfiles/FolderProfile.pubxml b/ConnectionTest/Server/Properties/PublishProfiles/FolderProfile.pubxml new file mode 100644 index 0000000..ccb19b1 --- /dev/null +++ b/ConnectionTest/Server/Properties/PublishProfiles/FolderProfile.pubxml @@ -0,0 +1,13 @@ + + + + + FileSystem + Release + Any CPU + netcoreapp3.1 + bin\Release\netcoreapp3.1\publish\ + + \ No newline at end of file diff --git a/ConnectionTest/Server/Server.csproj b/ConnectionTest/Server/Server.csproj new file mode 100644 index 0000000..f90f0c0 --- /dev/null +++ b/ConnectionTest/Server/Server.csproj @@ -0,0 +1,12 @@ + + + + Exe + netcoreapp3.1 + + + + + + + diff --git a/ZeroLevel.sln b/ZeroLevel.sln index 7301095..72c7ae5 100644 --- a/ZeroLevel.sln +++ b/ZeroLevel.sln @@ -51,6 +51,12 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FileTransferClient", "FileT EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FileTransferServer", "FileTransferTest\FileTransferServer\FileTransferServer.csproj", "{9BF859EE-EF90-4B5B-8576-E26770F2F792}" 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 GlobalSection(SolutionConfigurationPlatforms) = preSolution 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|x86.ActiveCfg = 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 GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -252,6 +282,8 @@ Global {931DEA89-42D1-4C06-9CB8-A3A0412093D6} = {03ACF314-93FC-46FE-9FB8-3F46A01A5A15} {F8B727E1-340D-4096-A784-E570AE13FABC} = {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 GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {A65DB16F-877D-4586-A9F3-8BBBFBAF5CEB} diff --git a/ZeroLevel/Services/Network/SocketServer.cs b/ZeroLevel/Services/Network/SocketServer.cs index 90b23c5..fa6296e 100644 --- a/ZeroLevel/Services/Network/SocketServer.cs +++ b/ZeroLevel/Services/Network/SocketServer.cs @@ -111,7 +111,10 @@ namespace ZeroLevel.Network try { _connection_set_lock.EnterWriteLock(); - _connections[client.Endpoint].Dispose(); + if (_connections.TryGetValue(client.Endpoint, out var ep)) + { + ep.Dispose(); + } _connections.Remove(client.Endpoint); } finally diff --git a/ZeroLevel/Services/Network/Utils/Router.cs b/ZeroLevel/Services/Network/Utils/Router.cs index 68ce084..7178c76 100644 --- a/ZeroLevel/Services/Network/Utils/Router.cs +++ b/ZeroLevel/Services/Network/Utils/Router.cs @@ -1,10 +1,8 @@ - -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Reflection; -using System.Threading.Tasks; using ZeroLevel.Network.SDL; using ZeroLevel.Services.Invokation; 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 (_noArguments) { - Task.Run(() => this._invoker.Invoke(this._instance, new object[] { client })); + this._invoker.Invoke(this._instance, new object[] { client }); } else { - Task.Run(() => - { - var incoming = (_typeReq == typeof(byte[])) ? data : MessageSerializer.DeserializeCompatible(_typeReq, data); - this._invoker.Invoke(this._instance, new object[] { client, incoming }); - }); + var incoming = (_typeReq == typeof(byte[])) ? data : MessageSerializer.DeserializeCompatible(_typeReq, data); + this._invoker.Invoke(this._instance, new object[] { client, incoming }); } } } - public void InvokeAsync(byte[] data, ISocketClient client, Action callback) + public void Invoke(byte[] data, ISocketClient client, Action callback) { if (_typeReq == null) { - Task.Run(() => { callback(this._invoker.Invoke(this._instance, new object[] { client })); }); + callback(this._invoker.Invoke(this._instance, new object[] { client })); } else { - Task.Run(() => - { - var incoming = (_typeReq == typeof(byte[])) ? data : MessageSerializer.DeserializeCompatible(_typeReq, data); - callback(this._invoker.Invoke(this._instance, new object[] { client, incoming })); - }); + var incoming = (_typeReq == typeof(byte[])) ? data : MessageSerializer.DeserializeCompatible(_typeReq, data); + callback(this._invoker.Invoke(this._instance, new object[] { client, incoming })); } } @@ -206,7 +198,7 @@ namespace ZeroLevel.Network { try { - invoker.InvokeAsync(frame.Payload, client); + invoker.Invoke(frame.Payload, client); } catch (Exception ex) { @@ -222,13 +214,13 @@ namespace ZeroLevel.Network } public void HandleRequest(Frame frame, ISocketClient client, int identity, Action handler) - { + { try { MRInvoker invoker; if (_requestors.TryGetValue(frame.Inbox, out invoker)) { - invoker.InvokeAsync(frame.Payload, client + invoker.Invoke(frame.Payload, client , result => { handler(identity, MessageSerializer.SerializeCompatible(result));