/// <summary>Sends a request to the EFT-Client</summary> /// <param name="request">The <see cref="EFTRequest"/> to send</param> /// <param name="member">Used for internal logging. Ignore</param> /// <returns>FALSE if an error occurs</returns> public async Task <bool> WriteRequestAsync(EFTRequest request, [CallerMemberName] string member = "") { SetCurrentRequest(request); string msgString = ""; try { msgString = (request is EFTPosAsPinpadRequest) ? _parser.EFTRequestToXMLString(request) : _parser.EFTRequestToString(request); } catch (Exception e) { Log(LogLevel.Error, tr => tr.Set($"An error occured parsing the request", e)); throw; } Log(LogLevel.Debug, tr => tr.Set($"Tx {msgString}")); // Send the request string to the IP client. try { await _clientStream.WriteRequestAsync(msgString); } catch (Exception e) { Log(LogLevel.Error, tr => tr.Set($"An error occured sending the request", e)); Disconnect(); throw new ConnectionException(e.Message, e.InnerException); } return(true); }
/// <summary>Sends a request to the EFT-Client</summary> /// <param name="request">The <see cref="EFTRequest"/> to send</param> /// <param name="member">Used for internal logging. Ignore</param> /// <returns>FALSE if an error occurs</returns> public bool DoRequest(EFTRequest request, [CallerMemberName] string member = "") { SetCurrentRequest(request); Log(LogLevel.Info, tr => tr.Set($"Request via {member}")); // Save the current synchronization context so we can use it to send events syncContext = System.Threading.SynchronizationContext.Current; if (requestInProgess) { Log(LogLevel.Error, tr => tr.Set("Ignored, request already in progress")); return(false); } if (!IsConnected) { Log(LogLevel.Info, tr => tr.Set($"Not connected in {member} request, trying to connect now...")); if (!Connect()) { Log(LogLevel.Error, tr => tr.Set("Connect failed")); return(false); } } var r = SendIPClientRequest(request); requestInProgess = r; return(r); }
/// <summary> /// Sends a request to the EFT-Client, and waits for the next EFT response of type T from the client. /// All other response types are discarded.. This function is useful for request/response pairs /// where other message types are not being handled (such as SetDialogRequest/SetDialogResponse). /// /// Since receipts and dialog messages cannot be handled with this function, it is not /// recommended for use with transaction, logon, settlement ets /// </summary> /// <remarks> /// This function will continue to wait until either a message is received, or an exception is thrown (e.g. socket disconnect, cancellationToken fires). /// It is important to ensure cancellationToken is configured correctly. /// </remarks> /// <typeparam name="T">The type of EFTResponse to wait for</typeparam> /// <param name="request">The <see cref="EFTRequest"/> to send</param> /// <param name="cancellationToken">The token to monitor for cancellation requests</param> /// <param name="member">Used for internal logging. Ignore</param> /// <returns> An EFTResponse if one could be read, otherwise null </returns> /// <exception cref="ConnectionException">The socket is closed.</exception> /// <exception cref="TaskCanceledException">The cancellation token was cancelled before the task completed</exception> public async Task <T> WriteRequestAndWaitAsync <T>(EFTRequest request, System.Threading.CancellationToken cancellationToken, [CallerMemberName] string member = "") where T : EFTResponse { if (!await WriteRequestAsync(request, member)) { return(null); } return(await ReadResponseAsync <T>(cancellationToken)); }
private void SetCurrentRequest(EFTRequest request) { // Always set _currentRequest to the last request we send currentRequest = request; if (request.GetIsStartOfTransactionRequest()) { _currentStartTxnRequest = request; } }
bool SendIPClientRequest(EFTRequest eftRequest) { // Store current request. this.currentRequest = eftRequest; // Build request var requestString = ""; try { requestString = _parser.EFTRequestToString(eftRequest); } catch (Exception e) { Log(LogLevel.Error, tr => tr.Set($"An error occured parsing the request", e)); throw; } Log(LogLevel.Debug, tr => tr.Set($"Tx {requestString}")); // Send the request string to the IP client. return(socket.Send(requestString)); }