Refactoring

Refactoring SocketServer
Added UInt16,  UInt32, UInt64, short, ushort types to serialization
pull/1/head
unknown 5 years ago
parent c2ad1b4d09
commit ccfbe6eca6

@ -17,11 +17,12 @@ namespace TestApp
protected override void StartAction()
{
Log.Info("Started");
ReadServiceInfo();
AutoregisterInboxes(UseHost());
var host = UseHost(8800);
AutoregisterInboxes(host);
host.OnConnect += Host_OnConnect;
host.OnDisconnect += Host_OnDisconnect;
int counter = 0;
Sheduller.RemindEvery(TimeSpan.FromSeconds(1), () =>
@ -29,7 +30,10 @@ namespace TestApp
Log.Info($"RPS: {counter}");
Interlocked.Exchange(ref counter, 0);
});
while(true)
Exchange.RoutesStorage.Set("test.app", new IPEndPoint(IPAddress.Loopback, 8800));
while (true)
{
try
{
@ -40,36 +44,16 @@ namespace TestApp
Thread.Sleep(300);
}
}
}
/*
Sheduller.RemindEvery(TimeSpan.FromSeconds(1), () =>
{
var client = Exchange.GetConnection("test.app");
client.Send("pum");
client.Send<string>(BaseSocket.DEFAULT_MESSAGE_INBOX, "'This is message'");
client.Request<DateTime, string>("d2s", DateTime.Now, s => Log.Info($"Response: {s}"));
client.Request<IPEndPoint, string>(BaseSocket.DEFAULT_REQUEST_INBOX,
new IPEndPoint(NetUtils.GetNonLoopbackAddress(), NetUtils.GetFreeTcpPort()),
s => Log.Info($"Response: {s}"));
client.Request<string>("now", s => Log.Info($"Response date: {s}"));
client.Request<string>(BaseSocket.DEFAULT_REQUEST_WITHOUT_ARGS_INBOX, s => Log.Info($"Response ip: {s}"));
});
*/
/*Sheduller.RemindEvery(TimeSpan.FromSeconds(3), () =>
{
Exchange.Request<ZeroServiceInfo>("test.app", "metainfo", info =>
{
var si = new StringBuilder();
si.AppendLine(info.Name);
si.AppendLine(info.ServiceKey);
si.AppendLine(info.Version);
private void Host_OnDisconnect(ISocketClient obj)
{
Log.Info($"Client '{obj.Endpoint.Address}:{obj.Endpoint.Port}' disconnected");
}
Log.Info("Service info:\r\n{0}", si.ToString());
});
});*/
private void Host_OnConnect(ExClient obj)
{
Log.Info($"Client '{obj.Socket.Endpoint.Address}:{obj.Socket.Endpoint.Port}' connected");
}
[ExchangeReplierWithoutArg("counter")]

@ -9,7 +9,7 @@ namespace TestApp
Bootstrap.Startup<MyService>(args,
() => Configuration.ReadSetFromIniFile("config.ini"))
.EnableConsoleLog(ZeroLevel.Services.Logging.LogLevel.System | ZeroLevel.Services.Logging.LogLevel.FullDebug)
.UseDiscovery()
//.UseDiscovery()
.Run()
.WaitWhileStatus(ZeroServiceStatus.Running)
.Stop();

@ -76,9 +76,6 @@
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
<Reference Include="ZeroLevel, Version=3.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>C:\Users\a.bozhenov\Desktop\SEOPortal\Utils\Semantic\packages\ZeroLevel.3.0.0\lib\netstandard2.0\ZeroLevel.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Contracts\BaseEntity.cs" />
@ -118,6 +115,12 @@
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ZeroLevel\ZeroLevel.csproj">
<Project>{06c9e60e-d449-41a7-9bf0-a829aaf5d214}</Project>
<Name>ZeroLevel</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.

