Пример #1
0
        private unsafe int WSARecv_Hook(IntPtr socket, ref WSABuffer buffer, int bufferCount, out int bytesTransferred, ref int socketFlags, IntPtr nativeOverlapped, IntPtr completionRoutine)
        {
            int result = Ws2_32.WSARecv(socket, ref buffer, bufferCount, out bytesTransferred, ref socketFlags, nativeOverlapped, completionRoutine);

            if (_socket == socket)
            {
                if (bytesTransferred > ushort.MaxValue || bytesTransferred == 0 || bufferCount != 1)
                {
                    return(result);
                }
                _serverPackets++;
                byte[] data = buffer.Buffer.ExtractBuffer(bytesTransferred);
                switch (_serverPackets)
                {
                case 1: PacketController.Preinitialize(this, data); break;

                case 2: sKey1 = data; break;

                case 3: sKey2 = data; PacketController.Initialize(cKey1, cKey2, sKey1, sKey2); break;

                default:
                    byte[] processed = PacketController.ProcessServerData(data);
                    if (processed.Take(4).SequenceEqual(GameClient.C_SIGNATURE))
                    {
                        Marshal.Copy(processed, 4, buffer.Buffer + bytesTransferred, processed.Length - 4);
                        bytesTransferred += processed.Length - 4;
                    }
                    break;
                }
            }
            return(0);
        }
 internal static extern SocketError WSASend(
     [In] SafeCloseSocket socketHandle,
     [In] ref WSABuffer buffer,
     [In] int bufferCount,
     [Out] out int bytesTransferred,
     [In] SocketFlags socketFlags,
     [In] SafeHandle overlapped,
     [In] IntPtr completionRoutine);
Пример #3
0
 internal static extern SocketError WSARecvFrom(
     [In] SafeCloseSocket socketHandle,
     [In] ref WSABuffer buffer,
     [In] int bufferCount,
     [Out] out int bytesTransferred,
     [In, Out] ref SocketFlags socketFlags,
     [In] IntPtr socketAddressPointer,
     [In] IntPtr socketAddressSizePointer,
     [In] SafeHandle overlapped,
     [In] IntPtr completionRoutine);
Пример #4
0
        private static unsafe SocketError Detour_WSARecv(
            IntPtr socketHandle,
            ref WSABuffer buffer,
            int bufferCount,
            out int bytesTransferred,
            ref SocketFlags socketFlags,
            NativeOverlapped *overlapped,
            IntPtr completionRoutine)
        {
            WSABuffer localBuffer = buffer;

            return(Interop.Winsock.WSARecv(socketHandle, &localBuffer, bufferCount, out bytesTransferred, ref socketFlags, overlapped, completionRoutine));
        }
Пример #5
0
        internal static unsafe SocketError WSASend(
            SafeHandle socketHandle,
            ref WSABuffer buffer,
            int bufferCount,
            out int bytesTransferred,
            SocketFlags socketFlags,
            NativeOverlapped *overlapped,
            IntPtr completionRoutine)
        {
            // We intentionally do NOT copy this back after the function completes:
            // We don't want to cause a race in async scenarios.
            // The WSABuffer struct should be unchanged anyway.
            WSABuffer localBuffer = buffer;

            return(WSASend(socketHandle, &localBuffer, bufferCount, out bytesTransferred, socketFlags, overlapped, completionRoutine));
        }
Пример #6
0
        internal static unsafe SocketError WSARecvFrom(
            SafeCloseSocket socketHandle,
            ref WSABuffer buffer,
            int bufferCount,
            out int bytesTransferred,
            ref SocketFlags socketFlags,
            IntPtr socketAddressPointer,
            IntPtr socketAddressSizePointer,
            SafeNativeOverlapped overlapped,
            IntPtr completionRoutine)
        {
            // We intentionally do NOT copy this back after the function completes:
            // We don't want to cause a race in async scenarios.
            // The WSABuffer struct should be unchanged anyway.
            WSABuffer localBuffer = buffer;

            return(WSARecvFrom(socketHandle, &localBuffer, bufferCount, out bytesTransferred, ref socketFlags, socketAddressPointer, socketAddressSizePointer, overlapped, completionRoutine));
        }
