Пример #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++);
            }
        }
Пример #2
0
        public void Start <T>() where T : new()
        {
            Thread thdListener = new Thread(() => { //thread 1 only for accepting socket
                this.tcpListener.Start();

                while (true)
                {
                    Socket clientSocket = tcpListener.AcceptSocket();

                    if (clientSocket.Connected)
                    {
                        SocketEntity socketEntity = null;

                        lock (entityList)
                        {
                            socketEntity = new SocketEntity()
                            {
                                ID = Guid.NewGuid().ToString("N"), socket = clientSocket
                            };

                            //listener.setEntity(socketEntity);
                            entityList.Add(socketEntity);
                            //adding the object to the list

                            onClientConnected(socketEntity);
                        }
                        // ParameterizedThreadStart a = new ParameterizedThreadStart(handlerThread(Socket socket, SimpleServerListenerTCP listener));
                        object listenerObj = (object)new T();
                        SimpleServerListenerTCP listener = (SimpleServerListenerTCP)listenerObj;
                        listener.setEntity(socketEntity);


                        Thread handlerTh = new Thread(() => handlerThread(listener)); //each socket will have their own handler thread
                        handlerTh.Name   = socketEntity.ID;
                        handlerTh.Start();
                    }
                }
            });

            thdListener.Start();
        }
Пример #3
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;
                }
            }
        }