|
using System.Net.Sockets;
using System.Threading;
using Hik.Communication.Scs.Communication.EndPoints.Tcp;
namespace Hik.Communication.Scs.Communication.Channels.Tcp
{
/// <summary>
/// This class is used to listen and accept incoming TCP
/// connection requests on a TCP port.
/// </summary>
internal class TcpConnectionListener : ConnectionListenerBase
{
/// <summary>
/// The endpoint address of the server to listen incoming connections.
/// </summary>
private readonly ScsTcpEndPoint _endPoint;
/// <summary>
/// Server socket to listen incoming connection requests.
/// </summary>
private TcpListener _listenerSocket;
/// <summary>
/// The thread to listen socket
/// </summary>
private Thread _thread;
/// <summary>
/// A flag to control thread's running
/// </summary>
private volatile bool _running;
/// <summary>
/// Creates a new TcpConnectionListener for given endpoint.
/// </summary>
/// <param name="endPoint">The endpoint address of the server to listen incoming connections</param>
public TcpConnectionListener(ScsTcpEndPoint endPoint)
{
_endPoint = endPoint;
}
/// <summary>
/// Starts listening incoming connections.
/// </summary>
public override void Start()
{
StartSocket();
_running = true;
_thread = new Thread(DoListenAsThread);
_thread.Start();
}
/// <summary>
/// Stops listening incoming connections.
/// </summary>
public override void Stop()
{
_running = false;
StopSocket();
}
/// <summary>
/// Starts listening socket.
/// </summary>
private void StartSocket()
{
_listenerSocket = new TcpListener(System.Net.IPAddress.Any, _endPoint.TcpPort);
_listenerSocket.Start();
}
/// <summary>
/// Stops listening socket.
/// </summary>
private void StopSocket()
{
try
{
_listenerSocket.Stop();
}
catch
{
}
}
/// <summary>
/// Entrance point of the thread.
/// This method is used by the thread to listen incoming requests.
/// </summary>
private void DoListenAsThread()
{
while (_running)
{
try
{
var clientSocket = _listenerSocket.AcceptSocket();
if (clientSocket.Connected)
{
OnCommunicationChannelConnected(new TcpCommunicationChannel(clientSocket));
}
}
catch
{
//Disconnect, wait for a while and connect again.
StopSocket();
Thread.Sleep(1000);
if (!_running)
{
return;
}
try
{
StartSocket();
}
catch
{
}
}
}
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I have started programming at 14 years old using Pascal as hobby. Then I interested in web development (HTML, JavaScript, ASP...) before university.
I graduated from Sakarya University Computer Engineering. At university, I learned C++, Visual Basic.NET, C#, ASP.NET and Java. I partly implemented ARP, IP and TCP protocols in Java as my final term project.
Now, I am working on Windows and web based software development mostly using Microsoft technologies in my own company.
My open source projects:
* ABP Framework:
https://abp.io
* jTable:
http://jtable.org
* Others:
https://github.com/hikalkan
My personal web site:
https://halilibrahimkalkan.com