Пример #7
0
        public unsafe int SendToClient(byte[] data, out int length)
        {
            Ws2_32.WSARecv_Delegate recv = LocalHook.GetProcAddress("ws2_32.dll", "WSARecv").ToDelegate <Ws2_32.WSARecv_Delegate>();
            byte[] buffer = new byte[ushort.MaxValue];
            fixed(byte *uBuffer = buffer)
            {
                for (int i = 0; i < data.Length; i++)
                {
                    buffer[i] = data[i];
                }
                WSABuffer wsa = new WSABuffer()
                {
                    Buffer = (IntPtr)uBuffer, Length = buffer.Length
                };

                Log.WriteLine($"WSALength: {wsa.Length}, WSABuffer: {wsa.Buffer.ToInt32()}, Transferred: {data.Length}");
                int socketFlags = 0;

                length = data.Length;
                return(recv(_socket, ref wsa, 1, out length, ref socketFlags, IntPtr.Zero, IntPtr.Zero));
            }
        }
Пример #8
0
        internal static int repl_wsa_recv(IntPtr socket, ref WSABuffer wsaBuffer, int bufferCount, out int bytesTransferred, ref int socketFlags, IntPtr overlapped, IntPtr completionRoutine)
        {
            WSARecvHook.Suspend();

            var length = WSARecv(socket, ref wsaBuffer, bufferCount, out bytesTransferred, ref socketFlags, overlapped, completionRoutine);
            var buffer = wsaBuffer.Pointer.Copy(0, wsaBuffer.Length);

            var packet = new Packet()
            {
                Channel     = PacketChannel.WSARecv,
                Buffer      = buffer,
                Source      = socket.GetSourceIPEndPoint(),
                Destination = socket.GetDestinationIPEndPoint(),
                Length      = length,
                Socket      = (int)socket
            };

            // TODO: allow modifying wsa_recv packets
            try { OnReceivePacket(packet); } catch { }

            WSARecvHook.Continue();
            return(length);
        }
Пример #9
0
 internal static extern int WSARecv(IntPtr socketHandle, ref WSABuffer buffer, int bufferCount, out int bytesTransferred, ref int socketFlags, IntPtr overlapped, IntPtr completionRoutine);
