示例#1
0
        //
        // SetUnmanagedStructures -
        // Fills in Overlapped Structures used in an Async Overlapped Winsock call
        //   these calls are outside the runtime and are unmanaged code, so we need
        //   to prepare specific structures and ints that lie in unmanaged memory
        //   since the Overlapped calls can be Async
        //

        internal void SetUnmanagedStructures(byte[] buffer, int offset, int size, SocketAddress socketAddress, bool pinSocketAddress)
        {
            //
            // Fill in Buffer Array structure that will be used for our send/recv Buffer
            //
            m_SocketAddress = socketAddress;
            if (pinSocketAddress && m_SocketAddress != null)
            {
                object[] objectsToPin = null;
                objectsToPin    = new object[2];
                objectsToPin[0] = buffer;

                m_SocketAddress.CopyAddressSizeIntoBuffer();
                objectsToPin[1] = m_SocketAddress.m_Buffer;

                base.SetUnmanagedStructures(objectsToPin);
            }
            else
            {
                base.SetUnmanagedStructures(buffer);
            }

            m_SingleBuffer.Length  = size;
            m_SingleBuffer.Pointer = Marshal.UnsafeAddrOfPinnedArrayElement(buffer, offset);
        }
        //
        // SetUnmanagedStructures -
        // Fills in Overlapped Structures used in an Async Overlapped Winsock call
        //   these calls are outside the runtime and are unmanaged code, so we need
        //   to prepare specific structures and ints that lie in unmanaged memory
        //   since the Overlapped calls can be Async
        //

        internal void SetUnmanagedStructures(byte[] buffer, int offset, int size, SocketAddress socketAddress, SocketFlags socketFlags)
        {
            //ipv4 or ipv6?
            bool ipv4 = ((Socket)(AsyncObject)).AddressFamily == AddressFamily.InterNetwork;
            bool ipv6 = ((Socket)(AsyncObject)).AddressFamily == AddressFamily.InterNetworkV6;

            m_MessageBuffer  = new byte[s_WSAMsgSize];
            m_WSABufferArray = new byte[s_WSABufferSize];

            //prepare control buffer
            if (ipv4)
            {
                m_ControlBuffer = new byte[s_ControlDataSize];
            }
            else if (ipv6)
            {
                m_ControlBuffer = new byte[s_ControlDataIPv6Size];
            }

            //pin buffers
            object[] objectsToPin = new object[(m_ControlBuffer != null)?5:4];
            objectsToPin[0] = buffer;
            objectsToPin[1] = m_MessageBuffer;
            objectsToPin[2] = m_WSABufferArray;

            //prepare socketaddress buffer
            m_SocketAddress = socketAddress;
            m_SocketAddress.CopyAddressSizeIntoBuffer();
            objectsToPin[3] = m_SocketAddress.m_Buffer;

            if (m_ControlBuffer != null)
            {
                objectsToPin[4] = m_ControlBuffer;
            }

            base.SetUnmanagedStructures(objectsToPin);

            //prepare data buffer
            m_WSABuffer          = (WSABuffer *)Marshal.UnsafeAddrOfPinnedArrayElement(m_WSABufferArray, 0);
            m_WSABuffer->Length  = size;
            m_WSABuffer->Pointer = Marshal.UnsafeAddrOfPinnedArrayElement(buffer, offset);


            //setup structure
            m_Message = (UnsafeNclNativeMethods.OSSOCK.WSAMsg *)Marshal.UnsafeAddrOfPinnedArrayElement(m_MessageBuffer, 0);
            m_Message->socketAddress = Marshal.UnsafeAddrOfPinnedArrayElement(m_SocketAddress.m_Buffer, 0);
            m_Message->addressLength = (uint)m_SocketAddress.Size;
            m_Message->buffers       = Marshal.UnsafeAddrOfPinnedArrayElement(m_WSABufferArray, 0);
            m_Message->count         = 1;

            if (m_ControlBuffer != null)
            {
                m_Message->controlBuffer.Pointer = Marshal.UnsafeAddrOfPinnedArrayElement(m_ControlBuffer, 0);
                m_Message->controlBuffer.Length  = m_ControlBuffer.Length;
            }

            m_Message->flags = socketFlags;
        }