private void HandleMessage( IMessageHandler cmd, AsyncStreamReader reader, Logger log ) { reader.BeginReadLine(log, (result) => { var ctx = reader.EndReadLine(result); if (ctx == null) { Console.WriteLine("Null String"); reader.Close(); Program.ShowInfo(Store.Instance); return; } if (ctx == string.Empty) { Console.WriteLine("Empty String"); reader.Close(); Task.Factory.StartNew(() => ProcessConnection(reader.BaseStream, log)); return; } Task.Factory.StartNew(() => HandlePayload(cmd, reader, log, null)); }, reader); }
private void HandlePayload( IMessageHandler cmd, AsyncStreamReader reader, Logger log, StringBuilder buffer ) { if ( buffer == null ) { buffer = new StringBuilder(); } reader.BeginReadLine(log, (result) => { var payload = reader.EndReadLine(result); if( string.IsNullOrEmpty(payload)) { if ( buffer.Length != 0 ) { // TODO : Commands that dont require lines read, for example LIST_FILES and LIST_LOCATION, it wont run Task.Factory.StartNew(() => { StreamWriter writer = new StreamWriter(reader.BaseStream); foreach (string response in buffer.ToString().Split('\n').Select(line => cmd.ProcessCommand(line, log)).Where(response => !string.IsNullOrEmpty(response))) { writer.WriteLine(response); } writer.Close(); }); } reader.Close(); Task.Factory.StartNew( () => ProcessConnection( reader.BaseStream, log ) ); return; } buffer.Append(buffer); Task.Factory.StartNew(() => HandlePayload(cmd, reader, log, buffer)); }, reader); }