Пример #10
0
 internal void MultipleSend(BufferOffsetSize[] buffers, SocketFlags socketFlags)
 {
     if (Socket.s_LoggingEnabled)
     Logging.Enter(Logging.Sockets, (object) this, "MultipleSend", "");
       if (this.CleanedUp)
     throw new ObjectDisposedException(this.GetType().FullName);
       WSABuffer[] buffersArray = new WSABuffer[buffers.Length];
       GCHandle[] gcHandleArray = (GCHandle[]) null;
       SocketError socketError;
       try
       {
     gcHandleArray = new GCHandle[buffers.Length];
     for (int index = 0; index < buffers.Length; ++index)
     {
       gcHandleArray[index] = GCHandle.Alloc((object) buffers[index].Buffer, GCHandleType.Pinned);
       buffersArray[index].Length = buffers[index].Size;
       buffersArray[index].Pointer = Marshal.UnsafeAddrOfPinnedArrayElement((Array) buffers[index].Buffer, buffers[index].Offset);
     }
     int bytesTransferred;
     socketError = UnsafeNclNativeMethods.OSSOCK.WSASend_Blocking(this.m_Handle.DangerousGetHandle(), buffersArray, buffersArray.Length, out bytesTransferred, socketFlags, (SafeHandle) SafeNativeOverlapped.Zero, IntPtr.Zero);
       }
       finally
       {
     if (gcHandleArray != null)
     {
       for (int index = 0; index < gcHandleArray.Length; ++index)
       {
     if (gcHandleArray[index].IsAllocated)
       gcHandleArray[index].Free();
       }
     }
       }
       if (socketError != SocketError.Success)
       {
     SocketException socketException = new SocketException();
     this.UpdateStatusAfterSocketError(socketException);
     if (Socket.s_LoggingEnabled)
       Logging.Exception(Logging.Sockets, (object) this, "MultipleSend", (Exception) socketException);
     throw socketException;
       }
       else
       {
     if (!Socket.s_LoggingEnabled)
       return;
     Logging.Exit(Logging.Sockets, (object) this, "MultipleSend", "");
       }
 }
 public int Send(IList<ArraySegment<byte>> buffers, SocketFlags socketFlags, out SocketError errorCode)
 {
     int num2;
     if (s_LoggingEnabled)
     {
         Logging.Enter(Logging.Sockets, this, "Send", "");
     }
     if (this.CleanedUp)
     {
         throw new ObjectDisposedException(base.GetType().FullName);
     }
     if (buffers == null)
     {
         throw new ArgumentNullException("buffers");
     }
     if (buffers.Count == 0)
     {
         throw new ArgumentException(SR.GetString("net_sockets_zerolist", new object[] { "buffers" }), "buffers");
     }
     this.ValidateBlockingMode();
     errorCode = SocketError.Success;
     int count = buffers.Count;
     WSABuffer[] buffersArray = new WSABuffer[count];
     GCHandle[] handleArray = null;
     try
     {
         handleArray = new GCHandle[count];
         for (int i = 0; i < count; i++)
         {
             ArraySegment<byte> segment = buffers[i];
             ValidationHelper.ValidateSegment(segment);
             handleArray[i] = GCHandle.Alloc(segment.Array, GCHandleType.Pinned);
             buffersArray[i].Length = segment.Count;
             buffersArray[i].Pointer = Marshal.UnsafeAddrOfPinnedArrayElement(segment.Array, segment.Offset);
         }
         errorCode = UnsafeNclNativeMethods.OSSOCK.WSASend_Blocking(this.m_Handle.DangerousGetHandle(), buffersArray, count, out num2, socketFlags, SafeNativeOverlapped.Zero, IntPtr.Zero);
         if (errorCode == SocketError.SocketError)
         {
             errorCode = (SocketError) Marshal.GetLastWin32Error();
         }
     }
     finally
     {
         if (handleArray != null)
         {
             for (int j = 0; j < handleArray.Length; j++)
             {
                 if (handleArray[j].IsAllocated)
                 {
                     handleArray[j].Free();
                 }
             }
         }
     }
     if (errorCode != SocketError.Success)
     {
         this.UpdateStatusAfterSocketError(errorCode);
         if (s_LoggingEnabled)
         {
             Logging.Exception(Logging.Sockets, this, "Send", new SocketException(errorCode));
             Logging.Exit(Logging.Sockets, this, "Send", 0);
         }
         return 0;
     }
     if (s_PerfCountersEnabled && (num2 > 0))
     {
         NetworkingPerfCounters.Instance.Increment(NetworkingPerfCounterName.SocketBytesSent, (long) num2);
         if (this.Transport == TransportType.Udp)
         {
             NetworkingPerfCounters.Instance.Increment(NetworkingPerfCounterName.SocketDatagramsSent);
         }
     }
     if (s_LoggingEnabled)
     {
         Logging.Exit(Logging.Sockets, this, "Send", num2);
     }
     return num2;
 }
