Ejemplo n.º 1
0
 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);
 }