public int ReceiveMessageFrom(byte[] buffer, int offset, int size, ref SocketFlags socketFlags, ref System.Net.EndPoint remoteEP, out IPPacketInformation ipPacketInformation)
{
ipPacketInformation = default(IPPacketInformation);
return default(int);
}
public int ReceiveMessageFrom (byte[] buffer, int offset, int size, ref SocketFlags socketFlags, ref EndPoint remoteEP, out IPPacketInformation ipPacketInformation)
{
ThrowIfDisposedAndClosed ();
ThrowIfBufferNull (buffer);
ThrowIfBufferOutOfRange (buffer, offset, size);
if (remoteEP == null)
throw new ArgumentNullException ("remoteEP");
// FIXME: figure out how we get hold of the IPPacketInformation
throw new NotImplementedException ();
}
开发者ID:Profit0004,项目名称:mono,代码行数:12,代码来源:Socket.cs
示例6: ReceiveMessageFrom
// Receives a datagram into a specific location in the data buffer and stores
// the end point.
public int ReceiveMessageFrom(byte[] buffer, int offset, int size, ref SocketFlags socketFlags, ref EndPoint remoteEP, out IPPacketInformation ipPacketInformation)
{
if (s_loggingEnabled)
{
Logging.Enter(Logging.Sockets, this, "ReceiveMessageFrom", "");
}
if (CleanedUp)
{
throw new ObjectDisposedException(this.GetType().FullName);
}
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);
}
ValidateBlockingMode();
// We don't do a CAS demand here because the contents of remoteEP aren't used by
// WSARecvMsg; all that matters is that we generate a unique-to-this-call SocketAddress
// with the right address family.
EndPoint endPointSnapshot = remoteEP;
Internals.SocketAddress socketAddress = SnapshotAndSerialize(ref endPointSnapshot);
// Save a copy of the original EndPoint.
Internals.SocketAddress socketAddressOriginal = IPEndPointExtensions.Serialize(endPointSnapshot);
SetReceivingPacketInformation();
Internals.SocketAddress receiveAddress;
int bytesTransferred;
SocketError errorCode = SocketPal.ReceiveMessageFrom(this, _handle, buffer, offset, size, ref socketFlags, socketAddress, out receiveAddress, out ipPacketInformation, out bytesTransferred);
// Throw an appropriate SocketException if the native call fails.
if (errorCode != SocketError.Success && errorCode != SocketError.MessageSize)
{
// Update the internal state of this socket according to the error before throwing.
SocketException socketException = new SocketException((int)errorCode);
UpdateStatusAfterSocketError(socketException);
if (s_loggingEnabled)
{
Logging.Exception(Logging.Sockets, this, "ReceiveMessageFrom", socketException);
}
throw socketException;
}
if (!socketAddressOriginal.Equals(receiveAddress))
{
try
{
remoteEP = endPointSnapshot.Create(receiveAddress);
}
catch
{
}
if (_rightEndPoint == null)
{
// Save a copy of the EndPoint so we can use it for Create().
_rightEndPoint = endPointSnapshot;
}
}
if (s_loggingEnabled)
{
Logging.Exit(Logging.Sockets, this, "ReceiveMessageFrom", errorCode);
}
return bytesTransferred;
}
//.........这里部分代码省略.........
}
break;
case SocketAsyncOperation.ReceiveMessageFrom:
if (bytesTransferred > 0) {
// Log and Perf counters.
if (s_LoggingEnabled) LogBuffer(bytesTransferred);
if (Socket.s_PerfCountersEnabled) UpdatePerfCounters(bytesTransferred, false);
}
// Deal with incoming address.
m_SocketAddress.SetSize(m_PtrSocketAddressBufferSize);
socketAddressOriginal = m_RemoteEndPoint.Serialize();
if(!socketAddressOriginal.Equals(m_SocketAddress)) {
try {
m_RemoteEndPoint = m_RemoteEndPoint.Create(m_SocketAddress);
}
catch {
}
}
// Extract the packet information.
unsafe {
IPAddress address = null;
UnsafeNclNativeMethods.OSSOCK.WSAMsg* PtrMessage = (UnsafeNclNativeMethods.OSSOCK.WSAMsg*)Marshal.UnsafeAddrOfPinnedArrayElement(m_WSAMessageBuffer, 0);
//ipv4
if(m_ControlBuffer.Length == s_ControlDataSize) {
UnsafeNclNativeMethods.OSSOCK.ControlData controlData = (UnsafeNclNativeMethods.OSSOCK.ControlData)Marshal.PtrToStructure(PtrMessage->controlBuffer.Pointer, typeof(UnsafeNclNativeMethods.OSSOCK.ControlData));
if(controlData.length != UIntPtr.Zero) {
address = new IPAddress((long)controlData.address);
}
m_ReceiveMessageFromPacketInfo = new IPPacketInformation(((address != null) ? address : IPAddress.None), (int)controlData.index);
}
//ipv6
else if(m_ControlBuffer.Length == s_ControlDataIPv6Size) {
UnsafeNclNativeMethods.OSSOCK.ControlDataIPv6 controlData = (UnsafeNclNativeMethods.OSSOCK.ControlDataIPv6)Marshal.PtrToStructure(PtrMessage->controlBuffer.Pointer, typeof(UnsafeNclNativeMethods.OSSOCK.ControlDataIPv6));
if(controlData.length != UIntPtr.Zero) {
address = new IPAddress(controlData.address);
}
m_ReceiveMessageFromPacketInfo = new IPPacketInformation(((address != null) ? address : IPAddress.IPv6None), (int)controlData.index);
}
//other
else {
m_ReceiveMessageFromPacketInfo = new IPPacketInformation();
}
}
break;
case SocketAsyncOperation.Send:
if (bytesTransferred > 0) {
// Log and Perf counters.
if (s_LoggingEnabled) LogBuffer(bytesTransferred);
if (Socket.s_PerfCountersEnabled) UpdatePerfCounters(bytesTransferred, true);
}
break;
case SocketAsyncOperation.SendPackets:
if(bytesTransferred > 0) {
// Log and Perf counters.
if(s_LoggingEnabled) LogSendPacketsBuffers(bytesTransferred);
if(Socket.s_PerfCountersEnabled) UpdatePerfCounters(bytesTransferred, true);
}
开发者ID:REALTOBIZ,项目名称:mono,代码行数:67,代码来源:Socket.cs
示例10: ReceiveMessageFrom
/// <summary>
/// Receives the specified number of bytes of data into the specified location of the data buffer, using the specified <see cref="T:System.Net.Sockets.SocketFlags"/>, and stores the endpoint and packet information.
/// </summary>
///
/// <returns>
/// The number of bytes received.
/// </returns>
/// <param name="buffer">An array of type <see cref="T:System.Byte"/> that is the storage location for received data.</param><param name="offset">The position in the <paramref name="buffer"/> parameter to store the received data.</param><param name="size">The number of bytes to receive.</param><param name="socketFlags">A bitwise combination of the <see cref="T:System.Net.Sockets.SocketFlags"/> values.</param><param name="remoteEP">An <see cref="T:System.Net.EndPoint"/>, passed by reference, that represents the remote server.</param><param name="ipPacketInformation">An <see cref="T:System.Net.Sockets.IPPacketInformation"/> holding address and interface information.</param><exception cref="T:System.ArgumentNullException"><paramref name="buffer"/> is null.- or- <paramref name="remoteEP"/> is null. </exception><exception cref="T:System.ArgumentOutOfRangeException"><paramref name="offset"/> is less than 0.-or- <paramref name="offset"/> is greater than the length of <paramref name="buffer"/>.-or- <paramref name="size"/> is less than 0.-or- <paramref name="size"/> is greater than the length of the <paramref name="buffer"/> minus the value of the offset parameter. </exception><exception cref="T:System.Net.Sockets.SocketException"><paramref name="socketFlags"/> is not a valid combination of values.-or- The <see cref="P:System.Net.Sockets.Socket.LocalEndPoint"/> property was not set.-or- The .NET Framework is running on an AMD 64-bit processor.-or- An error occurred when attempting to access the socket. See the Remarks section for more information. </exception><exception cref="T:System.ObjectDisposedException">The <see cref="T:System.Net.Sockets.Socket"/> has been closed. </exception><exception cref="T:System.NotSupportedException">The operating system is Windows 2000 or earlier, and this method requires Windows XP.</exception>
public int ReceiveMessageFrom(byte[] buffer, int offset, int size, ref SocketFlags socketFlags, ref EndPoint remoteEP, out IPPacketInformation ipPacketInformation)
{
if (Socket.s_LoggingEnabled)
Logging.Enter(Logging.Sockets, (object) this, "ReceiveMessageFrom", "");
if (this.CleanedUp)
throw new ObjectDisposedException(this.GetType().FullName);
if (buffer == null)
throw new ArgumentNullException("buffer");
if (remoteEP == null)
throw new ArgumentNullException("remoteEP");
if (!this.CanTryAddressFamily(remoteEP.AddressFamily))
{
throw new ArgumentException(SR.GetString("net_InvalidEndPointAddressFamily", (object) remoteEP.AddressFamily, (object) this.addressFamily), "remoteEP");
}
else
{
if (offset < 0 || offset > buffer.Length)
throw new ArgumentOutOfRangeException("offset");
if (size < 0 || size > buffer.Length - offset)
throw new ArgumentOutOfRangeException("size");
if (this.m_RightEndPoint == null)
throw new InvalidOperationException(SR.GetString("net_sockets_mustbind"));
this.ValidateBlockingMode();
EndPoint remoteEP1 = remoteEP;
SocketAddress socketAddress1 = this.SnapshotAndSerialize(ref remoteEP1);
ReceiveMessageOverlappedAsyncResult overlappedAsyncResult = new ReceiveMessageOverlappedAsyncResult(this, (object) null, (AsyncCallback) null);
overlappedAsyncResult.SetUnmanagedStructures(buffer, offset, size, socketAddress1, socketFlags);
SocketAddress socketAddress2 = remoteEP1.Serialize();
int bytesTransferred = 0;
SocketError socketError = SocketError.Success;
this.SetReceivingPacketInformation();
try
{
if (this.WSARecvMsg_Blocking(this.m_Handle.DangerousGetHandle(), Marshal.UnsafeAddrOfPinnedArrayElement((Array) overlappedAsyncResult.m_MessageBuffer, 0), out bytesTransferred, IntPtr.Zero, IntPtr.Zero) == SocketError.SocketError)
socketError = (SocketError) Marshal.GetLastWin32Error();
}
finally
{
overlappedAsyncResult.SyncReleaseUnmanagedStructures();
}
if (socketError != SocketError.Success && socketError != SocketError.MessageSize)
{
SocketException socketException = new SocketException(socketError);
this.UpdateStatusAfterSocketError(socketException);
if (Socket.s_LoggingEnabled)
Logging.Exception(Logging.Sockets, (object) this, "ReceiveMessageFrom", (Exception) socketException);
throw socketException;
}
else
{
if (!socketAddress2.Equals((object) overlappedAsyncResult.m_SocketAddress))
{
try
{
remoteEP = remoteEP1.Create(overlappedAsyncResult.m_SocketAddress);
}
catch
{
}
if (this.m_RightEndPoint == null)
this.m_RightEndPoint = remoteEP1;
}
socketFlags = overlappedAsyncResult.m_flags;
ipPacketInformation = overlappedAsyncResult.m_IPPacketInformation;
if (Socket.s_LoggingEnabled)
Logging.Exit(Logging.Sockets, (object) this, "ReceiveMessageFrom", (object) socketError);
return bytesTransferred;
}
}
}
/// <summary>
/// Ends a pending asynchronous read from a specific endpoint. This method also reveals more information about the packet than <see cref="M:System.Net.Sockets.Socket.EndReceiveFrom(System.IAsyncResult,[email protected])"/>.
/// </summary>
///
/// <returns>
/// If successful, the number of bytes received. If unsuccessful, returns 0.
/// </returns>
/// <param name="asyncResult">An <see cref="T:System.IAsyncResult"/> that stores state information and any user defined data for this asynchronous operation.</param><param name="socketFlags">A bitwise combination of the <see cref="T:System.Net.Sockets.SocketFlags"/> values for the received packet.</param><param name="endPoint">The source <see cref="T:System.Net.EndPoint"/>.</param><param name="ipPacketInformation">The <see cref="T:System.Net.IPAddress"/> and interface of the received packet.</param><exception cref="T:System.ArgumentNullException"><paramref name="asyncResult"/> is null-or- <paramref name="endPoint"/> is null. </exception><exception cref="T:System.ArgumentException"><paramref name="asyncResult"/> was not returned by a call to the <see cref="M:System.Net.Sockets.Socket.BeginReceiveMessageFrom(System.Byte[],System.Int32,System.Int32,System.Net.Sockets.SocketFlags,[email protected],System.AsyncCallback,System.Object)"/> method. </exception><exception cref="T:System.InvalidOperationException"><see cref="M:System.Net.Sockets.Socket.EndReceiveMessageFrom(System.IAsyncResult,[email protected],[email protected],[email protected])"/> was previously called for the asynchronous read. </exception><exception cref="T:System.Net.Sockets.SocketException">An error occurred when attempting to access the socket. See the Remarks section for more information. </exception><exception cref="T:System.ObjectDisposedException">The <see cref="T:System.Net.Sockets.Socket"/> has been closed. </exception>
public int EndReceiveMessageFrom(IAsyncResult asyncResult, ref SocketFlags socketFlags, ref EndPoint endPoint, out IPPacketInformation ipPacketInformation)
{
if (Socket.s_LoggingEnabled)
Logging.Enter(Logging.Sockets, (object) this, "EndReceiveMessageFrom", (object) asyncResult);
if (this.CleanedUp)
throw new ObjectDisposedException(this.GetType().FullName);
if (endPoint == null)
throw new ArgumentNullException("endPoint");
if (!this.CanTryAddressFamily(endPoint.AddressFamily))
{
throw new ArgumentException(SR.GetString("net_InvalidEndPointAddressFamily", (object) endPoint.AddressFamily, (object) this.addressFamily), "endPoint");
}
else
{
if (asyncResult == null)
throw new ArgumentNullException("asyncResult");
ReceiveMessageOverlappedAsyncResult overlappedAsyncResult = asyncResult as ReceiveMessageOverlappedAsyncResult;
if (overlappedAsyncResult == null || overlappedAsyncResult.AsyncObject != this)
throw new ArgumentException(SR.GetString("net_io_invalidasyncresult"), "asyncResult");
if (overlappedAsyncResult.EndCalled)
{
throw new InvalidOperationException(SR.GetString("net_io_invalidendcall", new object[1]
{
(object) "EndReceiveMessageFrom"
}));
}
else
{
SocketAddress socketAddress = this.SnapshotAndSerialize(ref endPoint);
int num = (int) overlappedAsyncResult.InternalWaitForCompletion();
overlappedAsyncResult.EndCalled = true;
overlappedAsyncResult.ExtractCache(ref this.Caches.ReceiveOverlappedCache);
overlappedAsyncResult.SocketAddress.SetSize(overlappedAsyncResult.GetSocketAddressSizePtr());
if (!socketAddress.Equals((object) overlappedAsyncResult.SocketAddress))
{
try
{
endPoint = endPoint.Create(overlappedAsyncResult.SocketAddress);
}
catch
{
}
}
if (Socket.s_PerfCountersEnabled && num > 0)
{
NetworkingPerfCounters.Instance.Increment(NetworkingPerfCounterName.SocketBytesReceived, (long) num);
if (this.Transport == TransportType.Udp)
NetworkingPerfCounters.Instance.Increment(NetworkingPerfCounterName.SocketDatagramsReceived);
}
if (overlappedAsyncResult.ErrorCode != 0 && overlappedAsyncResult.ErrorCode != 10040)
{
SocketException socketException = new SocketException(overlappedAsyncResult.ErrorCode);
this.UpdateStatusAfterSocketError(socketException);
if (Socket.s_LoggingEnabled)
Logging.Exception(Logging.Sockets, (object) this, "EndReceiveMessageFrom", (Exception) socketException);
throw socketException;
}
else
{
socketFlags = overlappedAsyncResult.m_flags;
ipPacketInformation = overlappedAsyncResult.m_IPPacketInformation;
if (Socket.s_LoggingEnabled)
Logging.Exit(Logging.Sockets, (object) this, "EndReceiveMessageFrom", (object) num);
return num;
}
}
}
}
/// <devdoc>
/// <para>Receives a datagram into a specific location in the data buffer and stores
/// the end point.</para>
/// </devdoc>
public int ReceiveMessageFrom(byte[] buffer, int offset, int size, ref SocketFlags socketFlags, ref EndPoint remoteEP, out IPPacketInformation ipPacketInformation)
{
if(Logging.On)Logging.Enter(Logging.Sockets, this, "ReceiveMessageFrom", "");
if (CleanedUp) {
throw new ObjectDisposedException(this.GetType().FullName);
}
if (buffer==null) {
throw new ArgumentNullException("buffer");
}
if (remoteEP==null) {
throw new ArgumentNullException("remoteEP");
}
if (offset<0 || offset>buffer.Length) {
throw new ArgumentOutOfRangeException("offset");
}
if (size<0 || size>buffer.Length-offset) {
throw new ArgumentOutOfRangeException("size");
}
if (m_RightEndPoint==null) {
throw new InvalidOperationException(SR.GetString(SR.net_sockets_mustbind));
}
ValidateBlockingMode();
// This will check the permissions for connect.
EndPoint endPointSnapshot = remoteEP;
SocketAddress socketAddress = CheckCacheRemote(ref endPointSnapshot, false);
ReceiveMessageOverlappedAsyncResult asyncResult = new ReceiveMessageOverlappedAsyncResult(this,null,null);
asyncResult.SetUnmanagedStructures(buffer, offset, size, socketAddress, socketFlags);
// save a copy of the original EndPoint
SocketAddress socketAddressOriginal = endPointSnapshot.Serialize();
//setup structure
int bytesTransfered = 0;
SocketError errorCode = SocketError.Success;
if(addressFamily == AddressFamily.InterNetwork) {
SetSocketOption(SocketOptionLevel.IP,SocketOptionName.PacketInformation,true);
}
else if (addressFamily == AddressFamily.InterNetworkV6){
SetSocketOption(SocketOptionLevel.IPv6,SocketOptionName.PacketInformation,true);
}
try
{
// This can throw ObjectDisposedException (retrieving the delegate AND resolving the handle).
if (WSARecvMsg_Blocking(
m_Handle.DangerousGetHandle(),
Marshal.UnsafeAddrOfPinnedArrayElement(asyncResult.m_MessageBuffer,0),
out bytesTransfered,
IntPtr.Zero,
IntPtr.Zero) == SocketError.SocketError)
{
errorCode = (SocketError)Marshal.GetLastWin32Error();
}
}
finally {
asyncResult.SyncReleaseUnmanagedStructures();
}
//
// if the native call fails we'll throw a SocketException
//
if (errorCode!=SocketError.Success && errorCode != SocketError.MessageSize) {
//
// update our internal state after this socket error and throw
//
SocketException socketException = new SocketException(errorCode);
UpdateStatusAfterSocketError(socketException);
if(Logging.On)Logging.Exception(Logging.Sockets, this, "ReceiveMessageFrom", socketException);
throw socketException;
}
if (!socketAddressOriginal.Equals(asyncResult.m_SocketAddress))
{
try {
remoteEP = endPointSnapshot.Create(asyncResult.m_SocketAddress);
}
catch {
}
if (m_RightEndPoint==null) {
//
// save a copy of the EndPoint so we can use it for Create()
//
m_RightEndPoint = endPointSnapshot;
}
}
socketFlags = asyncResult.m_flags;
ipPacketInformation = asyncResult.m_IPPacketInformation;
if(Logging.On)Logging.Exit(Logging.Sockets, this, "ReceiveMessageFrom", errorCode);
return bytesTransfered;
//.........这里部分代码省略.........
/// <devdoc>
/// <para>Receives a datagram into a specific location in the data buffer and stores
/// the end point.</para>
/// </devdoc>
public int ReceiveMessageFrom(byte[] buffer, int offset, int size, ref SocketFlags socketFlags, ref EndPoint remoteEP, out IPPacketInformation ipPacketInformation) {
if(s_LoggingEnabled)Logging.Enter(Logging.Sockets, this, "ReceiveMessageFrom", "");
if (CleanedUp) {
throw new ObjectDisposedException(this.GetType().FullName);
}
if (buffer==null) {
throw new ArgumentNullException("buffer");
}
if (remoteEP==null) {
throw new ArgumentNullException("remoteEP");
}
if (!CanTryAddressFamily(remoteEP.AddressFamily)) {
throw new ArgumentException(SR.GetString(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 (m_RightEndPoint==null) {
throw new InvalidOperationException(SR.GetString(SR.net_sockets_mustbind));
}
ValidateBlockingMode();
// We don't do a CAS demand here because the contents of remoteEP aren't used by
// WSARecvMsg; all that matters is that we generate a unique-to-this-call SocketAddress
// with the right address family
EndPoint endPointSnapshot = remoteEP;
SocketAddress socketAddress = SnapshotAndSerialize(ref endPointSnapshot);
ReceiveMessageOverlappedAsyncResult asyncResult = new ReceiveMessageOverlappedAsyncResult(this,null,null);
asyncResult.SetUnmanagedStructures(buffer, offset, size, socketAddress, socketFlags);
// save a copy of the original EndPoint
SocketAddress socketAddressOriginal = endPointSnapshot.Serialize();
//setup structure
int bytesTransfered = 0;
SocketError errorCode = SocketError.Success;
SetReceivingPacketInformation();
try
{
// This can throw ObjectDisposedException (retrieving the delegate AND resolving the handle).
if (WSARecvMsg_Blocking(
m_Handle.DangerousGetHandle(),
Marshal.UnsafeAddrOfPinnedArrayElement(asyncResult.m_MessageBuffer,0),
out bytesTransfered,
IntPtr.Zero,
IntPtr.Zero) == SocketError.SocketError)
{
errorCode = (SocketError)Marshal.GetLastWin32Error();
}
}
finally {
asyncResult.SyncReleaseUnmanagedStructures();
}
//
// if the native call fails we'll throw a SocketException
//
if (errorCode!=SocketError.Success && errorCode != SocketError.MessageSize) {
//
// update our internal state after this socket error and throw
//
SocketException socketException = new SocketException(errorCode);
UpdateStatusAfterSocketError(socketException);
if(s_LoggingEnabled)Logging.Exception(Logging.Sockets, this, "ReceiveMessageFrom", socketException);
throw socketException;
}
if (!socketAddressOriginal.Equals(asyncResult.m_SocketAddress))
{
try {
remoteEP = endPointSnapshot.Create(asyncResult.m_SocketAddress);
}
catch {
}
if (m_RightEndPoint==null) {
//
// save a copy of the EndPoint so we can use it for Create()
//
m_RightEndPoint = endPointSnapshot;
}
}
socketFlags = asyncResult.m_flags;
ipPacketInformation = asyncResult.m_IPPacketInformation;
//.........这里部分代码省略.........
开发者ID:REALTOBIZ,项目名称:mono,代码行数:101,代码来源:Socket.cs
示例16: EndReceiveMessageFrom
public int EndReceiveMessageFrom(IAsyncResult asyncResult, ref SocketFlags socketFlags, ref EndPoint endPoint, out IPPacketInformation ipPacketInformation) {
if(s_LoggingEnabled)Logging.Enter(Logging.Sockets, this, "EndReceiveMessageFrom", asyncResult);
if (CleanedUp) {
throw new ObjectDisposedException(this.GetType().FullName);
}
if (endPoint==null) {
throw new ArgumentNullException("endPoint");
}
if (!CanTryAddressFamily(endPoint.AddressFamily)) {
throw new ArgumentException(SR.GetString(SR.net_InvalidEndPointAddressFamily,
endPoint.AddressFamily, addressFamily), "endPoint");
}
if (asyncResult==null) {
throw new ArgumentNullException("asyncResult");
}
ReceiveMessageOverlappedAsyncResult castedAsyncResult = asyncResult as ReceiveMessageOverlappedAsyncResult;
if (castedAsyncResult==null || castedAsyncResult.AsyncObject!=this) {
throw new ArgumentException(SR.GetString(SR.net_io_invalidasyncresult), "asyncResult");
}
if (castedAsyncResult.EndCalled) {
throw new InvalidOperationException(SR.GetString(SR.net_io_invalidendcall, "EndReceiveMessageFrom"));
}
SocketAddress socketAddressOriginal = SnapshotAndSerialize(ref endPoint);
int bytesTransferred = (int)castedAsyncResult.InternalWaitForCompletion();
castedAsyncResult.EndCalled = true;
castedAsyncResult.ExtractCache(ref Caches.ReceiveOverlappedCache);
// Update socket address size
castedAsyncResult.SocketAddress.SetSize(castedAsyncResult.GetSocketAddressSizePtr());
if (!socketAddressOriginal.Equals(castedAsyncResult.SocketAddress)) {
try {
endPoint = endPoint.Create(castedAsyncResult.SocketAddress);
}
catch {
}
}
#if !FEATURE_PAL // perfcounter
if (s_PerfCountersEnabled)
{
if (bytesTransferred>0) {
NetworkingPerfCounters.Instance.Increment(NetworkingPerfCounterName.SocketBytesReceived, bytesTransferred);
if (Transport==TransportType.Udp) {
NetworkingPerfCounters.Instance.Increment(NetworkingPerfCounterName.SocketDatagramsReceived);
}
}
}
#endif //!FEATURE_PAL
GlobalLog.Print("Socket#" + ValidationHelper.HashString(this) + "::EndReceiveMessageFrom() bytesTransferred:" + bytesTransferred.ToString());
//
// if the asynchronous native call failed asynchronously
// we'll throw a SocketException
//
if ((SocketError)castedAsyncResult.ErrorCode!=SocketError.Success && (SocketError)castedAsyncResult.ErrorCode != SocketError.MessageSize) {
//
// update our internal state after this socket error and throw
//
SocketException socketException = new SocketException(castedAsyncResult.ErrorCode);
UpdateStatusAfterSocketError(socketException);
if(s_LoggingEnabled)Logging.Exception(Logging.Sockets, this, "EndReceiveMessageFrom", socketException);
throw socketException;
}
socketFlags = castedAsyncResult.m_flags;
ipPacketInformation = castedAsyncResult.m_IPPacketInformation;
if(s_LoggingEnabled)Logging.Exit(Logging.Sockets, this, "EndReceiveMessageFrom", bytesTransferred);
return bytesTransferred;
}
请发表评论