Esempio n. 1
0
        /*private async void HearthBit(Object source, ElapsedEventArgs e)
         * {
         *  try
         *  {
         *      await WriteServer(ServerCommands.HearthBit, ServerCommands.HearthBit, "");
         *  }
         *  catch (Exception exception)
         *  {
         *      ConsoleEx.WriteError("Nao consegui bater", exception);
         *  }
         * }
         *
         * private async void HearthBitCheck(Object source, ElapsedEventArgs e)
         * {
         *  try
         *  {
         *      if (_hearthBitCount >= _heartBitTimeOut)
         *      {
         *          _timerHeartBitCheck.Stop();
         *          ConsoleEx.WriteLine("Dispose do HB");
         *          Dispose();
         *      }
         *      else
         *      {
         *          _hearthBitCount += 1;
         *      }
         *  }
         *  catch (Exception exception)
         *  {
         *      ConsoleEx.WriteError("Nao consegui bater", exception);
         *  }
         * }
         */
        /// <summary>
        /// Escreve o buffer no socket
        /// </summary>
        private async void WriteBuffer(Object source, ElapsedEventArgs e)
        {
            string command = "";

            try
            {
                if (_bWriteBuffer)
                {
                    return;
                }
                try
                {
                    _bWriteBuffer = true;
                    if (_bufferWrite.Count > 0)
                    {
                        command = _bufferWrite.Dequeue();
                        //ConsoleEx.WriteLine("Escrevi" + command);

                        if (command != null)
                        {
                            string data = await StringCompressionAsync.CompressString(command);

                            await _sw.WriteLineAsync(data).ConfigureAwait(false);

                            await _sw.FlushAsync().ConfigureAwait(false);
                        }
                    }
                }
                finally
                {
                    _bWriteBuffer = false;
                }
            }
            catch (Exception exception)
            {
                ConsoleEx.WriteLine("Tentei escrever | " + command);
                _timerWrite.Enabled = false;
                Dispose();
                //ConsoleEx.WriteError("Erro no WriteBuffer", exception);
            }
        }
Esempio n. 2
0
        public async Task <Command> GetApiWaitCommand(string id)
        {
            Command cmd = null;

            _isWait = true;
            int timeCount = 0;

            //while (cmd == null && timeCount <= ServerCommands.TimeOutApiRequest)
            //{
            //    cmd = _waitCommandsList.First(c => c.Id == id);
            //    await Task.Delay(100);
            //    timeCount++;
            //}
            do
            {
                var data        = _sr?.ReadLine();
                var dataDescomp = await StringCompressionAsync.DecompressString(data);

                cmd = await SerializerAsync.DeserializeJson <Command>(dataDescomp);

                if (cmd != null && !cmd.IsWait)
                {
                    DelegateAction del;
                    if (!string.IsNullOrEmpty(cmd.Id) &&
                        _srvsCmdsBuffer.TryGetValue(cmd.Id, out del))
                    {
                        del.server[0] = dataDescomp;
                        del.Exec();
                    }
                    else if (_srvsCmdsBuffer.TryGetValue(ServerCommands.BroadCastId, out del))
                    {
                        del.server[0] = dataDescomp;
                        del.Exec();
                    }
                }
            } while (cmd != null && !cmd.IsWait);
            _isWait = false;

            return(cmd);
        }
Esempio n. 3
0
        private async void Read(Object source, ElapsedEventArgs e)
        {
            try
            {
//                _timerRead.Enabled = false;

                if (!_isWait && _clientSocket?.Client?.Available > 0)
                {
                    _hearthBitCount = 0;

                    var data = _sr?.ReadLine();

                    string dataDescomp = await StringCompressionAsync.DecompressString(data);

                    try
                    {
                        var bt = await SerializerAsync.DeserializeJson <Command>(dataDescomp);

                        if (bt != null)
                        {
                            if (!bt.Cmd.Equals(ServerCommands.HearthBit))
                            {
                                //_bufferRead.Enqueue(dataDescomp);
//                            ConsoleEx.WriteLine("Olha o que eu coloquei aqui: " +dataDescomp);


                                DelegateAction del;
                                if (!string.IsNullOrEmpty(bt.Id) &&
                                    _srvsCmdsBuffer.TryGetValue(bt.Id, out del))
                                {
                                    del.server[0] = dataDescomp;
                                    del.Exec();
                                }
                                else if (_srvsCmdsBuffer.TryGetValue(ServerCommands.BroadCastId, out del))
                                {
                                    del.server[0] = dataDescomp;
                                    del.Exec();
                                }
                            }
                        }
                        else
                        {
                            _bufferRead.Enqueue(dataDescomp);
                        }
                    }
                    catch (Exception ex)
                    {
                        ConsoleEx.WriteError("Read", ex);
                        //Dispose();
                    }
                }
            }
            catch (Exception exception)
            {
                ConsoleEx.WriteError("erro do Read:", exception);
                //Dispose();
            }
            finally
            {
//                _timerRead.Enabled = true;
            }
        }