Ejemplo n.º 1
0
        /// <summary>
        /// Метод, вызывающийся при завершении чтения
        /// </summary>
        /// <param name="ar"></param>
        private void OnCompleteSocketReceive(IAsyncResult ar)
        {
            //если сокет уже отключили, то просто выходим
            if (!_attached || _socket == null)
            {
                return;
            }

            SocketError socketError;
            int         length;

            try
            {
                //получаем количество прочитанных байт
                length = _socket.EndReceive(ar, out socketError);
            }
            catch (Exception e)
            {
                DoDisconnect(e);
                return;
            }

            //Проверяем на ошибки при чтении сокета
            if (socketError != SocketError.Success || length <= 0)
            {
                DoDisconnect(new Exception(socketError.ToString()));
                return;
            }

            _offset += length;//устанавливаем текущую длину данных

            //генерируем событие и отправляем его на вычетку
            var receiveEvent = new SocketReceiveEventArgs(this, _readCommandBuffer, _offset);

            try
            {
                AfterReceive?.Invoke(this, receiveEvent);
            }
            catch (Exception e)
            {
                //вычитка не получилась
                DoDisconnect(e);
                return;
            }

            //Проверяем, сколько удалось прочитать из буфера данных
            if (receiveEvent.FinishIndex > 0)
            {
                //устанавливаем новое семещение
                _offset = receiveEvent.Length - receiveEvent.FinishIndex;
                for (var i = 0; i < _offset; ++i)
                {
                    //переносим данные по новому смещению
                    _readCommandBuffer[i] = _readCommandBuffer[i + receiveEvent.FinishIndex];
                }
            }

            //повторяем процедуру чтения данных на сокете
            DoSocketReceive();
        }
Ejemplo n.º 2
0
            //public string ReceivedData { get; set; }
            //public event EventHandler AfterReceive;
            //public event EventHandler BeforeReceive;
            public override void OnReceive(Context context, Intent intent)
            {
                try
                {
                    if (IsBusy)
                    {
                        return;
                    }
                    IsBusy = true;
                    BeforeReceive?.Invoke(context, new EventArgs());

                    string _scanAll = cDataWedge.HandleDecodeData(intent);
                    string _pattern = @"(.*)\|(Scanner|MSR)\|(.*)\|(\d+)";
                    var    _matches = Regex.Match(_scanAll, _pattern);
                    string _scan    = _matches.Groups[1].ToString();
                    try
                    {
                        AfterReceive?.Invoke(context, new ReceiveEventArgs()
                        {
                            ReceivedData = _scan, Silent = silent
                        });
                    } catch (Exception ex)
                    {
                        if (!silent)
                        {
                            cSounds.Error(context);
                        }
                        Toast.MakeText(context, string.Format("Error in postprocess: {0}", ex.Message), ToastLength.Long).Show();
                        _scan  = "";
                        IsBusy = false;
                        return;
                    }
                    if (_scan == "")
                    {
                        if (!silent)
                        {
                            cSounds.Error(context);
                        }
                        Toast.MakeText(context, "Please enter valid data", ToastLength.Long).Show();
                        IsBusy = false;
                        return;
                    }

                    IsBusy = false;
                }
                catch //(Exception ex)
                {
                    IsBusy = false;
                }
            }
Ejemplo n.º 3
0
 private void DoCommandProcess(IRoomCommand command)
 {
     try
     {
         //возбуждаем событие приема команды
         AfterReceive?.Invoke(this, command);
     }
     catch (Exception e)
     {
         LogException(e);
     }
     finally
     {
         //отправляем команду обратно в пул на переиспользование
         _poolCommand.Free(command);
     }
 }
Ejemplo n.º 4
0
 protected virtual bool Response(Message message)
 {
     AfterReceive?.Invoke(this, new LinkEventArgs(message));
     return(false);
 }