/// <summary> /// Tyr to disconnect and then connect again /// </summary> private void Reconnect() { Logger.WriteDebug($"{decoderName}: reconnect -> start"); //Set status decoderStatus = ConnectionStatus.Reconnecting; //Raise event Connecting.RaiseEvent(this, null); //Stop timeouts receiveTimeout.Stop(); sendTimeout.Stop(); connectTimeout.Stop(); if (reconnectAttempts == 0) { //Start reconnect immediately reconnectTimer_Elapsed(this, null); } else { //Start reconnect after timer elapsed reconnectTimer.Start(); } //Increase counter reconnectAttempts++; }
private void StartListening() { if (decoderStatus == ConnectionStatus.Listening || decoderStatus == ConnectionStatus.ClientConnected) { return; } Logger.WriteDebug($"{decoderName}: Start to listening"); decoderStatus = ConnectionStatus.Listening; //Raise event Connecting.RaiseEvent(this, null); // Bind the socket to the local endpoint and listen for incoming connections var _localEP = new IPEndPoint(decoderIpAddress, decoderPort); listenerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); try { listenerSocket.Bind(_localEP); listenerSocket.Listen(1); // Start an asynchronous socket to listen for connections. Logger.WriteDebug($"{decoderName}: Waiting for client"); listenerSocket.BeginAccept(AcceptCallback, listenerSocket); } catch (Exception ex) { Logger.WriteDebug($"{decoderName}: {ex} -> {ex.Message}"); } }
/// <summary> /// Tyr to disconnect and then connect again /// </summary> private void Restart() { Logger.WriteDebug($"{decoderName}: Auto-Restart start"); decoderStatus = ConnectionStatus.Restart; //Raise event Connecting.RaiseEvent(this, null); //Start reconnect after timer elapsed restartTimer.Interval = restartAttempts == 0 ? 1000 : restartAttempts * 60000; restartTimer.Start(); }
public void Start() { try { Logger.WriteDebug(MethodBase.GetCurrentMethod(), "Starting"); var stopWatch = new Stopwatch(); stopWatch.Start(); IsRunning = false; if (_decoderPort <= 0 || _decoderPort >= 65535) { Logger.WriteDebug($"{_decoderName}: Wrong setting -> decoderPort"); Disconnected.RaiseEvent(this, null); return; } //Raise event Connecting.RaiseEvent(this, null); if (TryStartListener() == false) { Logger.WriteDebug($"{_decoderName}: try to make url reservation"); NetAclChecker.AddAddress($"http://+:{_decoderPort}/"); if (TryStartListener() == false) { Logger.WriteDebug($"{_decoderName}: unable to start listener"); return; } } //Raise event Connected.RaiseEvent(this, null); IsRunning = true; stopWatch.Stop(); Logger.WriteDebug(MethodBase.GetCurrentMethod(), $"Started -> {stopWatch.Elapsed}"); } catch (Exception ex) { Logger.WriteDebug($"{_decoderName}: {ex} -> {ex.Message}"); } }
/// <summary> /// Try connecting to the remote host /// </summary> private void Connect() { Logger.WriteDebug($"{decoderName}: connect -> start"); //Set status decoderStatus = ConnectionStatus.Connecting; //Raise event Connecting.RaiseEvent(this, null); try { //Release decoder if (decoderSocket != null) { decoderSocket.Close(); decoderSocket = null; } //Parse server host/ip Logger.WriteDebug($"{decoderName}: connect -> server({decoderServer})"); if (Regex.IsMatch(decoderServer, @"^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$") ) { // the string is a IP decoderIpAddress = IPAddress.Parse(decoderServer); } else if (Regex.IsMatch(decoderServer, @"^(([a-zA-Z]|[a-zA-Z][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z]|[A-Za-z][A-Za-z0-9\-]*[A-Za-z0-9])$")) { // the string is a host -> try to resolve var _ipHostInfo = Dns.GetHostEntry(decoderServer); foreach (var ipAdress in _ipHostInfo.AddressList) { if (Regex.IsMatch(ipAdress.ToString(), @"^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$") ) { decoderIpAddress = ipAdress; break; } } //If resolve fail -> abort if (decoderIpAddress == null) { Logger.WriteDebug($"{decoderName}: cannot resolve hostname {decoderServer}"); Disconnected.RaiseEvent(this, null); return; } } Logger.WriteDebug($"{decoderName}: connect -> ip({decoderIpAddress})"); //Create a TCP/IP socket var _remoteEP = new IPEndPoint(decoderIpAddress, decoderPort); decoderSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //Start timer for timeout connectTimeout.Start(); //Connect to the remote endpoint decoderSocket.BeginConnect(_remoteEP, ConnectCallback, decoderSocket); } catch (SocketException ex) { Logger.WriteDebug($"{decoderName}: connect -> {ex.Message}"); //Start reconnect Reconnect(); } catch (Exception ex) { Logger.WriteDebug($"{decoderName}: {ex} -> {ex.Message}"); } }