internal static partial SocketError WSAIoctl_Blocking( SafeSocketHandle socketHandle, int ioControlCode, byte[]?inBuffer, int inBufferSize, byte[]?outBuffer, int outBufferSize, out int bytesTransferred, IntPtr overlapped, IntPtr completionRoutine);
internal static extern SocketError WSAIoctl( SafeSocketHandle socketHandle, [In] int ioControlCode, [In, Out] ref Guid guid, [In] int guidSize, [Out] out IntPtr funcPtr, [In] int funcPtrSize, [Out] out int bytesTransferred, [In] IntPtr shouldBeNull, [In] IntPtr shouldBeNull2);
public async Task <ConnectionContext> ConnectToServer(CancellationToken cancellationToken = default) { var endpoint = ServerEndPoint.EndPoint; Socket socket; switch (endpoint) { case FileHandleEndPoint fileHandle: // todo: preserve so that socketHandle can be disposed when socket use has completed var socketHandle = new SafeSocketHandle((IntPtr)fileHandle.FileHandle, ownsHandle: true); socket = new Socket(socketHandle); break; case UnixDomainSocketEndPoint unix: socket = new Socket(unix.AddressFamily, SocketType.Stream, ProtocolType.Unspecified); await Connect(); break; case IPEndPoint ip: socket = new Socket(ip.AddressFamily, SocketType.Stream, ProtocolType.Tcp); // Kestrel expects IPv6Any to bind to both IPv6 and IPv4 if (ip.Address == IPAddress.IPv6Any) { socket.DualMode = true; } await Connect(); break; default: socket = new Socket(endpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp); await Connect(); break; } return(SocketTransportFactory.CreateSocketConnection(socket, MemoryPool <byte> .Shared, PipeScheduler.ThreadPool, _loggerFactory)); async ValueTask Connect() { await socket.ConnectAsync(endpoint, cancellationToken); if (socket.ProtocolType == ProtocolType.Tcp) { socket.NoDelay = true; } } }
public static SafeRioRequestQueueHandle CreateRequestQueue(SafeSocketHandle socket, SafeRioCompletionQueueHandle completionQueue, IntPtr context, uint maxOutstandingReceive, uint maxReceiveDataBuffers, uint maxOutstandingSend, uint maxSendDataBuffers) { Debug.Assert(!socket.IsInvalid); Debug.Assert(!completionQueue.IsInvalid); SafeRioRequestQueueHandle queue = s_rioCreateRequestQueue(socket, maxOutstandingReceive, maxReceiveDataBuffers, maxOutstandingSend, maxSendDataBuffers, completionQueue, completionQueue, context); if (queue.IsInvalid) { throw new SocketException(); } queue.SetDependencies(socket, completionQueue); return(queue); }
internal Interop.SafeRioRequestQueueHandle RegisterSocket(SafeSocketHandle socketHandle) { lock (_completionQueue) { while (true) { try { return(Interop.Rio.CreateRequestQueue(socketHandle, _completionQueue, IntPtr.Zero, 1, 1)); } catch (SocketException ex) when(ex.SocketErrorCode == SocketError.NoBufferSpaceAvailable) { GrowCompletionQueueUnlocked(); } } } }
public void Ctor_Dispose_HandleClosedIfOwnsHandle(bool ownsHandle) { Socket original = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); IntPtr handleValue = original.Handle; SafeSocketHandle handleClone = new SafeSocketHandle(handleValue, ownsHandle: ownsHandle); Socket socketClone = new Socket(handleClone); socketClone.Dispose(); bool manualCloseSucceeded = closesocket(handleValue) == 0; Assert.Equal(!ownsHandle, manualCloseSucceeded); #if DEBUG // The finalizer will fail to close the handle which leads to an assertion failure in Debug builds. GC.SuppressFinalize(original); GC.SuppressFinalize(original.SafeHandle);
/// <summary>Opens a stream around the specified socket file descriptor and with the specified access.</summary> /// <param name="fd">The socket file descriptor.</param> /// <param name="access">The access mode.</param> /// <returns>The opened stream.</returns> private static Stream OpenStream(int fd, FileAccess access) { Debug.Assert(fd >= 0); var socketHandle = new SafeSocketHandle((IntPtr)fd, ownsHandle: true); var socket = new Socket(socketHandle); if (!socket.Connected) { // WSL1 workaround -- due to issues with sockets syscalls // socket pairs fd's are erroneously inferred as not connected. // Fall back to using FileStream instead. GC.SuppressFinalize(socket); GC.SuppressFinalize(socketHandle); return(new FileStream( new SafeFileHandle((IntPtr)fd, ownsHandle: true), access, StreamBufferSize, isAsync: false)); } return(new NetworkStream(socket, access, ownsSocket: true)); }
private static unsafe void CreateSocket() { Debug.Assert(Monitor.IsEntered(s_gate)); Debug.Assert(Socket == null, "Socket is not null, must close existing socket before opening another."); var sh = new SafeSocketHandle(); IntPtr newSocket; Interop.Error result = Interop.Sys.CreateNetworkChangeListenerSocket(&newSocket); if (result != Interop.Error.SUCCESS) { string message = Interop.Sys.GetLastErrorInfo().GetErrorMessage(); sh.Dispose(); throw new NetworkInformationException(message); } Marshal.InitHandle(sh, newSocket); Socket = new Socket(sh); // Don't capture ExecutionContext. ThreadPool.UnsafeQueueUserWorkItem(
//internal Socket (AddressFamily family, SocketType type, ProtocolType proto, IntPtr sock) bool EnsureSocket() { lock (_lock) { if (nl_sock != null) { return(true); } IntPtr fd = CreateNLSocket(); if (fd.ToInt64() == -1) { return(false); } var safeHandle = new SafeSocketHandle(fd, true); nl_sock = new Socket(0, SocketType.Raw, ProtocolType.Udp, safeHandle); nl_args = new SocketAsyncEventArgs(); nl_args.SetBuffer(new byte [8192], 0, 8192); nl_args.Completed += OnDataAvailable; nl_sock.ReceiveAsync(nl_args); } return(true); }
internal static unsafe partial int WSADuplicateSocket( SafeSocketHandle s, uint dwProcessId, WSAPROTOCOL_INFOW *lpProtocolInfo );
internal static extern int WSAEventSelect(SafeSocketHandle S, SafeWaitHandle hEventObject, int lNetworkEvents);
internal static extern unsafe int recv( SafeSocketHandle socketHandle, [In] byte *pinnedBuffer, [In] int len, [In] SocketFlags socketFlags);
public static extern CURLMcode SocketAction(SafeMultiHandle multiHandle, SafeSocketHandle sockfd, CURLcselect evBitmask, out int runningHandles);
public static extern unsafe bool SubchannelRecvExactBytes( [In] SafeSocketHandle subchannelFd, [In] void *buf, [In] UIntPtr len);
public UnixSubchannel(SafeSocketHandle handle) { _handle = handle; }
internal static unsafe extern SocketError setsockopt( [In] SafeSocketHandle socketHandle, [In] SocketOptionLevel optionLevel, [In] SocketOptionName optionName, [In] byte *optionValue, [In] int optionLength);
internal static extern Error GetSocketType(SafeSocketHandle socket, out AddressFamily addressFamily, out SocketType socketType, out ProtocolType protocolType, out bool isListening);
internal static partial SocketError ioctlsocket( SafeSocketHandle socketHandle, int cmd, ref int argp);
public static extern unsafe bool SubchannelSendExactBytes( [In] SafeSocketHandle subchannelFd, [In] void *buf, [In] nuint len);
internal static extern SocketError ioctlsocket( [In] SafeSocketHandle socketHandle, [In] int cmd, [In, Out] ref int argp);
internal static extern SocketError getsockopt( [In] SafeSocketHandle socketHandle, [In] SocketOptionLevel optionLevel, [In] SocketOptionName optionName, [Out] out IPv6MulticastRequest optionValue, [In, Out] ref int optionLength);
internal static extern SocketError getsockopt( [In] SafeSocketHandle socketHandle, [In] SocketOptionLevel optionLevel, [In] SocketOptionName optionName, [Out] out Linger optionValue, [In, Out] ref int optionLength);
internal static extern SafeSocketHandle.InnerSafeCloseSocket accept( SafeSocketHandle socketHandle, [Out] byte[] socketAddress, [In, Out] ref int socketAddressSize);
internal static extern unsafe bool WSAGetOverlappedResult( [In] SafeSocketHandle socketHandle, [In] NativeOverlapped *overlapped, [Out] out uint bytesTransferred, [In] bool wait, [Out] out SocketFlags socketFlags);
public static extern unsafe bool SubchannelSendExactBytesAndFds( [In] SafeSocketHandle subchannelFd, [In] void *buf, [In] nuint len, [In] int *fds, [In] nuint fdCount);
internal static extern SocketError setsockopt( [In] SafeSocketHandle socketHandle, [In] SocketOptionLevel optionLevel, [In] SocketOptionName optionName, [In] ref IPv6MulticastRequest mreq, [In] int optionLength);
internal static extern SocketError setsockopt( [In] SafeSocketHandle socketHandle, [In] SocketOptionLevel optionLevel, [In] SocketOptionName optionName, [In] ref Linger linger, [In] int optionLength);
internal static unsafe partial bool WSAGetOverlappedResult( SafeSocketHandle socketHandle, NativeOverlapped *overlapped, out uint bytesTransferred, bool wait, out SocketFlags socketFlags);
internal static extern IntPtr accept( SafeSocketHandle socketHandle, [Out] byte[] socketAddress, [In, Out] ref int socketAddressSize);
internal static unsafe partial int send( SafeSocketHandle socketHandle, byte *pinnedBuffer, int len, SocketFlags socketFlags);