public ServiceTask Auth <T>(T authIn)
        {
            var task = new ServiceTask(() =>
            {
                ServiceResponse response = ServiceManager.Resolve <IMessageService>().Push(MessageType.Security, authIn).ConfigureAwait(true).GetAwaiter().GetResult();
                if (response != null)
                {
                    if (response.IsSuccess)
                    {
                        var authResponse = response.Get <AuthResponse>();
                        if (authResponse != null && authResponse.Success)
                        {
                            IProtocolService protocol = ServiceManager.Resolve <IProtocolService>();
                            protocol.Config.Token     = response.Token;

                            return(response);
                        }
                    }

                    return(response);
                }

                return(new ServiceResponse()
                {
                    IsSuccess = false,
                    Error = new System.Exception()
                });
            });

            task.Start();

            return(task);
        }
Beispiel #2
0
        public Task <ServiceResponse> Push(MessageType messageType, object messageData)
        {
            IProtocolService protocolService = ServiceManager.Resolve <IProtocolService>();

            MessageIn msgIn = new MessageIn()
            {
                Id    = meesageSeed++,
                Type  = messageType,
                Token = protocolService.Config.Token,
                Data  = JsonConvert.SerializeObject(messageData)
            };

            MessageQueueItem queueItem = new MessageQueueItem()
            {
                Id = msgIn.Id
            };

            queue.Add(msgIn.Id, queueItem);

            ServiceTask task = new ServiceTask(() =>
            {
                IConnectionService service = ServiceManager.Resolve <IConnectionService>();
                service.Send(msgIn);

                try
                {
                    DateTime start = DateTime.Now;
                    while (true)
                    {
                        if ((DateTime.Now - start).TotalSeconds > 2)
                        {
                            break;
                        }

                        if (queueItem.Result != null)
                        {
                            queue.Remove(msgIn.Id);

                            if (queueItem.Result.Success == MessageSuccess.Success)
                            {
                                return(new ServiceResponse()
                                {
                                    IsSuccess = true,
                                    Token = queueItem.Result.Token,
                                    Data = queueItem.Result.Data
                                });
                            }
                            else
                            {
                                var data           = JsonConvert.DeserializeObject <MessageError>(queueItem.Result.Data);
                                ExceptionCode code = ExceptionCode.Unknown;

                                if (queueItem.Result.Success == MessageSuccess.SeverError)
                                {
                                    code = ExceptionCode.ServerError;
                                }
                                else if (queueItem.Result.Success == MessageSuccess.DataInvalid)
                                {
                                    code = ExceptionCode.DataInvalid;
                                }

                                return(new ServiceResponse()
                                {
                                    IsSuccess = false,
                                    Token = queueItem.Result.Token,
                                    Error = new MessageException(code, data.ErrorMessage ?? code.ToString())
                                });
                            }
                        }

                        Task.Delay(10).Wait();
                    }

                    queue.Remove(msgIn.Id);

                    throw new MessageException(ExceptionCode.Timeout);
                }
                catch (Exception ex)
                {
                    return(new ServiceResponse()
                    {
                        IsSuccess = false,
                        Error = ex
                    });
                }
            });

            task.Start();

            return(task);
        }