IceTcpClient.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using CommonNetwork_ICE.Common;
  7. using Slice;
  8. using CommonNetwork_ICE.Util;
  9. using CommonNetwork_ICE.handler;
  10. using CommonLang.Protocol;
  11. using CommonNetwork_ICE.Msg;
  12. using CommonNetwork_ICE.Session;
  13. using CommonLang.Log;
  14. using CommonLang.Concurrent;
  15. using CommonNetwork_ICE.Connector;
  16. namespace CommonNetwork_ICE.Client
  17. {
  18. /// <summary>
  19. /// ICE TCP客户端连接器,指定解码器,初始化本对象后可向指定服务器发送ICE消息,接收消息请先绑定会话侦听对象
  20. /// </summary>
  21. public class IceTcpClient : IceClientConnector
  22. {
  23. private static Logger log = LoggerFactory.GetLogger("IceTcpClient");
  24. private TcpRouterClient tcpRouterClient;
  25. private object closeObj = new object();
  26. public IceTcpClient(IceClientNetSession session)
  27. : this(session, Env.ICE_SEND_REMOTE_IP.Clone().ToString(), Env.ICE_SEND_REMOTE_PORT)
  28. {
  29. }
  30. public IceTcpClient(IceClientNetSession session, String ip, int port) : base(session, ip, port)
  31. {
  32. tcpRouterClient = new TcpRouterClient(this, session);
  33. }
  34. internal override SenderDisp_ getClientSendHandler(IceClientNetSession session, IceMessageCodec Codec)
  35. {
  36. return null;
  37. }
  38. /// <summary>
  39. /// 初始化ICE接收和发送端
  40. /// </summary>
  41. public override void Open(int localListenPort)
  42. {
  43. startIceClientServer();
  44. if (IsConnected)
  45. {
  46. log.Log("已经连接上服务器IP【" + this.Connector.GetConnectorConfig().RemoteIp + "】和端口【" + this.Connector.GetConnectorConfig().RemotePort + "】,可以正常发送数据。");
  47. }
  48. else
  49. {
  50. throw new Exception("不能连接服务器,请检查远程IP【" + this.Connector.GetConnectorConfig().RemoteIp + "】和端口【" + this.Connector.GetConnectorConfig().RemotePort + "】。");
  51. }
  52. }
  53. /// <summary>
  54. /// 开启新线程启动客户端侦听
  55. /// </summary>
  56. private void startIceClientServer()
  57. {
  58. try
  59. {
  60. tcpRouterClient.Open(getInitData());
  61. }
  62. catch (Exception err)
  63. {
  64. session.onException(err);
  65. }
  66. }
  67. /// <summary>
  68. /// 关闭通讯通道
  69. /// </summary>
  70. public override void Close()
  71. {
  72. try
  73. {
  74. lock (closeObj)
  75. {
  76. if (IsConnected)
  77. {
  78. IsConnected = false;
  79. tcpRouterClient.Close();
  80. session.onClose();
  81. }
  82. }
  83. }
  84. catch (Exception err)
  85. {
  86. session.onException(err);
  87. }
  88. }
  89. /// <summary>
  90. /// 具体的发送方法
  91. /// </summary>
  92. /// <param name="transMessage"></param>
  93. internal override bool SendTo(TransMessage transMessage)
  94. {
  95. try
  96. {
  97. tcpRouterClient.Send(transMessage);
  98. }
  99. catch (Exception e)
  100. {
  101. session.onException(e);
  102. return false;
  103. }
  104. return true;
  105. }
  106. /// <summary>
  107. /// 初始化ICE TCP参数
  108. /// </summary>
  109. /// <returns></returns>
  110. private Ice.InitializationData getInitData()
  111. {
  112. Ice.InitializationData initData = new Ice.InitializationData();
  113. Ice.Properties props = Ice.Util.createProperties();
  114. props.setProperty("Ice.Default.Router", Constants.TCP_GATEWAY_INSTANCE_NAME + "/router:tcp -p " + Connector.GetConnectorConfig().RemotePort + " -h " + Connector.GetConnectorConfig().RemoteIp);
  115. props.setProperty("Ice.ACM.Client", "0");
  116. props.setProperty("Ice.RetryIntervals", "-1");
  117. props.setProperty("Ice.Trace.Network", "2");
  118. initData.properties = props;
  119. return initData;
  120. }
  121. }
  122. }