Пример #12
0
        internal void MultipleSend(BufferOffsetSize[] buffers, SocketFlags socketFlags) {
            if(s_LoggingEnabled)Logging.Enter(Logging.Sockets, this, "MultipleSend", "");
            if (CleanedUp) {
                throw new ObjectDisposedException(this.GetType().FullName);
            }
            //
            // parameter validation
            //
            GlobalLog.Assert(buffers != null, "Socket#{0}::MultipleSend()|buffers == null", ValidationHelper.HashString(this));
            GlobalLog.Print("Socket#" + ValidationHelper.HashString(this) + "::MultipleSend() buffers.Length:" + buffers.Length.ToString());

            WSABuffer[] WSABuffers = new WSABuffer[buffers.Length];
            GCHandle[] objectsToPin = null;
            int bytesTransferred;
            SocketError errorCode;

            try {
                objectsToPin = new GCHandle[buffers.Length];
                for (int i = 0; i < buffers.Length; ++i)
                {
                    objectsToPin[i] = GCHandle.Alloc(buffers[i].Buffer, GCHandleType.Pinned);
                    WSABuffers[i].Length = buffers[i].Size;
                    WSABuffers[i].Pointer = Marshal.UnsafeAddrOfPinnedArrayElement(buffers[i].Buffer, buffers[i].Offset);
                }

                // This can throw ObjectDisposedException.
                errorCode = UnsafeNclNativeMethods.OSSOCK.WSASend_Blocking(
                    m_Handle.DangerousGetHandle(),
                    WSABuffers,
                    WSABuffers.Length,
                    out bytesTransferred,
                    socketFlags,
                    SafeNativeOverlapped.Zero,
                    IntPtr.Zero);

                GlobalLog.Print("Socket#" + ValidationHelper.HashString(this) + "::MultipleSend() UnsafeNclNativeMethods.OSSOCK.WSASend returns:" + errorCode.ToString() + " size:" + buffers.Length.ToString());

            }
            finally {
                if (objectsToPin != null)
                    for (int i = 0; i < objectsToPin.Length; ++i)
                        if (objectsToPin[i].IsAllocated)
                            objectsToPin[i].Free();
            }

            if (errorCode!=SocketError.Success) {
                SocketException socketException = new SocketException();
                UpdateStatusAfterSocketError(socketException);
                if(s_LoggingEnabled)Logging.Exception(Logging.Sockets, this, "MultipleSend", socketException);
                throw socketException;
            }

            if(s_LoggingEnabled)Logging.Exit(Logging.Sockets, this, "MultipleSend", "");
        }
Пример #13
0
        public static SocketError Send(SafeCloseSocket handle, BufferOffsetSize[] buffers, SocketFlags socketFlags, out int bytesTransferred)
        {
            WSABuffer[] WSABuffers = new WSABuffer[buffers.Length];
            GCHandle[] objectsToPin = null;

            try
            {
                objectsToPin = new GCHandle[buffers.Length];
                for (int i = 0; i < buffers.Length; ++i)
                {
                    objectsToPin[i] = GCHandle.Alloc(buffers[i].Buffer, GCHandleType.Pinned);
                    WSABuffers[i].Length = buffers[i].Size;
                    WSABuffers[i].Pointer = Marshal.UnsafeAddrOfPinnedArrayElement(buffers[i].Buffer, buffers[i].Offset);
                }

                // This can throw ObjectDisposedException.
                SocketError errorCode = Interop.Winsock.WSASend_Blocking(
                    handle.DangerousGetHandle(),
                    WSABuffers,
                    WSABuffers.Length,
                    out bytesTransferred,
                    socketFlags,
                    SafeNativeOverlapped.Zero,
                    IntPtr.Zero);

                if (errorCode == SocketError.SocketError)
                {
                    errorCode = (SocketError)Marshal.GetLastWin32Error();
                }

                return errorCode;
            }
            finally
            {
                if (objectsToPin != null)
                {
                    for (int i = 0; i < objectsToPin.Length; ++i)
                    {
                        if (objectsToPin[i].IsAllocated)
                        {
                            objectsToPin[i].Free();
                        }
                    }
                }
            }
        }
Пример #14
0
 internal static extern SocketError WSASend(IntPtr socketHandle, ref WSABuffer buffer, int bufferCount, out int bytesTransferred, SocketFlags socketFlags, IntPtr overlapped, IntPtr completionRoutine);
