Example #1
0
        private void ReadQueryLoop()
        {
            string dataBuffer = null;

            while (true)
            {
                string line;
                try { line = tcpReader.ReadLine(); }
                catch (IOException) { line = null; }
                if (line == null)
                {
                    break;
                }
                else if (string.IsNullOrWhiteSpace(line))
                {
                    continue;
                }

                var message = line.Trim();
                if (message.StartsWith("error "))
                {
                    // we (hopefully) only need to lock here for the dequeue
                    lock (lockObj)
                    {
                        if (!(status == QueryClientStatus.Connected || status == QueryClientStatus.Connecting))
                        {
                            break;
                        }

                        var errorStatus = GenerateErrorStatus(message);
                        if (!errorStatus.Ok)
                        {
                            requestQueue.Dequeue().SetAnswer(errorStatus);
                        }
                        else
                        {
                            var response = GenerateResponse(dataBuffer);
                            dataBuffer = null;

                            requestQueue.Dequeue().SetAnswer(errorStatus, response);
                        }
                    }
                }
                else if (message.StartsWith("notify"))
                {
                    var notify = GenerateNotification(message);
                    InvokeEvent(notify);
                }
                else
                {
                    dataBuffer = line;
                }
            }
            status = QueryClientStatus.Disconnected;
        }
Example #2
0
        public void Close()
        {
            lock (lockObj)
            {
                TextMessageReceivedHandler = null;
                ClientEnterViewHandler     = null;
                ClientLeftViewHandler      = null;

                status = QueryClientStatus.Quitting;
                tcpWriter.WriteLine("quit");
                tcpWriter.Flush();
            }
        }
Example #3
0
        public void Connect(string hostname, int port)
        {
            if (string.IsNullOrWhiteSpace(hostname))
            {
                throw new ArgumentNullException(nameof(hostname));
            }
            if (port <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(port));
            }

            if (IsConnected)
            {
                Close();
            }

            status      = QueryClientStatus.Connecting;
            CurrentHost = hostname;
            CurrentPort = port;

            tcpClient.Connect(CurrentHost, CurrentPort);
            if (!tcpClient.Connected)
            {
                throw new InvalidOperationException("Could not connect to the query server.");
            }

            tcpStream = tcpClient.GetStream();
            tcpReader = new StreamReader(tcpStream);
            tcpWriter = new StreamWriter(tcpStream)
            {
                NewLine = "\n"
            };

            for (int i = 0; i < 3; i++)
            {
                tcpReader.ReadLine();
            }

            EventDispatcher.Init(ReadQueryLoop);
            status = QueryClientStatus.Connected;
        }
Example #4
0
        public TS3QueryClient(EventDispatchType dispatcher)
        {
            status    = QueryClientStatus.Disconnected;
            tcpClient = new TcpClient();
            isInQueue = false;

            switch (dispatcher)
            {
            case EventDispatchType.None: EventDispatcher = new NoEventDispatcher(); break;

            case EventDispatchType.CurrentThread: EventDispatcher = new CurrentThreadEventDisptcher(); break;

            case EventDispatchType.DoubleThread: EventDispatcher = new DoubleThreadEventDispatcher(); break;

            case EventDispatchType.AutoThreadPooled: throw new NotSupportedException();          //break;

            case EventDispatchType.NewThreadEach: throw new NotSupportedException();             //break;

            default: throw new NotSupportedException();
            }
        }