pull/1/head
a.bozhenov 6 years ago
parent 2be9f7b7e2
commit 63eb20dcef

@ -6,8 +6,7 @@ namespace TestApp
{
private static void Main(string[] args)
{
Log.AddConsoleLogger();
Bootstrap.Startup<MyService>(args);
Bootstrap.Startup<MyService>(args, () => Configuration.ReadSetFromIniFile("config.ini"));
}
}
}

@ -9,4 +9,10 @@
<ProjectReference Include="..\ZeroLevel\ZeroLevel.csproj" />
</ItemGroup>
<ItemGroup>
<None Update="config.ini">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

@ -0,0 +1,14 @@
discovery=127.0.0.1:5012
[service]
AppName=TestApp
AppKey=test.app
ServiceGroup=seo
ServiceType=site
[log]
path=logs
console=true
[webapi]
port=8183

@ -14,6 +14,7 @@ namespace ZeroLevel.Models
#region Static
private static readonly InvokeResult _successResultWitoutComment = new InvokeResult(true, String.Empty);
private static readonly InvokeResult _faultResultWitoutComment = new InvokeResult(false, String.Empty);
#endregion Static
@ -54,6 +55,8 @@ namespace ZeroLevel.Models
/// </summary>
public static InvokeResult Fault(string comment) { return new InvokeResult(false, comment); }
public static InvokeResult Fault() { return _faultResultWitoutComment; }
/// <summary>
/// Successfully
/// </summary>

@ -454,9 +454,9 @@ namespace ZeroLevel.Services.Config
public void CopyTo(IConfiguration config)
{
foreach (var key in config.Keys)
foreach (var key in this.Keys)
{
this.Append(key, config[key]);
config.Append(key, this[key]);
}
}
}

@ -30,6 +30,7 @@ namespace ZeroLevel
_emptySet = new BaseConfigurationSet();
_empty.Freeze(true);
_emptySet.FreezeConfiguration(true);
DefaultSet = Configuration.CreateSet();
}
#endregion Ctor

@ -118,6 +118,10 @@ namespace ZeroLevel
/// </summary>
IConfiguration Append(string key, string value);
/// <summary>
/// Append key and values list
/// </summary>
IConfiguration Append(string key, IEnumerable<string> values);
/// <summary>
/// Set key with one value, if any values by key exists, they will be dropped
/// </summary>

@ -97,8 +97,6 @@ namespace ZeroLevel.Services.Logging.Implementation
}
internal static TextFileLoggerOptions CreateOptionsBy(IConfiguration config, string path, string logPrefix)
{
if (config.Contains(logPrefix))
{
var options = new TextFileLoggerOptions().
SetFolderPath(path);
@ -134,8 +132,6 @@ namespace ZeroLevel.Services.Logging.Implementation
});
return options;
}
return null;
}
}
public sealed class TextFileLogger : ILogger

@ -30,7 +30,5 @@ namespace ZeroLevel.Network
void RegisterInbox<T>(string inbox, Action<T, long, IZBackward> handler);
void RegisterInbox<T>(Action<T, long, IZBackward> handler);
void RegisterInbox<Treq, Tresp>(string inbox, Func<Treq, long, IZBackward, Tresp> handler);
}
}

@ -1,5 +1,4 @@
using System;
using System.Net;
using System.Net;
using ZeroLevel.Models;
namespace ZeroLevel.Network
@ -8,7 +7,7 @@ namespace ZeroLevel.Network
{
IPEndPoint Endpoint { get; }
void SendBackward(Frame frame);
void SendBackward<T>(string inbox, T message);
InvokeResult SendBackward(Frame frame);
InvokeResult SendBackward<T>(string inbox, T message);
}
}

@ -23,9 +23,9 @@ namespace ZeroLevel.Services.Network.FileTransfer
_nameMapper = nameMapper ?? throw new Exception(nameof(nameMapper));
_disposeClient = disposeClient;
_client.RegisterInbox<FileStartFrame, InvokeResult>("__upload_file_start", (f, _, __) => Receiver.Incoming(f, nameMapper(_client)));
_client.RegisterInbox<FileFrame, InvokeResult>("__upload_file_frame", (f, _, __) => Receiver.Incoming(f));
_client.RegisterInbox<FileEndFrame, InvokeResult>("__upload_file_complete", (f, _, __) => Receiver.Incoming(f));
_client.RegisterInbox<FileStartFrame>("__upload_file_start", (f, _, __) => Receiver.Incoming(f, nameMapper(_client)));
_client.RegisterInbox<FileFrame>("__upload_file_frame", (f, _, __) => Receiver.Incoming(f));
_client.RegisterInbox<FileEndFrame>("__upload_file_complete", (f, _, __) => Receiver.Incoming(f));
}
public void Dispose()

