Esempio n. 1
0
        /// <summary>
        /// Initializes the message queues, read and write thread
        /// </summary>
        public IrcConnection()
        {
#if LOG4NET
            Logger.Init();
            Logger.Main.Debug("IrcConnection created");
#endif
            _SendBuffer[Priority.High]        = Queue.Synchronized(new Queue());
            _SendBuffer[Priority.AboveMedium] = Queue.Synchronized(new Queue());
            _SendBuffer[Priority.Medium]      = Queue.Synchronized(new Queue());
            _SendBuffer[Priority.BelowMedium] = Queue.Synchronized(new Queue());
            _SendBuffer[Priority.Low]         = Queue.Synchronized(new Queue());

            // setup own callbacks
            OnReadLine        += new ReadLineEventHandler(_SimpleParser);
            OnConnectionError += new EventHandler(_OnConnectionError);

            _ReadThread       = new ReadThread(this);
            _WriteThread      = new WriteThread(this);
            _IdleWorkerThread = new IdleWorkerThread(this);

            Assembly     assm      = Assembly.GetAssembly(this.GetType());
            AssemblyName assm_name = assm.GetName(false);

            AssemblyProductAttribute pr = (AssemblyProductAttribute)assm.GetCustomAttributes(typeof(AssemblyProductAttribute), false)[0];

            _VersionNumber = assm_name.Version.ToString();
            _VersionString = pr.Product + " " + _VersionNumber;
        }
Esempio n. 2
0
 protected void KillWriteThread()
 {
     if (writeThread != null)
     {
         writeThread.StopThread();
         writeThread = null;
     }
 }
Esempio n. 3
0
 protected void KillWriteThread()
 {
     if (writeThread != null)
     {
         writeThread.StopThread();
         Log.Info("UsbSerialDevice", "Write Thread stopped.");
         writeThread = null;
     }
 }
Esempio n. 4
0
        // Starts this peer's associated threads
        internal void Start()
        {
            // Begin reading
            Read();

            // Begin writing
            WriteThread.Start();

            // Invoke peer connected callback
            Server.OnPeerConnected?.Invoke(this, EventArgs.Empty);
        }
Esempio n. 5
0
        /// <summary>
        /// Starts this database
        /// </summary>
        public void Start()
        {
            // Create sqlite connection
            Connection = new SqliteConnection($"Data Source={DatabaseFile}");

            // Open sqlite connection
            Connection.Open();

            // Begin the write thread
            WriteThread.Start();
        }
Esempio n. 6
0
 protected void RestartWriteThread()
 {
     if (writeThread == null)
     {
         writeThread = new WriteThread(this);
         writeThread.Start();
         while (!writeThread.IsAlive)
         {
         }                                // Busy waiting
     }
 }
Esempio n. 7
0
 protected void RestartWriteThread()
 {
     if (writeThread == null)
     {
         writeThread = new WriteThread(this);
         writeThread.Start();
         while (!writeThread.IsAlive)
         {
         }                                // Busy waiting
         Log.Info("UsbSerialDevice", "Write Thread started.");
     }
 }
Esempio n. 8
0
        public static void Main(string[] args)
        {
            var writeThread  = new WriteThread <SampleData>(WriteBlockingCollection, "Output.txt"); //Create a object for write thread
            var writeThread1 = new Thread(writeThread.WriteData);                                   //Create write thread

            writeThread1.Start();                                                                   //start the write thread

            var readThread  = new ReadThread <SampleData>(ReadBlockingCollection, "Output.txt");    //Create a object for write thread
            var readThread1 = new Thread(readThread.ReadItem);                                      //Create read thread

            readThread1.Start();                                                                    //start read thread

            var random       = new Random(50000);
            int count        = 0;
            int totalObjects = 50000;
            var writeTask    = Task.Run(() =>
            {
                //create some random objects and add them to write queue
                while (count < totalObjects)
                {
                    WriteBlockingCollection.Add(new SampleData(random.Next(), $"Random Data {random.Next()}",
                                                               DateTime.Now.ToString(CultureInfo.InvariantCulture)));
                    count++;
                }
                //End adding new items to queue thus mentioning end of write operation
                WriteBlockingCollection.CompleteAdding();
                while (WriteBlockingCollection.Count > 0)
                {
                    Thread.Sleep(100);
                    //Just to make sure all are read
                }
                //Set this so that read thread can stop further reading from file
                Running = false;
            });
            var readTask = Task.Run(() =>
            {
                var readCount = 0;
                foreach (var item in ReadBlockingCollection.GetConsumingEnumerable())
                {
                    Console.WriteLine(item.ToString());
                    readCount++;
                }
                //Just verify total count should be
                Console.WriteLine($"Read Count From Read Thread :  {readCount}");
                Debug.Assert(totalObjects == count, "Either File is appended with new values or something wrong, delete Output.txt and recreate it");
            });

            Task.WaitAll(writeTask, readTask);
            writeThread1.Join();
            readThread1.Join();
            Console.WriteLine("Reading and writing from file Completed!. Press any key to continue");
            Console.ReadLine();
        }
