void ProcessEFTResponse(PINTResponse response) { try { switch (response) { case null: logger.Error($"Unable to handle null param {nameof(response)}"); break; //case SetDialogResponse r: // if (currentRequest is SetDialogRequest) // { // gotResponse = true; // hideDialogEvent.Set(); // } // break; //case PINTReceiptResponse r: // SendReceiptAcknowledgement(); // logger.Error($"IsPrePrint={((PINTReceiptResponse)response).IsPrePrint}"); // if (r.IsPrePrint == false) // { // FireClientResponseEvent(nameof(OnReceipt), OnReceipt, new EFTEventArgs<EFTReceiptResponse>(r)); // } // break; case PINPADDisplayResponse r: //DialogUIHandler.HandleDisplayResponse(r); FireClientResponseEvent(nameof(OnDisplay), OnDisplay, new EFTEventArgs <PINPADDisplayResponse>(r)); break; case PINPADLogonResponse r: FireClientResponseEvent(nameof(OnLogon), OnLogon, new EFTEventArgs <PINPADLogonResponse>(r)); break; //case ECRTransactionRequest r: // FireClientResponseEvent(nameof(OnTransaction), OnTransaction, new EFTEventArgs<ECRTransactionRequest>(r)); // break; //case TerminalGetLastTransactionResponse r: // FireClientResponseEvent(nameof(OnGetLastTransaction), OnGetLastTransaction, new EFTEventArgs<EFTGetLastTransactionResponse>(r)); // break; //case EFTStatusResponse r: // FireClientResponseEvent(nameof(OnStatus), OnStatus, new EFTEventArgs<EFTStatusResponse>(r)); // break; default: logger.Error($"Unknown response type", response); break; } } catch (Exception Ex) { logger.Error($"Unhandled error in {nameof(ProcessEFTResponse)}"); } }
bool ReceiveEFTResponse(byte[] data) { // Clear the receive buffer if 5 seconds has lapsed since the last message var tc = System.Environment.TickCount; if (tc - recvTickCount > 5000) { logger.Error($"Data is being cleared from the buffer due to a timeout. Content {recvBuf.ToString()}"); recvBuf = ""; } recvTickCount = System.Environment.TickCount; // Append receive data to our buffer recvBuf += System.Text.Encoding.ASCII.GetString(data, 0, data.Length); // Keep parsing until no more characters try { int index = 0; while (index < recvBuf.Length) { // Look for start character if (recvBuf[index] == (byte)'#') { // Check that we have enough bytes to validate length, if not wait for more if (recvBuf.Length < index + 5) { recvBuf = recvBuf.Substring(index); break; } // We have enough bytes to check for length index++; // Try to get the length of the new message. If it's not a valid length // we might have some corrupt data, keep checking for a valid message if (!int.TryParse(recvBuf.Substring(index, 4), out int length) || length <= 5) { continue; } // We have a valid length index += 4; // If our buffer doesn't contain enough data, wait for more if (recvBuf.Length < index + length - 5) { recvBuf = recvBuf.Substring(index - 5); continue; } // We have a valid response var response = recvBuf.Substring(index, length - 5); FireOnTcpReceive(response); // Process the response PINTResponse eftResponse = null; try { eftResponse = _parser.StringToEFTResponse(response); ProcessEFTResponse(eftResponse); if (eftResponse.GetType() == _currentStartTxnRequest?.GetPairedResponseType()) { // dialogUIHandler.HandleCloseDisplay(); } } catch (ArgumentException argumentException) { logger.Error("Error parsing response string", argumentException); } index += length - 5; } // Clear our buffer if we are all done if (index == recvBuf.Length) { recvBuf = ""; } } } catch (Exception ex) { // Fail gracefully. FireOnSocketFailEvent(ClientIPErrorType.Client_ParseError, ex.Message); logger.Error($"Exception (ReceiveEFTResponse): {ex.Message}"); return(false); } return(true); }