init commit all code
This commit is contained in:
215
93_nei/Getf.Service.Transfer.Client.WinService/ClientService.cs
Normal file
215
93_nei/Getf.Service.Transfer.Client.WinService/ClientService.cs
Normal file
@@ -0,0 +1,215 @@
|
||||
using Getf.Service.Transfer.Client.WinService.Entities;
|
||||
using Getf.Service.Transfer.Client.WinService.Helpers;
|
||||
using Newtonsoft.Json;
|
||||
using SuperSocket.ClientEngine;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Configuration;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
|
||||
namespace Getf.Service.Transfer.Client.WinService
|
||||
{
|
||||
public class ClientService : IDisposable
|
||||
{
|
||||
LogHelper _LogHelper;
|
||||
|
||||
private DateTime LastRecvTime = DateTime.Now;
|
||||
|
||||
private readonly IDataHandler DataHandler;
|
||||
private TcpClient TcpClient;
|
||||
private readonly string Ip;
|
||||
private readonly int Port;
|
||||
private Thread HeartThread;
|
||||
private bool IsRetrying;
|
||||
|
||||
public ClientService()
|
||||
{
|
||||
var serviceAddressInfo = ConfigurationManager.AppSettings["ServiceAddressInfo"].Split(':');
|
||||
Ip = serviceAddressInfo[0];
|
||||
Port = int.Parse(serviceAddressInfo[1]);
|
||||
|
||||
var assemblyInfo = System.Configuration.ConfigurationManager.AppSettings["DataHandlerClassInfo"].Split(',');
|
||||
DataHandler = Assembly.Load(assemblyInfo[1]).CreateInstance(assemblyInfo[0]) as IDataHandler;
|
||||
_LogHelper = new LogHelper();
|
||||
}
|
||||
|
||||
public void Start()
|
||||
{
|
||||
StartNewEasyClient();
|
||||
DoHeart();
|
||||
}
|
||||
|
||||
private void DoDebug()
|
||||
{
|
||||
for (var i = 0; i < 5; i++)
|
||||
{
|
||||
Thread t = new Thread(() =>
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
var r = GetRegisterEntity();
|
||||
r.Head.Type = 4;
|
||||
r.Data = new byte[100 * 1024 * 1024];
|
||||
Send(r.ToByte());
|
||||
Thread.Sleep(100);
|
||||
}
|
||||
});
|
||||
t.Start();
|
||||
}
|
||||
}
|
||||
|
||||
private void StartNewEasyClient()
|
||||
{
|
||||
TcpClient = new TcpClient(Ip, Port, OnDataGeted, OnConnected, OnClosed, OnError);
|
||||
}
|
||||
|
||||
private void OnDataGeted(TransInfo transInfo)
|
||||
{
|
||||
LastRecvTime = DateTime.Now;
|
||||
if (transInfo.Body == null)//心跳包
|
||||
{
|
||||
/*//应用已被注册 再试
|
||||
if (transInfo.TransResultInfo != null && transInfo.TransResultInfo.Code == -210)
|
||||
{
|
||||
Reconnect(null, null);
|
||||
}*/
|
||||
if (transInfo.TransResultInfo != null && transInfo.TransResultInfo.Message == "注册成功!")
|
||||
{
|
||||
_LogHelper.Info("注册成功");
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (transInfo.TransResultInfo != null && !String.IsNullOrWhiteSpace(transInfo.TransResultInfo.Message))
|
||||
{
|
||||
_LogHelper.Error("发生错误:" + transInfo.TransResultInfo.Message);
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
var transResult = DataHandler.HandlerData(transInfo);
|
||||
|
||||
var r = GetRegisterEntity();
|
||||
r.Body = new TransBody()
|
||||
{
|
||||
Key = transInfo.Body.Key
|
||||
};
|
||||
r.Head.Type = 3;
|
||||
r.TransResultInfo = transResult.TransResultInfo;
|
||||
r.Data = transResult.Data;
|
||||
|
||||
Send(r.ToByte());
|
||||
}
|
||||
|
||||
private void OnConnected(object sender, EventArgs e)
|
||||
{
|
||||
IsRetrying = false;
|
||||
_LogHelper.Info("连接成功");
|
||||
Send(GetRegisterEntity().ToByte());
|
||||
}
|
||||
|
||||
private void OnClosed(object sender, EventArgs e)
|
||||
{
|
||||
_LogHelper.Info("EasyClient.OnClosed");
|
||||
ReStart();
|
||||
}
|
||||
|
||||
private void OnError(object sender, EventArgs e)
|
||||
{
|
||||
_LogHelper.Info("连接出错,原因为:" + ((ErrorEventArgs)e).Exception.Message);
|
||||
ReStart();
|
||||
}
|
||||
|
||||
private void ReStart()
|
||||
{
|
||||
Thread.Sleep(5000);
|
||||
StartNewEasyClient();
|
||||
}
|
||||
|
||||
|
||||
|
||||
private void DoHeart()
|
||||
{
|
||||
#if DEBUG
|
||||
/*for (int i = 0; i < 100; i++)
|
||||
{
|
||||
var testThread = new Thread(() =>
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
SendHeart();
|
||||
}
|
||||
});
|
||||
testThread.Start();
|
||||
}*/
|
||||
|
||||
#endif
|
||||
|
||||
HeartThread = new Thread(() =>
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
if ((DateTime.Now - LastRecvTime).TotalMinutes > 10)
|
||||
{
|
||||
_LogHelper.Error("超过一分钟没收到心跳包的反馈信息");
|
||||
TcpClient.HeartLost();
|
||||
}
|
||||
if (TcpClient.IsConnected)
|
||||
{
|
||||
SendHeart();
|
||||
}
|
||||
Thread.Sleep(5 * 1000);
|
||||
}
|
||||
});
|
||||
HeartThread.Start();
|
||||
}
|
||||
|
||||
private void SendHeart()
|
||||
{
|
||||
var r = GetRegisterEntity();
|
||||
r.Head.Type = 4;
|
||||
Send(r.ToByte());
|
||||
}
|
||||
|
||||
private void Send(byte[] data)
|
||||
{
|
||||
if (!TcpClient.IsConnected) return;
|
||||
try
|
||||
{
|
||||
TcpClient.Send(data);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_LogHelper.Error("发送错误原因为:" + e.Message);
|
||||
}
|
||||
}
|
||||
|
||||
private TransInfo GetRegisterEntity()
|
||||
{
|
||||
var ts = TypeHelper.GetTimeStamp();
|
||||
var r = new TransInfo()
|
||||
{
|
||||
Head = new TransHead()
|
||||
{
|
||||
Type = 1,
|
||||
AppID = ConfigurationManager.AppSettings["AppID"],
|
||||
AppSecret = ConfigurationManager.AppSettings["AppSecret"],
|
||||
TimeStamp = ts,
|
||||
Sign = Md5Helper.Md5(ts + ConfigurationManager.AppSettings["Key"])
|
||||
}
|
||||
};
|
||||
return r;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
TcpClient.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user