Esempio n. 9
0
        /// <summary>
        /// Stops this database
        /// </summary>
        public void Stop()
        {
            // Signal a stop event
            StopEvent.Set();
            WriteThread.Join();

            // Dispose of sqlite connection
            if (Connection.State == ConnectionState.Open)
            {
                Connection.Close();
            }
            Connection.Dispose();
        }
Esempio n. 10
0
        /// <summary>
        /// Starts this instance.
        /// </summary>
        public void Start()
        {
            if (ReadThread != null)
            {
                ReadThread.Start();
            }

            if (WriteThread != null)
            {
                WriteThread.Start();
            }

            Running = true;
        }
Esempio n. 11
0
        /**
         * Clear socket and threads
         */
        private void Clear()
        {
            if (ReadThread.IsAlive)
            {
                ReadThread.Abort();
            }

            if (WriteThread.IsAlive)
            {
                WriteThread.Abort();
            }

            Socket.Close();
        }
Esempio n. 12
0
        /// <summary>
        /// 备份文件线程
        /// </summary>
        void BackUpRun()
        {
            int count = 0;

            while (true)
            {
                GetBackUpItems(out count);
                if (count < 0)
                {
                    WriteState("连接失败!");
                    Thread.Sleep(1000);
                }
                else
                {
                    break;
                }
            }
            if (count > MemchcheCount)
            {
                WriteState("Memcache 已经有数据" + count + "条,大于设置恢复的条数" + MemchcheCount + "条,当前不需要更新!");
            }
            else
            {
                if (System.IO.Directory.Exists(recoverPath))
                {
                    if (System.IO.Directory.Exists(recoverPath))
                    {
                        string[] dirs    = Directory.GetDirectories(recoverPath);
                        string[] servers = (string[])serverList.ToArray(typeof(string));
                        //遍历本地文件,写入memcache
                        WriteThread wt = null;
                        foreach (string path in dirs)
                        {
                            wt            = new WriteThread(path, new Help(0, 1, 0, true));
                            wt.frm        = this;
                            wt.serverList = servers;
                            dicWirte.Add(path, wt);
                            wt.Start();
                        }
                    }
                }
                else
                {
                    return;
                }
            }
        }
Esempio n. 13
0
        // Stops this peer's associated threads
        internal void Stop()
        {
            // Invoke peer disconnected callback
            Server.OnPeerDisconnected?.Invoke(this, EventArgs.Empty);

            // Set stop event
            StopEvent.Set();

            // Stop write thread
            if (WriteThread.IsAlive)
            {
                WriteThread.Join();
            }

            // Stop client
            Client.Close();
        }
Esempio n. 14
0
        private void FinishConnect(IAsyncResult result)
        {
            try
            {
                var beginClient = (TcpClient)result.AsyncState;
                if (beginClient == null)
                {
                    Close();
                    return;
                }
                beginClient.EndConnect(result);
                if (beginClient.Connected)
                {
                    // 启动读取和写入线程。
                    _readThread = new ReadThread(this, beginClient, _msgQueue);
                    _readThread.Start();

                    _writeThread = new WriteThread(this, beginClient, _msgQueue);
                    _writeThread.Start();

                    // 已经连接上。
                    Status  = SocketStatus.Connected;
                    _client = beginClient;
                }
                else
                {
                    // 未连接成功,关闭当前连接。
                    Close();
                }
            }
            catch (Exception e)
            {
                MyLog.ErrorAsync(Tag, string.Format("end connect error: {0}\n{1}", e.Message, e.StackTrace));
                Close();
            }
        }
