138 lines
3.2 KiB
C#
138 lines
3.2 KiB
C#
using Getf.Service.Transfer.Request.SDK.Entities;
|
|
using Getf.Service.Transfer.Request.SDK.Helpers;
|
|
using Newtonsoft.Json;
|
|
using SuperSocket.ClientEngine;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Configuration;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Text;
|
|
using System.Threading;
|
|
|
|
namespace Getf.Service.Transfer.Request.SDK
|
|
{
|
|
public class RequestSevice : IDisposable
|
|
{
|
|
private readonly EasyClient TcpClient;
|
|
|
|
private TransInfo _TransInfo;
|
|
|
|
ManualResetEvent _ManualResetEvent = new ManualResetEvent(false);
|
|
|
|
private string AppID = ConfigurationManager.AppSettings["Service_Transfer_AppID"];
|
|
private string AppSecret = ConfigurationManager.AppSettings["Service_Transfer_AppSecret"];
|
|
private string Key = ConfigurationManager.AppSettings["Service_Transfer_Key"];
|
|
private string Ip;
|
|
private int Port;
|
|
|
|
/*public RequestSevice()
|
|
{
|
|
var c = ConfigurationManager.AppSettings["Service_Transfer_AddressInfo"].Split(':');
|
|
_TcpClientService = new TcpClientService(c[0], int.Parse(c[1]));
|
|
}*/
|
|
public RequestSevice()
|
|
{
|
|
var c = ConfigurationManager.AppSettings["Service_Transfer_AddressInfo"].Split(':');
|
|
Ip = c[0];
|
|
Port = int.Parse(c[1]);
|
|
TcpClient = new EasyClient();
|
|
}
|
|
|
|
public RequestSevice(string ip, int port)
|
|
{
|
|
Ip = ip;
|
|
Port = port;
|
|
TcpClient = new EasyClient();
|
|
}
|
|
|
|
public RequestSevice(string ip, int port, string appID, string appSecret, string key)
|
|
{
|
|
Ip = ip;
|
|
Port = port;
|
|
AppID = appID;
|
|
AppSecret = appSecret;
|
|
Key = key;
|
|
TcpClient = new EasyClient();
|
|
}
|
|
|
|
|
|
public void Dispose()
|
|
{
|
|
_ManualResetEvent.Dispose();
|
|
TcpClient.Close();
|
|
}
|
|
|
|
public TransInfo GetResult(TransInfo transInfo)
|
|
{
|
|
_TransInfo = transInfo;
|
|
TcpClient.Initialize(new ReceiveFilter(), OnDataGeted);
|
|
TcpClient.Connected += OnConnected;
|
|
TcpClient.Error += OnError;
|
|
TcpClient.ConnectAsync(new IPEndPoint(IPAddress.Parse(Ip), Port));
|
|
_ManualResetEvent.WaitOne();
|
|
return _TransInfo;
|
|
}
|
|
public TransInfo GetResult(string targetAppID, string action = null, string param = null, string appID = null, string appSecret = null)
|
|
{
|
|
_TransInfo = GetTransInfo(targetAppID, action, param, appID, appSecret);
|
|
return GetResult(_TransInfo);
|
|
}
|
|
|
|
public TransInfo GetTransInfo(string targetAppID, string action, string param = null, string appID = null, string appSecret = null)
|
|
{
|
|
var ts = TypeHelper.GetTimeStamp();
|
|
var r = new TransInfo()
|
|
{
|
|
Head = new TransHead()
|
|
{
|
|
AppID = AppID,
|
|
AppSecret = AppSecret,
|
|
TimeStamp = ts,
|
|
Type = 2,
|
|
Sign = Md5Helper.Md5(ts + Key)
|
|
},
|
|
Body = new TransBody()
|
|
{
|
|
Action = action,
|
|
AppID = appID,
|
|
AppSecret = appSecret,
|
|
Param = param,
|
|
TargetAppID = targetAppID
|
|
}
|
|
};
|
|
return r;
|
|
}
|
|
|
|
private void OnDataGeted(TransInfo transInfo)
|
|
{
|
|
_TransInfo = transInfo;
|
|
_ManualResetEvent.Set();
|
|
}
|
|
|
|
private void OnError(object sender, ErrorEventArgs e)
|
|
{
|
|
_TransInfo.Head = null;
|
|
_TransInfo.Body = null;
|
|
_TransInfo.TransResultInfo = new TransResult()
|
|
{
|
|
Code = -101,
|
|
Message = e.Exception.Message
|
|
};
|
|
try
|
|
{
|
|
_ManualResetEvent.Set();
|
|
}
|
|
catch
|
|
{
|
|
|
|
}
|
|
}
|
|
|
|
private void OnConnected(object sender, EventArgs e)
|
|
{
|
|
TcpClient.Send(_TransInfo.ToByte());
|
|
}
|
|
}
|
|
}
|