private ISdkTicket SendTicketBlockingBase(ISdkTicket ticket) { var stopwatch = Stopwatch.StartNew(); var responseTimeout = SendTicketBase(ticket, true); var autoResetEvent = new AutoResetEvent(false); _autoResetEventsForBlockingRequests.TryAdd(ticket.TicketId, autoResetEvent); autoResetEvent.WaitOne(TimeSpan.FromMilliseconds(responseTimeout)); ISdkTicket responseTicket; if (_responsesFromBlockingRequests.TryRemove(ticket.TicketId, out responseTicket)) { stopwatch.Stop(); ReleaseAutoResetEventFromDictionary(ticket.TicketId); _executionLog.LogDebug($"Sending in blocking mode and successfully received response for {ticket.GetType().Name} {ticket.TicketId} took {stopwatch.ElapsedMilliseconds} ms."); return(responseTicket); } stopwatch.Stop(); ReleaseAutoResetEventFromDictionary(ticket.TicketId); _executionLog.LogDebug($"Sending in blocking mode and waiting for receiving response for {ticket.GetType().Name} {ticket.TicketId} took {stopwatch.ElapsedMilliseconds} ms. Response not received in required timeout."); var msg = $"The timeout for receiving response elapsed. Org. {ticket.GetType().Name}: {ticket.TicketId}."; _executionLog.LogInformation(msg); throw new TimeoutException(msg); }
/// <summary> /// Gets the byte MSG /// </summary> /// <param name="sdkTicket">The SDK ticket</param> /// <returns>System.Byte[]</returns> protected byte[] GetByteMsg(ISdkTicket sdkTicket) { var json = GetMappedDtoJsonMsg(sdkTicket); if (_feedLog.IsDebugEnabled) { _feedLog.Debug($"Sending {sdkTicket.GetType().Name}: {json}"); } else { _feedLog.Info($"Sending {sdkTicket.GetType().Name} with ticketId: {sdkTicket.TicketId}."); } if (_executionLog.IsDebugEnabled) { _executionLog.Debug($"Sending {sdkTicket.GetType().Name}: {json}"); } else { _executionLog.Info($"Sending {sdkTicket.GetType().Name} with ticketId: {sdkTicket.TicketId}."); } var msg = Encoding.UTF8.GetBytes(json); return(msg); }
private int SendTicketBase(ISdkTicket ticket, bool waitForResponse) { if (!ConnectionStatus.IsConnected) { var msg = $"Sending {ticket.GetType().Name} with ticketId: {ticket.TicketId} failed. Reason: disconnected from server."; _executionLog.LogWarning(msg); throw new InvalidOperationException(msg); } _executionLog.LogInformation($"Sending {ticket.GetType().Name} with ticketId: {ticket.TicketId}."); var ticketType = TicketHelper.GetTicketTypeFromTicket(ticket); var ticketSender = _ticketPublisherFactory.GetTicketSender(ticketType); ticketSender.SendTicket(ticket); var ticketCacheTimeout = ticketSender.GetCacheTimeout(ticket); if (waitForResponse) { return(ticketCacheTimeout); } else { lock (_lockForTicketsForNonBlockingRequestsCache) { if (TicketResponseTimedOut != null) { _cacheItemPolicyForTicketsForNonBlockingRequestsCache = new CacheItemPolicy { AbsoluteExpiration = new DateTimeOffset(DateTime.Now.AddMilliseconds(ticketCacheTimeout)), RemovedCallback = RemovedFromCacheForTicketsForNonBlockingRequestsCallback }; _ticketsForNonBlockingRequests.Add(ticket.TicketId, ticket, _cacheItemPolicyForTicketsForNonBlockingRequestsCache); } } } return(-1); }
public static TicketResponseType Convert(ISdkTicket ticket) { if (ticket == null) { throw new ArgumentNullException(nameof(ticket), "Ticket must not be null."); } if (ticket is ITicketResponse) { return(TicketResponseType.Ticket); } if (ticket is ITicketCancelResponse) { return(TicketResponseType.TicketCancel); } if (ticket is ITicketCashoutResponse) { return(TicketResponseType.TicketCashout); } if (ticket is ITicketNonSrSettleResponse) { return(TicketResponseType.TicketNonSrSettle); } throw new InvalidEnumArgumentException($"Invalid ticket type. Ticket type: {ticket.GetType()}."); }