Esempio n. 15
0
        /// <summary>
        /// Initializes the message queues, read and write thread
        /// </summary>
        public IrcConnection()
        {
            #if LOG4NET
            Logger.Init();
            Logger.Main.Debug("IrcConnection created");
            #endif
            _SendBuffer[Priority.High]        = Queue.Synchronized(new Queue());
            _SendBuffer[Priority.AboveMedium] = Queue.Synchronized(new Queue());
            _SendBuffer[Priority.Medium]      = Queue.Synchronized(new Queue());
            _SendBuffer[Priority.BelowMedium] = Queue.Synchronized(new Queue());
            _SendBuffer[Priority.Low]         = Queue.Synchronized(new Queue());

            // setup own callbacks
            OnReadLine        += new ReadLineEventHandler(_SimpleParser);
            OnConnectionError += new EventHandler(_OnConnectionError);

            _ReadThread  = new ReadThread(this);
            _WriteThread = new WriteThread(this);
            _IdleWorkerThread = new IdleWorkerThread(this);

            Assembly assm = Assembly.GetAssembly(this.GetType());
            AssemblyName assm_name = assm.GetName(false);

            AssemblyProductAttribute pr = (AssemblyProductAttribute)assm.GetCustomAttributes(typeof(AssemblyProductAttribute), false)[0];

            _VersionNumber = assm_name.Version.ToString();
            _VersionString = pr.Product+" "+_VersionNumber;
        }
Esempio n. 16
0
        /// <summary>
        /// Initializes the message queues, read and write thread
        /// </summary>
        public IrcConnection()
        {
            ProxyType = ProxyType.None;
            PingTimeout = 300;
            PingInterval = 60;
            IdleWorkerInterval = 60;
            SocketSendTimeout = 600;
            SocketReceiveTimeout = 600;
            Encoding = Encoding.Default;
            AutoRetryLimit = 3;
            AutoRetryDelay = 30;
            SendDelay = 200;
            sendBuffer[Priority.High] = Queue.Synchronized(new Queue());
            sendBuffer[Priority.AboveMedium] = Queue.Synchronized(new Queue());
            sendBuffer[Priority.Medium] = Queue.Synchronized(new Queue());
            sendBuffer[Priority.BelowMedium] = Queue.Synchronized(new Queue());
            sendBuffer[Priority.Low] = Queue.Synchronized(new Queue());

            // setup own callbacks
            OnReadLine += SimpleParser;
            OnConnectionError += _OnConnectionError;

            readThread = new ReadThread(this);
            writeThread = new WriteThread(this);
            idleWorkerThread = new IdleWorkerThread(this);

            Assembly assembly = Assembly.GetAssembly(GetType());
            AssemblyName assemblyName = assembly.GetName(false);

            var pr = (AssemblyProductAttribute)assembly.GetCustomAttributes(typeof(AssemblyProductAttribute), false)[0];

            VersionNumber = assemblyName.Version.ToString();
            VersionString = pr.Product + " " + VersionNumber;
        }
Esempio n. 17
0
        /// <summary>
        /// Initializes the message queues, read and write thread
        /// </summary>
        public IrcConnection()
        {
            #if LOG4NET
            Logger.Main.Debug("IrcConnection created");
            #endif

            // setup own callbacks
            OnReadLine        += new ReadLineEventHandler(_SimpleParser);
            OnConnectionError += new EventHandler(_OnConnectionError);

            _ReadThread  = new ReadThread(this);
            _WriteThread = new WriteThread(this);
            _IdleWorkerThread = new IdleWorkerThread(this);

            Assembly assm = Assembly.GetAssembly(this.GetType());
            AssemblyName assm_name = assm.GetName(false);

            AssemblyProductAttribute pr = (AssemblyProductAttribute)assm.GetCustomAttributes(typeof(AssemblyProductAttribute), false)[0];

            _VersionNumber = assm_name.Version.ToString();
            _VersionString = pr.Product+" "+_VersionNumber;
        }