@ -67,11 +67,16 @@ namespace ZeroLevel.NetworkUnitTests
var server = UseHost(6667);
IEnumerable<ZeroServiceInfo> received = null;
server.OnConnect += c =>
{
Log.Info(c.Status.ToString());
};
server.RegisterInbox<IEnumerable<ZeroServiceInfo>>("services", (_) => new[] { info1, info2 });
// Act
var client = Exchange.GetConnection(IPAddress.Loopback.ToString() + ":6667");
var ir = client.Request<IEnumerable<ZeroServiceInfo>>("services", response =>
var ir = client.Request<IEnumerable<ZeroServiceInfo>>("services", (response) =>
{
received = response;
locker.Set();

@ -146,6 +146,37 @@ namespace ZeroLevel.Serialization
MakePrimitiveTest<Int32>(Int32.MaxValue);
}
[Fact]
public void SerializeUInt32()
{
MakePrimitiveTest<UInt32>(-0);
MakePrimitiveTest<UInt32>(0);
MakePrimitiveTest<UInt32>(10);
MakePrimitiveTest<UInt32>(UInt32.MinValue);
MakePrimitiveTest<UInt32>(UInt32.MaxValue);
}
[Fact]
public void SerializeShort()
{
MakePrimitiveTest<short>(-0);
MakePrimitiveTest<short>(0);
MakePrimitiveTest<short>(-10);
MakePrimitiveTest<short>(10);
MakePrimitiveTest<short>(short.MinValue);
MakePrimitiveTest<short>(short.MaxValue);
}
[Fact]
public void SerializeUShort()
{
MakePrimitiveTest<ushort>(-0);
MakePrimitiveTest<ushort>(0);
MakePrimitiveTest<ushort>(10);
MakePrimitiveTest<ushort>(ushort.MinValue);
MakePrimitiveTest<ushort>(ushort.MaxValue);
}
[Fact]
public void SerializeInt64()
{
@ -159,6 +190,18 @@ namespace ZeroLevel.Serialization
MakePrimitiveTest<Int64>(Int64.MaxValue / 2);
}
[Fact]
public void SerializeUInt64()
{
MakePrimitiveTest<UInt64>(-0);
MakePrimitiveTest<UInt64>(0);
MakePrimitiveTest<UInt64>(10);
MakePrimitiveTest<UInt64>(UInt64.MinValue);
MakePrimitiveTest<UInt64>(UInt64.MaxValue);
MakePrimitiveTest<UInt64>(UInt64.MinValue / 2);
MakePrimitiveTest<UInt64>(UInt64.MaxValue / 2);
}
[Fact]
public void SerializeDecimal()
{

@ -1,4 +1,6 @@
namespace ZeroLevel.Network
using System;
namespace ZeroLevel.Network
{
public interface IRouter
: IServer

@ -1,4 +1,6 @@
namespace ZeroLevel.Network
using System;
namespace ZeroLevel.Network
{
public interface IServer
{
@ -23,5 +25,8 @@
bool ContainsInbox(string inbox);
bool ContainsHandlerInbox(string inbox);
bool ContainsRequestorInbox(string inbox);
event Action<ISocketClient> OnDisconnect;
event Action<ExClient> OnConnect;
}
}

@ -1,8 +1,5 @@
using System;
using System.Collections.Generic;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using ZeroLevel.Models;
using ZeroLevel.Services.Serialization;

@ -652,17 +652,17 @@ namespace ZeroLevel.Network
#region Host service
public IRouter UseHost()
{
return _cachee.GetServer(new IPEndPoint(IPAddress.Any, NetUtils.GetFreeTcpPort()), new Router()).Router;
return _cachee.GetServer(new IPEndPoint(IPAddress.Any, NetUtils.GetFreeTcpPort()), new Router());
}
public IRouter UseHost(int port)
{
return _cachee.GetServer(new IPEndPoint(IPAddress.Any, port), new Router()).Router;
return _cachee.GetServer(new IPEndPoint(IPAddress.Any, port), new Router());
}
public IRouter UseHost(IPEndPoint endpoint)
{
return _cachee.GetServer(endpoint, new Router()).Router;
return _cachee.GetServer(endpoint, new Router());
}
#endregion

@ -7,8 +7,8 @@ using System.Threading;
namespace ZeroLevel.Network
{
public class SocketServer
: BaseSocket
internal sealed class SocketServer
: BaseSocket, IRouter
{
private Socket _serverSocket;
private ReaderWriterLockSlim _connection_set_lock = new ReaderWriterLockSlim();
@ -55,8 +55,6 @@ namespace ZeroLevel.Network
{ }
}
public IRouter Router { get { return _router; } }
public SocketServer(IPEndPoint endpoint, IRouter router)
{
_router = router;
@ -128,5 +126,23 @@ namespace ZeroLevel.Network
Log.SystemError(ex, "[SocketServer.Dispose]");
}
}
#region IRouter
public void HandleMessage(Frame frame, ISocketClient client) => _router.HandleMessage(frame, client);
public byte[] HandleRequest(Frame frame, ISocketClient client) => _router.HandleRequest(frame, client);
public IServer RegisterInbox(string inbox, MessageHandler handler) => _router.RegisterInbox(inbox, handler);
public IServer RegisterInbox(MessageHandler handler) => _router.RegisterInbox(handler);
public IServer RegisterInbox<T>(string inbox, MessageHandler<T> handler) => _router.RegisterInbox<T>(inbox, handler);
public IServer RegisterInbox<T>(MessageHandler<T> handler) => _router.RegisterInbox<T>(handler);
public IServer RegisterInbox<Tresponse>(string inbox, RequestHandler<Tresponse> handler) => _router.RegisterInbox<Tresponse>(inbox, handler);
public IServer RegisterInbox<Trequest, Tresponse>(string inbox, RequestHandler<Trequest, Tresponse> handler) => _router.RegisterInbox<Trequest, Tresponse>(inbox, handler);
public IServer RegisterInbox<Tresponse>(RequestHandler<Tresponse> handler) => _router.RegisterInbox<Tresponse>(handler);
public IServer RegisterInbox<Trequest, Tresponse>(RequestHandler<Trequest, Tresponse> handler) => _router.RegisterInbox<Trequest, Tresponse>(handler);
public bool ContainsInbox(string inbox) => _router.ContainsInbox(inbox);
public bool ContainsHandlerInbox(string inbox) => _router.ContainsHandlerInbox(inbox);
public bool ContainsRequestorInbox(string inbox) => _router.ContainsRequestorInbox(inbox);
#endregion
}
}

@ -8,9 +8,12 @@ using ZeroLevel.Services.Serialization;
namespace ZeroLevel.Network
{
public class Router
internal sealed class Router
: IRouter
{
public event Action<ISocketClient> OnDisconnect = _ => { };
public event Action<ExClient> OnConnect = _ => { };
#region Routing
private sealed class MRInvoker
@ -293,6 +296,8 @@ namespace ZeroLevel.Network
internal sealed class NullRouter
: IRouter
{
public event Action<ISocketClient> OnDisconnect = _ => { };
public event Action<ExClient> OnConnect = _ => { };
public void HandleMessage(Frame frame, ISocketClient client) { }
public byte[] HandleRequest(Frame frame, ISocketClient client) { return null; }
public IServer RegisterInbox(string inbox, MessageHandler handler) { return this; }

@ -78,6 +78,14 @@ namespace ZeroLevel.Services.Serialization
List<IPEndPoint> ReadIPEndPointCollection();
List<UInt64> ReadUInt64Collection();
List<UInt32> ReadUInt32Collection();
List<short> ReadShortCollection();
List<ushort> ReadUShortCollection();
#endregion Extensions
}
}

@ -59,6 +59,14 @@ namespace ZeroLevel.Services.Serialization
void WriteCollection(IEnumerable<Int32> collection);
void WriteCollection(IEnumerable<UInt64> collection);
void WriteCollection(IEnumerable<UInt32> collection);
void WriteCollection(IEnumerable<short> collection);
void WriteCollection(IEnumerable<ushort> collection);
void WriteCollection(IEnumerable<Double> collection);
void WriteCollection(IEnumerable<Decimal> collection);

@ -344,6 +344,62 @@ namespace ZeroLevel.Services.Serialization
return collection;
}
public List<UInt64> ReadUInt64Collection()
{
int count = ReadInt32();
var collection = new List<UInt64>(count);
if (count > 0)
{
for (int i = 0; i < count; i++)
{
collection.Add(ReadULong());
}
}
return collection;
}
public List<UInt32> ReadUInt32Collection()
{
int count = ReadInt32();
var collection = new List<UInt32>(count);
if (count > 0)
{
for (int i = 0; i < count; i++)
{
collection.Add(ReadUInt32());
}
}
return collection;
}
public List<short> ReadShortCollection()
{
int count = ReadInt32();
var collection = new List<short>(count);
if (count > 0)
{
for (int i = 0; i < count; i++)
{
collection.Add(ReadShort());
}
}
return collection;
}
public List<ushort> ReadUShortCollection()
{
int count = ReadInt32();
var collection = new List<ushort>(count);
if (count > 0)
{
for (int i = 0; i < count; i++)
{
collection.Add(ReadUShort());
}
}
return collection;
}
public List<float> ReadFloatCollection()
{
int count = ReadInt32();

@ -275,6 +275,54 @@ namespace ZeroLevel.Services.Serialization
}
}
public void WriteCollection(IEnumerable<UInt64> collection)
{
WriteInt32(collection?.Count() ?? 0);
if (collection != null)
{
foreach (var item in collection)
{
WriteULong(item);
}
}
}
public void WriteCollection(IEnumerable<UInt32> collection)
{
WriteInt32(collection?.Count() ?? 0);
if (collection != null)
{
foreach (var item in collection)
{
WriteUInt32(item);
}
}
}
public void WriteCollection(IEnumerable<short> collection)
{
WriteInt32(collection?.Count() ?? 0);
if (collection != null)
{
foreach (var item in collection)
{
WriteShort(item);
}
}
}
public void WriteCollection(IEnumerable<ushort> collection)
{
WriteInt32(collection?.Count() ?? 0);
if (collection != null)
{
foreach (var item in collection)
{
WriteUShort(item);
}
}
}
public void WriteCollection(IEnumerable<Int64> collection)
{
WriteInt32(collection?.Count() ?? 0);

@ -53,9 +53,13 @@ namespace ZeroLevel.Services.Serialization
_cachee.Add(typeof(Byte), Create<Byte>());
_cachee.Add(typeof(Byte[]), Create<Byte[]>());
_cachee.Add(typeof(Int32), Create<Int32>());
_cachee.Add(typeof(UInt32), Create<UInt32>());
_cachee.Add(typeof(Int64), Create<Int64>());
_cachee.Add(typeof(UInt64), Create<UInt64>());
_cachee.Add(typeof(Double), Create<Double>());
_cachee.Add(typeof(float), Create<float>());
_cachee.Add(typeof(short), Create<short>());
_cachee.Add(typeof(ushort), Create<ushort>());
_cachee.Add(typeof(Decimal), Create<Decimal>());
_cachee.Add(typeof(DateTime), Create<DateTime>());
_cachee.Add(typeof(Guid), Create<Guid>());
@ -68,9 +72,13 @@ namespace ZeroLevel.Services.Serialization
_cachee.Add(typeof(IEnumerable<Byte>), Create<IEnumerable<Byte>>());
_cachee.Add(typeof(IEnumerable<Byte[]>), Create<IEnumerable<Byte[]>>());
_cachee.Add(typeof(IEnumerable<Int32>), Create<IEnumerable<Int32>>());
_cachee.Add(typeof(IEnumerable<UInt32>), Create<IEnumerable<UInt32>>());
_cachee.Add(typeof(IEnumerable<Int64>), Create<IEnumerable<Int64>>());
_cachee.Add(typeof(IEnumerable<UInt64>), Create<IEnumerable<UInt64>>());
_cachee.Add(typeof(IEnumerable<Double>), Create<IEnumerable<Double>>());
_cachee.Add(typeof(IEnumerable<float>), Create<IEnumerable<float>>());
_cachee.Add(typeof(IEnumerable<short>), Create<IEnumerable<short>>());
_cachee.Add(typeof(IEnumerable<ushort>), Create<IEnumerable<ushort>>());
_cachee.Add(typeof(IEnumerable<Decimal>), Create<IEnumerable<Decimal>>());
_cachee.Add(typeof(IEnumerable<DateTime>), Create<IEnumerable<DateTime>>());
_cachee.Add(typeof(IEnumerable<Guid>), Create<IEnumerable<Guid>>());
@ -83,9 +91,13 @@ namespace ZeroLevel.Services.Serialization
_enumTypesCachee.Add(typeof(Byte), typeof(IEnumerable<Byte>));
_enumTypesCachee.Add(typeof(Byte[]), typeof(IEnumerable<Byte[]>));
_enumTypesCachee.Add(typeof(Int32), typeof(IEnumerable<Int32>));
_enumTypesCachee.Add(typeof(UInt32), typeof(IEnumerable<UInt32>));
_enumTypesCachee.Add(typeof(Int64), typeof(IEnumerable<Int64>));
_enumTypesCachee.Add(typeof(UInt64), typeof(IEnumerable<UInt64>));
_enumTypesCachee.Add(typeof(Double), typeof(IEnumerable<Double>));
_enumTypesCachee.Add(typeof(float), typeof(IEnumerable<float>));
_enumTypesCachee.Add(typeof(short), typeof(IEnumerable<short>));
_enumTypesCachee.Add(typeof(ushort), typeof(IEnumerable<ushort>));
_enumTypesCachee.Add(typeof(Decimal), typeof(IEnumerable<Decimal>));
_enumTypesCachee.Add(typeof(DateTime), typeof(IEnumerable<DateTime>));
_enumTypesCachee.Add(typeof(Guid), typeof(IEnumerable<Guid>));
@ -104,6 +116,11 @@ namespace ZeroLevel.Services.Serialization
wrapper.ReadId = wrapper.Invoker.Configure(typeof(MemoryStreamReader), "ReadInt32").First();
wrapper.WriteId = wrapper.Invoker.Configure(typeof(MemoryStreamWriter), "WriteInt32").First();
}
else if (type == typeof(UInt32))
{
wrapper.ReadId = wrapper.Invoker.Configure(typeof(MemoryStreamReader), "ReadUInt32").First();
wrapper.WriteId = wrapper.Invoker.Configure(typeof(MemoryStreamWriter), "WriteUInt32").First();
}
else if (type == typeof(Boolean))
{
wrapper.ReadId = wrapper.Invoker.Configure(typeof(MemoryStreamReader), "ReadBoolean").First();
@ -139,6 +156,16 @@ namespace ZeroLevel.Services.Serialization
wrapper.ReadId = wrapper.Invoker.Configure(typeof(MemoryStreamReader), "ReadFloat").First();
wrapper.WriteId = wrapper.Invoker.Configure(typeof(MemoryStreamWriter), "WriteFloat").First();
}
else if (type == typeof(Int16))
{
wrapper.ReadId = wrapper.Invoker.Configure(typeof(MemoryStreamReader), "ReadShort").First();
wrapper.WriteId = wrapper.Invoker.Configure(typeof(MemoryStreamWriter), "WriteShort").First();
}
else if (type == typeof(UInt16))
{
wrapper.ReadId = wrapper.Invoker.Configure(typeof(MemoryStreamReader), "ReadUShort").First();
wrapper.WriteId = wrapper.Invoker.Configure(typeof(MemoryStreamWriter), "WriteUShort").First();
}
else if (type == typeof(Guid))
{
wrapper.ReadId = wrapper.Invoker.Configure(typeof(MemoryStreamReader), "ReadGuid").First();
@ -159,6 +186,11 @@ namespace ZeroLevel.Services.Serialization
wrapper.ReadId = wrapper.Invoker.Configure(typeof(MemoryStreamReader), "ReadLong").First();
wrapper.WriteId = wrapper.Invoker.Configure(typeof(MemoryStreamWriter), "WriteLong").First();
}
else if (type == typeof(UInt64))
{
wrapper.ReadId = wrapper.Invoker.Configure(typeof(MemoryStreamReader), "ReadULong").First();
wrapper.WriteId = wrapper.Invoker.Configure(typeof(MemoryStreamWriter), "WriteULong").First();
}
else if (type == typeof(String))
{
wrapper.ReadId = wrapper.Invoker.Configure(typeof(MemoryStreamReader), "ReadString").First();
@ -177,6 +209,11 @@ namespace ZeroLevel.Services.Serialization
wrapper.ReadId = wrapper.Invoker.Configure(typeof(MemoryStreamReader), "ReadInt32Collection").First();
wrapper.WriteId = wrapper.Invoker.Configure(typeof(MemoryStreamWriter), CreatePredicate<Tw>()).First();
}
else if (type == typeof(IEnumerable<UInt32>))
{
wrapper.ReadId = wrapper.Invoker.Configure(typeof(MemoryStreamReader), "ReadUInt32Collection").First();
wrapper.WriteId = wrapper.Invoker.Configure(typeof(MemoryStreamWriter), CreatePredicate<Tw>()).First();
}
else if (type == typeof(IEnumerable<Boolean>))
{
wrapper.ReadId = wrapper.Invoker.Configure(typeof(MemoryStreamReader), "ReadBooleanCollection").First();
@ -227,6 +264,21 @@ namespace ZeroLevel.Services.Serialization
wrapper.ReadId = wrapper.Invoker.Configure(typeof(MemoryStreamReader), "ReadInt64Collection").First();
wrapper.WriteId = wrapper.Invoker.Configure(typeof(MemoryStreamWriter), CreatePredicate<Tw>()).First();
}
else if (type == typeof(IEnumerable<UInt64>))
{
wrapper.ReadId = wrapper.Invoker.Configure(typeof(MemoryStreamReader), "ReadUInt64Collection").First();
wrapper.WriteId = wrapper.Invoker.Configure(typeof(MemoryStreamWriter), CreatePredicate<Tw>()).First();
}
else if (type == typeof(IEnumerable<Int16>))
{
wrapper.ReadId = wrapper.Invoker.Configure(typeof(MemoryStreamReader), "ReadShortCollection").First();
wrapper.WriteId = wrapper.Invoker.Configure(typeof(MemoryStreamWriter), CreatePredicate<Tw>()).First();
}
else if (type == typeof(IEnumerable<UInt16>))
{
wrapper.ReadId = wrapper.Invoker.Configure(typeof(MemoryStreamReader), "ReadUShortCollection").First();
wrapper.WriteId = wrapper.Invoker.Configure(typeof(MemoryStreamWriter), CreatePredicate<Tw>()).First();
}
else if (type == typeof(IEnumerable<String>))
{
wrapper.ReadId = wrapper.Invoker.Configure(typeof(MemoryStreamReader), "ReadStringCollection").First();

Loading…
Cancel
Save

Powered by TurnKey Linux.