Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5,401 changes: 2,704 additions & 2,697 deletions binaries/Scs.XML

Large diffs are not rendered by default.

Binary file modified binaries/Scs.dll
Binary file not shown.
13 changes: 10 additions & 3 deletions src/Scs/Communication/Scs/Client/IScsClient.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Hik.Communication.Scs.Communication;
using Hik.Communication.Scs.Communication;
using Hik.Communication.Scs.Communication.Channels;
using Hik.Communication.Scs.Communication.Messengers;

namespace Hik.Communication.Scs.Client
Expand All @@ -7,7 +8,13 @@ namespace Hik.Communication.Scs.Client
/// Represents a client to connect to server.
/// </summary>
public interface IScsClient : IMessenger, IConnectableClient
{
//Does not define any additional member
{
/// <summary>
/// Gets the communication channel for this client.
/// </summary>
/// <value>
/// The communication channel.
/// </value>
ICommunicationChannel CommunicationChannel { get; }
}
}
9 changes: 8 additions & 1 deletion src/Scs/Communication/Scs/Client/ScsClientBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,13 @@ protected virtual void OnMessageSent(IScsMessage message)
}
}

#endregion
#endregion

#region Implementation of IScsClient

/// <inheritdoc />
public ICommunicationChannel CommunicationChannel { get { return _communicationChannel; } }

#endregion
}
}
54 changes: 35 additions & 19 deletions src/Scs/Communication/Scs/Client/Tcp/ScsTcpClient.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Hik.Communication.Scs.Communication.Channels;
using System.Net.Sockets;
using Hik.Communication.Scs.Communication.Channels;
using Hik.Communication.Scs.Communication.Channels.Tcp;
using Hik.Communication.Scs.Communication.EndPoints.Tcp;
using System.Net;
Expand All @@ -13,28 +14,43 @@ internal class ScsTcpClient : ScsClientBase
/// <summary>
/// The endpoint address of the server.
/// </summary>
private readonly ScsTcpEndPoint _serverEndPoint;

/// <summary>
/// Creates a new ScsTcpClient object.
/// </summary>
/// <param name="serverEndPoint">The endpoint address to connect to the server</param>
public ScsTcpClient(ScsTcpEndPoint serverEndPoint)
{
_serverEndPoint = serverEndPoint;
}

private readonly ScsTcpEndPoint _serverEndPoint;

/// <summary>
/// The existing socket information or <c>null</c>.
/// </summary>
private SocketInformation? _existingSocketInformation;

/// <summary>
/// Creates a new ScsTcpClient object.
/// </summary>
/// <param name="serverEndPoint">The endpoint address to connect to the server</param>
/// <param name="existingSocketInformation">The existing socket information.</param>
public ScsTcpClient(ScsTcpEndPoint serverEndPoint, SocketInformation? existingSocketInformation)
{
_serverEndPoint = serverEndPoint;
_existingSocketInformation = existingSocketInformation;
}

