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 Send()
        {
            byte[] data;

            if (SLClient == null ||
                DBSend == null ||
                !SLClient.Connected ||
                DBSend.IsAllRead ||
                !DBSend.Read(out data))
            {
                return(false);
            }


            byte[] result_data = TcpAsyncCommon.CreatePacket(data, PacketMode);// result.ToArray();

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

            int sent = 0;
            int size = result_data.Length;

            SendTimeout.Reset();
            do
            {
                int packet = size - sent;
                if (packet > TcpAsyncServer.PacketSizeTCP)
                {
                    packet = TcpAsyncServer.PacketSizeTCP;
                }
                try
                {
                    sent += SLClient.Send(result_data, sent, packet, SocketFlags.None);
                    //Thread.Sleep(1);
                }
                catch (Exception ex)
                {
                    Exceptions.Write(ex);
                    this.StopClient();
                    return(false);
                }

                if (SendTimeout.IsTriggered)
                {
                    return(false);
                }
            }while (sent < size);

            return(true);
        }
Beispiel #4
0
        public void StopClient()
        {
            try
            {
                if (SLClient == null)
                {
                    return;
                }

                SLClient.Shutdown(SocketShutdown.Both);
                SLClient.Close();
                SLClient = null;
            }
            catch
            {
            }
        }
Beispiel #5
0
        public void StopClient()
        {
            try
            {
                if (SLClient == null)
                {
                    return;
                }

                SLClient.Shutdown(SocketShutdown.Both);
                SLClient.Close();
                SLClient = null;
            }
            catch (Exception ex)
            {
                ex.ToOutput();
            }
        }
Beispiel #6
0
        public void Stop()
        {
            TimrMain.Enabled = false;

            try
            {
                if (SLClient == null)
                {
                    return;
                }
                SLClient.Shutdown(SocketShutdown.Both);
                SLClient.Close();
            }
            catch
            {
            }

            SLClient = null;
        }
Beispiel #7
0
        private bool Send()
        {
            if (!SLClient.Connected || !DBSend.IsDataAvalaible)
            {
                return(false);
            }

            string sData = DBSend.Get(false); //Don't increment data, becouse it is not certain at this point, if it will be send

            if (sData == null)
            {
                return(false);
            }

            sData.Replace(@"\", @"\\");
            sData = sData + TcpAsyncCommon.EOM;

            byte[] baData = Encoding.ASCII.GetBytes(sData);

            if (baData.Length > 1024)
            {
                throw new Exception("Exception in Send() method, packet size is to large.");
            }

            try
            {
                SLClient.Send(baData);
            }
            catch
            {
                return(false);
            }

            DBSend.IndexerIcrement(); //Increment indexer, becouse data was successfully send.
            return(true);
        }
Beispiel #8
0
 public bool IsConnected()
 {
     return(SLClient.IsConnected());
 }
Beispiel #9
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);
        }