Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

To ensure thread safety change usages of stringbuilder to string. #6

Merged
merged 1 commit into from
Jan 19, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions SerialPortRx/SerialPortRxMixins.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static class SerialPortRxMixins
/// <returns>A string made up from the char values between the start and end chars</returns>
public static IObservable<string> BufferUntil(this IObservable<char> @this, IObservable<char> startsWith, IObservable<char> endsWith, int timeOut) => Observable.Create<string>(o => {
var dis = new CompositeDisposable();
var sb = new StringBuilder();
var str = "";

var startFound = false;
var elapsedTime = 0;
Expand All @@ -61,19 +61,19 @@ public static IObservable<string> BufferUntil(this IObservable<char> @this, IObs
elapsedTime = 0;
if (startFound || s == startsWithL) {
startFound = true;
sb.Append(s);
str = str + s;
if (s == endsWithL) {
o.OnNext(sb.ToString());
o.OnNext(str);
startFound = false;
sb.Clear();
str = "";
}
}
}).AddTo(dis);
Observable.Interval(TimeSpan.FromMilliseconds(1)).Subscribe(_ => {
elapsedTime++;
if (elapsedTime > timeOut) {
startFound = false;
sb.Clear();
str = "";
elapsedTime = 0;
}
}).AddTo(dis);
Expand All @@ -93,7 +93,7 @@ public static IObservable<string> BufferUntil(this IObservable<char> @this, IObs
/// <returns>A string made up from the char values between the start and end chars</returns>
public static IObservable<string> BufferUntil(this IObservable<char> @this, IObservable<char> startsWith, IObservable<char> endsWith, IObservable<string> defaultValue, int timeOut) => Observable.Create<string>(o => {
var dis = new CompositeDisposable();
var sb = new StringBuilder();
string str = "";

var startFound = false;
var elapsedTime = 0;
Expand All @@ -110,11 +110,11 @@ public static IObservable<string> BufferUntil(this IObservable<char> @this, IObs
elapsedTime = 0;
if (startFound || s == startsWithL) {
startFound = true;
sb.Append(s);
str = str + s;
if (s == endsWithL) {
o.OnNext(sb.ToString());
o.OnNext(str);
startFound = false;
sb.Clear();
str = "";
}
}
}).AddTo(dis);
Expand All @@ -124,7 +124,7 @@ public static IObservable<string> BufferUntil(this IObservable<char> @this, IObs
if (elapsedTime > timeOut) {
o.OnNext(defaultValueL);
startFound = false;
sb.Clear();
str = "";
elapsedTime = 0;
}
}).AddTo(dis);
Expand Down