123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using CommonNetwork_ICE.Common;
- using Slice;
- using CommonNetwork_ICE.Util;
- using CommonNetwork_ICE.handler;
- using CommonLang.Protocol;
- using CommonNetwork_ICE.Msg;
- using CommonNetwork_ICE.Session;
- using CommonLang.Log;
- using CommonLang.Concurrent;
- using CommonNetwork_ICE.Connector;
- namespace CommonNetwork_ICE.Client
- {
- /// <summary>
- /// ICE TCP客户端连接器,指定解码器,初始化本对象后可向指定服务器发送ICE消息,接收消息请先绑定会话侦听对象
- /// </summary>
- public class IceTcpClient : IceClientConnector
- {
- private static Logger log = LoggerFactory.GetLogger("IceTcpClient");
- private TcpRouterClient tcpRouterClient;
- private object closeObj = new object();
- public IceTcpClient(IceClientNetSession session)
- : this(session, Env.ICE_SEND_REMOTE_IP.Clone().ToString(), Env.ICE_SEND_REMOTE_PORT)
- {
- }
- public IceTcpClient(IceClientNetSession session, String ip, int port) : base(session, ip, port)
- {
- tcpRouterClient = new TcpRouterClient(this, session);
- }
- internal override SenderDisp_ getClientSendHandler(IceClientNetSession session, IceMessageCodec Codec)
- {
- return null;
- }
- /// <summary>
- /// 初始化ICE接收和发送端
- /// </summary>
- public override void Open(int localListenPort)
- {
- startIceClientServer();
- if (IsConnected)
- {
- log.Log("已经连接上服务器IP【" + this.Connector.GetConnectorConfig().RemoteIp + "】和端口【" + this.Connector.GetConnectorConfig().RemotePort + "】,可以正常发送数据。");
- }
- else
- {
- throw new Exception("不能连接服务器,请检查远程IP【" + this.Connector.GetConnectorConfig().RemoteIp + "】和端口【" + this.Connector.GetConnectorConfig().RemotePort + "】。");
- }
- }
- /// <summary>
- /// 开启新线程启动客户端侦听
- /// </summary>
- private void startIceClientServer()
- {
- try
- {
- tcpRouterClient.Open(getInitData());
- }
- catch (Exception err)
- {
- session.onException(err);
- }
- }
- /// <summary>
- /// 关闭通讯通道
- /// </summary>
- public override void Close()
- {
- try
- {
- lock (closeObj)
- {
- if (IsConnected)
- {
- IsConnected = false;
- tcpRouterClient.Close();
- session.onClose();
- }
- }
- }
- catch (Exception err)
- {
- session.onException(err);
- }
- }
- /// <summary>
- /// 具体的发送方法
- /// </summary>
- /// <param name="transMessage"></param>
- internal override bool SendTo(TransMessage transMessage)
- {
- try
- {
- tcpRouterClient.Send(transMessage);
- }
- catch (Exception e)
- {
- session.onException(e);
- return false;
- }
- return true;
- }
- /// <summary>
- /// 初始化ICE TCP参数
- /// </summary>
- /// <returns></returns>
- private Ice.InitializationData getInitData()
- {
- Ice.InitializationData initData = new Ice.InitializationData();
- Ice.Properties props = Ice.Util.createProperties();
- props.setProperty("Ice.Default.Router", Constants.TCP_GATEWAY_INSTANCE_NAME + "/router:tcp -p " + Connector.GetConnectorConfig().RemotePort + " -h " + Connector.GetConnectorConfig().RemoteIp);
- props.setProperty("Ice.ACM.Client", "0");
- props.setProperty("Ice.RetryIntervals", "-1");
- props.setProperty("Ice.Trace.Network", "2");
- initData.properties = props;
- return initData;
- }
- }
- }
|