diff --git a/ZeroLevel.EventServer/ZeroLevel.EventServer.csproj b/ZeroLevel.EventServer/ZeroLevel.EventServer.csproj index c57316f..e3deba8 100644 --- a/ZeroLevel.EventServer/ZeroLevel.EventServer.csproj +++ b/ZeroLevel.EventServer/ZeroLevel.EventServer.csproj @@ -1,4 +1,4 @@ - + Exe @@ -6,7 +6,7 @@ - + diff --git a/ZeroLevel.SQL/ZeroLevel.SQL.csproj b/ZeroLevel.SQL/ZeroLevel.SQL.csproj index 6948e18..c6b6cda 100644 --- a/ZeroLevel.SQL/ZeroLevel.SQL.csproj +++ b/ZeroLevel.SQL/ZeroLevel.SQL.csproj @@ -19,7 +19,7 @@ - + diff --git a/ZeroLevel.SqLite/ZeroLevel.SqLite.csproj b/ZeroLevel.SqLite/ZeroLevel.SqLite.csproj index 1093592..4a55243 100644 --- a/ZeroLevel.SqLite/ZeroLevel.SqLite.csproj +++ b/ZeroLevel.SqLite/ZeroLevel.SqLite.csproj @@ -1,4 +1,4 @@ - + netstandard2.0 @@ -14,8 +14,8 @@ Based on System.Data.SQLite.Core - - + + diff --git a/ZeroLevel.UnitTests/SerializationTests.cs b/ZeroLevel.UnitTests/SerializationTests.cs index 4a0326f..c7d5e98 100644 --- a/ZeroLevel.UnitTests/SerializationTests.cs +++ b/ZeroLevel.UnitTests/SerializationTests.cs @@ -122,6 +122,7 @@ namespace ZeroLevel.Serialization public void SerializeIPAddress() { var comparator = new Func((left, right) => NetUtils.Compare(left, right) == 0); + MakePrimitiveTest(null, comparator); MakePrimitiveTest(IPAddress.Any, comparator); MakePrimitiveTest(IPAddress.Broadcast, comparator); MakePrimitiveTest(IPAddress.IPv6Any, comparator); @@ -136,6 +137,7 @@ namespace ZeroLevel.Serialization public void SerializeIPEndPoint() { var comparator = new Func((left, right) => NetUtils.Compare(left, right) == 0); + MakePrimitiveTest(null, comparator); MakePrimitiveTest(new IPEndPoint(IPAddress.Any, 1), comparator); MakePrimitiveTest(new IPEndPoint(IPAddress.Broadcast, 600), comparator); MakePrimitiveTest(new IPEndPoint(IPAddress.IPv6Any, IPEndPoint.MaxPort), comparator); diff --git a/ZeroLevel.UnitTests/ZeroLevel.UnitTests.csproj b/ZeroLevel.UnitTests/ZeroLevel.UnitTests.csproj index bff09b3..2efb601 100644 --- a/ZeroLevel.UnitTests/ZeroLevel.UnitTests.csproj +++ b/ZeroLevel.UnitTests/ZeroLevel.UnitTests.csproj @@ -7,7 +7,7 @@ - + all diff --git a/ZeroLevel/Services/Network/Contracts/IServiceRoutesStorage.cs b/ZeroLevel/Services/Network/Contracts/IServiceRoutesStorage.cs index fba6050..5fc4a9e 100644 --- a/ZeroLevel/Services/Network/Contracts/IServiceRoutesStorage.cs +++ b/ZeroLevel/Services/Network/Contracts/IServiceRoutesStorage.cs @@ -13,6 +13,11 @@ namespace ZeroLevel.Network void Set(string key, string type, string group, IPEndPoint endpoint); void Set(string key, string type, string group, IEnumerable endpoints); + bool ContainsKey(string key); + bool ContainsType(string type); + bool ContainsGroup(string group); + + void Remove(string key); void Remove(IPEndPoint endpoint); IEnumerable> GetAll(); diff --git a/ZeroLevel/Services/Network/SocketClient.cs b/ZeroLevel/Services/Network/SocketClient.cs index f61e907..7ee39be 100644 --- a/ZeroLevel/Services/Network/SocketClient.cs +++ b/ZeroLevel/Services/Network/SocketClient.cs @@ -69,7 +69,7 @@ namespace ZeroLevel.Network } catch (Exception ex) { - Log.SystemError(ex, $"[SocketClient.ctor] connection fault. Endpoint: {Endpoint.Address}:{Endpoint.Port}"); + Log.SystemError(ex, $"[SocketClient.ctor] connection fault. Endpoint: {ep.Address}:{ep.Port}"); Broken(); return; } diff --git a/ZeroLevel/Services/Network/Utils/NetUtils.cs b/ZeroLevel/Services/Network/Utils/NetUtils.cs index a7066ce..88fea84 100644 --- a/ZeroLevel/Services/Network/Utils/NetUtils.cs +++ b/ZeroLevel/Services/Network/Utils/NetUtils.cs @@ -36,6 +36,7 @@ namespace ZeroLevel.Network public static bool TestConnection(IPEndPoint endpoint, int timeout = 100) { + if (endpoint == null) return false; using (var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) { socket.SetIPProtectionLevel(IPProtectionLevel.Unrestricted); @@ -72,6 +73,10 @@ namespace ZeroLevel.Network public static int Compare(this IPEndPoint x, IPEndPoint y) { + if (x == null && y == null) return 0; + if (x == null) return 1; + if (y == null) return -1; + var xx = x.Address.ToString(); var yy = y.Address.ToString(); var result = string.CompareOrdinal(xx, yy); @@ -80,6 +85,10 @@ namespace ZeroLevel.Network public static int Compare(this IPAddress x, IPAddress y) { + if (x == null && y == null) return 0; + if (x == null) return 1; + if (y == null) return -1; + var xx = x.ToString(); var yy = y.ToString(); return string.CompareOrdinal(xx, yy); @@ -87,6 +96,8 @@ namespace ZeroLevel.Network public static IPEndPoint CreateIPEndPoint(string endPoint) { + if (string.IsNullOrWhiteSpace(endPoint)) return null; + string[] ep = endPoint.Split(':'); if (ep.Length < 2) throw new FormatException("Invalid endpoint format"); IPAddress ip; diff --git a/ZeroLevel/Services/Network/Utils/ServiceRouteStorage.cs b/ZeroLevel/Services/Network/Utils/ServiceRouteStorage.cs index 7b2365c..c0f3c4e 100644 --- a/ZeroLevel/Services/Network/Utils/ServiceRouteStorage.cs +++ b/ZeroLevel/Services/Network/Utils/ServiceRouteStorage.cs @@ -228,6 +228,24 @@ namespace ZeroLevel.Network } } + public bool ContainsKey(string key) => _tableByKey.ContainsKey(key); + + public bool ContainsType(string type) => _tableByTypes.ContainsKey(type); + + public bool ContainsGroup(string group) => _tableByGroups.ContainsKey(group); + + public void Remove(string key) + { + if (_tableByKey.ContainsKey(key)) + { + var eps = _tableByKey[key].Source.ToList(); + foreach (var ep in eps) + { + RemoveLocked(ep); + } + } + } + public void Remove(IPEndPoint endpoint) { _lock.EnterWriteLock(); diff --git a/ZeroLevel/Services/Serialization/MemoryStreamReader.cs b/ZeroLevel/Services/Serialization/MemoryStreamReader.cs index aaf6a56..bcc6742 100644 --- a/ZeroLevel/Services/Serialization/MemoryStreamReader.cs +++ b/ZeroLevel/Services/Serialization/MemoryStreamReader.cs @@ -203,15 +203,25 @@ namespace ZeroLevel.Services.Serialization public IPAddress ReadIP() { - var addr = ReadBytes(); - return new IPAddress(addr); + var exists = ReadByte(); + if (exists == 1) + { + var addr = ReadBytes(); + return new IPAddress(addr); + } + return null; } public IPEndPoint ReadIPEndpoint() { - var addr = ReadIP(); - var port = ReadInt32(); - return new IPEndPoint(addr, port); + var exists = ReadByte(); + if (exists == 1) + { + var addr = ReadIP(); + var port = ReadInt32(); + return new IPEndPoint(addr, port); + } + return null; } /// diff --git a/ZeroLevel/Services/Serialization/MemoryStreamWriter.cs b/ZeroLevel/Services/Serialization/MemoryStreamWriter.cs index 1884220..cb75469 100644 --- a/ZeroLevel/Services/Serialization/MemoryStreamWriter.cs +++ b/ZeroLevel/Services/Serialization/MemoryStreamWriter.cs @@ -182,13 +182,29 @@ namespace ZeroLevel.Services.Serialization public void WriteIP(IPAddress ip) { - WriteBytes(ip.GetAddressBytes()); + if (ip == null) + { + WriteByte(0); + } + else + { + WriteByte(1); + WriteBytes(ip.GetAddressBytes()); + } } public void WriteIPEndpoint(IPEndPoint endpoint) { - WriteIP(endpoint.Address); - WriteInt32(endpoint.Port); + if (endpoint == null) + { + WriteByte(0); + } + else + { + WriteByte(1); + WriteIP(endpoint.Address); + WriteInt32(endpoint.Port); + } } public byte[] Complete() diff --git a/ZeroLevel/ZeroLevel.csproj b/ZeroLevel/ZeroLevel.csproj index 872d9d4..61cd51d 100644 --- a/ZeroLevel/ZeroLevel.csproj +++ b/ZeroLevel/ZeroLevel.csproj @@ -37,11 +37,11 @@ - - - + + + - +