TradeTcpLinkProxy.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using Muchinfo.MTPClient.Data.Enums;
  2. using Muchinfo.MTPClient.Infrastructure.Helpers;
  3. using Muchinfo.MTPClient.NetworkCore;
  4. using System;
  5. namespace Muchinfo.MTPClient.Infrastructure.LinkProxy.TCP
  6. {
  7. /// <summary>
  8. /// 业务代理连接
  9. /// </summary>
  10. public class TradeTcpLinkProxy : IDisposable
  11. {
  12. public string _host; ////主机地址
  13. public int _port; ////通信端口号
  14. private TCPManager _tcpManager;
  15. public TCPConnectState TcpConnectState
  16. {
  17. get { return (_tcpManager == null) ? TCPConnectState.Closed : _tcpManager.tcpConnectState; }
  18. }
  19. /// <summary>
  20. /// 创建业务连接
  21. /// </summary>
  22. public TradeTcpLinkProxy(string host, int port)
  23. {
  24. _host = host;
  25. _port = port;
  26. _tcpManager = new TCPManager(DatagramType.Datagram50, new TradeTcpCallback(this));
  27. LogInfoHelper.WriteInfo("开始连接交易服务..");
  28. _tcpManager.Connect(_host, _port, ConnectSuccess, ConnectFail);
  29. }
  30. /// <summary>
  31. /// 连接失败
  32. /// </summary>
  33. /// <param name="errorCode">错误码</param>
  34. /// <param name="desc">连接描述</param>
  35. private void ConnectFail(int errorCode, string desc)
  36. {
  37. //todo:通知连接失败
  38. LogInfoHelper.WriteInfo("交易务连接失败![" + errorCode + "] " + desc);
  39. MessengerHelper.DefaultSend(false, MessengerTokens.TradeCreateConnect);
  40. }
  41. /// <summary>
  42. /// 连接成功
  43. /// </summary>
  44. private void ConnectSuccess()
  45. {
  46. //todo:通知连接成功
  47. LogInfoHelper.WriteInfo("交易服务连接成功!");
  48. MessengerHelper.DefaultSend(true, MessengerTokens.TradeCreateConnect);
  49. }
  50. /// <summary>
  51. /// 发送消息,
  52. /// </summary>
  53. /// <param name="package">消息内容</param>
  54. /// <param name="successAction">成功回复</param>
  55. /// <param name="failAction">错误回复</param>
  56. public void SendPackage(TCPPackage package, Action<TCPPackage> successAction, Action<int, string> failAction)
  57. {
  58. if (_tcpManager == null)
  59. {
  60. //todo:抛异常
  61. return;
  62. }
  63. _tcpManager.SendMessage(package, successAction, failAction);
  64. }
  65. /// <summary>
  66. /// 通知通信组件开始发送心跳的方法
  67. /// </summary>
  68. public void StartSendBeat()
  69. {
  70. if (_tcpManager == null)
  71. {
  72. //todo:抛异常
  73. return;
  74. }
  75. LogInfoHelper.WriteInfo("交易登录成功,启动发送心跳包!");
  76. _tcpManager.StartSendBeats();
  77. }
  78. /// <summary>
  79. /// 执行与释放或重置非托管资源相关的应用程序定义的任务。
  80. /// </summary>
  81. public void Dispose()
  82. {
  83. if (_tcpManager != null)
  84. {
  85. _tcpManager.Disconnect();
  86. _tcpManager = null;
  87. }
  88. }
  89. /// <summary>
  90. /// 设置断线重连标志
  91. /// </summary>
  92. public void SetReconnectFlag(bool flag)
  93. {
  94. if (null != _tcpManager) _tcpManager.SetReconnectFlag(flag);
  95. }
  96. }
  97. }