Example #1
0
        private void ManagePushNotificationMessage(List <string> splitedData, ref byte[] buffer)
        {
            //Logger.WriteLine("~~~PushNotifiation - BUFFER BEFORE -> " + Encoding.UTF8.GetString(buffer));

            var indexOfPushNotif = ByteArrayHelper.IndexOfSequence(buffer, bytesPushNotification);

            var dataBeforeNotif        = buffer.Take(indexOfPushNotif).ToArray();
            var dataAfterNotifWithData = buffer.Skip(indexOfPushNotif + bytesPushNotification.Length).ToArray();

            // If PushNotification Contains EOCR
            if (ByteArrayHelper.IndexOfSequence(buffer, bytesEocr) >= 0)
            {
                var eocrIndex = ByteArrayHelper.IndexOfSequence(buffer, bytesEocr);

                var notifDataBytes = dataAfterNotifWithData.Take(eocrIndex).ToArray();
                var notifData      = Encoding.UTF8.GetString(notifDataBytes).Split(':').ToList();
                if (string.IsNullOrEmpty(notifData[0]))
                {
                    notifData.Skip(1);
                }

                var dataAfterNotif = dataAfterNotifWithData.Skip(eocrIndex + 5).ToArray();

                buffer = dataBeforeNotif.Concat(dataAfterNotif).ToArray();

                // This will exclude buffer=":" (PushNotifs that start with ":") case.
                if (buffer.Length == 1 && buffer[0] == byteColon)
                {
                    buffer = new byte[0];
                }

                if (buffer.Length > 2 && buffer[indexOfPushNotif - 1] == byteColon && buffer[indexOfPushNotif - 2] == byteColon)
                {
                    buffer = buffer.Take(buffer.Length - 1).ToArray();
                }

                if (buffer.Length > 0)
                {
                    splitedData = Encoding.UTF8.GetString(buffer).Split(':').ToList();
                }
                else
                {
                    CommandResponseBuffer.Complete();
                    CommandResponseBuffer = new BufferBlock <byte[]>();
                    splitedData           = new List <string>();
                }

                //Logger.WriteLine("~~~PushNotifiation - BUFFER AFTER -> " + Encoding.UTF8.GetString(buffer));
                Task.Factory.StartNew(AppendNewCustomFileHash(notifData[0], notifData[1], notifData[2]));
            }
            else
            {
                //read from network till EOCR occurs and then get transmited data !!
                Logger.WriteLine("Excepție: PushNotification - mesajul a fost pus in chunk-uri diferite. Caz netratat");
            }
        }
Example #2
0
        private void ManageEocrMessage(List <string> splitedData, byte[] buffer)
        {
            var bytesBeforeEocr = ByteArrayHelper.IndexOfSequence(buffer, bytesEocr);

            // is there a message like: ::EOCR: ???
            if ((bytesBeforeEocr == 1 && splitedData[0].Equals("") && buffer.Count() == 6) ||
                (splitedData[0].Equals("EOCR") && buffer.Count() == 5))
            {
                //IF message is :EOCR: OR EOCR:
                CommandResponseBuffer.Complete();
                CommandResponseBuffer = new BufferBlock <byte[]>();
            }
            else
            {
                var dataBeforeEocr = buffer.Take(bytesBeforeEocr).ToArray();

                // Send data before EOCR if there is some
                if (dataBeforeEocr.Length > 0 && dataBeforeEocr[dataBeforeEocr.Length - 1].Equals(byteColon))
                {
                    // Cuts the ":" before EOCR and sends data.
                    dataBeforeEocr = dataBeforeEocr.Take(dataBeforeEocr.Length - 1).ToArray();
                    CommandResponseBuffer.Post(dataBeforeEocr);
                }

                // Wait till the buffer is empty to complete the message            !!!
                while (CommandResponseBuffer.Count > 0)
                {
                    Thread.Sleep(10);
                }
                CommandResponseBuffer.Complete();
                CommandResponseBuffer = new BufferBlock <byte[]>();

                // Send remained data if there is some
                var bytesToEocr = bytesBeforeEocr + 5;
                if (bytesToEocr < buffer.Count())
                {
                    //Logger.WriteLine("~~~AsyncReading(EOCR - RemainedData) : " + Encoding.UTF8.GetString(buffer));
                    var dataAfterEocr = buffer.Skip(bytesToEocr).ToArray();
                    CommandResponseBuffer.Post(dataAfterEocr);
                }
                // This is not necessary: else if (bytesToEocr == buffer.Count()) !!! I THINK !!!
            }
        }