Example #1
0
        void System_Diagnostics_TraceSource_TraceInformation(string[] args)
        {
            var ts = new System.Diagnostics.TraceSource("TraceTest");

            ts.TraceInformation("0"); // Compliant
            ts.TraceInformation("{0}", 42);

            ts.TraceInformation("{0}", args[0], args[1]); // Noncompliant
//          ^^^^^^^^^^^^^^^^^^^
            ts.TraceInformation("{2}", 1, 2, 3);          // Noncompliant
        }
        public static IObservable <T> TraceSubscriptions <T>(this IObservable <T> source, TraceSource trace)
        {
            Contract.Requires(source != null);
            Contract.Requires(trace != null);
            Contract.Ensures(Contract.Result <IObservable <T> >() != null);

            return(Observable.Create <T>(observer =>
            {
                trace.TraceInformation(Rxx.Diagnostics.Properties.Text.DefaultSubscribingMessage);

                var subscription = new CompositeDisposable(
                    Disposable.Create(() => trace.TraceInformation(Rxx.Diagnostics.Properties.Text.DefaultDisposingSubscriptionMessage)),
                    source.SubscribeSafe(observer),
                    Disposable.Create(() => trace.TraceInformation(Rxx.Diagnostics.Properties.Text.DefaultDisposedSubscriptionMessage)));

                trace.TraceInformation(Rxx.Diagnostics.Properties.Text.DefaultSubscribedMessage);

                return subscription;
            }));
        }
        void System_Diagnostics_TraceSource_TraceInformation(string[] args)
        {
            var ts = new System.Diagnostics.TraceSource("");

            ts.TraceInformation("0");
            ts.TraceInformation("{0}", 42);
            ts.TraceInformation("{");                // Compliant

            ts.TraceInformation("[0}", args[0]);     // Noncompliant
            ts.TraceInformation("{-1}", args[0]);    // Noncompliant
            ts.TraceInformation("{0} {1}", args[0]); // Noncompliant
            ts.TraceInformation(null, "foo", "bar"); // Noncompliant
        }
        public static IObservable <T> TraceSubscriptions <T>(this IObservable <T> source, TraceSource trace, string subscribingMessage, string subscribedMessage, string disposingMessage, string disposedMessage)
        {
            Contract.Requires(source != null);
            Contract.Requires(trace != null);
            Contract.Requires(subscribingMessage != null);
            Contract.Requires(subscribedMessage != null);
            Contract.Requires(disposingMessage != null);
            Contract.Requires(disposedMessage != null);
            Contract.Ensures(Contract.Result <IObservable <T> >() != null);

            return(Observable.Create <T>(observer =>
            {
                trace.TraceInformation(subscribingMessage);

                var subscription = new CompositeDisposable(
                    Disposable.Create(() => trace.TraceInformation(disposingMessage)),
                    source.SubscribeSafe(observer),
                    Disposable.Create(() => trace.TraceInformation(disposedMessage)));

                trace.TraceInformation(subscribedMessage);

                return subscription;
            }));
        }
        private void thread_start()
        {
            long nEventRecvCount = 0;

            appTrace.TraceEvent(System.Diagnostics.TraceEventType.Verbose, 0, "[thread_start] IN");
            appTrace.TraceEvent(System.Diagnostics.TraceEventType.Information, 0, "[thread_start] IN");

            Encoding enc = Encoding.UTF8;

            //System.Net.IPAddress ipAddsAny = System.Net.IPAddress.Any;
            //            System.Net.Sockets.TcpListener listener = new System.Net.Sockets.TcpListener(ipAddsAny, 55561);

            //  System.Net.IPAddress Ip = new System.Net.IPAddress(new byte[] { 192, 168, 0, 1 });
            // System.Net.IPAddress Ip = new System.Net.IPAddress(new byte[] { 192, 168, 77, 210 });
            //System.Net.IPAddress Ip = new System.Net.IPAddress(new byte[] { 192, 168, 77, 245 });

            System.Net.IPAddress Ip;
            string LocalIp = MainClass2.Setting.LocalIp;

            Utils.ToLog(String.Format("Запуск слушателя событий адрес {0}, порт {1}", LocalIp, MainClass2.Setting.EventLisenterPort));

            if (LocalIp.ToLower() == "localhost")
            {
                Ip = System.Net.IPAddress.Any;
            }
            else
            {
                try
                {
                    string s = LocalIp.Split("."[0])[0];
                    Ip = new System.Net.IPAddress(new byte[] { byte.Parse(LocalIp.Split("."[0])[0]), byte.Parse(LocalIp.Split("."[0])[1]), byte.Parse(LocalIp.Split("."[0])[2]), byte.Parse(LocalIp.Split("."[0])[3]) });
                }
                catch
                {
                    Ip = System.Net.IPAddress.Any;
                }
            }


            System.Net.Sockets.TcpListener listener = new System.Net.Sockets.TcpListener(Ip, MainClass2.Setting.EventLisenterPort);



            try
            {
                while (true)
                {
                    listener.Start();

                    Console.WriteLine("Start listern on Port{0}", MainClass2.Setting.EventLisenterPort);

                    System.Net.Sockets.TcpClient tcp;
                    tcp = listener.AcceptTcpClient();
                    Utils.ToLog(String.Format("Cлушатель событий запущен"));
                    Console.WriteLine("Client connected.");
                    appTrace.TraceInformation("Client connected.");

                    nEventRecvCount = 0;

                    System.Net.Sockets.NetworkStream ns = tcp.GetStream();

                    int    resSize;
                    string cmd       = "";
                    bool   CloseSock = false;
                    do
                    {
                        byte[] bytes = new byte[tcp.ReceiveBufferSize];
                        resSize = ns.Read(bytes, 0, tcp.ReceiveBufferSize);

                        if (resSize == 0)
                        {
                            Console.WriteLine("Client is disconnected.");
                            appTrace.TraceInformation("Client is disconnected.");
                            CloseSock = true;
                            break;
                        }

                        long s = 0;
                        long e = 0;
                        for (e = 0; e < resSize; e++)
                        {
                            if (bytes[e] == 0)
                            {
                                cmd += System.Text.Encoding.ASCII.GetString(bytes, (int)s, (int)(e - s));
                                Console.WriteLine("cmd : " + cmd);
                                // Here the event handling should be located.>>>>>>>>>>>>>>>
                                CheckRecvComand(cmd, nEventRecvCount);

                                // if one packet includes several events, then the next event
                                // also should be handled.
                                s = e + 1;

                                cmd = "";
                            }
                            else if (e == resSize - 1)
                            {
                                // If one event is separated to several packets, then they
                                // should be merged to one string.
                                cmd += System.Text.Encoding.ASCII.GetString(bytes, (int)s, (int)(e - s + 1));
                            }
                        }
                    } while (true);

                    if (CloseSock == false)
                    {
                        tcp.Close();
                        Console.WriteLine("Disconnected");
                        appTrace.TraceEvent(System.Diagnostics.TraceEventType.Error, 0, "Disconnected");
                        continue;
                    }

                    listener.Stop();
                    Console.WriteLine("Listener is closed.");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message.ToString());
                appTrace.TraceEvent(System.Diagnostics.TraceEventType.Critical, 0, ex.Message.ToString());
            }
        }