@ -13,6 +13,18 @@ namespace ZeroLevel.Services.Network.FileTransfer
{
private readonly FileStream _stream;
internal DateTime _writeTime { get; private set; } = DateTime.UtcNow;
private bool _gotCompleteMessage = false;
public bool GotCompleteMessage() => _gotCompleteMessage = true;
public bool ReadyToRemove()
{
if (_gotCompleteMessage)
{
return (DateTime.UtcNow - _writeTime).TotalSeconds > 15;
}
return false;
}
public FileWriter(string path)
{
@ -59,7 +71,7 @@ namespace ZeroLevel.Services.Network.FileTransfer
{
foreach (var pair in _incoming)
{
if (pair.Value.IsTimeoutBy(TimeSpan.FromMinutes(3)))
if (pair.Value.IsTimeoutBy(TimeSpan.FromMinutes(3)) || pair.Value.ReadyToRemove())
{
Remove(pair.Key);
}
@ -116,7 +128,11 @@ namespace ZeroLevel.Services.Network.FileTransfer
{
lock (_locker)
{
Remove(info.UploadFileTaskId);
FileWriter stream;
if (_incoming.TryGetValue(info.UploadFileTaskId, out stream))
{
stream.GotCompleteMessage();
}
}
}
catch (Exception ex)

@ -1,6 +1,4 @@
using System;
using System.IO;
using System.Threading;
using ZeroLevel.Models;
using ZeroLevel.Network;
using ZeroLevel.Services.Network.FileTransfer.Model;
@ -43,54 +41,21 @@ namespace ZeroLevel.Services.Network.FileTransfer
internal override void ExecuteSendFile(FileReader reader, FileTransferTask task)
{
/*
Log.Info($"Start upload file {reader.Path}");
var startinfo = reader.GetStartInfo();
using (var signal = new ManualResetEvent(false))
{
bool next = false;
if (false == task.Client.RequestBackward<FileStartFrame, InvokeResult>("__upload_file_start", startinfo,
r =>
{
next = r.Success;
signal.Set();
}).Success)
if (false == task.Client.SendBackward<FileStartFrame>("__upload_file_start", startinfo).Success)
{
next = false;
signal.Set();
return;
}
signal.WaitOne(5000);
if (next)
{
foreach (var chunk in reader.Read())
{
signal.Reset();
if (task.Client.RequestBackward<FileFrame, InvokeResult>("__upload_file_frame", chunk, r => next = r.Success).Success == false)
{
next = false;
signal.Set();
}
signal.WaitOne();
if (!next)
{
break;
}
}
}
if (next)
if (task.Client.SendBackward<FileFrame>("__upload_file_frame", chunk).Success == false)
{
task.Client.RequestBackward<FileEndFrame, InvokeResult>("__upload_file_complete", reader.GetCompleteInfo(), r =>
{
if (r.Success == false)
{
Log.Warning($"Unsuccess send file. {r.Comment}");
}
});
return;
}
}
Log.Debug($"Stop upload file {reader.Path}");*/
task.Client.SendBackward<FileEndFrame>("__upload_file_complete", reader.GetCompleteInfo());
Log.Debug($"Stop upload file {reader.Path}");
}
}
}

@ -71,11 +71,6 @@ namespace ZeroLevel.Network
_router.RegisterInbox(DEFAULT_MESSAGE_INBOX, handler);
}
public void RegisterInbox<Treq, Tresp>(string inbox, Func<Treq, long, IZBackward, Tresp> handler)
{
_router.RegisterInbox<Treq, Tresp>(inbox, handler);
}
public InvokeResult Request<Tresp>(Action<Tresp> callback)
{
return _fe.Request<Tresp>(DEFAULT_REQUEST_INBOX, resp => callback(resp));
@ -106,14 +101,14 @@ namespace ZeroLevel.Network
return _fe.Send<T>(inbox, obj);
}
public void SendBackward(Frame frame)
public InvokeResult SendBackward(Frame frame)
{
_fe.Send(frame);
return _fe.Send(frame);
}
public void SendBackward<T>(string inbox, T obj)
public InvokeResult SendBackward<T>(string inbox, T obj)
{
Send(inbox, obj);
return Send(inbox, obj);
}
}
}

