Refactoring

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

@ -22,7 +22,7 @@ namespace ZeroLevel.Network
{ {
try try
{ {
_client.Send(Frame.FromPool(inbox)); _client.Send(FrameFactory.Create(inbox));
return true; return true;
} }
catch (Exception ex) catch (Exception ex)
@ -36,7 +36,7 @@ namespace ZeroLevel.Network
{ {
try try
{ {
_client.Send(Frame.FromPool(inbox, data)); _client.Send(FrameFactory.Create(inbox, data));
return true; return true;
} }
catch (Exception ex) catch (Exception ex)
@ -50,7 +50,7 @@ namespace ZeroLevel.Network
{ {
try try
{ {
_client.Send(Frame.FromPool(BaseSocket.DEFAULT_MESSAGE_INBOX, MessageSerializer.SerializeCompatible<T>(message))); _client.Send(FrameFactory.Create(BaseSocket.DEFAULT_MESSAGE_INBOX, MessageSerializer.SerializeCompatible<T>(message)));
return true; return true;
} }
catch (Exception ex) catch (Exception ex)
@ -64,7 +64,7 @@ namespace ZeroLevel.Network
{ {
try try
{ {
_client.Send(Frame.FromPool(inbox, MessageSerializer.SerializeCompatible<T>(message))); _client.Send(FrameFactory.Create(inbox, MessageSerializer.SerializeCompatible<T>(message)));
return true; return true;
} }
catch (Exception ex) catch (Exception ex)
@ -78,7 +78,7 @@ namespace ZeroLevel.Network
{ {
try try
{ {
_client.Request(Frame.FromPool(inbox), f => callback(f)); _client.Request(FrameFactory.Create(inbox), f => callback(f));
return true; return true;
} }
catch (Exception ex) catch (Exception ex)
@ -92,7 +92,7 @@ namespace ZeroLevel.Network
{ {
try try
{ {
_client.Request(Frame.FromPool(inbox, data), f => callback(f)); _client.Request(FrameFactory.Create(inbox, data), f => callback(f));
return true; return true;
} }
catch (Exception ex) catch (Exception ex)
@ -106,7 +106,7 @@ namespace ZeroLevel.Network
{ {
try try
{ {
_client.Request(Frame.FromPool(inbox), f => callback(MessageSerializer.DeserializeCompatible<Tresponse>(f))); _client.Request(FrameFactory.Create(inbox), f => callback(MessageSerializer.DeserializeCompatible<Tresponse>(f)));
return true; return true;
} }
catch (Exception ex) catch (Exception ex)
@ -120,7 +120,7 @@ namespace ZeroLevel.Network
{ {
try try
{ {
_client.Request(Frame.FromPool(BaseSocket.DEFAULT_REQUEST_INBOX), f => callback(MessageSerializer.DeserializeCompatible<Tresponse>(f))); _client.Request(FrameFactory.Create(BaseSocket.DEFAULT_REQUEST_INBOX), f => callback(MessageSerializer.DeserializeCompatible<Tresponse>(f)));
return true; return true;
} }
catch (Exception ex) catch (Exception ex)
@ -134,7 +134,7 @@ namespace ZeroLevel.Network
{ {
try try
{ {
_client.Request(Frame.FromPool(inbox, MessageSerializer.SerializeCompatible<Trequest>(request)), _client.Request(FrameFactory.Create(inbox, MessageSerializer.SerializeCompatible<Trequest>(request)),
f => callback(MessageSerializer.DeserializeCompatible<Tresponse>(f))); f => callback(MessageSerializer.DeserializeCompatible<Tresponse>(f)));
return true; return true;
} }
@ -149,7 +149,7 @@ namespace ZeroLevel.Network
{ {
try try
{ {
_client.Request(Frame.FromPool(BaseSocket.DEFAULT_REQUEST_WITHOUT_ARGS_INBOX, MessageSerializer.SerializeCompatible<Trequest>(request)), _client.Request(FrameFactory.Create(BaseSocket.DEFAULT_REQUEST_WITHOUT_ARGS_INBOX, MessageSerializer.SerializeCompatible<Trequest>(request)),
f => callback(MessageSerializer.DeserializeCompatible<Tresponse>(f))); f => callback(MessageSerializer.DeserializeCompatible<Tresponse>(f)));
return true; return true;
} }

@ -1,31 +0,0 @@
using ZeroLevel.Services.Serialization;
namespace ZeroLevel.Services.Network
{
public class LongRequest<T>
: IBinarySerializable
{
public LongRequest() { }
public LongRequest<T> Create(T value, string inbox) => new LongRequest<T>
{
Body = value,
Inbox = inbox
};
public T Body { get; set; }
public string Inbox { get; set; }
public void Deserialize(IBinaryReader reader)
{
this.Inbox = reader.ReadString();
this.Body = reader.ReadCompatible<T>();
}
public void Serialize(IBinaryWriter writer)
{
writer.WriteString(this.Inbox);
writer.WriteCompatible<T>(this.Body);
}
}
}

@ -7,12 +7,12 @@ using ZeroLevel.Services.Serialization;
namespace ZeroLevel.Network namespace ZeroLevel.Network
{ {
/*
[Serializable] [Serializable]
[DataContract] [DataContract]
public sealed class Frame : public sealed class Frame :
IEquatable<Frame>, IEquatable<Frame>,
IBinarySerializable, IBinarySerializable
ICloneable
{ {
private static ObjectPool<Frame> _pool = new ObjectPool<Frame>(() => new Frame(), 256); private static ObjectPool<Frame> _pool = new ObjectPool<Frame>(() => new Frame(), 256);
@ -51,21 +51,10 @@ namespace ZeroLevel.Network
[DataMember] [DataMember]
public byte[] Payload { get; set; } public byte[] Payload { get; set; }
public bool IsRequest { get; set; }
public Frame() public Frame()
{ {
} }
public Frame(Frame other)
{
var data = MessageSerializer.Serialize(other);
using (var reader = new MemoryStreamReader(data))
{
Deserialize(reader);
}
}
public void Deserialize(IBinaryReader reader) public void Deserialize(IBinaryReader reader)
{ {
this.Inbox = reader.ReadString(); this.Inbox = reader.ReadString();
@ -120,8 +109,6 @@ namespace ZeroLevel.Network
if (other == null) return false; if (other == null) return false;
if (ReferenceEquals(this, other)) if (ReferenceEquals(this, other))
return true; return true;
if (this.GetType() != other.GetType())
return false;
if (string.Compare(this.Inbox, other.Inbox, true) != 0) return false; if (string.Compare(this.Inbox, other.Inbox, true) != 0) return false;
if (ArrayExtensions.UnsafeEquals(this.Payload, other.Payload) == false) return false; if (ArrayExtensions.UnsafeEquals(this.Payload, other.Payload) == false) return false;
return true; return true;
@ -131,10 +118,101 @@ namespace ZeroLevel.Network
{ {
return base.GetHashCode(); return base.GetHashCode();
} }
}
*/
public struct Frame :
IBinarySerializable
{
public string Inbox { get; set; }
public byte[] Payload { get; set; }
public void Deserialize(IBinaryReader reader)
{
this.Inbox = reader.ReadString();
this.Payload = reader.ReadBytes();
}
public void Serialize(IBinaryWriter writer)
{
writer.WriteString(this.Inbox);
writer.WriteBytes(this.Payload);
}
public T Read<T>()
{
if (this.Payload == null || this.Payload.Length == 0) return default(T);
return MessageSerializer.DeserializeCompatible<T>(this.Payload);
}
public IEnumerable<T> ReadCollection<T>() where T : IBinarySerializable
{
return MessageSerializer.DeserializeCollection<T>(this.Payload);
}
public object Clone() public string ReadText()
{ {
return new Frame(this); if (this.Payload == null || this.Payload.Length == 0) return null;
return Encoding.UTF32.GetString(this.Payload);
}
public void Write<T>(T data) where T : IBinarySerializable
{
this.Payload = MessageSerializer.Serialize<T>(data);
}
public void Write<T>(IEnumerable<T> items) where T : IBinarySerializable
{
this.Payload = MessageSerializer.Serialize<T>(items);
}
public void Write(string data)
{
this.Payload = Encoding.UTF32.GetBytes(data);
}
public bool Equals(Frame other)
{
if (string.Compare(this.Inbox, other.Inbox, true) != 0) return false;
if (ArrayExtensions.UnsafeEquals(this.Payload, other.Payload) == false) return false;
return true;
}
public override int GetHashCode()
{
return base.GetHashCode();
} }
} }
public static class FrameFactory
{
public static Frame Create()
{
return new Frame
{
Inbox = null,
Payload = null
};
}
public static Frame Create(string inbox)
{
return new Frame
{
Inbox = inbox,
Payload = null
};
}
public static Frame Create(string inbox, byte[] payload)
{
return new Frame
{
Inbox = inbox,
Payload = payload
};
}
}
} }

@ -138,10 +138,8 @@ namespace ZeroLevel.Network
public void Request(Frame frame, Action<byte[]> callback, Action<string> fail = null) public void Request(Frame frame, Action<byte[]> callback, Action<string> fail = null)
{ {
if (frame == null) throw new ArgumentNullException(nameof(frame));
if (Status != SocketClientStatus.Working) throw new Exception($"[SocketClient.Request] Socket status: {Status}"); if (Status != SocketClientStatus.Working) throw new Exception($"[SocketClient.Request] Socket status: {Status}");
var data = NetworkPacketFactory.Reqeust(MessageSerializer.Serialize(frame), out int id); var data = NetworkPacketFactory.Reqeust(MessageSerializer.Serialize(frame), out int id);
frame.Release();
if (!_send_queue.IsAddingCompleted) if (!_send_queue.IsAddingCompleted)
{ {
@ -160,10 +158,8 @@ namespace ZeroLevel.Network
public void Send(Frame frame) public void Send(Frame frame)
{ {
if (frame == null) throw new ArgumentNullException(nameof(frame));
if (Status != SocketClientStatus.Working) throw new Exception($"[SocketClient.Send] Socket status: {Status}"); if (Status != SocketClientStatus.Working) throw new Exception($"[SocketClient.Send] Socket status: {Status}");
var data = NetworkPacketFactory.Message(MessageSerializer.Serialize(frame)); var data = NetworkPacketFactory.Message(MessageSerializer.Serialize(frame));
frame.Release();
if (!_send_queue.IsAddingCompleted) if (!_send_queue.IsAddingCompleted)
{ {

@ -58,28 +58,4 @@ namespace ZeroLevel.Services.Semantic.Model
return '\0'; return '\0';
} }
} }
public class Node
{
private byte Sym;
private Node NextSibling;
private Node NextChild;
public void Append(string line, int position)
{
}
}
public class Tree
{
private Node _root;
public void Append(string word)
{
_root.Append(word, 0);
}
}
} }

@ -1,69 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using ZeroLevel.Services.Serialization;
namespace ZeroLevel.Services.Shedulling
{
public interface ITaskDb
{
long GetLastCounter();
void Store(TaskRecord task);
}
public class TaskRecord
{
public long Identity;
public string TypeName;
public string MethodName;
public IBinarySerializable Parameter;
public TaskGroup Parent;
}
public class TaskGroup
{
public long Identity;
public IEnumerable<TaskRecord> Tasks;
public TaskPipeline Parent;
}
public class TaskPipeline
{
public long Identity;
IEnumerable<TaskGroup> Pipeline;
public TaskQueue Parent;
}
public class TaskQueue
{
public long Identity;
}
public class TaskDispatcher
{
ISheduller sheduller;
public TaskDispatcher(ITaskDb db)
{
sheduller.SetInitialIndex(db.GetLastCounter());
}
private MethodInfo FindMethod(TaskRecord task)
{
var type = Type.GetType(task.TypeName);
var method = type.GetMethods().First(m => m.Name.Equals(task.MethodName, StringComparison.Ordinal));
return method;
}
public static void Add<T>(Expression<Action<T>> methodCall)
{
}
}
}
Loading…
Cancel
Save

Powered by TurnKey Linux.