示例#1
0
        private Telegram ProcessTelegram(Telegram telegram)
        {
            if (telegram == null)
            {
                throw new ArgumentNullException("telegram");
            }

            // заполняем служебные поля телеграммы
            FillSysFields(telegram);

            // сериализуем телеграмму в бинарный формат
            var tmp = GSerialize.SerializeBytes(telegram);

            var client     = GetClient();
            var resultData = client.ProcessTelegramAsync(tmp);
            var waitTime   = telegram.TimeOut ?? DefaultTimeout;

            if (!resultData.Wait(waitTime == 0 ? -1 : waitTime))
            {
                throw new TimeoutException(ExceptionResources.TimeoutExceptionMessage);
            }

            var data = resultData.Result;

            if (data == null)
            {
                return(null);
            }

            var resultTelegram = GSerialize.DeserializeBytes <Telegram>(data);

            return(resultTelegram);
        }
示例#2
0
 public byte[] SetMessage <T>(SocketMessageTemplate.ActionType action, T result)
 {
     return(SetMessage(new SocketMessageTemplate
     {
         Action = (int)action,
         Result = typeof(T) == typeof(byte[]) ? result as byte[] : GSerialize.SerializeBytes(result),
     }));
 }
示例#3
0
        private async Task <byte[]> ProcessTelegramTaskBasedAsync(byte[] data)
        {
            try
            {
                _processes++;
                if (_processes > 1)
                {
                    Log.DebugFormat("Parallel processing of {0} telegrams", _processes);
                }

                _channelTimer.Change(_channelTimeout, _channelTimeout);
                if (_manager == null)
                {
                    throw new OperationException("IServiceManager is not initialized");
                }

                return(await Task.Factory.StartNew(() =>
                {
                    if (data == null || data.Length == 0)
                    {
                        Log.Warn("Receive clear data (null or length=0");
                        return null;
                    }

                    //жестко обрубаем поток, в случае отмены операции
                    using (_cancelOperation.Token.Register(Thread.CurrentThread.Abort))
                    {
                        // разжимаем в телеграмму
                        var telegram = GSerialize.DeserializeBytes <Telegram>(data);

                        // обрабатываем
                        var resultTelegram = _manager.ProcessTelegram(telegram);

                        // сериализуем ответ
                        return GSerialize.SerializeBytes(resultTelegram);
                    }
                }, _cancelOperation.Token));
            }
            catch (Exception ex)
            {
                if (_cancelOperation.IsCancellationRequested)
                {
                    Log.DebugFormat("All processes were cancelled for client session '{0}'", _sessionId);
                }
                else
                {
                    Log.Debug(ex);
                }
                return(null);
            }
            finally
            {
                _processes--;
            }
        }
示例#4
0
 public byte[] SetMessage(SocketMessageTemplate message)
 {
     return(message == null ? null : GSerialize.SerializeBytes(message));
 }