Пример #1
0
        /// <summary>
        /// This is synchronised TCP Recive method, received data will be only written into buffer if received bytes contains End Of Message string.
        /// </summary>
        /// <param name="iTimeout">Determines, how long in [ms] method can be executed, if iTimeout is lower then 0, method will not be terminated. </param>
        private void Receive(int timeout = -1)
        {
            DateTime DTStart    = DateTime.Now;
            TickTime TTStart    = TickTime.Now;
            bool     bTimeout   = false;
            bool     bDataFound = false;

            byte[] baData;

            string sData           = sDataBuffer;
            string sMessage        = null;
            string sMessageResidue = null;
            int    iRecivedCount;

            while (!bTimeout && !bDataFound)
            {
                if (timeout > 0 && TickTime.Timeout(TTStart, timeout, TickTime.Unit.ms))
                {
                    bTimeout = true;
                }

                baData        = new byte[1024];
                iRecivedCount = SLClient.Receive(baData);
                sData        += Encoding.ASCII.GetString(baData, 0, iRecivedCount);


                sMessage = sData.ExtractTag(TcpAsyncCommon.SOM, TcpAsyncCommon.EOM, out sMessageResidue);

                if (sMessage == null)
                {
                    continue;
                }
                else
                {
                    bDataFound = true;
                }
            }

            if (!bDataFound)
            {
                sDataBuffer = sData;
                return;
            }

            sDataBuffer = sMessageResidue;
            sData       = sMessage;

            //sData.Replace(TcpAsyncClient.SOM, "");
            //sData.Replace(TcpAsyncClient.EOM, "");
            sData.Replace(@"\\", @"\");

            DBReceive.Set(sData);
        }
Пример #2
0
        /// <summary>
        /// This is synchronised TCP Recive method, received data will be only written into buffer if received bytes contains End Of Message string.
        /// </summary>
        /// <param name="iTimeout">Determines, how long in [ms] method can be executed, if iTimeout is lower then 0, method will not be terminated. </param>
        private void Receive(int iTimeout = -1)
        {
            DateTime DTStart    = DateTime.Now;
            bool     bTimeout   = false;
            bool     bDataFound = false;

            byte[] baData;

            string sData           = sDataBuffer;
            string sMessage        = null;
            string sMessageResidue = null;
            int    iRecivedCount;

            while (!bTimeout && !bDataFound)
            {
                if (iTimeout > 0 && (DateTime.Now - DTStart).TotalMilliseconds > iTimeout)
                {
                    bTimeout = true;
                }

                baData        = new byte[1024];
                iRecivedCount = SLClient.Receive(baData);
                sData        += Encoding.ASCII.GetString(baData, 0, iRecivedCount);


                sMessage = Abbreviate.String.Extract(sData, TcpAsyncCommon.SOM, TcpAsyncCommon.EOM, out sMessageResidue);

                if (sMessage == null)
                {
                    continue;
                }
                else
                {
                    bDataFound = true;
                }
            }

            if (!bDataFound)
            {
                sDataBuffer = sData;
                return;
            }

            sDataBuffer = sMessageResidue;
            sData       = sMessage;

            //sData.Replace(TcpAsyncClient.SOM, "");
            //sData.Replace(TcpAsyncClient.EOM, "");
            sData.Replace(@"\\", @"\");

            DBReceive.Set(sData);
        }