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(TcpSocketSendResult target) { target.Pin(buffer.Data); Overlapped overlapped = new Overlapped { AsyncResult = target }; NativeOverlapped *native = overlapped.UnsafePack(null, null); IntPtr reference = Marshal.UnsafeAddrOfPinnedArrayElement(buffer.Data, buffer.Offset); TcpSocketInterop.WSABuffer data = new TcpSocketInterop.WSABuffer { length = buffer.Count, buffer = reference }; int written; uint result = TcpSocketInterop.WSASend(handle, &data, 1, out written, 0, native, IntPtr.Zero); uint error = TcpSocketInterop.WSAGetLastError(); 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(UdpSocketSendResult target) { target.Pin(buffer.Data); Overlapped overlapped = new Overlapped { AsyncResult = target }; NativeOverlapped *native = overlapped.UnsafePack(null, null); IntPtr reference = Marshal.UnsafeAddrOfPinnedArrayElement(buffer.Data, buffer.Offset); TcpSocketInterop.WSABuffer data = new TcpSocketInterop.WSABuffer { length = buffer.Count, buffer = reference }; byte[] address = endpoint.Address.GetAddressBytes(); byte[] addressData = { 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 }; IntPtr addressBuffer = Marshal.UnsafeAddrOfPinnedArrayElement(addressData, 0); target.Pin(addressData); int written; uint result = UdpSocketInterop.WSASendTo(handle, &data, 1, out written, 0, addressBuffer, addressData.Length, native, IntPtr.Zero); uint error = TcpSocketInterop.WSAGetLastError(); if (result != 0 && error != 997) { target.Fail(error); } }
public void Listen(int backlog) { int value = TcpSocketInterop.listen(handle, backlog); if (value != 0) { throw new Exception(); } worker.Add(handle); }
public TcpSocket Tcp() { IntPtr handle = TcpSocketInterop.WSASocket(2, 1, 6, IntPtr.Zero, 0, 1); if (handle == new IntPtr(-1)) { throw new Exception(); } return(new TcpSocketInstance(handle, worker)); }
public bool Execute(IntPtr handle) { byte[] data = { 0x02, 0x00, (byte)(endpoint.Port / 256), (byte)(endpoint.Port % 256), 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; GCHandle pinned = GCHandle.Alloc(data, GCHandleType.Pinned); byte[] bytes = endpoint.Address.GetAddressBytes(); Array.Copy(bytes, 0, data, 4, 4); IntPtr pointer = Marshal.UnsafeAddrOfPinnedArrayElement(data, 0); int value = TcpSocketInterop.bind(handle, pointer, data.Length); pinned.Free(); return(value == 0); }
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); } }
private unsafe bool GetDelegate <T>(TcpSocketAcceptResult target, out T accept, string id) { int sent; IntPtr ptr = IntPtr.Zero; Guid guid = new Guid(id); 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) { target.Fail(); accept = default(T); return(true); } accept = (T)(object)Marshal.GetDelegateForFunctionPointer(ptr, typeof(T)); return(false); }
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 unsafe void Complete(NativeOverlapped *overlapped, int affected) { uint ignore; uint flags; uint result = TcpSocketInterop.WSAGetOverlappedResult(Handle, overlapped, out ignore, false, out flags); Affected = affected; IsCompleted = true; if (result != 0 || affected > 0) { Release(); OnCompleted(); } else { Fail(); } }
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 Dispose() { TcpSocketInterop.closesocket(handle); }
public void Fail() { Fail(TcpSocketInterop.GetLastError()); }