-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor ExecuteWithTimeout to avoid closure.
- Loading branch information
Showing
3 changed files
with
102 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
namespace ZeroMQ | ||
{ | ||
using System; | ||
using System.Diagnostics; | ||
using System.Threading; | ||
|
||
// TODO: Refactor and use compiled expressions? | ||
internal static class ExecuteExtensions | ||
{ | ||
public delegate TResult ThirdParamOut<in T1, in T2, T3, out TResult>(T1 arg1, T2 arg2, out T3 arg3); | ||
|
||
public static TResult WithTimeout<T1, T2, TResult>(this ZmqSocket socket, Func<T1, T2, TResult> method, T1 arg1, T2 arg2, TimeSpan timeout) | ||
{ | ||
if ((int)timeout.TotalMilliseconds < 1) | ||
{ | ||
return method(arg1, arg2); | ||
} | ||
|
||
TResult receiveResult; | ||
|
||
var timer = Stopwatch.StartNew(); | ||
var spin = new SpinWait(); | ||
|
||
do | ||
{ | ||
receiveResult = method(arg1, arg2); | ||
|
||
if (socket.ReceiveStatus != ReceiveStatus.TryAgain) | ||
{ | ||
break; | ||
} | ||
|
||
spin.SpinOnce(); | ||
} | ||
while (timer.Elapsed <= timeout); | ||
|
||
return receiveResult; | ||
} | ||
|
||
public static TResult WithTimeout<T1, T2, T3, TResult>(this ZmqSocket socket, Func<T1, T2, T3, TResult> method, T1 arg1, T2 arg2, T3 arg3, TimeSpan timeout) | ||
{ | ||
if ((int)timeout.TotalMilliseconds < 1) | ||
{ | ||
return method(arg1, arg2, arg3); | ||
} | ||
|
||
TResult receiveResult; | ||
|
||
var timer = Stopwatch.StartNew(); | ||
var spin = new SpinWait(); | ||
|
||
do | ||
{ | ||
receiveResult = method(arg1, arg2, arg3); | ||
|
||
if (socket.ReceiveStatus != ReceiveStatus.TryAgain) | ||
{ | ||
break; | ||
} | ||
|
||
spin.SpinOnce(); | ||
} | ||
while (timer.Elapsed <= timeout); | ||
|
||
return receiveResult; | ||
} | ||
|
||
public static TResult WithTimeout<T1, T2, T3, TResult>(this ZmqSocket socket, ThirdParamOut<T1, T2, T3, TResult> method, T1 arg1, T2 arg2, out T3 arg3, TimeSpan timeout) | ||
{ | ||
if ((int)timeout.TotalMilliseconds < 1) | ||
{ | ||
return method(arg1, arg2, out arg3); | ||
} | ||
|
||
TResult receiveResult; | ||
|
||
var timer = Stopwatch.StartNew(); | ||
var spin = new SpinWait(); | ||
|
||
do | ||
{ | ||
receiveResult = method(arg1, arg2, out arg3); | ||
|
||
if (socket.ReceiveStatus != ReceiveStatus.TryAgain) | ||
{ | ||
break; | ||
} | ||
|
||
spin.SpinOnce(); | ||
} | ||
while (timer.Elapsed <= timeout); | ||
|
||
return receiveResult; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters