private static readonly Regex tagRegex = new Regex($"^\\{TikSpecialProperties.Tag}=(?<TAG>.+)$"); // .tag=1234 public IEnumerable <ITikSentence> CallCommandSync(params string[] commandRows) { EnsureOpened(); //read .tag from commandRows - if present var tagOrEmptyString = string.Empty; foreach (var row in commandRows) { var match = tagRegex.Match(row); if (match.Success) { tagOrEmptyString = match.Groups["TAG"].Value; break; } } if (_sendTagWithSyncCommand && string.IsNullOrEmpty(tagOrEmptyString)) { tagOrEmptyString = TagSequence.Next().ToString(); commandRows = commandRows.Concat(new string[] { string.Format("{0}={1}", TikSpecialProperties.Tag, tagOrEmptyString) }).ToArray(); } lock (_writeLockObj) { WriteCommand(commandRows); } return(GetAll(tagOrEmptyString).ToList()); }
public void ExecuteAsync(Action <ITikReSentence> oneResponseCallback, Action <ITikTrapSentence> errorCallback = null, Action onDoneCallback = null) { EnsureConnectionSet(); EnsureNotRunning(); System.Diagnostics.Debug.Assert(_asyncLoadingThread == null); int tag = TagSequence.Next(); _isRuning = true; _asynchronouslyRunningTag = tag; try { string[] commandRows = ConstructCommandText(TikCommandParameterFormat.NameValue); _asyncLoadingThread = _connection.CallCommandAsync(commandRows, tag.ToString(), response => { ApiReSentence reResponse = response as ApiReSentence; if (reResponse != null) { if (oneResponseCallback != null) { oneResponseCallback(reResponse); } } else { ApiTrapSentence trapResponse = response as ApiTrapSentence; if (trapResponse != null) { if (trapResponse.CategoryCode == "2" && trapResponse.Message == "interrupted") { //correct state - async operation has been Cancelled. } else { //incorrect - any error occurs if (errorCallback != null) { errorCallback(trapResponse); } } } else if (response is ApiDoneSentence || response is ApiFatalSentence) { //REMARKS: we are expecting !trap + !done sentences when any error occurs _isRuning = false; _asynchronouslyRunningTag = -1; _asyncLoadingThread = null; if (response is ApiDoneSentence && onDoneCallback != null) { onDoneCallback(); } } } }); } catch { _isRuning = false; _asynchronouslyRunningTag = -1; throw; } finally { //still running } }