本文整理汇总了C#中System.Net.Sockets.IPPacketInformation类的典型用法代码示例。如果您正苦于以下问题:C# IPPacketInformation类的具体用法?C# IPPacketInformation怎么用?C# IPPacketInformation使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
IPPacketInformation类属于System.Net.Sockets命名空间,在下文中一共展示了IPPacketInformation类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: InitIPPacketInformation
private unsafe void InitIPPacketInformation()
{
IPAddress address = null;
if (this.m_ControlBuffer.Length == s_ControlDataSize)
{
UnsafeNclNativeMethods.OSSOCK.ControlData data = (UnsafeNclNativeMethods.OSSOCK.ControlData) Marshal.PtrToStructure(this.m_Message.controlBuffer.Pointer, typeof(UnsafeNclNativeMethods.OSSOCK.ControlData));
if (data.length != UIntPtr.Zero)
{
address = new IPAddress((long) data.address);
}
this.m_IPPacketInformation = new IPPacketInformation((address != null) ? address : IPAddress.None, (int) data.index);
}
else if (this.m_ControlBuffer.Length == s_ControlDataIPv6Size)
{
UnsafeNclNativeMethods.OSSOCK.ControlDataIPv6 pv = (UnsafeNclNativeMethods.OSSOCK.ControlDataIPv6) Marshal.PtrToStructure(this.m_Message.controlBuffer.Pointer, typeof(UnsafeNclNativeMethods.OSSOCK.ControlDataIPv6));
if (pv.length != UIntPtr.Zero)
{
address = new IPAddress(pv.address);
}
this.m_IPPacketInformation = new IPPacketInformation((address != null) ? address : IPAddress.IPv6None, (int) pv.index);
}
else
{
this.m_IPPacketInformation = new IPPacketInformation();
}
}
开发者ID:pritesh-mandowara-sp,项目名称:DecompliedDotNetLibraries,代码行数:26,代码来源:ReceiveMessageOverlappedAsyncResult.cs
示例2: CompletionCallback
public void CompletionCallback(int numBytes, byte[] socketAddress, int socketAddressSize, SocketFlags receivedFlags, IPPacketInformation ipPacketInformation, SocketError errorCode)
{
Debug.Assert(_socketAddress != null);
Debug.Assert(socketAddress == null || _socketAddress.Buffer == socketAddress);
_socketAddressSize = socketAddressSize;
_socketFlags = receivedFlags;
_ipPacketInformation = ipPacketInformation;
base.CompletionCallback(numBytes, errorCode);
}
示例3: GetInterface
public static NetworkInterface GetInterface(IPPacketInformation packetInfo)
{
int interfaceId = packetInfo.Interface;
NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
foreach (var netInterface in interfaces)
{
var ipv4InterfaceProps = netInterface.GetIPProperties().GetIPv4Properties();
if (ipv4InterfaceProps != null &&
ipv4InterfaceProps.Index == interfaceId)
return netInterface;
}
return null;
}
示例4: ReceiveMessageFrom
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);
}
示例5: ReceiveMessageFrom
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 ();
}
示例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;
}
示例7: EndReceiveMessageFrom
public int EndReceiveMessageFrom (IAsyncResult asyncResult,
ref SocketFlags socketFlags,
ref EndPoint endPoint,
out IPPacketInformation ipPacketInformation)
{
if (disposed && closed)
throw new ObjectDisposedException (GetType ().ToString ());
if (asyncResult == null)
throw new ArgumentNullException ("asyncResult");
if (endPoint == null)
throw new ArgumentNullException ("endPoint");
SocketAsyncResult req = asyncResult as SocketAsyncResult;
if (req == null)
throw new ArgumentException ("Invalid IAsyncResult", "asyncResult");
if (Interlocked.CompareExchange (ref req.EndCalled, 1, 0) == 1)
throw InvalidAsyncOp ("EndReceiveMessageFrom");
throw new NotImplementedException ();
}
示例8: ReceiveMessageFrom
public static SocketError ReceiveMessageFrom(Socket socket, SafeCloseSocket handle, byte[] buffer, int offset, int count, ref SocketFlags socketFlags, Internals.SocketAddress socketAddress, out Internals.SocketAddress receiveAddress, out IPPacketInformation ipPacketInformation, out int bytesTransferred)
{
byte[] socketAddressBuffer = socketAddress.Buffer;
int socketAddressLen = socketAddress.Size;
bool isIPv4, isIPv6;
Socket.GetIPProtocolInformation(socket.AddressFamily, socketAddress, out isIPv4, out isIPv6);
SocketError errorCode;
if (!handle.IsNonBlocking)
{
errorCode = handle.AsyncContext.ReceiveMessageFrom(buffer, offset, count, ref socketFlags, socketAddressBuffer, ref socketAddressLen, isIPv4, isIPv6, handle.ReceiveTimeout, out ipPacketInformation, out bytesTransferred);
}
else
{
if (!TryCompleteReceiveMessageFrom(handle.FileDescriptor, buffer, offset, count, socketFlags, socketAddressBuffer, ref socketAddressLen, isIPv4, isIPv6, out bytesTransferred, out socketFlags, out ipPacketInformation, out errorCode))
{
errorCode = SocketError.WouldBlock;
}
}
socketAddress.InternalSize = socketAddressLen;
receiveAddress = socketAddress;
return errorCode;
}
示例9: FinishOperationSuccess
//.........这里部分代码省略.........
}
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);
}
示例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;
}
}
}
示例11: EndReceiveMessageFrom
/// <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;
}
}
}
}
示例12: ReceiveMessageFrom
/// <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;
//.........这里部分代码省略.........
示例13: EndReceiveMessageFrom
public int EndReceiveMessageFrom(IAsyncResult asyncResult, ref SocketFlags socketFlags, ref EndPoint endPoint, out IPPacketInformation ipPacketInformation)
{
if(Logging.On)Logging.Enter(Logging.Sockets, this, "EndReceiveMessageFrom", asyncResult);
if (CleanedUp) {
throw new ObjectDisposedException(this.GetType().FullName);
}
if (endPoint==null) {
throw new ArgumentNullException("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"));
}
int bytesTransferred = (int)castedAsyncResult.InternalWaitForCompletion();
castedAsyncResult.EndCalled = true;
castedAsyncResult.ExtractCache(ref Caches.ReceiveOverlappedCache);
// Update socket address size
castedAsyncResult.SocketAddress.SetSize(castedAsyncResult.GetSocketAddressSizePtr());
// pick up the saved copy of the original EndPoint from the asyncResult
SocketAddress socketAddressOriginal = endPoint.Serialize();
if (!socketAddressOriginal.Equals(castedAsyncResult.SocketAddress)) {
try {
endPoint = endPoint.Create(castedAsyncResult.SocketAddress);
}
catch {
}
}
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(Logging.On)Logging.Exception(Logging.Sockets, this, "EndReceiveMessageFrom", socketException);
throw socketException;
}
socketFlags = castedAsyncResult.m_flags;
ipPacketInformation = castedAsyncResult.m_IPPacketInformation;
if(Logging.On)Logging.Exit(Logging.Sockets, this, "EndReceiveMessageFrom", bytesTransferred);
return bytesTransferred;
}
示例14: ReceiveMessageFrom
/// <summary>
/// Extends ReceiveMessageFrom so that buffer offset of 0 and call to Array.Length are not needed.
/// <example>
/// socket.ReceiveMessageFrom(buffer, socketFlags, remoteEP, ipPacketInformation);
/// </example>
/// </summary>
public static Int32 ReceiveMessageFrom(this Socket socket, Byte[] buffer, ref SocketFlags socketFlags, ref System.Net.EndPoint remoteEP, out IPPacketInformation ipPacketInformation)
{
if(socket == null) throw new ArgumentNullException("socket");
if(buffer == null) throw new ArgumentNullException("buffer");
return socket.ReceiveMessageFrom(buffer, 0, buffer.Length, ref socketFlags, ref remoteEP, out ipPacketInformation);
}
示例15: ReceiveMessageFrom
/// <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;
//.........这里部分代码省略.........