@ -0,0 +1,98 @@
using System;
using System.Collections.Generic;
using ZeroLevel.Network;
using ZeroLevel.Services.Pools;
namespace ZeroLevel.Network
{
internal sealed class RequestBuffer
{
private readonly object _reqeust_lock = new object();
private Dictionary<long, RequestInfo> _requests = new Dictionary<long, RequestInfo>();
private static ObjectPool<RequestInfo> _ri_pool = new ObjectPool<RequestInfo>(() => new RequestInfo());
public void RegisterForFrame(Frame frame, Action<Frame> callback, Action<string> fail = null)
{
var ri = _ri_pool.Allocate();
lock (_reqeust_lock)
{
ri.Reset(callback, fail);
_requests.Add(frame.FrameId, ri);
}
}
public void Fail(long frameId, string message)
{
RequestInfo ri = null;
lock (_reqeust_lock)
{
if (_requests.ContainsKey(frameId))
{
ri = _requests[frameId];
_requests.Remove(frameId);
}
}
if (ri != null)
{
ri.Fail(message);
_ri_pool.Free(ri);
}
}
public void Success(long frameId, Frame frame)
{
RequestInfo ri = null;
lock (_reqeust_lock)
{
if (_requests.ContainsKey(frameId))
{
ri = _requests[frameId];
_requests.Remove(frameId);
}
}
if (ri != null)
{
ri.Success(frame);
_ri_pool.Free(ri);
}
}
public void StartSend(long frameId)
{
RequestInfo ri = null;
lock (_reqeust_lock)
{
if (_requests.ContainsKey(frameId))
{
ri = _requests[frameId];
}
}
if (ri != null)
{
ri.StartSend();
}
}
public void TestForTimeouts()
{
var now_ticks = DateTime.UtcNow.Ticks;
var to_remove = new List<long>();
lock (_reqeust_lock)
{
foreach (var pair in _requests)
{
if (pair.Value.Sended == false) continue;
var diff = now_ticks - pair.Value.Timestamp;
if (diff > ZBaseNetwork.MAX_REQUEST_TIME_TICKS)
{
to_remove.Add(pair.Key);
}
}
}
foreach (var key in to_remove)
{
Fail(key, "Timeout");
}
}
}
}

@ -29,7 +29,7 @@ namespace ZeroLevel.Network
/// <summary>
/// The period of the request, after which it is considered unsuccessful
/// </summary>
protected const long MAX_REQUEST_TIME_TICKS = 30000 * TimeSpan.TicksPerMillisecond;
internal const long MAX_REQUEST_TIME_TICKS = 30000 * TimeSpan.TicksPerMillisecond;
public const int MAX_REQUEST_TIME_MS = 30000;

