Ejemplo n.º 1
0
        public IConnector CreateConnector(EProtocolType type)
        {
            switch (type)
            {
            case EProtocolType.TCP:
                return(new TCPConnector());

            case EProtocolType.UDP:
                return(new UDPConnector());

            default:
                LogUtil.ShowWarning("暂不支持的类型");
                return(null);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 创建一个Socket连接,连接远程地址address,address的格式为“[IP地址或者Host地址]:[端口]”
        /// </summary>
        /// <param name="address"></param>
        public Connector MakeConnection(string address, EProtocolType protocol, IDataHandler dataHandler, IEventHandler eventHandler)
        {
            if (connectorTable == null)
            {
                return(null);
            }
            if (string.IsNullOrEmpty(address))
            {
                return(null);
            }
            string[] tmp = address.Split(':');
            if (tmp.Length != 2)
            {
                return(null);
            }
            if (string.IsNullOrEmpty(tmp[0]))
            {
                return(null);
            }
            Connector connector = new Connector();

            connector.ProtocolType = protocol;
            connector.EventHandler = this;
            connector.DataHandler  = this;
            ConnectorParameter parameter = new ConnectorParameter {
                connector = connector, dataHandler = dataHandler, eventHandler = eventHandler
            };

            connectorTable[connector] = parameter;
            IPAddress ipAddress = null;

            if (!IPAddress.TryParse(tmp[0], out ipAddress))
            {
                connector.Host = address;
            }
            else
            {
                connector.IP = address;
            }
            connector.Start();
            return(connector);
        }