Ejemplo n.º 1
0
 private void CallBackSend(IAsyncResult iar)
 {
     try
     {
         TcpLink tcpLink = iar.AsyncState as TcpLink;
         // ((Socket)iar.AsyncState).EndSend(iar);
         tcpLink.m_client.Client.EndSend(iar);
     }
     catch (Exception ex)
     {
         m_nDelayTimeWhenErr += 200;
         if (m_ArrSendByte != null && m_ArrSendByte.Length > 0)
         {
             Thread.Sleep(m_nDelayTimeWhenErr);
             WriteData(m_ArrSendByte, m_ArrSendByte.Length);
             //   ShowLog(ex.Message);
         }
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        ///网口打开时通过回调检测是否连接超时。 5秒种
        /// </summary>
        /// <param name="asyncResult"></param>
        private void CallBackConnect(IAsyncResult asyncResult)
        {
            try
            {
                m_bConnectSuccess = false;
                TcpLink tcpLink = asyncResult.AsyncState as TcpLink;
                //TcpClient tcpClient = asyncResult.AsyncState as TcpClient;
                TcpClient tcpClient = tcpLink.m_client;
                if (tcpClient.Client != null && tcpClient.Connected)
                {
                    tcpClient.EndConnect(asyncResult);
                    m_bConnectSuccess = true;
                    if (!SynchronizationCom)//异步通讯
                    {
                        StartRecvMessage();
                    }
                    TimeoutObject.Set();
                }
                else if (tcpClient.Client != null && !tcpClient.Connected)
                {
                    tcpClient.Client.Shutdown(SocketShutdown.Both);
                    tcpClient.Client.Disconnect(true);
                    tcpClient.EndConnect(asyncResult);
                    tcpClient.Client.BeginConnect(tcpLink.m_strIP, tcpLink.m_nPort, new AsyncCallback(CallBackConnect), tcpLink);
                    tcpLink.m_count++;
                }
            }
            catch (Exception ex)
            {
                m_bConnectSuccess = false;
                socketException   = ex;

                TcpLink   tcpLink   = asyncResult.AsyncState as TcpLink;
                TcpClient tcpClient = tcpLink.m_client;
                ConnectServer();
                tcpLink.m_count++;
            }
            finally
            {
                //TimeoutObject.Set();
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 接收数据操作回调函数
        /// </summary>
        /// <param name="ar"></param>
        private void RecvCallBack(IAsyncResult ar)
        {
            TcpLink asyTcpSocket = null;

            try
            {
                asyTcpSocket        = (TcpLink)ar.AsyncState;
                m_nDelayTimeWhenErr = 30;
                //获取异步SOCKET从网络上接收到的数据的数量
                int bytesRead = asyTcpSocket.m_client.Client.EndReceive(ar);

                //_logger.Warn($"{DateTime.Now.ToString()}:{m_strIP},{m_nPort}:Receive bytes:" + bytesRead);

                //if (bytesRead == 0)
                //{
                //    if (!asyTcpSocket.m_client.Client.Connected)
                //        asyTcpSocket.Disconnect();//20180616
                //    m_recvString = "";
                //    m_recvLength = 0;
                //    _logger.Warn($"{m_strIP},{m_nPort}:接收数据过程中出现错误,关闭连接!11");
                //    if (SocketErrorEvent != null)
                //    {
                //        SocketErrorEvent(this, new AsyTcpSocketEventArgs($"{m_strIP},{m_nPort}:接收数据过程中出现错误,关闭连接!"));
                //    }

                //}
                if (bytesRead > 0)
                {
                    m_nDelayTimeWhenErr = 30;
                    if (isReceiveProess)
                    {
                        //处理数据  获取结束符
                        ReceiveProess(bytesRead);
                    }
                    else
                    {
                        //不处理数据 给接收事件者处理
                        //根据接收到数据的数量动态申请控件来保存接收的数据
                        byte[] recvbuffer = new byte[bytesRead];
                        m_recvLength = bytesRead;
                        m_recvString = Encoding.UTF8.GetString(recvbuffer);//.Replace("\0",null);//.Trim();
                        if (m_recvString.Length > 0)
                        {
                            RaiseDataReceived(recvbuffer);
                        }
                    }
                }
            }
            //如果出现错误的话就释放所有的资源,这样能够保证对于下次的数据传输不会造成影响
            catch (Exception ex)
            {
                m_nDelayTimeWhenErr = 200;
                m_recvString        = "";
                m_recvLength        = 0;
                if (SocketErrorEvent != null)
                {
                    _logger.Warn($"{m_strIP},{m_nPort}:接收数据过程中出现错误!22");
                    SocketErrorEvent(this, new AsyTcpSocketEventArgs($"{m_strIP},{m_nPort}:接收数据过程中出现错误!"));
                }
                sbReceiveDataBuffer.Clear();
                Err($"{m_strIP},{m_nPort}:接收数据过程中出现错误!22");
                //  if (!asyTcpSocket.m_client.Client.Connected)
                //      asyTcpSocket.Disconnect();//20180616
            }
            finally
            {
                //重复效用回调函数,准备接收数据
                try
                {
                    if (m_nDelayTimeWhenErr != 0)
                    {
                        Thread.Sleep(m_nDelayTimeWhenErr);
                    }
                    m_nDelayTimeWhenErr = 0;
                    m_client.Client.BeginReceive(m_recvBuffer, 0, m_recvBuffer.Length, 0, new AsyncCallback(RecvCallBack), this);
                }
                catch (Exception ex)
                {
                    sbReceiveDataBuffer.Clear();
                    if (SocketErrorEvent != null)
                    {
                        _logger.Warn($"{m_strIP},{m_nPort}:准备接收数据过程中出现错误!33");
                        SocketErrorEvent(this, new AsyTcpSocketEventArgs($"{m_strIP},{m_nPort}:准备接收数据过程中出现错误!"));
                    }
                    if (m_client.Client.Connected)
                    {
                        m_client.Client.Disconnect(true);
                    }
                    m_client.Client.BeginConnect(m_strIP, m_nPort, new AsyncCallback(CallBackConnect), this);
                }
            }
        }