using Getf.Service.Transfer.Client.WinService.Entities; using SuperSocket.ClientEngine; using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Text; using System.Threading; namespace Getf.Service.Transfer.Client.WinService { public class TcpClient : IDisposable { //private Thread SendHeartThread; string Ip { get; set; } int Port { get; set; } EasyClient EasyClient; bool IsHeartLosted = false; Action OnClosed; public bool IsConnected { get { return EasyClient.IsConnected; } } public TcpClient(string ip, int port, Action onDataGeted, Action onConnected, Action onClosed, Action onError) { Ip = ip; Port = port; EasyClient = new EasyClient(); EasyClient.Initialize(new ReceiveFilter(), onDataGeted); EasyClient.Connected += new EventHandler(onConnected); EasyClient.Closed += EasyClient_Closed; EasyClient.Error += new EventHandler(onError); EasyClient.ConnectAsync(new IPEndPoint(IPAddress.Parse(Ip), Port)); OnClosed = onClosed; //SendHeartThread = new Thread(); } public void Send(byte[] data) { EasyClient.Send(data); } private void EasyClient_Closed(object sender, EventArgs e) { if (IsHeartLosted) return; OnClosed?.Invoke(sender, e); } public void HeartLost() { //IsHeartLosted = true; EasyClient.Close(); } public void Dispose() { HeartLost(); //SendHeartThread.Abort(); } } }