Ejemplo n.º 1
0
        public async Task ReadFromServer(DataReader reader, DataWriter writer)
        {
            // set the DataReader to only wait for available data
            reader.InputStreamOptions = InputStreamOptions.Partial;
            try
            {
                await reader.LoadAsync(socketReceiveBufferSize);

                while (reader.UnconsumedBufferLength > 0)
                {
                    bool breakLoop = false;
                    byte readChar;
                    do
                    {
                        if (reader.UnconsumedBufferLength > 0)
                        {
                            readChar = reader.ReadByte();
                        }
                        else
                        {
                            breakLoop = true;
                            break;
                        }
                    } while (!dataStreamLineReader.Add(readChar));

                    if (breakLoop)
                    {
                        return;
                    }

                    // Read next line from data stream.
                    var line = dataStreamLineReader.SafeFlushLine();

                    if (line == null)
                    {
                        break;
                    }
                    if (line.Length == 0)
                    {
                        continue;
                    }

                    await parent.RecieveLine(line);
                }
            }
            catch (Exception e)
            {
                parent.AddError("Error with connection: " + e.Message);
                parent.AddError(e.StackTrace);
                parent.ReadOrWriteFailed = true;
                parent.IsConnected       = false;

                if (Server != null)
                {
                    Disconnect(attemptReconnect: Config.GetBoolean(Config.AutoReconnect));
                }

                return;
            }
        }
Ejemplo n.º 2
0
        public async Task ReadFromServer(DataReader reader, DataWriter writer)
        {
            // set the DataReader to only wait for available data
            reader.InputStreamOptions = InputStreamOptions.Partial;
            if (!IsAuthed)
            {
                AttemptAuth();
            }
            else
            {
                try
                {
                    await reader.LoadAsync(socketReceiveBufferSize);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                    Debug.WriteLine(ex.StackTrace);
                    if (!Transferred)
                    {
                        Disconnect();
                    }
                    return;
                }

                while (reader.UnconsumedBufferLength > 0)
                {
                    bool breakLoop = false;
                    byte readChar;
                    do
                    {
                        if (reader.UnconsumedBufferLength > 0)
                        {
                            readChar = reader.ReadByte();
                        }
                        else
                        {
                            breakLoop = true;
                            break;
                        }
                    } while (!dataStreamLineReader.Add(readChar));

                    if (breakLoop)
                    {
                        return;
                    }

                    // Read next line from data stream.
                    var line = dataStreamLineReader.SafeFlushLine();
                    if (line == null)
                    {
                        break;
                    }
                    if (line.Length == 0)
                    {
                        continue;
                    }
                    if (line.StartsWith("ERROR"))
                    {
                        Disconnect();
                        return;
                    }

                    if (line.StartsWith("PING"))
                    {
                        await WriteLine(writer, line.Replace("PING", "PONG"));

                        return;
                    }

                    await HandleLine(line);
                }
            }
        }