private IAsyncResult BeginSendFileInternal(string fileName, byte[] preBuffer, byte[] postBuffer, TransmitFileOptions flags, AsyncCallback callback, object state) { FileStream fileStream = OpenFile(fileName); TransmitFileAsyncResult asyncResult = new TransmitFileAsyncResult(this, state, callback); asyncResult.StartPostingAsyncOp(false); SocketError errorCode = SocketPal.SendFileAsync(_handle, fileStream, preBuffer, postBuffer, flags, asyncResult); // Check for synchronous exception if (errorCode != SocketError.Success) { SocketException socketException = new SocketException((int)errorCode); UpdateStatusAfterSocketError(socketException); if (NetEventSource.IsEnabled) { NetEventSource.Error(this, socketException); } throw socketException; } asyncResult.FinishPostingAsyncOp(ref Caches.SendClosureCache); return(asyncResult); }
private IAsyncResult BeginSendFileInternal(string fileName, byte[] preBuffer, byte[] postBuffer, TransmitFileOptions flags, AsyncCallback callback, object state) { FileStream fileStream = OpenFile(fileName); TransmitFileAsyncResult asyncResult = new TransmitFileAsyncResult(this, state, callback); asyncResult.StartPostingAsyncOp(false); SocketError errorCode = SocketPal.SendFileAsync(_handle, fileStream, preBuffer, postBuffer, flags, asyncResult); // Check for synchronous exception if (!CheckErrorAndUpdateStatus(errorCode)) { throw new SocketException((int)errorCode); } asyncResult.FinishPostingAsyncOp(ref Caches.SendClosureCache); return(asyncResult); }
private async Task SendFileInternalAsync(FileStream?fileStream, byte[]?preBuffer, byte[]?postBuffer) { SocketError errorCode = SocketError.Success; using (fileStream) { // Send the preBuffer, if any // This will throw on error if (preBuffer != null && preBuffer.Length > 0) { // Using "this." makes the extension method kick in await this.SendAsync(new ArraySegment <byte>(preBuffer), SocketFlags.None).ConfigureAwait(false); } // Send the file, if any if (fileStream != null) { var tcs = new TaskCompletionSource <SocketError>(); errorCode = SocketPal.SendFileAsync(_handle, fileStream, (_, socketError) => tcs.SetResult(socketError)); if (errorCode == SocketError.IOPending) { errorCode = await tcs.Task.ConfigureAwait(false); } } } if (errorCode != SocketError.Success) { UpdateSendSocketErrorForDisposed(ref errorCode); UpdateStatusAfterSocketErrorAndThrowException(errorCode); } // Send the postBuffer, if any // This will throw on error if (postBuffer != null && postBuffer.Length > 0) { // Using "this." makes the extension method kick in await this.SendAsync(new ArraySegment <byte>(postBuffer), SocketFlags.None).ConfigureAwait(false); } }
private async Task SendFileInternalAsync(FileStream fileStream, byte[] preBuffer, byte[] postBuffer) { SocketError errorCode = SocketError.Success; using (fileStream) { // Send the preBuffer, if any // This will throw on error if (preBuffer != null && preBuffer.Length > 0) { // Using "this." makes the extension method kick in await this.SendAsync(new ArraySegment <byte>(preBuffer), SocketFlags.None).ConfigureAwait(false); } // Send the file, if any if (fileStream != null) { var tcs = new TaskCompletionSource <bool>(); // This can throw ObjectDisposedException. errorCode = SocketPal.SendFileAsync(_handle, fileStream, (bytesTransferred, socketError) => { if (socketError != SocketError.Success) { // Synchronous exception from SendFileAsync SocketException socketException = new SocketException((int)errorCode); UpdateStatusAfterSocketError(socketException); if (NetEventSource.IsEnabled) { NetEventSource.Error(this, socketException); } tcs.SetException(socketException); } tcs.SetResult(true); }); await tcs.Task.ConfigureAwait(false); } } if (errorCode != SocketError.Success) { // Synchronous exception from SendFileAsync SocketException socketException = new SocketException((int)errorCode); UpdateStatusAfterSocketError(socketException); if (NetEventSource.IsEnabled) { NetEventSource.Error(this, socketException); } throw socketException; } // Send the postBuffer, if any // This will throw on error if (postBuffer != null && postBuffer.Length > 0) { // Using "this." makes the extension method kick in await this.SendAsync(new ArraySegment <byte>(postBuffer), SocketFlags.None).ConfigureAwait(false); } }