@ -1,10 +1,8 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using ZeroLevel.Services.Pools;
using ZeroLevel.Services.Serialization;
namespace ZeroLevel.Network
@ -12,96 +10,7 @@ namespace ZeroLevel.Network
public class ZSocketClient
: ZBaseNetwork, IZTransport
{
private class RequestBuffer
{
private readonly object _reqeust_lock = new object();
private Dictionary<long, RequestInfo> _requests = new Dictionary<long, RequestInfo>();
private static ObjectPool<RequestInfo> _ri_pool = new ObjectPool<RequestInfo>(() => new RequestInfo());
public void RegisterForFrame(Frame frame, Action<Frame> callback, Action<string> fail = null)
{
var ri = _ri_pool.Allocate();
lock (_reqeust_lock)
{
ri.Reset(callback, fail);
_requests.Add(frame.FrameId, ri);
}
}
public void Fail(long frameId, string message)
{
RequestInfo ri = null;
lock (_reqeust_lock)
{
if (_requests.ContainsKey(frameId))
{
ri = _requests[frameId];
_requests.Remove(frameId);
}
}
if (ri != null)
{
ri.Fail(message);
_ri_pool.Free(ri);
}
}
public void Success(long frameId, Frame frame)
{
RequestInfo ri = null;
lock (_reqeust_lock)
{
if (_requests.ContainsKey(frameId))
{
ri = _requests[frameId];
_requests.Remove(frameId);
}
}
if (ri != null)
{
ri.Success(frame);
_ri_pool.Free(ri);
}
}
public void StartSend(long frameId)
{
RequestInfo ri = null;
lock (_reqeust_lock)
{
if (_requests.ContainsKey(frameId))
{
ri = _requests[frameId];
}
}
if (ri != null)
{
ri.StartSend();
}
}
public void TestForTimeouts()
{
var now_ticks = DateTime.UtcNow.Ticks;
var to_remove = new List<long>();
lock (_reqeust_lock)
{
foreach (var pair in _requests)
{
if (pair.Value.Sended == false) continue;
var diff = now_ticks - pair.Value.Timestamp;
if (diff > ZBaseNetwork.MAX_REQUEST_TIME_TICKS)
{
to_remove.Add(pair.Key);
}
}
}
foreach (var key in to_remove)
{
Fail(key, "Timeout");
}
}
}
#region Private

@ -3,6 +3,7 @@ using System.Collections.Concurrent;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using ZeroLevel.Models;
using ZeroLevel.Services.Serialization;
namespace ZeroLevel.Network
@ -52,7 +53,7 @@ namespace ZeroLevel.Network
_stream.BeginRead(_buffer, 0, DEFAULT_RECEIVE_BUFFER_SIZE, ReceiveAsyncCallback, null);
}
public void SendBackward(Frame frame)
public InvokeResult SendBackward(Frame frame)
{
if (frame != null && Status == ZTransportStatus.Working && false == _send_queue.IsCompleted && false == _send_queue.IsAddingCompleted)
{
@ -60,6 +61,7 @@ namespace ZeroLevel.Network
try
{
_send_queue.Add(NetworkStreamFastObfuscator.PrepareData(data));
return InvokeResult.Succeeding();
}
catch (ObjectDisposedException)
{
@ -70,6 +72,30 @@ namespace ZeroLevel.Network
frame?.Release();
}
}
return InvokeResult.Fault();
}
public InvokeResult SendBackward<T>(string inbox, T message)
{
var frame = FrameBuilder.BuildFrame<T>(message, inbox);
if (Status == ZTransportStatus.Working && false == _send_queue.IsCompleted && false == _send_queue.IsAddingCompleted)
{
var data = MessageSerializer.Serialize(frame);
try
{
_send_queue.Add(NetworkStreamFastObfuscator.PrepareData(data));
return InvokeResult.Succeeding();
}
catch (ObjectDisposedException)
{
// Ignore
}
finally
{
frame?.Release();
}
}
return InvokeResult.Fault();
}
private void SendFramesJob()
@ -205,26 +231,5 @@ namespace ZeroLevel.Network
if (other == null) return false;
return this.Endpoint.Compare(other.Endpoint) == 0;
}
public void SendBackward<T>(string inbox, T message)
{
var frame = FrameBuilder.BuildFrame<T>(message, inbox);
if (Status == ZTransportStatus.Working && false == _send_queue.IsCompleted && false == _send_queue.IsAddingCompleted)
{
var data = MessageSerializer.Serialize(frame);
try
{
_send_queue.Add(NetworkStreamFastObfuscator.PrepareData(data));
}
catch (ObjectDisposedException)
{
// Ignore
}
finally
{
frame?.Release();
}
}
}
}
}

@ -5,16 +5,16 @@
<Description>Infrastructure layer library</Description>
<Authors>ogoun</Authors>
<Company>ogoun</Company>
<AssemblyVersion>2.0.6.0</AssemblyVersion>
<AssemblyVersion>2.0.7.0</AssemblyVersion>
<PackageReleaseNotes>Refactoring configuration and log
File client/server (no stable)</PackageReleaseNotes>
File client/server update(no stable)</PackageReleaseNotes>
<PackageProjectUrl>https://github.com/ogoun/Zero/wiki</PackageProjectUrl>
<Copyright>Copyright Ogoun 2019</Copyright>
<PackageLicenseUrl>https://opensource.org/licenses/MIT</PackageLicenseUrl>
<PackageIconUrl>https://raw.githubusercontent.com/ogoun/Zero/master/zero.png</PackageIconUrl>
<RepositoryUrl>https://github.com/ogoun/Zero</RepositoryUrl>
<RepositoryType>GitHub</RepositoryType>
<Version>2.0.6</Version>
<Version>2.0.7</Version>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">

Loading…
Cancel
Save

Powered by TurnKey Linux.