<commit_before>﻿using System;
using System.Collections;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using S7.Net.Interfaces;
using Double = System.Double;

namespace S7.Net
{
    public class Plc : IPlc
    {
        private Socket _mSocket; //TCP connection to device

        public string IP { get; set; }
        public CpuType CPU { get; set; }
        public Int16 Rack { get; set; }
        public Int16 Slot { get; set; }
        public string Name { get; set; }
        public object Tag { get; set; }
        public bool IsAvailable
        {
            get
            {
                Ping ping = new Ping();
                PingReply result = ping.Send(IP);
                return result != null && result.Status == IPStatus.Success;
            }
        }
        public bool IsConnected
        {
            get
            {
                if (_mSocket != null)
                {
                    return _mSocket.Connected;
                }
                else
                {
                    return false;
                }
            }
        }
        public string LastErrorString { get; private set; }
        public ErrorCode LastErrorCode { get; private set; }

        public Plc() : this(CpuType.S7400, "localhost", 0, 2) { }

        public Plc(CpuType cpu, string ip, Int16 rack, Int16 slot, string name = "", object tag = null)
        {<commit_msg>Improved IsConnected and IsAvailable as shown in Issue #8.<commit_after>﻿using System;
using System.Collections;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using S7.Net.Interfaces;
using Double = System.Double;

namespace S7.Net
{
    public class Plc : IPlc
    {
        private Socket _mSocket; //TCP connection to device

        public string IP { get; set; }
        public CpuType CPU { get; set; }
        public Int16 Rack { get; set; }
        public Int16 Slot { get; set; }
        public string Name { get; set; }
        public object Tag { get; set; }

        /// <summary>
        /// Pings the IP address and returns true if the result of the ping is Success.
        /// </summary>
        public bool IsAvailable
        {
            get
            {
                Ping ping = new Ping();
                PingReply result = ping.Send(IP);
                return result != null && result.Status == IPStatus.Success;
            }
        }

        /// <summary>
        /// Checks if the socket is connected and polls the other peer (the plc) to see if it's connected.
        /// This is the variable that you should continously check to see if the communication is working
        /// See also: http://stackoverflow.com/questions/2661764/how-to-check-if-a-socket-is-connected-disconnected-in-c
        /// </summary>
        public bool IsConnected
        {
            get
            {
                try
                {
                    if (_mSocket == null)
                        return false;
                    return !((_mSocket.Poll(1000, SelectMode.SelectRead) && (_mSocket.Available == 0)) || !_mSocket.Connected);
                }
                catch { return false; }
            }
        }
        public string LastErrorString { get; private set; }
        public ErrorCode LastErrorCode { get; private set; }

        public Plc() : this(CpuType.S7400, "localhost", 0, 2) { }

        public Plc(CpuType cpu, string ip, Int16 rack, Int16 slot, string name = "", object tag = null)
        {
{'fraction_non_alphanumeric': 0.09217638691322902, 'fraction_numerical': 0.012802275960170697, 'mean_word_length': 1.5645514223194747, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7338937', 'n_tokens_mistral': 992, 'n_tokens_neox': 931, 'n_words': 307}