示例#1
0
        /// <summary>
        /// 当连接时
        /// </summary>
        public void Connect()
        {
            if (string.IsNullOrEmpty(host))
            {
                OnError(this, new ErrorEventArgs(new ArgumentNullException("host", GetType().ToString() + ", Name:" + Name + " , host is invalid")));
                return;
            }

            if (port < IPEndPoint.MinPort || port > IPEndPoint.MaxPort)
            {
                OnError(this, new ErrorEventArgs(new ArgumentOutOfRangeException("port", GetType().ToString() + ", Name:" + Name + " , port is invalid")));
                return;
            }

            lock (locker)
            {
                Disconnect();
                if (packer != null)
                {
                    packer.Clear();
                }

                tcpConnector            = new TcpConnector(host, port);
                tcpConnector.OnConnect += OnConnect;
                tcpConnector.OnClose   += OnClose;
                tcpConnector.OnError   += OnError;
                tcpConnector.OnMessage += OnMessage;
                tcpConnector.Connect();
            }
        }
示例#2
0
        /// <summary>
        /// 链接
        /// </summary>
        public void Connect()
        {
            lock (locker)
            {
                Disconnect();
                if (packer != null)
                {
                    packer.Clear();
                }

                udpConnector            = new UdpConnector();
                udpConnector.OnConnect += OnConnect;
                udpConnector.OnClose   += OnClose;
                udpConnector.OnError   += OnError;
                udpConnector.OnMessage += OnMessage;

                if (string.IsNullOrEmpty(host) || port < IPEndPoint.MinPort || port > IPEndPoint.MaxPort)
                {
                    udpConnector.Connect();
                }
                else
                {
                    udpConnector.Connect(host, port);
                }
            }
        }