private IAsyncResult BeginDownLevelSendFile( string fileName, bool flowContext, AsyncCallback callback, object state) { if(Logging.On)Logging.Enter(Logging.Sockets, this, "BeginSendFile", ""); if (CleanedUp) { throw new ObjectDisposedException(this.GetType().FullName); } if (!Connected) { throw new NotSupportedException(SR.GetString(SR.net_notconnected)); } GlobalLog.Print("Socket#" + ValidationHelper.HashString(this) + "::BeginDownLevelSendFile() SRC:" + ValidationHelper.ToString(LocalEndPoint) + " DST:" + ValidationHelper.ToString(RemoteEndPoint) + " fileName:" + fileName); FileStream fileStream = null; if (fileName != null && fileName.Length>0) { fileStream = new FileStream(fileName,FileMode.Open,FileAccess.Read,FileShare.Read); } DownLevelSendFileAsyncResult asyncResult = null; IAsyncResult fileResult = null; try { asyncResult = new DownLevelSendFileAsyncResult(fileStream, this, state, callback); // If we're flowing context, indicate that here. if (flowContext) { asyncResult.StartPostingAsyncOp(false); } fileResult = fileStream.BeginRead(asyncResult.buffer, 0, asyncResult.buffer.Length, new AsyncCallback(DownLevelSendFileCallback), asyncResult); } catch(Exception e) { if (!NclUtilities.IsFatal(e)) { DownLevelSendFileCleanup(fileStream); } throw; } if (fileResult.CompletedSynchronously) { DoDownLevelSendFileCallback(fileResult, asyncResult); } // Finished without throwing - seal up the result and give it out. NOP if we're not flowing. asyncResult.FinishPostingAsyncOp(ref Caches.SendClosureCache); if(Logging.On)Logging.Exit(Logging.Sockets, this, "BeginSendFile",0); return asyncResult; }
private IAsyncResult BeginDownLevelSendFile(string fileName, bool flowContext, AsyncCallback callback, object state) { if (s_LoggingEnabled) { Logging.Enter(Logging.Sockets, this, "BeginSendFile", ""); } if (this.CleanedUp) { throw new ObjectDisposedException(base.GetType().FullName); } if (!this.Connected) { throw new NotSupportedException(SR.GetString("net_notconnected")); } FileStream stream = null; if ((fileName != null) && (fileName.Length > 0)) { stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read); } DownLevelSendFileAsyncResult result = null; IAsyncResult result2 = null; try { result = new DownLevelSendFileAsyncResult(stream, this, state, callback); if (flowContext) { result.StartPostingAsyncOp(false); } result2 = stream.BeginRead(result.buffer, 0, result.buffer.Length, new AsyncCallback(Socket.DownLevelSendFileCallback), result); } catch (Exception exception) { if (!NclUtilities.IsFatal(exception)) { DownLevelSendFileCleanup(stream); } throw; } if (result2.CompletedSynchronously) { DoDownLevelSendFileCallback(result2, result); } result.FinishPostingAsyncOp(ref this.Caches.SendClosureCache); if (s_LoggingEnabled) { Logging.Exit(Logging.Sockets, this, "BeginSendFile", 0); } return result; }
private static void DoDownLevelSendFileCallback(IAsyncResult result, DownLevelSendFileAsyncResult context) { try { while (true) { if (!context.writing) { int read = context.fileStream.EndRead(result); if (read > 0) { context.writing = true; result = context.socket.BeginSend(context.buffer, 0, read, SocketFlags.None, new AsyncCallback(DownLevelSendFileCallback), context); if (!result.CompletedSynchronously) break; } else { DownLevelSendFileCleanup(context.fileStream); context.InvokeCallback(); break; } } else { int written = context.socket.EndSend(result); context.writing = false; result = context.fileStream.BeginRead(context.buffer, 0, context.buffer.Length, new AsyncCallback(DownLevelSendFileCallback), context); if (!result.CompletedSynchronously) break; } } } catch (Exception exception) { if (NclUtilities.IsFatal(exception)) throw; DownLevelSendFileCleanup(context.fileStream); context.InvokeCallback(exception); } }
private static void DoDownLevelSendFileCallback(IAsyncResult result, DownLevelSendFileAsyncResult context) { try { Label_0000: if (!context.writing) { int size = context.fileStream.EndRead(result); if (size > 0) { context.writing = true; result = context.socket.BeginSend(context.buffer, 0, size, SocketFlags.None, new AsyncCallback(Socket.DownLevelSendFileCallback), context); if (result.CompletedSynchronously) { goto Label_0000; } } else { DownLevelSendFileCleanup(context.fileStream); context.InvokeCallback(); } } else { context.socket.EndSend(result); context.writing = false; result = context.fileStream.BeginRead(context.buffer, 0, context.buffer.Length, new AsyncCallback(Socket.DownLevelSendFileCallback), context); if (result.CompletedSynchronously) { goto Label_0000; } } } catch (Exception exception) { if (NclUtilities.IsFatal(exception)) { throw; } DownLevelSendFileCleanup(context.fileStream); context.InvokeCallback(exception); } }