Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: hikalkan/scs
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v1.1.1.0
Choose a base ref
...
head repository: hikalkan/scs
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
  • 12 commits
  • 78 files changed
  • 4 contributors

Commits on Sep 28, 2016

  1. Added nuget packaging.

    hikalkan committed Sep 28, 2016
    Copy the full SHA
    8f192c2 View commit details
  2. gitignore change

    hikalkan committed Sep 28, 2016
    Copy the full SHA
    02d753f View commit details

Commits on Dec 11, 2017

  1. Test with Trace.WriteLine

    rolphes committed Dec 11, 2017
    Copy the full SHA
    0c7c69f View commit details

Commits on Dec 12, 2017

  1. Write all exceptions to System.Diagnostics.Trace.Write()

    Handle exceptions during deserialization to avoid disconnect - reconnect cycle
    rolphes committed Dec 12, 2017
    Copy the full SHA
    e982356 View commit details
  2. Merge pull request #28 from rolphes/master

    Write all exceptions to System.Diagnostics.Trace
    hikalkan authored Dec 12, 2017
    Copy the full SHA
    130492a View commit details

Commits on Dec 13, 2017

  1. reset the received memory stream before SerializationException is thr…

    …own - otherwise the same erroneous message is received all over agin
    rolphes committed Dec 13, 2017
    Copy the full SHA
    786c18f View commit details
  2. Merge pull request #29 from rolphes/master

    reset the received memory stream before SerializationException
    hikalkan authored Dec 13, 2017
    Copy the full SHA
    fd048db View commit details

Commits on Mar 8, 2019

  1. Copy the full SHA
    cc516ef View commit details
  2. 缓存代理对象

    NeedJustWord committed Mar 8, 2019
    Copy the full SHA
    b3959f4 View commit details

Commits on Apr 1, 2019

  1. 这个状态检查会导致本该释放的socket连接不能释放,不能通知上层代码连接已经断开,从而导致上层(Hik.Communication.Sc…

    …sServices.Service.ScsServiceApplication的_serviceClients)一直缓存着已经断开连接的客户端,最后耗光内存。
    
    此问题是我在配合consul做服务发现时发现的,consul的tcp健康检查会不断的连接断开,在ReceiveCallback函数报异常去执行Disconnect函数,但是Disconnect函数因为条件判断为true直接return了
    NeedJustWord committed Apr 1, 2019
    Copy the full SHA
    8bf6fa8 View commit details

Commits on Feb 13, 2021

  1. Merge pull request #39 from NeedJustWord/master

    将SCS升级到.net standard 2.0
    hikalkan authored Feb 13, 2021
    Copy the full SHA
    c37ca99 View commit details
  2. Update README.md

    hikalkan authored Feb 13, 2021
    Copy the full SHA
    f47e58b View commit details
