| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318 |
- using Muchinfo.Communication;
- using Muchinfo.TASClient.Service.Utilities;
- using System;
- using System.Collections.Generic;
- using System.Threading;
- using System.Threading.Tasks;
- namespace Muchinfo.TASClient.Service.LinkProxy.TCP
- {
- public class QuotationTcpLinkProxy
- {
- private TcpPoint _client;
- private object syncRoot = new object();
- private IDatagramResolver _resolver;
- private string _address;
- private int _port;
- private bool _isOnLine = false;
- private const int c_timeOutSencond = 30; //异步超时时间
- private Task timeOutTask;
- private int _heartbeatInterval = 6;
- public event EventHandler<DatagramEventArgs> DatagramRecieved;
- public event EventHandler<CommunicationStateEventArgs> StateChanged;
- private uint _sessionid = 0;
- private static object _lockSessionid = new object();
- private Dictionary<uint, Action<Datagram, IDatagramResolver>> requestDictionary =
- new Dictionary<uint, Action<Datagram, IDatagramResolver>>(); //回调字典
- private Dictionary<uint, int> timeOutSencondDic = new Dictionary<uint, int>(); //会话ID 超时时间
- private Dictionary<uint, AsyncResult<Datagram>> asyncDictionary = new Dictionary<uint, AsyncResult<Datagram>>();
- //回调字典
- /// <summary>
- /// 创建Tcp代理,打开时会阻塞
- /// </summary>
- /// <param name="host">The host.</param>
- /// <param name="port">The port.</param>
- /// <param name="heartbeatInterval">心跳时间</param>
- public QuotationTcpLinkProxy(string host, int port, int heartbeatInterval = 6)
- {
- _address = host;
- _port = port;
- _heartbeatInterval = heartbeatInterval;
- Open();
- }
- /// <summary>
- /// 打开连接
- /// </summary>
- public void Open()
- {
- this.Close(); //先关闭之前的连接
- InitializeClient();
- }
- /// <summary>
- /// 关闭当前连接
- /// </summary>
- public void Close()
- {
- if (this._client != null)
- {
- this._client.StateChanged -= this.Client_StateChanged;
- this._client.DatagramReceived -= this.Client_DatagramRecieved;
- this.Dispose();
- this._client = null;
- this.Enabled = false;
- requestDictionary.Clear();
- if (timeOutTask != null)
- {
- timeOutTask.Dispose();
- }
- }
- }
- public uint SessionId
- {
- get
- {
- lock (_lockSessionid)
- {
- do
- {
- if (_sessionid == uint.MaxValue)
- {
- _sessionid = 0;
- }
- ++_sessionid;
- } while (requestDictionary.ContainsKey(_sessionid));
- return _sessionid;
- }
- }
- }
- private void CheckTimeOutService()
- {
- while (true)
- {
- Thread.Sleep(1000);
- var keys = new List<uint>(timeOutSencondDic.Keys);
- foreach (var key in keys)
- {
- if (timeOutSencondDic[key] <= 0)
- {
- timeOutSencondDic.Remove(key);
- if (requestDictionary.ContainsKey(key)) //处理超时
- {
- requestDictionary.Remove(key);
- }
- else if (asyncDictionary.ContainsKey(key))
- {
- asyncDictionary[key].SetTimeOut();
- asyncDictionary.Remove(key);
- }
- }
- else
- {
- timeOutSencondDic[key] -= 1;
- }
- }
- }
- }
- /// <summary>
- /// 回调的方法请求行情数据
- /// </summary>
- /// <param name="asyncCallAction">回调的方法</param>
- /// <param name="param">参数</param>
- /// <returns>返回0表示发送</returns>
- public uint AsyncSend(Action<Datagram, IDatagramResolver> asyncCallAction, Datagram param)
- {
- uint id = SessionId;
- requestDictionary[id] = asyncCallAction;
- timeOutSencondDic[id] = c_timeOutSencond;
- param.SessionId = id;
- try
- {
- if (this._client != null)
- {
- this._client.AsyncSend(param);
- return id;
- }
- }
- catch
- {
- requestDictionary.Remove(id);
- timeOutSencondDic.Remove(id);
- //todo:状态栏显示失败原因
- }
- return 0; ////发送失败
- }
- public uint AsyncSend(AsyncResult<Datagram> asyncResult, Datagram param)
- {
- uint id = SessionId;
- asyncDictionary[id] = asyncResult;
- timeOutSencondDic[id] = c_timeOutSencond;
- param.SessionId = id;
- try
- {
- if (this._client != null)
- {
- this._client.AsyncSend(param);
- return id;
- }
- }
- catch
- {
- asyncDictionary.Remove(id);
- timeOutSencondDic.Remove(id);
- //todo:状态栏显示失败原因
- }
- return 0; ////发送失败
- }
- private void InitializeClient()
- {
- lock (this.syncRoot)
- {
- try
- {
- if (this._client == null)
- {
- this._client = new TcpPoint(_address, _port);
- if (this._resolver != null)
- {
- this._client.Resolver = this._resolver;
- }
- this._client.StateChanged += this.Client_StateChanged;
- this._client.DatagramReceived += this.Client_DatagramRecieved;
- this._client.HeartbeatInterval = _heartbeatInterval;
- this._client.HeartbeatEnabled = true;
- this._client.Open();
- this.Enabled = true;
- timeOutTask = Task.Factory.StartNew(() => CheckTimeOutService());
- }
- }
- catch (Exception ex)
- {
- if (this._client != null)
- {
- this.Client_StateChanged(this,
- new CommunicationStateEventArgs(this._client.SessionId, CommunicationState.Closed));
- }
- this.Close();
- }
- }
- }
- public bool Enabled
- {
- get;
- private set;
- }
- /// <summary>
- /// 异步等时间
- /// </summary>
- public int AsycnTimeOutSencond
- {
- get { return c_timeOutSencond; }
- }
- public IDatagramResolver Resolver
- {
- get
- {
- if (this._client != null)
- {
- return this._client.Resolver;
- }
- return null;
- }
- }
- /// <summary>
- /// Sends the specified datagram.
- /// </summary>
- /// <param name="datagram">The datagram.</param>
- public void Send(Datagram datagram)
- {
- try
- {
- if (this._client != null)
- {
- this._client.AsyncSend(datagram);
- }
- }
- catch (Exception ex)
- {
- }
- }
- private void Client_StateChanged(object sender, CommunicationStateEventArgs e)
- {
- if (StateChanged != null)
- {
- StateChanged(this, e);
- _isOnLine = e.State == CommunicationState.Opened;
- }
- }
- private void Client_DatagramRecieved(object sender, DatagramEventArgs e)
- {
- var sessionid = e.Datagram.SessionId;
- if (requestDictionary.ContainsKey(sessionid))
- {
- Task.Factory.StartNew(() =>
- {
- try
- {
- //回调函数中处理异常
- requestDictionary[sessionid].Invoke(e.Datagram, this.Resolver);
- }
- finally
- {
- requestDictionary.Remove(sessionid);
- if (timeOutSencondDic.ContainsKey(sessionid))
- {
- timeOutSencondDic.Remove(sessionid);
- }
- }
- });
- }
- else if (asyncDictionary.ContainsKey(sessionid))
- {
- asyncDictionary[sessionid].Complete(e.Datagram, DateTime.Now);
- }
- if (DatagramRecieved != null)
- {
- DatagramRecieved(this, e);
- }
- }
- public void Dispose()
- {
- lock (this.syncRoot)
- {
- if (this._client != null)
- {
- if (this._client.State != CommunicationState.Closing && this._client.State != CommunicationState.Closed)
- {
- this._client.Close();
- }
- else
- {
- this._client.Dispose();
- }
- this._client = null;
- }
- }
- }
- }
- }
|