refactoring

pull/1/head
Ogoun 5 years ago
parent eff1deaaef
commit 4f77d66a06

@ -1,12 +1,6 @@
using Newtonsoft.Json; using Newtonsoft.Json;
using System;
using System.IO;
using System.Text;
using ZeroLevel; using ZeroLevel;
using ZeroLevel.Implementation.Semantic.Helpers;
using ZeroLevel.Logging; using ZeroLevel.Logging;
using ZeroLevel.Services.Serialization;
using ZeroLevel.Services.Trees;
namespace TestApp namespace TestApp
{ {

@ -4,6 +4,34 @@ namespace System.Linq
{ {
public static class LinqExtension public static class LinqExtension
{ {
public static IEnumerable<T[]> ZipLongest<T>(this IEnumerable<T> left, IEnumerable<T> right)
{
IEnumerator<T> leftEnumerator = left.GetEnumerator();
IEnumerator<T> rightEnumerator = right.GetEnumerator();
bool hasLeft = leftEnumerator.MoveNext();
bool hasRight = rightEnumerator.MoveNext();
while (hasLeft || hasRight)
{
if (hasLeft && hasRight)
{
yield return new T[] { leftEnumerator.Current, rightEnumerator.Current };
}
else if (hasLeft)
{
yield return new T[] { leftEnumerator.Current, default(T) };
}
else if (hasRight)
{
yield return new T[] { default(T), rightEnumerator.Current };
}
hasLeft = leftEnumerator.MoveNext();
hasRight = rightEnumerator.MoveNext();
}
}
public static IEnumerable<TSource> DistinctBy<TSource, TKey> public static IEnumerable<TSource> DistinctBy<TSource, TKey>
(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector) (this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
{ {

@ -88,8 +88,6 @@ namespace ZeroLevel.Network
return response; return response;
} }
public static Tresponse Request<Tresponse>(this IClientSet exchange, string alias) public static Tresponse Request<Tresponse>(this IClientSet exchange, string alias)
{ {
Tresponse response = default; Tresponse response = default;

@ -23,6 +23,7 @@ namespace ZeroLevel.Network
public static IncomingFrame NewFrame() => new IncomingFrame(); public static IncomingFrame NewFrame() => new IncomingFrame();
} }
private class SendFrame private class SendFrame
{ {
private SendFrame() { } private SendFrame() { }

@ -60,7 +60,6 @@ namespace ZeroLevel.Network
packet[7] = id[2]; packet[7] = id[2];
packet[8] = id[3]; packet[8] = id[3];
packet[9] = (byte)(MAGIC ^ packet[1] ^ packet[2] ^ packet[3] ^ packet[4]); packet[9] = (byte)(MAGIC ^ packet[1] ^ packet[2] ^ packet[3] ^ packet[4]);
HashData(data, packet[9]); HashData(data, packet[9]);

@ -1,51 +1,27 @@
using System; using System;
using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading;
using ZeroLevel.Services.Pools; using ZeroLevel.Services.Pools;
namespace ZeroLevel.Network namespace ZeroLevel.Network
{ {
internal sealed class RequestBuffer internal sealed class RequestBuffer
{ {
private SpinLock _reqeust_lock = new SpinLock(); private ConcurrentDictionary<long, RequestInfo> _requests = new ConcurrentDictionary<long, RequestInfo>();
private Dictionary<long, RequestInfo> _requests = new Dictionary<long, RequestInfo>();
private static ObjectPool<RequestInfo> _ri_pool = new ObjectPool<RequestInfo>(() => new RequestInfo()); private static ObjectPool<RequestInfo> _ri_pool = new ObjectPool<RequestInfo>(() => new RequestInfo());
public void RegisterForFrame(int identity, Action<byte[]> callback, Action<string> fail = null) public void RegisterForFrame(int identity, Action<byte[]> callback, Action<string> fail = null)
{ {
var ri = _ri_pool.Allocate(); var ri = _ri_pool.Allocate();
bool take = false; ri.Reset(callback, fail);
try _requests[identity] = ri;
{
_reqeust_lock.Enter(ref take);
ri.Reset(callback, fail);
_requests.Add(identity, ri);
}
finally
{
if (take) _reqeust_lock.Exit(false);
}
} }
public void Fail(long frameId, string message) public void Fail(long frameId, string message)
{ {
RequestInfo ri = null; RequestInfo ri;
bool take = false; if (_requests.TryRemove(frameId, out ri))
try
{
_reqeust_lock.Enter(ref take);
if (_requests.ContainsKey(frameId))
{
ri = _requests[frameId];
_requests.Remove(frameId);
}
}
finally
{
if (take) _reqeust_lock.Exit(false);
}
if (ri != null)
{ {
ri.Fail(message); ri.Fail(message);
_ri_pool.Free(ri); _ri_pool.Free(ri);
@ -54,22 +30,8 @@ namespace ZeroLevel.Network
public void Success(long frameId, byte[] data) public void Success(long frameId, byte[] data)
{ {
RequestInfo ri = null; RequestInfo ri;
bool take = false; if (_requests.TryRemove(frameId, out ri))
try
{
_reqeust_lock.Enter(ref take);
if (_requests.ContainsKey(frameId))
{
ri = _requests[frameId];
_requests.Remove(frameId);
}
}
finally
{
if (take) _reqeust_lock.Exit(false);
}
if (ri != null)
{ {
ri.Success(data); ri.Success(data);
_ri_pool.Free(ri); _ri_pool.Free(ri);
@ -78,21 +40,8 @@ namespace ZeroLevel.Network
public void StartSend(long frameId) public void StartSend(long frameId)
{ {
RequestInfo ri = null; RequestInfo ri;
bool take = false; if (_requests.TryGetValue(frameId, out ri))
try
{
_reqeust_lock.Enter(ref take);
if (_requests.ContainsKey(frameId))
{
ri = _requests[frameId];
}
}
finally
{
if (take) _reqeust_lock.Exit(false);
}
if (ri != null)
{ {
ri.StartSend(); ri.StartSend();
} }
@ -100,47 +49,27 @@ namespace ZeroLevel.Network
public void Timeout(List<long> frameIds) public void Timeout(List<long> frameIds)
{ {
bool take = false; RequestInfo ri;
try for (int i = 0; i < frameIds.Count; i++)
{ {
_reqeust_lock.Enter(ref take); if (_requests.TryRemove(frameIds[i], out ri))
for (int i = 0; i < frameIds.Count; i++)
{ {
if (_requests.ContainsKey(frameIds[i])) _ri_pool.Free(ri);
{
_ri_pool.Free(_requests[frameIds[i]]);
_requests.Remove(frameIds[i]);
}
} }
} }
finally
{
if (take) _reqeust_lock.Exit(false);
}
} }
public void TestForTimeouts() public void TestForTimeouts()
{ {
var now_ticks = DateTime.UtcNow.Ticks; var now_ticks = DateTime.UtcNow.Ticks;
var to_remove = new List<long>(); var to_remove = new List<long>();
KeyValuePair<long, RequestInfo>[] to_proceed; foreach (var pair in _requests)
bool take = false;
try
{
_reqeust_lock.Enter(ref take);
to_proceed = _requests.Select(x => x).ToArray();
}
finally
{
if (take) _reqeust_lock.Exit(false);
}
for (int i = 0; i < to_proceed.Length; i++)
{ {
if (to_proceed[i].Value.Sended == false) continue; if (pair.Value.Sended == false) continue;
var diff = now_ticks - to_proceed[i].Value.Timestamp; var diff = now_ticks - pair.Value.Timestamp;
if (diff > BaseSocket.MAX_REQUEST_TIME_TICKS) if (diff > BaseSocket.MAX_REQUEST_TIME_TICKS)
{ {
to_remove.Add(to_proceed[i].Key); to_remove.Add(pair.Key);
} }
} }
Timeout(to_remove); Timeout(to_remove);

@ -1,4 +1,5 @@
using System.Collections.Generic; using System;
using System.Collections.Generic;
using System.Text; using System.Text;
using System.Threading; using System.Threading;
using ZeroLevel.Services.Serialization; using ZeroLevel.Services.Serialization;
@ -77,6 +78,48 @@ namespace ZeroLevel.Services.Trees
this._is_teminate = reader.ReadBoolean(); this._is_teminate = reader.ReadBoolean();
this._transtions = reader.ReadDictionary<char, State>(); this._transtions = reader.ReadDictionary<char, State>();
} }
public void Reverse(State head)
{
var path = new Stack<Tuple<char, bool>>();
foreach (var s in _transtions)
{
s.Value.Forward(head, s.Key, path);
}
}
private void Forward(State head, char ch, Stack<Tuple<char, bool>> path)
{
path.Push(Tuple.Create(ch, _is_teminate));
if (_is_teminate)
{
Backward(head, path);
}
foreach (var s in _transtions)
{
s.Value.Forward(head, s.Key, path);
}
path.Pop();
}
private void Backward(State head, Stack<Tuple<char, bool>> path)
{
State current = head;
foreach (var pair in path)
{
if (false == current._transtions.ContainsKey(pair.Item1))
{
var next = new State();
current._transtions.Add(pair.Item1, next);
current = next;
}
else
{
current = current._transtions[pair.Item1];
}
}
current._is_teminate = true;
}
} }
public class DSA public class DSA
@ -124,5 +167,25 @@ namespace ZeroLevel.Services.Trees
{ {
return _initialState.Iterator(new StringBuilder()); return _initialState.Iterator(new StringBuilder());
} }
public void Reverse()
{
var reverse_initial = new State();
_initialState.Reverse(reverse_initial);
_initialState = reverse_initial;
}
public void Optimize()
{
// merge
// reverse
// merge
// reverse
/*var reverse_initial = new State();
_initialState.Reverse(reverse_initial);
_initialState = new State();
reverse_initial.Reverse(_initialState);*/
}
} }
} }

Loading…
Cancel
Save

Powered by TurnKey Linux.