Showing with 5,617 additions and 55 deletions.
  1. +7 −0 .gitignore
  2. BIN nuget/nuget.exe
  3. +1 −0 nuget/pack.bat
  4. +197 −0 src/Scs.Core/Collections/ThreadSafeSortedList.cs
  5. +110 −0 src/Scs.Core/Communication/Scs/Client/ClientReConnecter.cs
  6. +43 −0 src/Scs.Core/Communication/Scs/Client/IConnectableClient.cs
  7. +13 −0 src/Scs.Core/Communication/Scs/Client/IScsClient.cs
  8. +330 −0 src/Scs.Core/Communication/Scs/Client/ScsClientBase.cs
  9. +30 −0 src/Scs.Core/Communication/Scs/Client/ScsClientFactory.cs
  10. +61 −0 src/Scs.Core/Communication/Scs/Client/Tcp/ScsTcpClient.cs
  11. +49 −0 src/Scs.Core/Communication/Scs/Client/Tcp/TcpHelper.cs
  12. +176 −0 src/Scs.Core/Communication/Scs/Communication/Channels/CommunicationChannelBase.cs
  13. +24 −0 src/Scs.Core/Communication/Scs/Communication/Channels/CommunicationChannelEventArgs.cs
  14. +38 −0 src/Scs.Core/Communication/Scs/Communication/Channels/ConnectionListenerBase.cs
  15. +38 −0 src/Scs.Core/Communication/Scs/Communication/Channels/ICommunicationChannel.cs
  16. +26 −0 src/Scs.Core/Communication/Scs/Communication/Channels/IConnectionListener.cs
  17. +214 −0 src/Scs.Core/Communication/Scs/Communication/Channels/Tcp/TcpCommunicationChannel.cs
  18. +125 −0 src/Scs.Core/Communication/Scs/Communication/Channels/Tcp/TcpConnectionListener.cs
  19. +50 −0 src/Scs.Core/Communication/Scs/Communication/CommunicationException.cs
  20. +50 −0 src/Scs.Core/Communication/Scs/Communication/CommunicationStateException.cs
  21. +18 −0 src/Scs.Core/Communication/Scs/Communication/CommunicationStates.cs
  22. +67 −0 src/Scs.Core/Communication/Scs/Communication/EndPoints/ScsEndPoint.cs
  23. +84 −0 src/Scs.Core/Communication/Scs/Communication/EndPoints/Tcp/ScsTcpEndPoint.cs
  24. +18 −0 src/Scs.Core/Communication/Scs/Communication/Messages/IScsMessage.cs
  25. +24 −0 src/Scs.Core/Communication/Scs/Communication/Messages/MessageEventArgs.cs
  26. +44 −0 src/Scs.Core/Communication/Scs/Communication/Messages/PingMessage.cs
  27. +59 −0 src/Scs.Core/Communication/Scs/Communication/Messages/ScsMessage.cs
  28. +59 −0 src/Scs.Core/Communication/Scs/Communication/Messages/ScsRawDataMessage.cs
  29. +58 −0 src/Scs.Core/Communication/Scs/Communication/Messages/ScsTextMessage.cs
  30. +44 −0 src/Scs.Core/Communication/Scs/Communication/Messengers/IMessenger.cs
  31. +387 −0 src/Scs.Core/Communication/Scs/Communication/Messengers/RequestReplyMessenger.cs
  32. +216 −0 src/Scs.Core/Communication/Scs/Communication/Messengers/SynchronizedMessenger.cs
  33. +324 −0 ...Core/Communication/Scs/Communication/Protocols/BinarySerialization/BinarySerializationProtocol.cs
  34. +17 −0 ...mmunication/Scs/Communication/Protocols/BinarySerialization/BinarySerializationProtocolFactory.cs
  35. +39 −0 src/Scs.Core/Communication/Scs/Communication/Protocols/IScsWireProtocol.cs
  36. +14 −0 src/Scs.Core/Communication/Scs/Communication/Protocols/IScsWireProtocolFactory.cs
  37. +28 −0 src/Scs.Core/Communication/Scs/Communication/Protocols/WireProtocolManager.cs
  38. +42 −0 src/Scs.Core/Communication/Scs/Server/IScsServer.cs
  39. +38 −0 src/Scs.Core/Communication/Scs/Server/IScsServerClient.cs
  40. +168 −0 src/Scs.Core/Communication/Scs/Server/ScsServerBase.cs
  41. +223 −0 src/Scs.Core/Communication/Scs/Server/ScsServerClient.cs
  42. +20 −0 src/Scs.Core/Communication/Scs/Server/ScsServerFactory.cs
  43. +24 −0 src/Scs.Core/Communication/Scs/Server/ScsServerManager.cs
  44. +24 −0 src/Scs.Core/Communication/Scs/Server/ServerClientEventArgs.cs
  45. +35 −0 src/Scs.Core/Communication/Scs/Server/Tcp/ScsTcpServer.cs
  46. +24 −0 src/Scs.Core/Communication/ScsServices/Client/IScsServiceClient.cs
  47. +276 −0 src/Scs.Core/Communication/ScsServices/Client/ScsServiceClient.cs
  48. +36 −0 src/Scs.Core/Communication/ScsServices/Client/ScsServiceClientBuilder.cs
  49. +83 −0 src/Scs.Core/Communication/ScsServices/Communication/AutoConnectRemoteInvokeProxy.cs
  50. +51 −0 src/Scs.Core/Communication/ScsServices/Communication/Messages/ScsRemoteException.cs
  51. +36 −0 src/Scs.Core/Communication/ScsServices/Communication/Messages/ScsRemoteInvokeMessage.cs
  52. +33 −0 src/Scs.Core/Communication/ScsServices/Communication/Messages/ScsRemoteInvokeReturnMessage.cs
  53. +92 −0 src/Scs.Core/Communication/ScsServices/Communication/RemoteInvokeProxy.cs
  54. +49 −0 src/Scs.Core/Communication/ScsServices/Service/IScsServiceApplication.cs
  55. +44 −0 src/Scs.Core/Communication/ScsServices/Service/IScsServiceClient.cs
  56. +43 −0 src/Scs.Core/Communication/ScsServices/Service/ScsService.cs
  57. +370 −0 src/Scs.Core/Communication/ScsServices/Service/ScsServiceApplication.cs
  58. +26 −0 src/Scs.Core/Communication/ScsServices/Service/ScsServiceAttribute.cs
  59. +21 −0 src/Scs.Core/Communication/ScsServices/Service/ScsServiceBuilder.cs
  60. +147 −0 src/Scs.Core/Communication/ScsServices/Service/ScsServiceClient.cs
  61. +23 −0 src/Scs.Core/Communication/ScsServices/Service/ScsServiceClientFactory.cs
  62. +24 −0 src/Scs.Core/Communication/ScsServices/Service/ServiceClientEventArgs.cs
  63. +33 −0 src/Scs.Core/Proxy/DispatchProxyDelegate.cs
  64. +9 −0 src/Scs.Core/Proxy/IInterceptor.cs
  65. +101 −0 src/Scs.Core/Proxy/ProxyGenerator.cs
  66. +12 −0 src/Scs.Core/Scs.Core.csproj
  67. +172 −0 src/Scs.Core/Threading/SequentialItemProcessor.cs
  68. +167 −0 src/Scs.Core/Threading/Timer.cs
  69. +21 −2 src/Scs.sln
  70. +2 −1 src/Scs/Communication/Scs/Client/ClientReConnecter.cs
  71. +4 −4 src/Scs/Communication/Scs/Client/ScsClientBase.cs
  72. +20 −16 src/Scs/Communication/Scs/Communication/Channels/Tcp/TcpCommunicationChannel.cs
  73. +6 −5 src/Scs/Communication/Scs/Communication/Channels/Tcp/TcpConnectionListener.cs
  74. +16 −7 src/Scs/Communication/Scs/Communication/Protocols/BinarySerialization/BinarySerializationProtocol.cs
  75. +4 −4 src/Scs/Communication/ScsServices/Client/ScsServiceClient.cs
  76. +12 −12 src/Scs/Communication/ScsServices/Service/ScsServiceApplication.cs
  77. +2 −2 src/Scs/Properties/AssemblyInfo.cs
  78. +2 −2 src/Scs/Threading/SequentialItemProcessor.cs
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -44,6 +44,9 @@ ipch/
*.opensdf
*.sdf

