Пример #1
0
        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);
            }
        }
Пример #2
0
        public void Connect(IPEndPoint endpoint, TcpSocketConnectCallback callback)
        {
            TcpSocketConnectRoutine routine = new TcpSocketConnectRoutine(handle, worker, endpoint);
            TcpSocketConnectResult  result  = new TcpSocketConnectResult
            {
                Handle      = handle,
                Socket      = this,
                Endpoint    = endpoint,
                OnConnected = callback
            };

            routine.Execute(result);
        }
Пример #3
0
        public Task <TcpSocketConnect> Connect(IPEndPoint endpoint)
        {
            TcpSocketConnectResult result = new TcpSocketConnectResult
            {
                Socket   = this,
                Handle   = handle,
                Endpoint = endpoint,
                Event    = new ManualResetEvent(false)
            };

            TcpSocketConnectRoutine routine = new TcpSocketConnectRoutine(handle, worker, endpoint);
            Task <TcpSocketConnect> task    = Task.Factory.FromAsync(result, result.Unpack);

            routine.Execute(result);
            return(task);
        }