Esempio n. 1
0
        // Private callback invoked when icmpsendecho APIs succeed.
        private static void PingCallback(object state, bool signaled)
        {
            Ping ping = (Ping)state;
            PingCompletedEventArgs eventArgs           = null;
            bool               cancelled               = false;
            AsyncOperation     asyncOp                 = null;
            SendOrPostCallback onPingCompletedDelegate = null;

            try
            {
                lock (ping._lockObject)
                {
                    cancelled = ping._cancelled;
                    asyncOp   = ping._asyncOp;
                    onPingCompletedDelegate = ping._onPingCompletedDelegate;

                    if (!cancelled)
                    {
                        // Parse reply buffer.
                        SafeLocalAllocHandle buffer = ping._replyBuffer;

                        // Marshals and constructs new reply.
                        PingReply reply;

                        if (ping._ipv6)
                        {
                            Interop.IpHlpApi.Icmp6EchoReply icmp6Reply = Marshal.PtrToStructure <Interop.IpHlpApi.Icmp6EchoReply>(buffer.DangerousGetHandle());
                            reply = new PingReply(icmp6Reply, buffer.DangerousGetHandle(), ping._sendSize);
                        }
                        else
                        {
                            Interop.IpHlpApi.IcmpEchoReply icmpReply = Marshal.PtrToStructure <Interop.IpHlpApi.IcmpEchoReply>(buffer.DangerousGetHandle());
                            reply = new PingReply(icmpReply);
                        }

                        eventArgs = new PingCompletedEventArgs(reply, null, false, asyncOp.UserSuppliedState);
                    }
                    else
                    {
                        // Canceled.
                        eventArgs = new PingCompletedEventArgs(null, null, true, asyncOp.UserSuppliedState);
                    }
                }
            }
            catch (Exception e)
            {
                // In case of failure, create a failed event arg.
                PingException pe = new PingException(SR.net_ping, e);
                eventArgs = new PingCompletedEventArgs(null, pe, false, asyncOp.UserSuppliedState);
            }
            finally
            {
                ping.FreeUnmanagedStructures();
                ping.UnregisterWaitHandle();
                ping.Finish(true);
            }

            asyncOp.PostOperationCompleted(onPingCompletedDelegate, eventArgs);
        }
Esempio n. 2
0
        //private callback invoked when icmpsendecho apis succeed
        private static void PingCallback(object state, bool signaled)
        {
            Ping ping = (Ping)state;
            PingCompletedEventArgs eventArgs           = null;
            bool               cancelled               = false;
            AsyncOperation     asyncOp                 = null;
            SendOrPostCallback onPingCompletedDelegate = null;

            try
            {
                lock (ping.lockObject) {
                    cancelled = ping.cancelled;
                    asyncOp   = ping.asyncOp;
                    onPingCompletedDelegate = ping.onPingCompletedDelegate;

                    if (!cancelled)
                    {
                        //parse reply buffer
                        SafeLocalFree buffer = ping.replyBuffer;

                        //marshals and constructs new reply
                        PingReply reply;

                        if (ping.ipv6)
                        {
                            Icmp6EchoReply icmp6Reply = (Icmp6EchoReply)Marshal.PtrToStructure(buffer.DangerousGetHandle(), typeof(Icmp6EchoReply));
                            reply = new PingReply(icmp6Reply, buffer.DangerousGetHandle(), ping.sendSize);
                        }
                        else
                        {
                            IcmpEchoReply icmpReply = (IcmpEchoReply)Marshal.PtrToStructure(buffer.DangerousGetHandle(), typeof(IcmpEchoReply));
                            reply = new PingReply(icmpReply);
                        }

                        eventArgs = new PingCompletedEventArgs(reply, null, false, asyncOp.UserSuppliedState);
                    }
                    else     //cancelled
                    {
                        eventArgs = new PingCompletedEventArgs(null, null, true, asyncOp.UserSuppliedState);
                    }
                }
            }
            // in case of failure, create a failed event arg
            catch (Exception e) {
                PingException pe = new PingException(SR.GetString(SR.net_ping), e);
                eventArgs = new PingCompletedEventArgs(null, pe, false, asyncOp.UserSuppliedState);
            }
            finally {
                ping.FreeUnmanagedStructures();
                ping.UnregisterWaitHandle();
                ping.Finish(true);
            }

            asyncOp.PostOperationCompleted(onPingCompletedDelegate, eventArgs);
        }
Esempio n. 3
0
        private static void PingCallback(object state, bool signaled)
        {
            Ping ping = (Ping)state;
            PingCompletedEventArgs arg   = null;
            bool               cancelled = false;
            AsyncOperation     asyncOp   = null;
            SendOrPostCallback d         = null;

            try
            {
                lock (ping.lockObject)
                {
                    cancelled = ping.cancelled;
                    asyncOp   = ping.asyncOp;
                    d         = ping.onPingCompletedDelegate;
                    if (!cancelled)
                    {
                        PingReply     reply;
                        SafeLocalFree replyBuffer = ping.replyBuffer;
                        if (!ping.ipv6 && !ComNetOS.IsVista)
                        {
                            UnsafeNetInfoNativeMethods.IcmpParseReplies(replyBuffer.DangerousGetHandle(), 0x100ff);
                        }
                        if (ping.ipv6)
                        {
                            Icmp6EchoReply reply2 = (Icmp6EchoReply)Marshal.PtrToStructure(replyBuffer.DangerousGetHandle(), typeof(Icmp6EchoReply));
                            reply = new PingReply(reply2, replyBuffer.DangerousGetHandle(), ping.sendSize);
                        }
                        else
                        {
                            IcmpEchoReply reply3 = (IcmpEchoReply)Marshal.PtrToStructure(replyBuffer.DangerousGetHandle(), typeof(IcmpEchoReply));
                            reply = new PingReply(reply3);
                        }
                        arg = new PingCompletedEventArgs(reply, null, false, asyncOp.UserSuppliedState);
                    }
                    else
                    {
                        arg = new PingCompletedEventArgs(null, null, true, asyncOp.UserSuppliedState);
                    }
                }
            }
            catch (Exception exception)
            {
                PingException error = new PingException(SR.GetString("net_ping"), exception);
                arg = new PingCompletedEventArgs(null, error, false, asyncOp.UserSuppliedState);
            }
            finally
            {
                ping.FreeUnmanagedStructures();
                ping.UnregisterWaitHandle();
                ping.Finish(true);
            }
            asyncOp.PostOperationCompleted(d, arg);
        }