# Visual Studio 2015 cache/options directory
.vs/

# Visual Studio profiler
*.psess
*.vsp
@@ -106,3 +109,7 @@ Generated_Code #added for RIA/Silverlight projects
_UpgradeReport_Files/
Backup*/
UpgradeLog*.XML

!nuget.exe
nuget/push.bat
nuget/*.nupkg
Binary file added nuget/nuget.exe
Binary file not shown.
1 change: 1 addition & 0 deletions nuget/pack.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"NuGet.exe" "pack" "..\src\Scs\Scs.csproj" -Properties Configuration=Release -IncludeReferencedProjects -Symbols
197 changes: 197 additions & 0 deletions src/Scs.Core/Collections/ThreadSafeSortedList.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
using System.Collections.Generic;
using System.Threading;

namespace Hik.Collections
{
/// <summary>
/// This class is used to store key-value based items in a thread safe manner.
/// It uses System.Collections.Generic.SortedList internally.
/// </summary>
/// <typeparam name="TK">Key type</typeparam>
/// <typeparam name="TV">Value type</typeparam>
public class ThreadSafeSortedList<TK, TV>
{
/// <summary>
/// Gets/adds/replaces an item by key.
/// </summary>
/// <param name="key">Key to get/set value</param>
/// <returns>Item associated with this key</returns>
public TV this[TK key]
{
get
{
_lock.EnterReadLock();
try
{
return _items.ContainsKey(key) ? _items[key] : default(TV);
}
finally
{
_lock.ExitReadLock();
}
}

set
{
_lock.EnterWriteLock();
try
{
_items[key] = value;
}
finally
{
_lock.ExitWriteLock();
}
}
}

/// <summary>
/// Gets count of items in the collection.
/// </summary>
public int Count
{
get
{
_lock.EnterReadLock();
try
{
return _items.Count;
}
finally
{
_lock.ExitReadLock();
}
}
}

/// <summary>
/// Internal collection to store items.
/// </summary>
protected readonly SortedList<TK, TV> _items;

/// <summary>
/// Used to synchronize access to _items list.
/// </summary>
protected readonly ReaderWriterLockSlim _lock;

/// <summary>
/// Creates a new ThreadSafeSortedList object.
/// </summary>
public ThreadSafeSortedList()
{
_items = new SortedList<TK, TV>();
_lock = new ReaderWriterLockSlim(LockRecursionPolicy.NoRecursion);
}

/// <summary>
/// Checks if collection contains spesified key.
/// </summary>
/// <param name="key">Key to check</param>
/// <returns>True; if collection contains given key</returns>
public bool ContainsKey(TK key)
{
_lock.EnterReadLock();
try
{
return _items.ContainsKey(key);
}
finally
{
_lock.ExitReadLock();
}
}

/// <summary>
/// Checks if collection contains spesified item.
/// </summary>
/// <param name="item">Item to check</param>
/// <returns>True; if collection contains given item</returns>
public bool ContainsValue(TV item)
{
_lock.EnterReadLock();
try
{
return _items.ContainsValue(item);
}
finally
{
_lock.ExitReadLock();
}
}

/// <summary>
/// Removes an item from collection.
/// </summary>
/// <param name="key">Key of item to remove</param>
public bool Remove(TK key)
{
_lock.EnterWriteLock();
try
{
if (!_items.ContainsKey(key))
{
return false;
}

_items.Remove(key);
return true;
}
finally
{
_lock.ExitWriteLock();
}
}

/// <summary>
/// Gets all items in collection.
/// </summary>
/// <returns>Item list</returns>
public List<TV> GetAllItems()
{
_lock.EnterReadLock();
try
{
return new List<TV>(_items.Values);
}
finally
{
_lock.ExitReadLock();
}
}

/// <summary>
/// Removes all items from list.
/// </summary>
public void ClearAll()
{
_lock.EnterWriteLock();
try
{
_items.Clear();
}
finally
{
_lock.ExitWriteLock();
}
}

/// <summary>
/// Gets then removes all items in collection.
/// </summary>
/// <returns>Item list</returns>
public List<TV> GetAndClearAllItems()
{
_lock.EnterWriteLock();
try
{
var list = new List<TV>(_items.Values);
_items.Clear();
return list;
}
finally
{
_lock.ExitWriteLock();
}
}
}
}
110 changes: 110 additions & 0 deletions src/Scs.Core/Communication/Scs/Client/ClientReConnecter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
using System;
using Hik.Communication.Scs.Communication;
using Hik.Threading;

namespace Hik.Communication.Scs.Client
{
/// <summary>
/// This class is used to automatically re-connect to server if disconnected.
/// It attempts to reconnect to server periodically until connection established.
/// </summary>
public class ClientReConnecter : IDisposable
{
/// <summary>
/// Reconnect check period.
/// Default: 20 seconds.
/// </summary>
public int ReConnectCheckPeriod
{
get { return _reconnectTimer.Period; }
set { _reconnectTimer.Period = value; }
}

/// <summary>
/// Reference to client object.
/// </summary>
private readonly IConnectableClient _client;

/// <summary>
/// Timer to attempt ro reconnect periodically.
/// </summary>
private readonly Timer _reconnectTimer;

/// <summary>
/// Indicates the dispose state of this object.
/// </summary>
private volatile bool _disposed;

/// <summary>
/// Creates a new ClientReConnecter object.
/// It is not needed to start ClientReConnecter since it automatically
/// starts when the client disconnected.
/// </summary>
/// <param name="client">Reference to client object</param>
/// <exception cref="ArgumentNullException">Throws ArgumentNullException if client is null.</exception>
public ClientReConnecter(IConnectableClient client)
{
if (client == null)
{
throw new ArgumentNullException("client");
}

_client = client;
_client.Disconnected += Client_Disconnected;
_reconnectTimer = new Timer(20000);
_reconnectTimer.Elapsed += ReconnectTimer_Elapsed;
_reconnectTimer.Start();
}

/// <summary>
/// Disposes this object.
/// Does nothing if already disposed.
/// </summary>
public void Dispose()
{
if (_disposed)
{
return;
}

_disposed = true;
_client.Disconnected -= Client_Disconnected;
_reconnectTimer.Stop();
}

/// <summary>
/// Handles Disconnected event of _client object.
/// </summary>
/// <param name="sender">Source of the event</param>
/// <param name="e">Event arguments</param>
private void Client_Disconnected(object sender, EventArgs e)
{
_reconnectTimer.Start();
}

/// <summary>
/// Hadles Elapsed event of _reconnectTimer.
/// </summary>
/// <param name="sender">Source of the event</param>
/// <param name="e">Event arguments</param>
private void ReconnectTimer_Elapsed(object sender, EventArgs e)
{
if (_disposed || _client.CommunicationState == CommunicationStates.Connected)
{
_reconnectTimer.Stop();
return;
}

try
{
_client.Connect();
_reconnectTimer.Stop();
}
catch (Exception exception)
{
//No need to catch since it will try to re-connect again
System.Diagnostics.Trace.Write($"ReconnectTimer_Elapsed: {exception}");
}
}
}
}
43 changes: 43 additions & 0 deletions src/Scs.Core/Communication/Scs/Client/IConnectableClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using Hik.Communication.Scs.Communication;

namespace Hik.Communication.Scs.Client
{
/// <summary>
/// Represents a client for SCS servers.
/// </summary>
public interface IConnectableClient : IDisposable
{
/// <summary>
/// This event is raised when client connected to server.
/// </summary>
event EventHandler Connected;

/// <summary>
/// This event is raised when client disconnected from server.
/// </summary>
event EventHandler Disconnected;

/// <summary>
/// Timeout for connecting to a server (as milliseconds).
/// Default value: 15 seconds (15000 ms).
/// </summary>
int ConnectTimeout { get; set; }

/// <summary>
/// Gets the current communication state.
/// </summary>
CommunicationStates CommunicationState { get; }

/// <summary>
/// Connects to server.
/// </summary>
void Connect();

/// <summary>
/// Disconnects from server.
/// Does nothing if already disconnected.
/// </summary>
void Disconnect();
}
}
13 changes: 13 additions & 0 deletions src/Scs.Core/Communication/Scs/Client/IScsClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Hik.Communication.Scs.Communication;
using Hik.Communication.Scs.Communication.Messengers;

namespace Hik.Communication.Scs.Client
{
/// <summary>
/// Represents a client to connect to server.
/// </summary>
public interface IScsClient : IMessenger, IConnectableClient
{
//Does not define any additional member
}
}
Loading