| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- using Muchinfo.MTPClient.Data.Enums;
- using Muchinfo.MTPClient.Infrastructure.Helpers;
- using Muchinfo.MTPClient.NetworkCore;
- using System;
- namespace Muchinfo.MTPClient.Infrastructure.LinkProxy.TCP
- {
- /// <summary>
- /// 业务代理连接
- /// </summary>
- public class TradeTcpLinkProxy : IDisposable
- {
- public string _host; ////主机地址
- public int _port; ////通信端口号
- private TCPManager _tcpManager;
- public TCPConnectState TcpConnectState
- {
- get { return (_tcpManager == null) ? TCPConnectState.Closed : _tcpManager.tcpConnectState; }
- }
- /// <summary>
- /// 创建业务连接
- /// </summary>
- public TradeTcpLinkProxy(string host, int port)
- {
- _host = host;
- _port = port;
- _tcpManager = new TCPManager(DatagramType.Datagram50, new TradeTcpCallback(this));
- LogInfoHelper.WriteInfo("开始连接交易服务..");
- _tcpManager.Connect(_host, _port, ConnectSuccess, ConnectFail);
- }
- /// <summary>
- /// 连接失败
- /// </summary>
- /// <param name="errorCode">错误码</param>
- /// <param name="desc">连接描述</param>
- private void ConnectFail(int errorCode, string desc)
- {
- //todo:通知连接失败
- LogInfoHelper.WriteInfo("交易务连接失败![" + errorCode + "] " + desc);
- MessengerHelper.DefaultSend(false, MessengerTokens.TradeCreateConnect);
- }
- /// <summary>
- /// 连接成功
- /// </summary>
- private void ConnectSuccess()
- {
- //todo:通知连接成功
- LogInfoHelper.WriteInfo("交易服务连接成功!");
- MessengerHelper.DefaultSend(true, MessengerTokens.TradeCreateConnect);
- }
- /// <summary>
- /// 发送消息,
- /// </summary>
- /// <param name="package">消息内容</param>
- /// <param name="successAction">成功回复</param>
- /// <param name="failAction">错误回复</param>
- public void SendPackage(TCPPackage package, Action<TCPPackage> successAction, Action<int, string> failAction)
- {
- if (_tcpManager == null)
- {
- //todo:抛异常
- return;
- }
- _tcpManager.SendMessage(package, successAction, failAction);
- }
- /// <summary>
- /// 通知通信组件开始发送心跳的方法
- /// </summary>
- public void StartSendBeat()
- {
- if (_tcpManager == null)
- {
- //todo:抛异常
- return;
- }
- LogInfoHelper.WriteInfo("交易登录成功,启动发送心跳包!");
- _tcpManager.StartSendBeats();
- }
- /// <summary>
- /// 执行与释放或重置非托管资源相关的应用程序定义的任务。
- /// </summary>
- public void Dispose()
- {
- if (_tcpManager != null)
- {
- _tcpManager.Disconnect();
- _tcpManager = null;
- }
- }
- /// <summary>
- /// 设置断线重连标志
- /// </summary>
- public void SetReconnectFlag(bool flag)
- {
- if (null != _tcpManager) _tcpManager.SetReconnectFlag(flag);
- }
- }
- }
|