question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Hi,

It seems that some of the useful APIs are missing from Theraot.Core:

  • System.Threading.Tasks.TaskEx.CompletedTask property
  • System.Threading.Tasks.TaskEx.FromCanceled method group
  • System.Threading.Tasks.TaskEx.FromException method group
  • SemaphoreSlim.WaitAsync (as extension method)
  • System.Runtime.CompilerServices.MethodImplAttribute (adding this will save trouble from doing #if NET40 || NET35 || … compiler macros)
  • System.IO.Stream.ReadAsync(Byte[], Int32, Int32, CancellationToken) (as extension method)
  • System.IO.Stream.WriteAsync(Byte[], Int32, Int32, CancellationToken) (as extension method)
  • System.IO.Stream.FlushAsync(CancellationToken) (as extension method)
  • System.Net.Security.SslStream.AuthenticateAsClientAsync(String, System.Security.Cryptography.X509Certificates.X509CertificateCollection, System.Security.Authentication.SslProtocols, Boolean) (as extension method)
  • System.Span<T> and System.ReadOnlySpan<T> - I’m not sure if these are feasible/possible to port back, as they utilize some new JIT instrics

Here is some code for extension methods taken straight from my libraries. They’re not perfect especially performance-wise, but are a good start.

   public static async Task WaitAsync(
      this SemaphoreSlim semaphore,
      Int32 tick = 100
      ) {
      while ( !semaphore.Wait( 0 ) )
      {
         await TaskEx.Delay( tick );
      }
   }

   public static async Task<Boolean> WaitAsync(
      this SemaphoreSlim semaphore,
      TimeSpan timeout,
      CancellationToken token = default,
      Int32 tick = 100
      ) {
      var curTimeout = 0L;
      Boolean retVal;
      var noTimeout = TimeSpan.FromMilliseconds( -1 ) == timeout;
      while ( !( retVal = semaphore.Wait( 0 ) ) && ( noTimeout || curTimeout < timeout.TotalMilliseconds ) )
      {
         // Delay is not precise (waits _at least_ given amount), TODO refactor this to use DateTime.UtcNow
         await TaskEx.Delay( tick, token );
         curTimeout += tick;
      }

      return retVal && curTimeout < timeout.TotalMilliseconds;
   }

      public static Task<Int32> ReadAsync(
         this Stream stream,
         Byte[] buffer,
         Int32 offset,
         Int32 count,
         CancellationToken token
         )
      {
         token.ThrowIfCancellationRequested();
         // Bind parameters to tuple to avoid allocation of delegate class
         var readArgs = (stream, buffer, offset, count);
         return Task.Factory.FromAsync(
           ( rArgs, cb, state ) => rArgs.Item1.BeginRead( rArgs.Item2, rArgs.Item3, rArgs.Item4, cb, state ),
           ( result ) => ( (Stream) result.AsyncState ).EndRead( result ),
           readArgs,
           stream
           );
      }

      public static Task WriteAsync(
         this Stream stream,
         Byte[] buffer,
         Int32 offset,
         Int32 count,
         CancellationToken token
         )
      {
         token.ThrowIfCancellationRequested();
         // Bind parameters to tuple to avoid allocation of delegate class
         var writeArgs = (stream, buffer, offset, count);
         return Task.Factory.FromAsync(
           ( wArgs, cb, state ) => wArgs.Item1.BeginWrite( wArgs.Item2, wArgs.Item3, wArgs.Item4, cb, state ),
           ( result ) => ( (Stream) result.AsyncState ).EndWrite( result ),
           writeArgs,
           stream
           );
      }

      public static Task FlushAsync(
         this Stream stream,
         CancellationToken token
         )
      {
         token.ThrowIfCancellationRequested();
         return TaskEx.Run( () => stream.Flush(), token );
      }

      public static Task AuthenticateAsClientAsync(
         this System.Net.Security.SslStream stream,
         String targetHost,
         System.Security.Cryptography.X509Certificates.X509CertificateCollection clientCertificates,
         System.Security.Authentication.SslProtocols enabledSslProtocols,
         Boolean checkCertificateRevocation
         // TODO no CancellationToken in official API but maybe we could provide it?
         )
      {
         // Bind parameters to tuple to avoid allocation of delegate class
         var authArgs = (stream, targetHost, clientCertificates, enabledSslProtocols, checkCertificateRevocation);
         return Task.Factory.FromAsync(
            ( aArgs, cb, state ) => aArgs.stream.BeginAuthenticateAsClient( aArgs.targetHost, aArgs.clientCertificates, aArgs.enabledSslProtocols, aArgs.checkCertificateRevocation, cb, state ),
            result => ( (System.Net.Security.SslStream) result.AsyncState ).EndAuthenticateAsClient( result ),
            authArgs,
            stream
            );
      }

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
theraotcommented, Jan 31, 2019

Most of the requested APIs has been added, they are in the lastes beta.

I opened new issues for the last missing parts.

I’m closing this.

0reactions
theraotcommented, Jan 3, 2019

@stazz

I want to report that TaskEx.CompletedTask, TaskEx.FromCanceled, TaskEx.FromException has been added.

I have also added new TaskEx.FromCancellation (that is a task that will be cancelled when the passed CancellationToken is cancelled, allowing you to pass a non-cancelled CancellationToken, while FromCanceled would throw).

And new TaskEx.FromWaitHandle that will create a task that you can use to wait on a WaitHandle.


SemaphoreSlim.WaitAsync has been added too.


I did also add MethodImplAttribute in a lot places where it makes sense…


About the Stream extensions… the main hiccup is that I do not have FromAsync. I have been experimenting with this, I hope to add it soon.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Your native host connector do not support following APIs: v6
Probably you should upgrade native host connector or install plugins for missing APIs. Refer documentation for instructions.
Read more >
State of the API: Microservices Gone Macro and Zombie APIs
Zombie APIs and microservices gone macro are among the concerns developers expressed in the latest State of the API report.
Read more >
Your native host connector do not support following APIs
You should probably upgrade native host connector or install plugins for missing APIs. Refer to the documentation for instructions.
Read more >
Your native host connector do not support following APIs: v6
Probably you should upgrade native host connector or install plugins for missing APIs. This should not prevent you installing extensions ...
Read more >
Gnome Extensions complains about my native host ...
You should probably upgrade native host connector or install plugins for missing APIs. Refer to the documentation for instructions.".
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found