예제 #1
0
        /// <summary>
        ///    Listens for data from server. The Main processing thread.
        /// </summary>
        private void ListenForData()
        {
            while (IsConnected)
            {
                var readingData = new byte[0x1000];
                ReadData = new BufferChunk(readingData);
                var numberOfBytesRead = 0;
                fNetworkStream = Sender.GetStream();

                try {
                    while (fNetworkStream.DataAvailable)
                    {
                        var len = fNetworkStream.Read(readingData, 0, readingData.Length);
                        numberOfBytesRead += len;
                        ReadData          += readingData;
                    }
                } catch {
                    //ignore errors
                }

                if (numberOfBytesRead < PacketHeader.Length)
                {
                    Thread.Sleep(200);
                    continue;
                }

                Header = ReadData.NextPacketHeader();

                switch ((EPacketType)Header.type)
                {
                case EPacketType.Info:
                    if (InfoReceived != null)
                    {
                        InfoReceived.Invoke();
                    }

                    break;

                case EPacketType.Error:
                    if (ErrorReceived != null)
                    {
                        ErrorReceived.Invoke();
                    }

                    break;

                case EPacketType.Data:
                {
                    var cmd = (EDataCommand)Header.value;

                    if (cmd == EDataCommand.STATE)
                    {
                        StateData = ReadData.StateData();
                        ProcessState(StateData);
                    }

                    ProcessData(ReadData, numberOfBytesRead);
                    break;
                }
                }

                Thread.Sleep(200);
            }

            Disconnect();
        }