本文整理汇总了C#中System.Net.Sockets.OverlappedAsyncResult类的典型用法代码示例。如果您正苦于以下问题:C# OverlappedAsyncResult类的具体用法?C# OverlappedAsyncResult怎么用?C# OverlappedAsyncResult使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
OverlappedAsyncResult类属于System.Net.Sockets命名空间,在下文中一共展示了OverlappedAsyncResult类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SendToAsync
public static unsafe SocketError SendToAsync(SafeCloseSocket handle, byte[] buffer, int offset, int count, SocketFlags socketFlags, Internals.SocketAddress socketAddress, OverlappedAsyncResult asyncResult)
{
// Set up asyncResult for overlapped WSASendTo.
// This call will use completion ports.
asyncResult.SetUnmanagedStructures(buffer, offset, count, socketAddress, false /* don't pin RemoteEP*/);
int bytesTransferred;
SocketError errorCode = Interop.Winsock.WSASendTo(
handle,
ref asyncResult._singleBuffer,
1, // There is only ever 1 buffer being sent.
out bytesTransferred,
socketFlags,
asyncResult.GetSocketAddressPtr(),
asyncResult.SocketAddress.Size,
asyncResult.OverlappedHandle,
IntPtr.Zero);
if (errorCode != SocketError.Success)
{
errorCode = GetLastSocketError();
}
return errorCode;
}
示例2: BeginReceive
internal IAsyncResult BeginReceive(IList<ArraySegment<byte>> buffers, SocketFlags socketFlags, out SocketError errorCode, AsyncCallback callback, object state)
{
if (s_loggingEnabled)
{
Logging.Enter(Logging.Sockets, this, "BeginReceive", "");
}
if (CleanedUp)
{
throw new ObjectDisposedException(this.GetType().FullName);
}
// Validate input parameters.
if (buffers == null)
{
throw new ArgumentNullException("buffers");
}
if (buffers.Count == 0)
{
throw new ArgumentException(SR.Format(SR.net_sockets_zerolist, "buffers"), "buffers");
}
// We need to flow the context here. But we don't need to lock the context - we don't use it until the callback.
OverlappedAsyncResult asyncResult = new OverlappedAsyncResult(this, state, callback);
asyncResult.StartPostingAsyncOp(false);
// Run the receive with this asyncResult.
errorCode = DoBeginReceive(buffers, socketFlags, asyncResult);
if (errorCode != SocketError.Success && errorCode != SocketError.IOPending)
{
asyncResult = null;
}
else
{
// We're not throwing, so finish the async op posting code so we can return to the user.
// If the operation already finished, the callback will be called from here.
asyncResult.FinishPostingAsyncOp(ref Caches.ReceiveClosureCache);
}
if (s_loggingEnabled)
{
Logging.Exit(Logging.Sockets, this, "BeginReceive", asyncResult);
}
return asyncResult;
}
示例3: BeginReceiveFrom
// Routine Description:
//
// BeginReceiveFrom - Async implimentation of RecvFrom call,
//
// Called when we want to start an async receive.
// We kick off the receive, and if it completes synchronously we'll
// call the callback. Otherwise we'll return an IASyncResult, which
// the caller can use to wait on or retrieve the final status, as needed.
//
// Uses Winsock 2 overlapped I/O.
//
// Arguments:
//
// ReadBuffer - status line that we wish to parse
// Index - Offset into ReadBuffer to begin reading from
// Request - Size of Buffer to recv
// Flags - Additonal Flags that may be passed to the underlying winsock call
// remoteEP - EndPoint that are to receive from
// Callback - Delegate function that holds callback, called on completeion of I/O
// State - State used to track callback, set by caller, not required
//
// Return Value:
//
// IAsyncResult - Async result used to retreive result
internal IAsyncResult BeginReceiveFrom(byte[] buffer, int offset, int size, SocketFlags socketFlags, ref EndPoint remoteEP, AsyncCallback callback, object state)
{
if (s_loggingEnabled)
{
Logging.Enter(Logging.Sockets, this, "BeginReceiveFrom", "");
}
if (CleanedUp)
{
throw new ObjectDisposedException(this.GetType().FullName);
}
// Validate input parameters.
if (buffer == null)
{
throw new ArgumentNullException("buffer");
}
if (remoteEP == null)
{
throw new ArgumentNullException("remoteEP");
}
if (!CanTryAddressFamily(remoteEP.AddressFamily))
{
throw new ArgumentException(SR.Format(SR.net_InvalidEndPointAddressFamily, remoteEP.AddressFamily, _addressFamily), "remoteEP");
}
if (offset < 0 || offset > buffer.Length)
{
throw new ArgumentOutOfRangeException("offset");
}
if (size < 0 || size > buffer.Length - offset)
{
throw new ArgumentOutOfRangeException("size");
}
if (_rightEndPoint == null)
{
throw new InvalidOperationException(SR.net_sockets_mustbind);
}
// We don't do a CAS demand here because the contents of remoteEP aren't used by
// WSARecvFrom; all that matters is that we generate a unique-to-this-call SocketAddress
// with the right address family
Internals.SocketAddress socketAddress = SnapshotAndSerialize(ref remoteEP);
// Set up the result and set it to collect the context.
OverlappedAsyncResult asyncResult = new OverlappedAsyncResult(this, state, callback);
asyncResult.StartPostingAsyncOp(false);
// Start the ReceiveFrom.
DoBeginReceiveFrom(buffer, offset, size, socketFlags, remoteEP, socketAddress, asyncResult);
// Capture the context, maybe call the callback, and return.
asyncResult.FinishPostingAsyncOp(ref Caches.ReceiveClosureCache);
if (asyncResult.CompletedSynchronously && !asyncResult.SocketAddressOriginal.Equals(asyncResult.SocketAddress))
{
try
{
remoteEP = remoteEP.Create(asyncResult.SocketAddress);
}
catch
{
}
}
if (s_loggingEnabled)
{
Logging.Exit(Logging.Sockets, this, "BeginReceiveFrom", asyncResult);
}
return asyncResult;
}
示例4: DoBeginSend
private SocketError DoBeginSend(IList<ArraySegment<byte>> buffers, SocketFlags socketFlags, OverlappedAsyncResult asyncResult)
{
GlobalLog.Print("Socket#" + Logging.HashString(this) + "::BeginSend() SRC:" + Logging.ObjectToString(LocalEndPoint) + " DST:" + Logging.ObjectToString(RemoteEndPoint) + " buffers:" + buffers);
// Guarantee to call CheckAsyncCallOverlappedResult if we call SetUnamangedStructures with a cache in order to
// avoid a Socket leak in case of error.
SocketError errorCode = SocketError.SocketError;
try
{
GlobalLog.Print("BeginSend: asyncResult:" + Logging.HashString(asyncResult));
errorCode = SocketPal.SendAsync(_handle, buffers, socketFlags, asyncResult);
GlobalLog.Print("Socket#" + Logging.HashString(this) + "::BeginSend() Interop.Winsock.WSASend returns:" + errorCode.ToString() + " returning AsyncResult:" + Logging.HashString(asyncResult));
}
finally
{
errorCode = asyncResult.CheckAsyncCallOverlappedResult(errorCode);
}
// Throw an appropriate SocketException if the native call fails synchronously.
if (errorCode != SocketError.Success)
{
UpdateStatusAfterSocketError(errorCode);
if (s_loggingEnabled)
{
Logging.Exception(Logging.Sockets, this, "BeginSend", new SocketException((int)errorCode));
}
}
return errorCode;
}
示例5: ReceiveAsync
public static SocketError ReceiveAsync(SafeCloseSocket handle, byte[] buffer, int offset, int count, SocketFlags socketFlags, OverlappedAsyncResult asyncResult)
{
return handle.AsyncContext.ReceiveAsync(buffer, offset, count, socketFlags, asyncResult.CompletionCallback);
}
示例6: ReceiveFromAsync
public static SocketError ReceiveFromAsync(SafeCloseSocket handle, byte[] buffer, int offset, int count, SocketFlags socketFlags, Internals.SocketAddress socketAddress, OverlappedAsyncResult asyncResult)
{
asyncResult.SocketAddress = socketAddress;
return handle.AsyncContext.ReceiveFromAsync(buffer, offset, count, socketFlags, socketAddress.Buffer, socketAddress.InternalSize, asyncResult.CompletionCallback);
}
示例7: DoBeginReceive
private SocketError DoBeginReceive(byte[] buffer, int offset, int size, SocketFlags socketFlags, OverlappedAsyncResult asyncResult)
{
if (GlobalLog.IsEnabled)
{
GlobalLog.Print("Socket#" + LoggingHash.HashString(this) + "::BeginReceive() size:" + size.ToString());
}
#if DEBUG
IntPtr lastHandle = _handle.DangerousGetHandle();
#endif
// Guarantee to call CheckAsyncCallOverlappedResult if we call SetUnamangedStructures with a cache in order to
// avoid a Socket leak in case of error.
SocketError errorCode = SocketError.SocketError;
try
{
errorCode = SocketPal.ReceiveAsync(_handle, buffer, offset, size, socketFlags, asyncResult);
if (GlobalLog.IsEnabled)
{
GlobalLog.Print("Socket#" + LoggingHash.HashString(this) + "::BeginReceive() Interop.Winsock.WSARecv returns:" + errorCode.ToString() + " returning AsyncResult:" + LoggingHash.HashString(asyncResult));
}
}
finally
{
errorCode = asyncResult.CheckAsyncCallOverlappedResult(errorCode);
}
// Throw an appropriate SocketException if the native call fails synchronously.
if (errorCode != SocketError.Success)
{
// TODO: https://github.com/dotnet/corefx/issues/5426
//if (GlobalLog.IsEnabled)
//{
// GlobalLog.AssertFormat("Socket#{0}::DoBeginReceive()|GetLastWin32Error() returned zero.", LoggingHash.HashString(this));
//}
// Update the internal state of this socket according to the error before throwing.
UpdateStatusAfterSocketError(errorCode);
if (s_loggingEnabled)
{
NetEventSource.Exception(NetEventSource.ComponentType.Socket, this, nameof(BeginReceive), new SocketException((int)errorCode));
}
asyncResult.InvokeCallback(new SocketException((int)errorCode));
}
#if DEBUG
else
{
_lastReceiveHandle = lastHandle;
_lastReceiveThread = Environment.CurrentManagedThreadId;
_lastReceiveTick = Environment.TickCount;
}
#endif
return errorCode;
}
示例8: DoBeginSend
private SocketError DoBeginSend(IList<ArraySegment<byte>> buffers, SocketFlags socketFlags, OverlappedAsyncResult asyncResult)
{
GlobalLog.Print("Socket#" + ValidationHelper.HashString(this) + "::BeginSend() SRC:" + ValidationHelper.ToString(LocalEndPoint) + " DST:" + ValidationHelper.ToString(RemoteEndPoint) + " buffers:" + buffers);
// Guarantee to call CheckAsyncCallOverlappedResult if we call SetUnamangedStructures with a cache in order to
// avoid a Socket leak in case of error.
SocketError errorCode = SocketError.SocketError;
try
{
// Set up asyncResult for overlapped WSASend.
// This call will use completion ports on WinNT and Overlapped IO on Win9x.
asyncResult.SetUnmanagedStructures(buffers, ref Caches.SendOverlappedCache);
GlobalLog.Print("BeginSend: asyncResult:" + ValidationHelper.HashString(asyncResult));
// This can throw ObjectDisposedException.
int bytesTransferred;
errorCode = UnsafeNclNativeMethods.OSSOCK.WSASend(
m_Handle,
asyncResult.m_WSABuffers,
asyncResult.m_WSABuffers.Length,
out bytesTransferred,
socketFlags,
asyncResult.OverlappedHandle,
IntPtr.Zero);
if (errorCode!=SocketError.Success) {
errorCode = (SocketError)Marshal.GetLastWin32Error();
}
GlobalLog.Print("Socket#" + ValidationHelper.HashString(this) + "::BeginSend() UnsafeNclNativeMethods.OSSOCK.WSASend returns:" + errorCode.ToString() + " returning AsyncResult:" + ValidationHelper.HashString(asyncResult));
}
finally
{
errorCode = asyncResult.CheckAsyncCallOverlappedResult(errorCode);
}
//
// if the asynchronous native call fails synchronously
// we'll throw a SocketException
//
if (errorCode != SocketError.Success)
{
asyncResult.ExtractCache(ref Caches.SendOverlappedCache);
UpdateStatusAfterSocketError(errorCode);
if(s_LoggingEnabled)Logging.Exception(Logging.Sockets, this, "BeginSend", new SocketException(errorCode));
}
return errorCode;
}
示例9: DoBeginSend
private SocketError DoBeginSend(byte[] buffer, int offset, int size, SocketFlags socketFlags, OverlappedAsyncResult asyncResult)
{
if (GlobalLog.IsEnabled)
{
GlobalLog.Print("Socket#" + LoggingHash.HashString(this) + "::BeginSend() SRC:" + LoggingHash.ObjectToString(LocalEndPoint) + " DST:" + LoggingHash.ObjectToString(RemoteEndPoint) + " size:" + size.ToString());
}
// Guarantee to call CheckAsyncCallOverlappedResult if we call SetUnamangedStructures with a cache in order to
// avoid a Socket leak in case of error.
SocketError errorCode = SocketError.SocketError;
try
{
// Get the Send going.
if (GlobalLog.IsEnabled)
{
GlobalLog.Print("BeginSend: asyncResult:" + LoggingHash.HashString(asyncResult) + " size:" + size.ToString());
}
errorCode = SocketPal.SendAsync(_handle, buffer, offset, size, socketFlags, asyncResult);
if (GlobalLog.IsEnabled)
{
GlobalLog.Print("Socket#" + LoggingHash.HashString(this) + "::BeginSend() Interop.Winsock.WSASend returns:" + errorCode.ToString() + " size:" + size.ToString() + " returning AsyncResult:" + LoggingHash.HashString(asyncResult));
}
}
finally
{
errorCode = asyncResult.CheckAsyncCallOverlappedResult(errorCode);
}
// Throw an appropriate SocketException if the native call fails synchronously.
if (errorCode != SocketError.Success)
{
UpdateStatusAfterSocketError(errorCode);
if (s_loggingEnabled)
{
NetEventSource.Exception(NetEventSource.ComponentType.Socket, this, nameof(BeginSend), new SocketException((int)errorCode));
}
}
return errorCode;
}
示例10: DoBeginSendTo
private void DoBeginSendTo(byte[] buffer, int offset, int size, SocketFlags socketFlags, EndPoint endPointSnapshot, Internals.SocketAddress socketAddress, OverlappedAsyncResult asyncResult)
{
if (GlobalLog.IsEnabled)
{
GlobalLog.Print("Socket#" + LoggingHash.HashString(this) + "::DoBeginSendTo() size:" + size.ToString());
}
EndPoint oldEndPoint = _rightEndPoint;
// Guarantee to call CheckAsyncCallOverlappedResult if we call SetUnamangedStructures with a cache in order to
// avoid a Socket leak in case of error.
SocketError errorCode = SocketError.SocketError;
try
{
if (_rightEndPoint == null)
{
_rightEndPoint = endPointSnapshot;
}
errorCode = SocketPal.SendToAsync(_handle, buffer, offset, size, socketFlags, socketAddress, asyncResult);
if (GlobalLog.IsEnabled)
{
GlobalLog.Print("Socket#" + LoggingHash.HashString(this) + "::DoBeginSendTo() Interop.Winsock.WSASend returns:" + errorCode.ToString() + " size:" + size + " returning AsyncResult:" + LoggingHash.HashString(asyncResult));
}
}
catch (ObjectDisposedException)
{
_rightEndPoint = oldEndPoint;
throw;
}
finally
{
errorCode = asyncResult.CheckAsyncCallOverlappedResult(errorCode);
}
// Throw an appropriate SocketException if the native call fails synchronously.
if (errorCode != SocketError.Success)
{
// Update the internal state of this socket according to the error before throwing.
_rightEndPoint = oldEndPoint;
SocketException socketException = new SocketException((int)errorCode);
UpdateStatusAfterSocketError(socketException);
if (s_loggingEnabled)
{
NetEventSource.Exception(NetEventSource.ComponentType.Socket, this, nameof(BeginSendTo), socketException);
}
throw socketException;
}
if (GlobalLog.IsEnabled)
{
GlobalLog.Print("Socket#" + LoggingHash.HashString(this) + "::DoBeginSendTo() size:" + size.ToString() + " returning AsyncResult:" + LoggingHash.HashString(asyncResult));
}
}
示例11: ReceiveFromAsync
public static unsafe SocketError ReceiveFromAsync(SafeCloseSocket handle, byte[] buffer, int offset, int count, SocketFlags socketFlags, Internals.SocketAddress socketAddress, OverlappedAsyncResult asyncResult)
{
// Set up asyncResult for overlapped WSARecvFrom.
// This call will use completion ports on WinNT and Overlapped IO on Win9x.
asyncResult.SetUnmanagedStructures(buffer, offset, count, socketAddress, true);
int bytesTransferred;
SocketError errorCode = Interop.Winsock.WSARecvFrom(
handle,
ref asyncResult._singleBuffer,
1,
out bytesTransferred,
ref socketFlags,
asyncResult.GetSocketAddressPtr(),
asyncResult.GetSocketAddressSizePtr(),
asyncResult.OverlappedHandle,
IntPtr.Zero);
if (errorCode != SocketError.Success)
{
errorCode = GetLastSocketError();
}
return errorCode;
}
示例12: ReceiveAsync
public static unsafe SocketError ReceiveAsync(SafeCloseSocket handle, IList<ArraySegment<byte>> buffers, SocketFlags socketFlags, OverlappedAsyncResult asyncResult)
{
// Set up asyncResult for overlapped WSASend.
// This call will use completion ports.
asyncResult.SetUnmanagedStructures(buffers);
// This can throw ObjectDisposedException.
int bytesTransferred;
SocketError errorCode = Interop.Winsock.WSARecv(
handle,
asyncResult._wsaBuffers,
asyncResult._wsaBuffers.Length,
out bytesTransferred,
ref socketFlags,
asyncResult.OverlappedHandle,
IntPtr.Zero);
if (errorCode != SocketError.Success)
{
errorCode = GetLastSocketError();
}
return errorCode;
}
示例13: SendAsync
public static SocketError SendAsync(SafeCloseSocket handle, BufferOffsetSize[] buffers, SocketFlags socketFlags, OverlappedAsyncResult asyncResult)
{
return handle.AsyncContext.SendAsync(new BufferList(buffers), GetPlatformSocketFlags(socketFlags), asyncResult.CompletionCallback);
}
示例14: DoBeginReceive
private SocketError DoBeginReceive(IList<ArraySegment<byte>> buffers, SocketFlags socketFlags, OverlappedAsyncResult asyncResult)
{
#if DEBUG
IntPtr lastHandle = m_Handle.DangerousGetHandle();
#endif
// Guarantee to call CheckAsyncCallOverlappedResult if we call SetUnamangedStructures with a cache in order to
// avoid a Socket leak in case of error.
SocketError errorCode = SocketError.SocketError;
try
{
// Set up asyncResult for overlapped WSASend.
// This call will use completion ports on WinNT and Overlapped IO on Win9x.
asyncResult.SetUnmanagedStructures(buffers, ref Caches.ReceiveOverlappedCache);
// This can throw ObjectDisposedException.
int bytesTransferred;
errorCode = UnsafeNclNativeMethods.OSSOCK.WSARecv(
m_Handle,
asyncResult.m_WSABuffers,
asyncResult.m_WSABuffers.Length,
out bytesTransferred,
ref socketFlags,
asyncResult.OverlappedHandle,
IntPtr.Zero);
if (errorCode!=SocketError.Success) {
errorCode = (SocketError)Marshal.GetLastWin32Error();
GlobalLog.Assert(errorCode != SocketError.Success, "Socket#{0}::DoBeginReceive()|GetLastWin32Error() returned zero.", ValidationHelper.HashString(this));
}
GlobalLog.Print("Socket#" + ValidationHelper.HashString(this) + "::DoBeginReceive() UnsafeNclNativeMethods.OSSOCK.WSARecv returns:" + errorCode.ToString() + " returning AsyncResult:" + ValidationHelper.HashString(asyncResult));
}
finally
{
errorCode = asyncResult.CheckAsyncCallOverlappedResult(errorCode);
}
//
// if the asynchronous native call fails synchronously
// we'll throw a SocketException
//
if (errorCode != SocketError.Success)
{
//
// update our internal state after this socket error and throw
asyncResult.ExtractCache(ref Caches.ReceiveOverlappedCache);
UpdateStatusAfterSocketError(errorCode);
if(s_LoggingEnabled)Logging.Exception(Logging.Sockets, this, "BeginReceive", new SocketException(errorCode));
}
#if DEBUG
else
{
m_LastReceiveHandle = lastHandle;
m_LastReceiveThread = Thread.CurrentThread.ManagedThreadId;
m_LastReceiveTick = Environment.TickCount;
}
#endif
return errorCode;
}
示例15: SendToAsync
public static SocketError SendToAsync(SafeCloseSocket handle, byte[] buffer, int offset, int count, SocketFlags socketFlags, Internals.SocketAddress socketAddress, OverlappedAsyncResult asyncResult)
{
asyncResult.SocketAddress = socketAddress;
return handle.AsyncContext.SendToAsync(buffer, offset, count, GetPlatformSocketFlags(socketFlags), socketAddress.Buffer, socketAddress.Size, asyncResult.CompletionCallback);
}