63 lines
1.5 KiB
C#
63 lines
1.5 KiB
C#
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<object, EventArgs> OnClosed;
|
|
public bool IsConnected { get { return EasyClient.IsConnected; } }
|
|
|
|
public TcpClient(string ip, int port, Action<TransInfo> onDataGeted, Action<object, EventArgs> onConnected, Action<object, EventArgs> onClosed, Action<object, EventArgs> 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<ErrorEventArgs>(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();
|
|
}
|
|
}
|
|
}
|