Пример #15
0
        public static SocketError Receive(SafeCloseSocket handle, IList<ArraySegment<byte>> buffers, ref SocketFlags socketFlags, out int bytesTransferred)
        {
            int count = buffers.Count;
            WSABuffer[] WSABuffers = new WSABuffer[count];
            GCHandle[] objectsToPin = null;

            try
            {
                objectsToPin = new GCHandle[count];
                for (int i = 0; i < count; ++i)
                {
                    ArraySegment<byte> buffer = buffers[i];
                    RangeValidationHelpers.ValidateSegment(buffer);
                    objectsToPin[i] = GCHandle.Alloc(buffer.Array, GCHandleType.Pinned);
                    WSABuffers[i].Length = buffer.Count;
                    WSABuffers[i].Pointer = Marshal.UnsafeAddrOfPinnedArrayElement(buffer.Array, buffer.Offset);
                }

                // This can throw ObjectDisposedException.
                SocketError errorCode = Interop.Winsock.WSARecv_Blocking(
                    handle.DangerousGetHandle(),
                    WSABuffers,
                    count,
                    out bytesTransferred,
                    ref socketFlags,
                    SafeNativeOverlapped.Zero,
                    IntPtr.Zero);

                if ((SocketError)errorCode == SocketError.SocketError)
                {
                    errorCode = (SocketError)Marshal.GetLastWin32Error();
                }

                return errorCode;
            }
            finally
            {
                if (objectsToPin != null)
                {
                    for (int i = 0; i < objectsToPin.Length; ++i)
                    {
                        if (objectsToPin[i].IsAllocated)
                        {
                            objectsToPin[i].Free();
                        }
                    }
                }
            }
        }
Пример #16
0
        public int Receive(IList<ArraySegment<byte>> buffers, SocketFlags socketFlags, out SocketError errorCode)
        {
            if(Logging.On)Logging.Enter(Logging.Sockets, this, "Receive", "");
            if (CleanedUp) {
                throw new ObjectDisposedException(this.GetType().FullName);
            }

            if (buffers==null) {
               throw new ArgumentNullException("buffers");
            }

            if(buffers.Count == 0){
                throw new ArgumentException(SR.GetString(SR.net_sockets_zerolist,"buffers"), "buffers");
            }

            ValidateBlockingMode();
            GlobalLog.Print("Socket#" + ValidationHelper.HashString(this) + "::Receive() SRC:" + ValidationHelper.ToString(LocalEndPoint) + " DST:" + ValidationHelper.ToString(RemoteEndPoint));

            //make sure we don't let the app mess up the buffer array enough to cause
            //corruption.
            int count = buffers.Count;
            WSABuffer[] WSABuffers = new WSABuffer[count];
            GCHandle[] objectsToPin = null;
            int bytesTransferred;
            errorCode = SocketError.Success;

            try {
                objectsToPin = new GCHandle[count];
                for (int i = 0; i < count; ++i)
                {
                    ArraySegment<byte> buffer = buffers[i];
                    objectsToPin[i] = GCHandle.Alloc(buffer.Array, GCHandleType.Pinned);
                    WSABuffers[i].Length = buffer.Count;
                    WSABuffers[i].Pointer = Marshal.UnsafeAddrOfPinnedArrayElement(buffer.Array, buffer.Offset);
                }

                // This can throw ObjectDisposedException.
                errorCode = UnsafeNclNativeMethods.OSSOCK.WSARecv_Blocking(
                    m_Handle.DangerousGetHandle(),
                    WSABuffers,
                    buffers.Count,
                    out bytesTransferred,
                    ref socketFlags,
                    IntPtr.Zero,
                    IntPtr.Zero );

                if ((SocketError)errorCode==SocketError.SocketError) {
                    errorCode = (SocketError)Marshal.GetLastWin32Error();
                }
            }
            finally {
                if (objectsToPin != null)
                    for (int i = 0; i < objectsToPin.Length; ++i)
                        if (objectsToPin[i].IsAllocated)
                            objectsToPin[i].Free();
            }

            if (errorCode != SocketError.Success) {
                //
                // update our internal state after this socket error and throw
                //
                UpdateStatusAfterSocketError(errorCode);
                if(Logging.On){
                    Logging.Exception(Logging.Sockets, this, "Receive", new SocketException(errorCode));
                    Logging.Exit(Logging.Sockets, this, "Receive", 0);
                }
                return 0;
            }

            if(Logging.On)Logging.Exit(Logging.Sockets, this, "Receive", bytesTransferred);

            return bytesTransferred;
        }
