init commit all code
This commit is contained in:
@@ -0,0 +1,207 @@
|
||||
using Getf.Service.Transfer.Client.WinService.Helpers;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
|
||||
namespace Getf.Service.Transfer.Client.WinService
|
||||
{
|
||||
public class TcpClientService : IDisposable
|
||||
{
|
||||
private Socket _SocketClient;
|
||||
private readonly string _IP;
|
||||
private readonly int _Port;
|
||||
|
||||
public event Action<TcpClientService> OnError;
|
||||
|
||||
public event Action<TcpClientService> OnConnected;
|
||||
|
||||
public event Action<TcpClientService, byte[]> OnDataGeted;
|
||||
|
||||
private byte[] _Buffer;
|
||||
|
||||
public bool IsConnected = false;
|
||||
|
||||
bool IsDisposed = false;
|
||||
LogHelper _LogHelper;
|
||||
|
||||
public TcpClientService(string ip, int port)
|
||||
{
|
||||
_IP = ip;
|
||||
_Port = port;
|
||||
_LogHelper = new LogHelper();
|
||||
_Buffer = new byte[100 * 1024 * 1024];
|
||||
}
|
||||
|
||||
public void Connect()
|
||||
{
|
||||
if (_SocketClient != null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_SocketClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
||||
_SocketClient.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
|
||||
IPAddress ipAddress = IPAddress.Parse(_IP);
|
||||
var endPoint = new IPEndPoint(ipAddress, _Port);
|
||||
_LogHelper.Debug("开始连接");
|
||||
_SocketClient.BeginConnect(endPoint, new AsyncCallback(Connected), null);
|
||||
}
|
||||
|
||||
private void Connected(IAsyncResult asyncResult)
|
||||
{
|
||||
try
|
||||
{
|
||||
_SocketClient.EndConnect(asyncResult);
|
||||
_LogHelper.Debug("连接成功");
|
||||
IsConnected = true;
|
||||
TriggerConnected();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_LogHelper.Error("连接失败原因为:" + e.Message);
|
||||
TriggerError();
|
||||
}
|
||||
}
|
||||
|
||||
public void ReceiveLoop()
|
||||
{
|
||||
Thread thread = new Thread(() =>
|
||||
{
|
||||
while (!IsDisposed)
|
||||
{
|
||||
Receive();
|
||||
}
|
||||
});
|
||||
thread.Start();
|
||||
}
|
||||
|
||||
public void Receive()
|
||||
{
|
||||
ManualResetEvent manualResetEvent = new ManualResetEvent(false);
|
||||
_LogHelper.Debug("开始监听接收数据");
|
||||
try
|
||||
{
|
||||
_SocketClient.BeginReceive(_Buffer, 0, _Buffer.Length, SocketFlags.None, new AsyncCallback(Received), new object[] { manualResetEvent, _Buffer });
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_LogHelper.Error("接收数据失败原因为:" + e.Message);
|
||||
Thread.Sleep(100000);
|
||||
manualResetEvent.Set();
|
||||
}
|
||||
manualResetEvent.WaitOne();
|
||||
manualResetEvent.Dispose();
|
||||
}
|
||||
|
||||
private void Received(IAsyncResult asyncResult)
|
||||
{
|
||||
if (IsDisposed)
|
||||
{
|
||||
return;
|
||||
}
|
||||
var objs = asyncResult.AsyncState as object[];
|
||||
var manualResetEvent = objs[0] as ManualResetEvent;
|
||||
var buffer = objs[1] as byte[];
|
||||
try
|
||||
{
|
||||
var length = _SocketClient.EndReceive(asyncResult);
|
||||
_LogHelper.Debug("接收到服务器推送的数据");
|
||||
TriggerDataGeted(buffer);
|
||||
if (buffer != null)
|
||||
{
|
||||
Array.Clear(buffer, 0, length);
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_LogHelper.Error("接收到服务器推送的数据失败原因为:" + e.Message);
|
||||
TriggerError();
|
||||
}
|
||||
manualResetEvent.Set();
|
||||
}
|
||||
|
||||
public void Send(byte[] buffer)
|
||||
{
|
||||
_LogHelper.Debug("开始发送数据");
|
||||
try
|
||||
{
|
||||
_SocketClient.BeginSend(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(Sended), null);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_LogHelper.Error("发送数据失败原因为:" + e.Message);
|
||||
TriggerError();
|
||||
}
|
||||
}
|
||||
|
||||
private void Sended(IAsyncResult asyncResult)
|
||||
{
|
||||
if (IsDisposed)
|
||||
{
|
||||
return;
|
||||
}
|
||||
try
|
||||
{
|
||||
_SocketClient.EndSend(asyncResult);
|
||||
_LogHelper.Debug("发送数据成功");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_LogHelper.Error("发送数据失败原因为:" + e.Message);
|
||||
TriggerError();
|
||||
}
|
||||
}
|
||||
|
||||
private void TriggerError()
|
||||
{
|
||||
IsConnected = false;
|
||||
if (OnError != null)
|
||||
{
|
||||
OnError(this);
|
||||
}
|
||||
}
|
||||
private void TriggerConnected()
|
||||
{
|
||||
if (OnConnected != null)
|
||||
{
|
||||
OnConnected(this);
|
||||
}
|
||||
}
|
||||
|
||||
private void TriggerDataGeted(byte[] buffer)
|
||||
{
|
||||
if (OnDataGeted != null)
|
||||
{
|
||||
OnDataGeted(this, buffer);
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
IsDisposed = true;
|
||||
try
|
||||
{
|
||||
_Buffer = null;
|
||||
if (_SocketClient != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
_SocketClient.Disconnect(false);
|
||||
}
|
||||
catch
|
||||
{
|
||||
|
||||
}
|
||||
_SocketClient.Dispose();
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user