Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches

◆ AsyncModeCopyToAsync()

static unsafe async Task System.IO.Strategies.FileStreamHelpers.AsyncModeCopyToAsync ( SafeFileHandle handle,
bool canSeek,
long filePosition,
Stream destination,
int bufferSize,
CancellationToken cancellationToken )
inlinestaticpackage

Definition at line 325 of file FileStreamHelpers.cs.

326 {
327 AsyncCopyToAwaitable readAwaitable = new AsyncCopyToAwaitable(handle);
328 if (canSeek)
329 {
331 }
335 try
336 {
337 if (cancellationToken.CanBeCanceled)
338 {
339 cancellationReg = cancellationToken.UnsafeRegister(delegate(object s)
340 {
341 AsyncCopyToAwaitable asyncCopyToAwaitable = (AsyncCopyToAwaitable)s;
342 lock (asyncCopyToAwaitable.CancellationLock)
343 {
344 if (asyncCopyToAwaitable._nativeOverlapped != null)
345 {
347 }
348 }
349 }, readAwaitable);
350 }
351 while (true)
352 {
353 cancellationToken.ThrowIfCancellationRequested();
354 readAwaitable.ResetForNextOperation();
355 try
356 {
357 readAwaitable._nativeOverlapped = handle.ThreadPoolBinding.AllocateNativeOverlapped(awaitableOverlapped);
358 if (canSeek)
359 {
360 readAwaitable._nativeOverlapped->OffsetLow = (int)readAwaitable._position;
361 readAwaitable._nativeOverlapped->OffsetHigh = (int)(readAwaitable._position >> 32);
362 }
363 if (ReadFileNative(handle, copyBuffer, readAwaitable._nativeOverlapped, out var errorCode) < 0)
364 {
365 switch (errorCode)
366 {
367 case 38:
368 case 109:
369 readAwaitable.MarkCompleted();
370 break;
371 default:
372 throw Win32Marshal.GetExceptionForWin32Error(errorCode, handle.Path);
373 case 997:
374 break;
375 }
376 }
378 switch (readAwaitable._errorCode)
379 {
380 case 995u:
381 throw new OperationCanceledException(cancellationToken.IsCancellationRequested ? cancellationToken : new CancellationToken(canceled: true));
382 default:
383 throw Win32Marshal.GetExceptionForWin32Error((int)readAwaitable._errorCode, handle.Path);
384 case 0u:
385 case 38u:
386 case 109u:
387 {
388 int numBytes = (int)readAwaitable._numBytes;
389 if (numBytes == 0)
390 {
391 return;
392 }
393 if (canSeek)
394 {
396 }
397 break;
398 }
399 }
400 }
401 finally
402 {
404 lock (readAwaitable.CancellationLock)
405 {
406 nativeOverlapped = readAwaitable._nativeOverlapped;
408 }
409 if (nativeOverlapped != null)
410 {
411 handle.ThreadPoolBinding.FreeNativeOverlapped(nativeOverlapped);
412 }
413 }
414 await destination.WriteAsync(new ReadOnlyMemory<byte>(copyBuffer, 0, (int)readAwaitable._numBytes), cancellationToken).ConfigureAwait(continueOnCapturedContext: false);
415 }
416 }
417 finally
418 {
419 cancellationReg.Dispose();
420 awaitableOverlapped.Dispose();
422 }
423 }
static unsafe bool CancelIoEx(SafeHandle handle, NativeOverlapped *lpOverlapped)
static ArrayPool< T > Shared
Definition ArrayPool.cs:7
static unsafe int ReadFileNative(SafeFileHandle handle, Span< byte > bytes, NativeOverlapped *overlapped, out int errorCode)

References Interop.Kernel32.CancelIoEx(), System.cancellationToken, System.destination, System.Runtime.Serialization.Dictionary, System.IO.Win32Marshal.GetExceptionForWin32Error(), System.handle, System.IO.Strategies.FileStreamHelpers.ReadFileNative(), System.s, System.IO.Strategies.FileStreamHelpers.AsyncCopyToAwaitable.s_callback, and System.Buffers.ArrayPool< T >.Shared.

Referenced by System.IO.Strategies.AsyncWindowsFileStreamStrategy.AsyncModeCopyToAsync(), and System.IO.Strategies.Net5CompatFileStreamStrategy.AsyncModeCopyToAsync().