private void Work()
        {
            try
            {
                string request = string.Empty;
                writer = new StreamWriter(socket.GetStream(), Encoding.ASCII);

                //while waiting for queue to populate and thread not killed
                while (isRunning)
                {
                    try
                    {
                        request = blockingOutQueue.Dequeue();
                        writer.WriteLine(request);
                        writer.Flush();

                        if (numWriteAttempt > 0)
                        {
                            numWriteAttempt = 0;
                        }
                    }
                    catch (IOException ioe)
                    {
                        //Has writing to socket failed and may server be disconnected?
                        if (numWriteAttempt++ >= NUM_WRITE_ATTEMPTS_BEFORE_FAIL)
                        {
                            //notify connection listener if any
                            if (null != connectionListener)
                            {
                                connectionListener.OnGazeApiConnectionStateChanged(false);
                            }

                            //server must be disconnected, shut down network layer
                            networkLayer.Close();
                        }
                        else
                        {
                            //else retry request asap
                            blockingOutQueue.Enqueue(request);
                        }
                    }
                    catch (Exception e)
                    {
                        Debug.WriteLine("Exception while writing to socket: " + e.Message);
                    }
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine("Exception while establishing outgoing socket connection: " + e.Message);
            }
            finally
            {
                Debug.WriteLine("OutgoingStreamHandler closing down");
            }
        }
        private void Work()
        {
            try
            {
                reader = new StreamReader(socket.GetStream(), Encoding.ASCII);

                while (isRunning)
                {
                    try
                    {
                        while (!reader.EndOfStream)
                        {
                            string response = reader.ReadLine();

                            if (null != responseListener && !string.IsNullOrEmpty(response))
                            {
                                responseListener.OnGazeApiResponse(response);
                            }
                        }
                    }
                    catch (IOException ioe)
                    {
                        //Are we closing down or has reading from socket failed?
                        if (isRunning && !IsSocketConnected())
                        {
                            //notify connection listener if any
                            if (null != connectionListener)
                            {
                                connectionListener.OnGazeApiConnectionStateChanged(false);
                            }

                            //server must be disconnected, shut down network layer
                            networkLayer.Close();
                        }
                    }
                    catch (Exception e)
                    {
                        Debug.WriteLine("Exception while reading from socket: " + e.Message);
                    }
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine("Exception while establishing incoming socket connection: " + e.Message);
            }
            finally
            {
                Debug.WriteLine("IncommingStreamHandler closing down");
            }
        }
Exemple #3
0
        /// <summary>
        /// Deactivates TET C# Client and all under lying routines. Should be called when
        /// a application closes down.
        /// </summary>
        public void Deactivate()
        {
            if (null != heartbeatHandler)
            {
                heartbeatHandler.Stop();
                heartbeatHandler = null;
            }

            if (null != apiManager)
            {
                apiManager.Close();
                apiManager = null;
            }

            //clearing listeners will stop heartbeat and broadcasting threads
            ClearListeners();

            isActive = false;
        }