Esempio n. 1
0
        private void bw_DoWork(object sender, DoWorkEventArgs e)
        {
            SimpleServerListenerTCP listener = (SimpleServerListenerTCP)((object[])e.Argument)[0];
            Thread handlerThread             = (Thread)((object[])e.Argument)[1];

            int row = 0;

            while (handlerThread.IsAlive)
            {
                if (!IsConnected(listener.socketEntity.socket)) // the hearbeat
                {
                    lock (entityList)
                    {
                        entityList.Remove(listener.socketEntity);
                        handlerThread.Abort();
                        listener.onDisconnected();
                    }

                    return;
                }

                Thread.Sleep(100);
                Console.WriteLine("child thread is running.." + row++);
            }
        }
Esempio n. 2
0
        public void handlerThread(SimpleServerListenerTCP listener)
        {
            Thread        thisThread    = Thread.CurrentThread;
            NetworkStream networkStream = new NetworkStream(listener.socketEntity.socket);
            int           thisRead      = 0;



            listener.onConnected();

            // BackgroundWorker bw = new BackgroundWorker();
            // bw.DoWork += bw_DoWork;
            // object[] param = new object[]{listener, thisThread};
            // bw.RunWorkerAsync(param);


            Thread heartbeatThread = new Thread(() =>
            { // start new thread to listen for heatbeat
                while (true)
                {
                    if (!IsConnected(listener.socketEntity.socket)) // the hearbeat
                    {
                        lock (entityList)
                        {
                            listener.onDisconnected();
                            entityList.Remove(listener.socketEntity);
                            thisThread.Abort();
                        }

                        return;
                    }

                    Thread.Sleep(100);
                }
            });

            heartbeatThread.IsBackground = true;
            heartbeatThread.Start();



            bool foundProtocol = false;


            while (true) //listener for receiving data
            {
                int    blockSize = 1024;
                Byte[] dataByte  = new Byte[blockSize];


                try
                {
                    thisRead = networkStream.Read(dataByte, 0, blockSize);

                    if (thisRead > 0)
                    {
                        if (!foundProtocol)
                        {
                            for (int dataBytePos = 0; dataBytePos < dataByte.Length; dataBytePos++)
                            {
                                byte[] bufferCursor = new byte[Protocol.Length];
                                int    pos          = 0;

                                for (int cursor = dataBytePos; (cursor < Protocol.Length + dataBytePos) && (cursor + Protocol.Length - 1 < dataByte.Length); cursor++)
                                {
                                    bufferCursor[pos++] = dataByte[cursor];
                                }

                                if (bufferCursor.SequenceEqual(Protocol.MsgHeader))
                                {
                                    Console.WriteLine("Found MessageHeader");
                                }

                                else if (bufferCursor.SequenceEqual(Protocol.FileHeader))
                                {
                                    Console.WriteLine("Found FileHeader");
                                }
                            }
                        }

                        else
                        {
                        }
                    }
                }

                catch (Exception ex)
                {
                    Console.WriteLine(ex.InnerException);

                    //lock (entityList)
                    //{

                    //    //listener.onDisconnected();
                    //    //entityList.Remove(listener.socketEntity);
                    //    thisThread.Abort();

                    //}

                    return;
                }
            }
        }