/// <summary>
/// Creates a communication channel using ServerIpAddress and ServerPort.
/// </summary>
/// <returns>Ready communication channel to communicate</returns>
protected override ICommunicationChannel CreateCommunicationChannel()
{
return new TcpCommunicationChannel(
TcpHelper.ConnectToServer(
new IPEndPoint(IPAddress.Parse(_serverEndPoint.IpAddress), _serverEndPoint.TcpPort),
ConnectTimeout
));
protected override ICommunicationChannel CreateCommunicationChannel()
{
Socket socket;

if (_existingSocketInformation.HasValue)
{
socket = new Socket(_existingSocketInformation.Value);
_existingSocketInformation = null;
}
else
{
socket = TcpHelper.ConnectToServer( new IPEndPoint(_serverEndPoint.IpAddress, _serverEndPoint.TcpPort), ConnectTimeout);
}

return new TcpCommunicationChannel(socket);
}
}
}
1 change: 1 addition & 0 deletions src/Scs/Communication/Scs/Client/Tcp/TcpHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public static Socket ConnectToServer(EndPoint endPoint, int timeoutMs)
var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
socket.UseOnlyOverlappedIO = true;
socket.Blocking = false;
socket.Connect(endPoint);
socket.Blocking = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Hik.Communication.Scs.Communication.Channels
/// <summary>
/// This class provides base functionality for all communication channel classes.
/// </summary>
internal abstract class CommunicationChannelBase : ICommunicationChannel
public abstract class CommunicationChannelBase : ICommunicationChannel
{
#region Public events

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Hik.Communication.Scs.Communication.Channels
/// Represents a communication channel.
/// A communication channel is used to communicate (send/receive messages) with a remote application.
/// </summary>
internal interface ICommunicationChannel : IMessenger
public interface ICommunicationChannel : IMessenger
{
/// <summary>
/// This event is raised when client disconnected from server.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using System;
using System;
using System.Collections;
using System.Data;
using System.Net;
using System.Net.Sockets;
using System.Net.Sockets;
using System.Threading;
using Hik.Communication.Scs.Communication.EndPoints;
using Hik.Communication.Scs.Communication.EndPoints.Tcp;
using Hik.Communication.Scs.Communication.Messages;
Expand All @@ -10,10 +13,17 @@ namespace Hik.Communication.Scs.Communication.Channels.Tcp
/// <summary>
/// This class is used to communicate with a remote application over TCP/IP protocol.
/// </summary>
internal class TcpCommunicationChannel : CommunicationChannelBase
{
#region Public properties

public class TcpCommunicationChannel : CommunicationChannelBase
{
#region Constants

private const ushort PING_REQUEST = 0x0779;
private const ushort PING_RESPONSE = 0x0988;

#endregion

#region Public properties

///<summary>
/// Gets the endpoint of remote application.
///</summary>
Expand Down Expand Up @@ -78,7 +88,25 @@ public TcpCommunicationChannel(Socket clientSocket)

#endregion

#region Public methods
#region Public methods

/// <summary>
/// Duplicates the client socket and closes.
/// </summary>
/// <param name="processId">The process identifier.</param>
/// <returns></returns>
/// <summary>The callee should dispose anything relying on this channel immediately.</summary>
public SocketInformation DuplicateSocketAndClose(int processId)
{
// request ping from host to kill our async BeginReceive
_clientSocket.Send(BitConverter.GetBytes(PING_REQUEST));

// wait for response
while (_running) Thread.Sleep(20);

// finally
return _clientSocket.DuplicateAndClose(processId);
}

/// <summary>
/// Disconnects from remote application and closes channel.
Expand Down Expand Up @@ -172,7 +200,23 @@ private void ReceiveCallback(IAsyncResult ar)
//Get received bytes count
var bytesRead = _clientSocket.EndReceive(ar);
if (bytesRead > 0)
{
{
// handle special packets
if (bytesRead == 2)
{
switch (BitConverter.ToUInt16(_buffer, 0))
{
case PING_REQUEST:
_clientSocket.Send(BitConverter.GetBytes(PING_RESPONSE));
goto CONT_RECEIVE;

case PING_RESPONSE:
// instigated by DuplicateSocketAndClose
_running = false;
return;
}
}

LastReceivedMessageTime = DateTime.Now;

//Copy received bytes to a new byte array
Expand All @@ -193,6 +237,7 @@ private void ReceiveCallback(IAsyncResult ar)
throw new CommunicationException("Tcp socket is closed");
}

CONT_RECEIVE:
//Read more bytes if still running
if (_running)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Net.Sockets;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Hik.Communication.Scs.Communication.EndPoints.Tcp;

Expand Down Expand Up @@ -64,7 +65,7 @@ public override void Stop()
/// </summary>
private void StartSocket()
{
_listenerSocket = new TcpListener(System.Net.IPAddress.Any, _endPoint.TcpPort);
_listenerSocket = new TcpListener(_endPoint.IpAddress ?? IPAddress.Any, _endPoint.TcpPort);
_listenerSocket.Start();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System;
using System.Net;
using System.Net.Sockets;
using Hik.Communication.Scs.Client;
using Hik.Communication.Scs.Client.Tcp;
using Hik.Communication.Scs.Server;
Expand All @@ -7,14 +9,20 @@
namespace Hik.Communication.Scs.Communication.EndPoints.Tcp
{
/// <summary>
/// Represens a TCP end point in SCS.
/// Represents a TCP end point in SCS.
/// </summary>
public sealed class ScsTcpEndPoint : ScsEndPoint
{
public sealed class ScsTcpEndPoint : ScsEndPoint
{
#region Private fields

private SocketInformation? _existingSocketInformation;

#endregion

///<summary>
/// IP address of the server.
///</summary>
public string IpAddress { get; set; }
public IPAddress IpAddress { get; set; }

///<summary>
/// Listening TCP Port for incoming connection requests on server.
Expand All @@ -28,18 +36,30 @@ public sealed class ScsTcpEndPoint : ScsEndPoint
public ScsTcpEndPoint(int tcpPort)
{
TcpPort = tcpPort;
}

/// <summary>
/// Creates a new ScsTcpEndPoint object with specified IP address and port number.
/// </summary>
/// <param name="ipAddress">IP address of the server</param>
/// <param name="port">Listening TCP Port for incoming connection requests on server</param>
public ScsTcpEndPoint(string ipAddress, int port)
{
IpAddress = ipAddress;
TcpPort = port;
}
}

/// <summary>
/// Creates a new ScsTcpEndPoint object with specified IP address and port number.
/// </summary>
/// <param name="ipAddress">IP address of the server</param>
/// <param name="port">Listening TCP Port for incoming connection requests on server</param>
/// <param name="socketInformation">The existing socket information.</param>
public ScsTcpEndPoint(string ipAddress, int port, SocketInformation? socketInformation = null)
: this(IPAddress.Parse(ipAddress), port, socketInformation)
{ }

/// <summary>
/// Creates a new ScsTcpEndPoint object with specified IP address and port number.
/// </summary>
/// <param name="ipAddress">IP address of the server</param>
/// <param name="port">Listening TCP Port for incoming connection requests on server</param>
/// <param name="socketInformation">The existing socket information.</param>
public ScsTcpEndPoint(IPAddress ipAddress, int port, SocketInformation? socketInformation = null)
{
IpAddress = ipAddress;
TcpPort = port;
_existingSocketInformation = socketInformation;
}

/// <summary>
/// Creates a new ScsTcpEndPoint from a string address.
Expand All @@ -50,7 +70,7 @@ public ScsTcpEndPoint(string ipAddress, int port)
public ScsTcpEndPoint(string address)
{
var splittedAddress = address.Trim().Split(':');
IpAddress = splittedAddress[0].Trim();
IpAddress = IPAddress.Parse(splittedAddress[0].Trim());
TcpPort = Convert.ToInt32(splittedAddress[1].Trim());
}

Expand All @@ -67,9 +87,11 @@ internal override IScsServer CreateServer()
/// Creates a Scs Client that uses this end point to connect to server.
/// </summary>
/// <returns>Scs Client</returns>
internal override IScsClient CreateClient()
{
return new ScsTcpClient(this);
internal override IScsClient CreateClient()
{
var client = new ScsTcpClient(this, _existingSocketInformation);
_existingSocketInformation = null;
return client;
}

/// <summary>
Expand All @@ -78,7 +100,7 @@ internal override IScsClient CreateClient()
/// <returns>String representation of this end point object</returns>
public override string ToString()
{
return string.IsNullOrEmpty(IpAddress) ? ("tcp://" + TcpPort) : ("tcp://" + IpAddress + ":" + TcpPort);
return IpAddress == null ? ("tcp://" + TcpPort) : ("tcp://" + IpAddress + ":" + TcpPort);
}
}
}
21 changes: 17 additions & 4 deletions src/Scs/Communication/ScsServices/Client/IScsServiceClient.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
using Hik.Communication.Scs.Client;

using Hik.Communication.Scs.Client;
using Hik.Communication.Scs.Communication.Channels;

namespace Hik.Communication.ScsServices.Client
{
/// <summary>
/// Represents a service client that consumes a SCS service.
/// </summary>
/// <typeparam name="T">Type of service interface</typeparam>
public interface IScsServiceClient<out T> : IConnectableClient where T : class
{
{
/// <summary>
/// Gets the communication channel for client.
/// </summary>
ICommunicationChannel CommunicationChannel { get; }

/// <summary>
/// Reference to the service proxy to invoke remote service methods.
/// </summary>
Expand All @@ -19,6 +25,13 @@ public interface IScsServiceClient<out T> : IConnectableClient where T : class
/// Use -1 for no timeout (wait indefinite).
/// Default value: 60000 (1 minute).
/// </summary>
int Timeout { get; set; }
int Timeout { get; set; }

/// <summary>
/// Gets a service proxy for the specified <typeparamref name="TServiceInterface" />.
/// </summary>
/// <typeparam name="TServiceInterface">the service interface type</typeparam>
/// <returns></returns>
TServiceInterface GetServiceProxy<TServiceInterface>();
}
}
Loading