Beispiel #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();
            }
        }
Beispiel #2
0
 /// <summary>
 /// 断开连接
 /// </summary>
 public void Disconnect()
 {
     if (tcpConnector != null)
     {
         tcpConnector.Dispose();
     }
     tcpConnector = null;
 }
Beispiel #3
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;
            }

            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();
        }
Beispiel #4
0
 /// <summary>
 /// 断开连接
 /// </summary>
 public void Disconnect()
 {
     if (tcpConnector != null)
     {
         tcpConnector.Dispose();
     }
     tcpConnector = null;
 }