private void StartReceive(object sender, DoWorkEventArgs e)
        {
            int sleecount = 0;

            while (this.socket.Connected)
            {
#if _Not_Uset_NS_
                #region   直接使用Socket收数据
                if (socket != null && socket.Connected)
                {
                    if (socket.Poll(1, SelectMode.SelectRead))
                    {
                        try
                        {
                            byte[] buffer    = new byte[1024];
                            int    readBytes = socket.Receive(buffer);
                            if (readBytes == 0)
                            {
                                //连接断开
                                nOffset = 0;
                                Array.Clear(rbuf, 0, rbuf.Length);
                                break;
                            }
                            else
                            {
                                string cmdTarget  = "";
                                int    indexofArr = 0;
                                Array.Copy(buffer, nOffset, rbuf, nOffset, readBytes);
                                nOffset += readBytes;
                                if (!CommunicateFun.CheckEndChar(rbuf, System.Text.Encoding.Default.GetBytes(m_strLine), out indexofArr))
                                {
                                }
                                else
                                {
                                    byte[] buffer2 = new byte[indexofArr];
                                    Array.Copy(rbuf, buffer2, indexofArr);
                                    cmdTarget = IP.ToString() + "#" + Port.ToString() + "#" + System.Text.Encoding.Default.GetString(buffer2);
                                    nOffset   = 0;
                                    //调用服务器内注册函数
                                    Array.Clear(rbuf, 0, rbuf.Length);
                                    try
                                    {
                                        this.OnCommandReceived(new CommandEventArgs(cmdTarget));
                                    }
                                    catch (Exception esd)
                                    {
                                    }
                                    cmdTarget = "";
                                    nOffset   = 0;
                                }
                            }
                        }
                        catch (Exception es)
                        {
                            //连接断开
                            nOffset = 0;
                            Array.Clear(rbuf, 0, rbuf.Length);
                            continue;
                        }
                    }
                }//end if socket is OK and data is OK

                #endregion
#else
                if (networkStream.DataAvailable)
                {
                    //if (this.networkStream.Length != 0)
                    //{
                    int temp = 2048; //256;
                                     //  temp = (int)this.networkStream.Length;
                    byte[] buffer    = new byte[temp];
                    int    readBytes = this.networkStream.Read(buffer, 0, temp);
                    if (readBytes == 0)
                    {
                        break;
                    }
                    string cmdTarget = "";

                    byte[] rbuf = new byte[readBytes];

                    Array.Copy(buffer, rbuf, readBytes);
                    cmdTarget = System.Text.Encoding.Default.GetString(rbuf);
                    this.OnCommandReceived(new CommandEventArgs(cmdTarget.Trim()));
                    //}
                }
#endif//endif _Not_Uset_NS_
                //try
                //{
                //    if (sleecount >= 100)
                //    {
                //        sleecount = 0;
                //    }
                //}
                //catch (System.Exception)
                //{

                //    break;
                //}
                System.Threading.Thread.Sleep(1);
                //sleecount++;
            }
            this.OnDisconnected(new ClientEventArgs(this.socket));
            this.Disconnect();
        }
Exemple #2
0
        /// <summary>
        ///  获取网口缓存数据    直到有结束符
        ///  /// </summary>
        /// <param name="count"></param>
        private void ReceiveProess(int count)
        {
            string receiveString = Encoding.Default.GetString(m_recvBuffer, 0, count);
            //  _logger.Info(DateTime.Now.ToString() + $"IP:{m_strIP} Port:{m_nPort} 结束符:{m_strLine} 接收字串:" + receiveString);
            int tsLength   = m_strLine.Length;
            int indexofarr = -1;

            try
            {
                string cmdTarget  = "";
                int    indexofArr = 0;
                Array.Copy(m_recvBuffer, nOffset, rbuf, nOffset, count);
                nOffset += count;
                if (!CommunicateFun.CheckEndChar(rbuf, System.Text.Encoding.Default.GetBytes(m_strLine), out indexofArr))
                {
                }
                else
                {
                    try
                    {
                        byte[] bytearr = new byte[indexofArr];
                        Array.Copy(rbuf, bytearr, indexofArr);
                        this.RaiseDataReceived(bytearr);
                    }
                    catch (Exception ex)
                    {
                    }

                    nOffset = 0;
                    Array.Clear(rbuf, 0, rbuf.Length);
                    Array.Clear(m_recvBuffer, 0, m_recvBuffer.Length);
                }
            }
            catch (Exception es)
            {
                //连接断开
                nOffset = 0;
                Array.Clear(rbuf, 0, rbuf.Length);
            }

            //遍历字符串 直到结束符
            //for (int i = 0; i < receiveString.Length;)
            //{
            //    if (i <= receiveString.Length - tsLength)
            //    {
            //        if (receiveString.Substring(i, tsLength) != m_strLine)
            //        {
            //            sbReceiveDataBuffer.Append(receiveString[i]);
            //            i++;
            //        }
            //        else
            //        {
            //            try
            //            {
            //                _logger.Info(DateTime.Now.ToString() + $"IP:{m_strIP} Port:{m_nPort} 找到结束符{m_strLine} 字串:" + Encoding.Default.GetBytes(sbReceiveDataBuffer.ToString()));
            //                this.RaiseDataReceived(Encoding.Default.GetBytes(sbReceiveDataBuffer.ToString().ToCharArray()));
            //            }
            //            catch(Exception EX)
            //            {
            //            }
            //            finally
            //            {
            //                sbReceiveDataBuffer.Clear();
            //                i += tsLength;
            //            }

            //        }
            //    }
            //    else
            //    {
            //        sbReceiveDataBuffer.Append(receiveString[i]);
            //        i++;
            //    }
            //}
        }