public unsafe void Execute(UdpSocketReceiveResult target) { target.Pin(buffer.Data); IntPtr array = Marshal.UnsafeAddrOfPinnedArrayElement(buffer.Data, buffer.Offset); Overlapped overlapped = new Overlapped { AsyncResult = target }; NativeOverlapped *native = overlapped.UnsafePack(null, null); TcpSocketInterop.WSABuffer data = new TcpSocketInterop.WSABuffer { length = buffer.Count, buffer = array }; byte[] addressData = new byte[16]; IntPtr addressPointer = Marshal.UnsafeAddrOfPinnedArrayElement(addressData, 0); target.Address = addressData; target.Pin(addressData); int read, flags = 0, size = addressData.Length; int result = UdpSocketInterop.WSARecvFrom(handle, &data, 1, out read, ref flags, addressPointer, ref size, native, IntPtr.Zero); uint error = TcpSocketInterop.GetLastError(); if (result == -1 && error != 997) { target.Fail(error); } }
public unsafe void Execute(TcpSocketConnectResult target) { int sent; IntPtr ptr = IntPtr.Zero; Guid guid = new Guid("{0x25a207b9,0x0ddf3,0x4660,{0x8e,0xe9,0x76,0xe5,0x8c,0x74,0x06,0x3e}}"); int result = TcpSocketInterop.WSAIoctl(handle, unchecked ((int)0xC8000006), ref guid, sizeof(Guid), out ptr, sizeof(IntPtr), out sent, IntPtr.Zero, IntPtr.Zero); if (result != 0) { throw new Exception(); } byte[] address = endpoint.Address.GetAddressBytes(); byte[] data = { 0x02, 0x00, (byte)(endpoint.Port / 256), (byte)(endpoint.Port % 256), address[0], address[1], address[2], address[3], 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; TcpSocketInterop.ConnectExDelegate connectex = (TcpSocketInterop.ConnectExDelegate)Marshal.GetDelegateForFunctionPointer(ptr, typeof(TcpSocketInterop.ConnectExDelegate)); Overlapped overlapped = new Overlapped { AsyncResult = target }; NativeOverlapped *native = overlapped.UnsafePack(null, null); target.Pin(data); worker.Add(handle); IntPtr buffer = Marshal.UnsafeAddrOfPinnedArrayElement(data, 0); result = connectex.Invoke(handle, buffer, data.Length, IntPtr.Zero, 0, out sent, native); uint error = TcpSocketInterop.GetLastError(); if (result == 0 && error != 997) { target.Fail(error); } }
public unsafe void Execute(TcpSocketDisconnectResult target) { int sent; IntPtr ptr = IntPtr.Zero; Guid guid = new Guid("{0x7fda2e11,0x8630,0x436f,{0xa0, 0x31, 0xf5, 0x36, 0xa6, 0xee, 0xc1, 0x57}} "); int result = TcpSocketInterop.WSAIoctl(handle, unchecked ((int)0xC8000006), ref guid, sizeof(Guid), out ptr, sizeof(IntPtr), out sent, IntPtr.Zero, IntPtr.Zero); if (result != 0) { throw new Exception(); } TcpSocketInterop.DisconnectExDelegate disconnect = (TcpSocketInterop.DisconnectExDelegate)Marshal.GetDelegateForFunctionPointer(ptr, typeof(TcpSocketInterop.DisconnectExDelegate)); Overlapped overlapped = new Overlapped { AsyncResult = target }; NativeOverlapped *native = overlapped.UnsafePack(null, null); result = disconnect.Invoke(handle, native, 0x02, 0); uint error = TcpSocketInterop.GetLastError(); if (result == 0 && error != 997) { target.Fail(error); } }
public unsafe void Execute(TcpSocketAcceptResult target) { TcpSocketInterop.AcceptExDelegate accept; if (GetDelegate(target, out accept, "{0xb5367df1,0xcbac,0x11cf,{0x95,0xca,0x00,0x80,0x5f,0x48,0xa1,0x92}}")) { return; } TcpSocketInterop.GetAcceptExSockaddrsDelegate sockaddr; if (GetDelegate(target, out sockaddr, "{0xb5367df2,0xcbac,0x11cf,{0x95,0xca,0x00,0x80,0x5f,0x48,0xa1,0x92}}")) { return; } byte[] data = new byte[64]; IntPtr connection = TcpSocketInterop.WSASocket(2, 1, 6, IntPtr.Zero, 0, 1); target.Connection = new TcpSocketInstance(connection, worker); worker.Add(connection); target.Pin(data); IntPtr buffer = Marshal.UnsafeAddrOfPinnedArrayElement(data, 0); Overlapped overlapped = new Overlapped { AsyncResult = target }; NativeOverlapped *native = overlapped.UnsafePack(null, null); target.Buffer = buffer; target.Sockaddrs = sockaddr; int received; int result = accept(handle, connection, buffer, 0, 32, 32, out received, native); uint error = TcpSocketInterop.GetLastError(); if (result == 0 && error == 0) { if (received > 0) { target.Complete(native, received); } } else if (result == 0 && error != 997) { target.Fail(error); } }
public unsafe void Execute(SocketResult target) { target.Pin(buffer.Data); IntPtr array = Marshal.UnsafeAddrOfPinnedArrayElement(buffer.Data, buffer.Offset); Overlapped overlapped = new Overlapped { AsyncResult = target }; NativeOverlapped *native = overlapped.UnsafePack(null, null); uint read; uint result = TcpSocketInterop.ReadFile(handle, array, (uint)buffer.Count, out read, native); uint error = TcpSocketInterop.GetLastError(); if (result == 0 && error != 997) { target.Fail(error); } }
public TcpSocketInfo Info() { byte[] data = new byte[128]; GCHandle pinned = GCHandle.Alloc(data, GCHandleType.Pinned); int length = data.Length; IntPtr pointer = Marshal.UnsafeAddrOfPinnedArrayElement(data, 0); int result = TcpSocketInterop.getsockname(handle, pointer, ref length); uint error = TcpSocketInterop.GetLastError(); pinned.Free(); byte[] address = new byte[4]; Array.Copy(data, 4, address, 0, 4); int port = data[2] * 256 + data[3]; IPEndPoint endpoint = new IPEndPoint(new IPAddress(address), port); return(new TcpSocketInfo(SocketStatus.OK, this, endpoint)); }
public void Fail() { Fail(TcpSocketInterop.GetLastError()); }