Beispiel #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);
        }
Beispiel #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);
        }
Beispiel #3
0
        private bool Receive()
        {
            List <byte> result_buffer = new List <byte>();

            result_buffer.AddToEnd(residue);

            int received     = 0;
            int received_now = 0;

            while (SLClient.IsAvailableToRead())
            {
                try
                {
                    received_now = 0;
                    received_now = SLClient.Receive(buffer_input, 0, buffer_input.Length, SocketFlags.None);
                    //Thread.Sleep(1);
                }
                catch (Exception ex)
                {
                    Exceptions.Write(ex);
                    this.StopClient();
                    break;
                }

                if (received_now > 0)
                {
                    result_buffer.AddRange(buffer_input.Take(received_now));
                    received += received_now;
                }
                else
                {
                    break;
                }
            }

            var packet = result_buffer.GetArray();

            if (packet.IsNullOrEmpty())
            {
                residue = null;
                return(false);
            }

            int  offset;
            int  length;
            byte compression;

            if (!TcpAsyncCommon.RecreatePacket(ref packet, out offset, out length, out compression, PacketMode) || (offset + length) > packet.Length)
            {
                residue = packet;
                return(false);
            }

            byte[] result = packet.SubArray(offset, length);

            //+ 1 represents EndByte
            residue = packet.SubArray((offset + length + 1), packet.Length - (offset + length + 1));

            if (result.IsNullOrEmpty())
            {
                return(false);
            }

            if (compression == 1)
            {
                result = result.UnGZip();
            }

            DBReceive.Write(result);
            return(true);
        }