Пример #17
0
        public int Receive(IList<ArraySegment<byte>> buffers, SocketFlags socketFlags, out SocketError errorCode) {
            if(s_LoggingEnabled)Logging.Enter(Logging.Sockets, this, "Receive", "");
            if (CleanedUp) {
                throw new ObjectDisposedException(this.GetType().FullName);
            }

            if (buffers==null) {
               throw new ArgumentNullException("buffers");
            }

            if(buffers.Count == 0){
                throw new ArgumentException(SR.GetString(SR.net_sockets_zerolist,"buffers"), "buffers");
            }


            ValidateBlockingMode();
            GlobalLog.Print("Socket#" + ValidationHelper.HashString(this) + "::Receive() SRC:" + ValidationHelper.ToString(LocalEndPoint) + " DST:" + ValidationHelper.ToString(RemoteEndPoint));

            //make sure we don't let the app mess up the buffer array enough to cause
            //corruption.
            int count = buffers.Count;
            WSABuffer[] WSABuffers = new WSABuffer[count];
            GCHandle[] objectsToPin = null;
            int bytesTransferred;
            errorCode = SocketError.Success;

            try {
                objectsToPin = new GCHandle[count];
                for (int i = 0; i < count; ++i)
                {
                    ArraySegment<byte> buffer = buffers[i];
                    ValidationHelper.ValidateSegment(buffer);
                    objectsToPin[i] = GCHandle.Alloc(buffer.Array, GCHandleType.Pinned);
                    WSABuffers[i].Length = buffer.Count;
                    WSABuffers[i].Pointer = Marshal.UnsafeAddrOfPinnedArrayElement(buffer.Array, buffer.Offset);
                }

                // This can throw ObjectDisposedException.
                errorCode = UnsafeNclNativeMethods.OSSOCK.WSARecv_Blocking(
                    m_Handle.DangerousGetHandle(),
                    WSABuffers,
                    count,
                    out bytesTransferred,
                    ref socketFlags,
                    SafeNativeOverlapped.Zero,
                    IntPtr.Zero );

                if ((SocketError)errorCode==SocketError.SocketError) {
                    errorCode = (SocketError)Marshal.GetLastWin32Error();
                }
#if TRAVE
                try
                {
                    GlobalLog.Print("Socket#" + ValidationHelper.HashString(this) + "::Receive() SRC:" + ValidationHelper.ToString(LocalEndPoint) + " DST:" + ValidationHelper.ToString(RemoteEndPoint) + " UnsafeNclNativeMethods.OSSOCK.send returns errorCode:" + errorCode + " bytesTransferred:" + bytesTransferred);
                }
                catch (ObjectDisposedException) { }
#endif
            }
            finally {
                if (objectsToPin != null)
                    for (int i = 0; i < objectsToPin.Length; ++i)
                        if (objectsToPin[i].IsAllocated)
                            objectsToPin[i].Free();
            }

            if (errorCode != SocketError.Success) {
                //
                // update our internal state after this socket error and throw
                //
                UpdateStatusAfterSocketError(errorCode);
                if(s_LoggingEnabled){
                    Logging.Exception(Logging.Sockets, this, "Receive", new SocketException(errorCode));
                    Logging.Exit(Logging.Sockets, this, "Receive", 0);
                }
                return 0;
            }



#if !FEATURE_PAL // perfcounter
            if (s_PerfCountersEnabled)
            {
                bool peek = ((int)socketFlags & (int)SocketFlags.Peek)!=0;

                if (bytesTransferred>0 && !peek) {
                    NetworkingPerfCounters.Instance.Increment(NetworkingPerfCounterName.SocketBytesReceived, bytesTransferred);
                    if (Transport==TransportType.Udp) {
                        NetworkingPerfCounters.Instance.Increment(NetworkingPerfCounterName.SocketDatagramsReceived);
                    }
                }
            }
#endif //!FEATURE_PAL

#if TRAVE
            try
            {
                GlobalLog.Print("Socket#" + ValidationHelper.HashString(this) + "::Receive() SRC:" + ValidationHelper.ToString(LocalEndPoint) + " DST:" + ValidationHelper.ToString(RemoteEndPoint) + " bytesTransferred:" + bytesTransferred);
            }
            catch (ObjectDisposedException) { }
#endif

            if(s_LoggingEnabled)Logging.Exit(Logging.Sockets, this, "Receive", bytesTransferred);

            return bytesTransferred;
        }
