Forums - C# Corner

Forum guidelines
Ivan Climov

Ivan Climov

  • 1.7k
  • 138
  • 1.7k

How to transfer data from the third-level class ?

Jul 9 2019 3:44 AM
I need to transfer the data from HTTPServer toWinForm.
I use HTTPServer (https://github.com/jeske/SimpleHttpServer)
 
I transferred the project SimpleHttpServer (https://github.com/jeske/SimpleHttpServer/tree/master/SimpleHttpServer) in your project WinForm.
I run HTTPServer with the following code.
Code.
  1. private void Form1_Load(object sender, EventArgs e)  
  2. {  
  3.     StartHttpServer();  
  4. }  
  5.   
  6. static void StartHttpServer()  
  7. {  
  8.     try  
  9.     {  
  10.         var route_config = new List();  
  11.         HttpServer httpServer = new HttpServer(8080, route_config);  
  12.   
  13.         Thread thread = new Thread(new ThreadStart(httpServer.Listen));  
  14.         thread.Start();  
  15.     }  
  16.     catch (Exception ex)  
  17.     {  
  18.         string s = ex.Message;  
  19.         string t = ex.StackTrace;  
  20.         // throw;  
  21.         MessageBox.Show(s + " \r\n " +  
  22.             t);  
  23.     }  
  24. }  
If the server receives a POST request with JSON, then the request comes in the class HttpProcessor.cs method HandleClient.
 
Code
  1. public void HandleClient(TcpClient tcpClient)  
  2. {  
  3.     Stream inputStream = GetInputStream(tcpClient);  
  4.     Stream outputStream = GetOutputStream(tcpClient);  
  5.     HttpRequest request = GetRequest(inputStream, outputStream);  
  6.  
  7.     #region ??? ?????????.  
  8.     // string s = request.  
  9.     RequestContent?.Invoke(request);  
  10.     #endregion  
  11.   
  12.     // ????????? ? ?????????? ??????... (route and handle the request...)  
  13.     HttpResponse response = RouteRequest(inputStream, outputStream, request);  
  14.   
  15.     // *  
  16.     //    Console.WriteLine("{0} {1}",response.StatusCode,request.Url);  
  17.     // ????????? ????? ?? ????????? ??? ?????? (build a default response for errors)  
  18.     if (response.Content == null)  
  19.     {  
  20.         if (response.StatusCode != "200")  
  21.         {  
  22.             response.ContentAsUTF8 = string.Format("{0} {1} 

     {2}", response.StatusCode, request.Url, response.ReasonPhrase);  

  23.         }  
  24.     }  
  25.   
  26.     WriteResponse(outputStream, response);  
  27.   
  28.     outputStream.Flush();  
  29.     outputStream.Close();  
  30.     outputStream = null;  
  31.   
  32.     inputStream.Close();  
  33.     inputStream = null;  
  34.   
  35. }  
To get JSON from the HttpProcessor.cs class method HandleClient.
I plan to use the public event Action RequestContent;.
 
Code
  1. public event Action RequestContent;  
  2.   
  3. public void HandleClient(TcpClient tcpClient)  
  4. {  
  5.     // ....  
  6.     RequestContent?.Invoke(request);  
  7.     // ....   
  8. }  
Question
1. How to transfer data from the third-level class (the HttpProcessor.cs class) to the first-level class (the WinForm class)?
2. How are there other solutions to replace using the public event Action RequestContent;?