Пример #18
0
 /// <summary>
 /// Sends the set of buffers in the list to a connected <see cref="T:System.Net.Sockets.Socket"/>, using the specified <see cref="T:System.Net.Sockets.SocketFlags"/>.
 /// </summary>
 /// 
 /// <returns>
 /// The number of bytes sent to the <see cref="T:System.Net.Sockets.Socket"/>.
 /// </returns>
 /// <param name="buffers">A list of <see cref="T:System.ArraySegment`1"/>s of type <see cref="T:System.Byte"/> that contains the data to be sent.</param><param name="socketFlags">A bitwise combination of the <see cref="T:System.Net.Sockets.SocketFlags"/> values.</param><param name="errorCode">A <see cref="T:System.Net.Sockets.SocketError"/> object that stores the socket error.</param><exception cref="T:System.ArgumentNullException"><paramref name="buffers"/> is null.</exception><exception cref="T:System.ArgumentException"><paramref name="buffers"/> is empty.</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 Send(IList<ArraySegment<byte>> buffers, SocketFlags socketFlags, out SocketError errorCode)
 {
     if (Socket.s_LoggingEnabled)
     Logging.Enter(Logging.Sockets, (object) this, "Send", "");
       if (this.CleanedUp)
     throw new ObjectDisposedException(this.GetType().FullName);
       if (buffers == null)
     throw new ArgumentNullException("buffers");
       if (buffers.Count == 0)
       {
     throw new ArgumentException(SR.GetString("net_sockets_zerolist", new object[1]
     {
       (object) "buffers"
     }), "buffers");
       }
       else
       {
     this.ValidateBlockingMode();
     errorCode = SocketError.Success;
     int count = buffers.Count;
     WSABuffer[] buffersArray = new WSABuffer[count];
     GCHandle[] gcHandleArray = (GCHandle[]) null;
     int bytesTransferred;
     try
     {
       gcHandleArray = new GCHandle[count];
       for (int index = 0; index < count; ++index)
       {
     ArraySegment<byte> segment = buffers[index];
     ValidationHelper.ValidateSegment(segment);
     gcHandleArray[index] = GCHandle.Alloc((object) segment.Array, GCHandleType.Pinned);
     buffersArray[index].Length = segment.Count;
     buffersArray[index].Pointer = Marshal.UnsafeAddrOfPinnedArrayElement((Array) segment.Array, segment.Offset);
       }
       errorCode = UnsafeNclNativeMethods.OSSOCK.WSASend_Blocking(this.m_Handle.DangerousGetHandle(), buffersArray, count, out bytesTransferred, socketFlags, (SafeHandle) SafeNativeOverlapped.Zero, IntPtr.Zero);
       if (errorCode == SocketError.SocketError)
     errorCode = (SocketError) Marshal.GetLastWin32Error();
     }
     finally
     {
       if (gcHandleArray != null)
       {
     for (int index = 0; index < gcHandleArray.Length; ++index)
     {
       if (gcHandleArray[index].IsAllocated)
         gcHandleArray[index].Free();
     }
       }
     }
     if (errorCode != SocketError.Success)
     {
       this.UpdateStatusAfterSocketError(errorCode);
       if (Socket.s_LoggingEnabled)
       {
     Logging.Exception(Logging.Sockets, (object) this, "Send", (Exception) new SocketException(errorCode));
     Logging.Exit(Logging.Sockets, (object) this, "Send", (object) 0);
       }
       return 0;
     }
     else
     {
       if (Socket.s_PerfCountersEnabled && bytesTransferred > 0)
       {
     NetworkingPerfCounters.Instance.Increment(NetworkingPerfCounterName.SocketBytesSent, (long) bytesTransferred);
     if (this.Transport == TransportType.Udp)
       NetworkingPerfCounters.Instance.Increment(NetworkingPerfCounterName.SocketDatagramsSent);
       }
       if (Socket.s_LoggingEnabled)
     Logging.Exit(Logging.Sockets, (object) this, "Send", (object) bytesTransferred);
       return bytesTransferred;
     }
       }
 }
 internal static unsafe extern int WSARecv
 (
     IntPtr handle, WSABuffer* buffers, int bufferCount, out int bytesTransferred,
     ref int socketFlags,
     NativeOverlapped* nativeOverlapped,
     IntPtr completionRoutine
 );
 internal void MultipleSend(BufferOffsetSize[] buffers, SocketFlags socketFlags)
 {
     SocketError error;
     if (s_LoggingEnabled)
     {
         Logging.Enter(Logging.Sockets, this, "MultipleSend", "");
     }
     if (this.CleanedUp)
     {
         throw new ObjectDisposedException(base.GetType().FullName);
     }
     WSABuffer[] buffersArray = new WSABuffer[buffers.Length];
     GCHandle[] handleArray = null;
     try
     {
         int num;
         handleArray = new GCHandle[buffers.Length];
         for (int i = 0; i < buffers.Length; i++)
         {
             handleArray[i] = GCHandle.Alloc(buffers[i].Buffer, GCHandleType.Pinned);
             buffersArray[i].Length = buffers[i].Size;
             buffersArray[i].Pointer = Marshal.UnsafeAddrOfPinnedArrayElement(buffers[i].Buffer, buffers[i].Offset);
         }
         error = UnsafeNclNativeMethods.OSSOCK.WSASend_Blocking(this.m_Handle.DangerousGetHandle(), buffersArray, buffersArray.Length, out num, socketFlags, SafeNativeOverlapped.Zero, IntPtr.Zero);
     }
     finally
     {
         if (handleArray != null)
         {
             for (int j = 0; j < handleArray.Length; j++)
             {
                 if (handleArray[j].IsAllocated)
                 {
                     handleArray[j].Free();
                 }
             }
         }
     }
     if (error != SocketError.Success)
     {
         SocketException socketException = new SocketException();
         this.UpdateStatusAfterSocketError(socketException);
         if (s_LoggingEnabled)
         {
             Logging.Exception(Logging.Sockets, this, "MultipleSend", socketException);
         }
         throw socketException;
     }
     if (s_LoggingEnabled)
     {
         Logging.Exit(Logging.Sockets, this, "MultipleSend", "");
     }
 }
Пример #21
-1
        public Socket(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType)
            : base(addressFamily, socketType, protocolType)
        {
            m_disposed = false;

            m_inOverlapped = new Overlapped(this);
            m_outOverlapped = new Overlapped(this);

            m_sendWSABuffer = new WSABuffer();
            m_receiveWSABuffer = new WSABuffer();

            InitSocket();
            InitDynamicMethods();
        }