Advertisement
Gronos02

Untitled

Apr 27th, 2021
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.97 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.IO.Pipes;
  5. using System.Threading;
  6.  
  7. namespace ConsoleApplication1
  8. {
  9.     public class PipeServer
  10.     {
  11.         bool running;
  12.         Thread runningThread;
  13.         EventWaitHandle terminateHandle = new EventWaitHandle(false, EventResetMode.AutoReset);
  14.         public string PipeName { get; set; }
  15.  
  16.         void ServerLoop()
  17.         {
  18.             while (running)
  19.             {
  20.                 ProcessNextClient();
  21.             }
  22.  
  23.             terminateHandle.Set();
  24.         }
  25.  
  26.         public void Run()
  27.         {
  28.             running = true;
  29.             runningThread = new Thread(ServerLoop);
  30.             runningThread.Start();
  31.         }
  32.  
  33.         public void Stop()
  34.         {
  35.             running = false;
  36.             terminateHandle.WaitOne();
  37.         }
  38.  
  39.         public virtual string ProcessRequest(string message)
  40.         {
  41.             return "";
  42.         }
  43.  
  44.         public void ProcessClientThread(object o)
  45.         {
  46.             NamedPipeServerStream pipeStream = (NamedPipeServerStream)o;
  47.             //TODO FOR YOU: Write code for handling pipe client here
  48.             StreamReader reader = new StreamReader(pipeStream);
  49.             int number = Process.GetCurrentProcess().Threads.Count;
  50.             Console.WriteLine(number);
  51.             string line = "";
  52.             line = reader.ReadLine();
  53.  
  54.             pipeStream.Close();
  55.             pipeStream.Dispose();
  56.  
  57.         }
  58.  
  59.         public void ProcessNextClient()
  60.         {
  61.             try
  62.             {
  63.                 NamedPipeServerStream pipeStream = new NamedPipeServerStream(PipeName, PipeDirection.InOut, 254);
  64.                 Console.WriteLine(PipeName);
  65.                 pipeStream.WaitForConnection();
  66.  
  67.                 //Spawn a new thread for each request and continue waiting
  68.                 Thread t = new Thread(ProcessClientThread);
  69.                 t.Start(pipeStream);
  70.             }
  71.             catch (Exception e)
  72.             {//If there are no more avail connections (254 is in use already) then just keep looping until one is avail
  73.             }
  74.         }
  75.  
  76.         class Program
  77.         {
  78.             static void Main(string[] args)
  79.             {
  80.                 var pipedServer = new PipeServer();
  81.                 pipedServer.PipeName = "TestPipe";
  82.                 pipedServer.Run();
  83.                 int number = Process.GetCurrentProcess().Threads.Count;
  84.                 Console.WriteLine(number);
  85.             }
  86.         }
  87.  
  88.     }
  89. }
  90.  
  91.  
  92. /*
  93.  
  94. var server = new NamedPipeServerStream("PipesOfPiece");
  95. server.WaitForConnection();
  96.  
  97. StreamReader reader = new StreamReader(server);
  98. StreamWriter writer = new StreamWriter(server);
  99. string line = "";
  100. line = reader.ReadLine();
  101. var client_request = line.Split(".");
  102. if (client_request[0] == @"-w")
  103. {
  104.     try
  105.     {
  106.         string writePath = @"D:\_Useful stuff here\Developing\ForServ\Response.txt";
  107.         using (StreamWriter sw = new StreamWriter(writePath, true, System.Text.Encoding.Default))
  108.         {
  109.             sw.WriteLine(client_request[1]);
  110.         }
  111.         Console.WriteLine("Write complete");
  112.     }
  113.     catch (Exception e)
  114.     {
  115.         Console.WriteLine(e.Message);
  116.     }
  117. }
  118.  
  119. if (client_request[0] == @"-r")
  120. {
  121.     string response_string = "";
  122.  
  123.     try
  124.     {
  125.         string readPath = @"D:\_Useful stuff here\Developing\ForServ\Response.txt";
  126.         using (StreamReader sr = new StreamReader(readPath, System.Text.Encoding.Default))
  127.         {
  128.             string line2;
  129.             while ((line2 = sr.ReadLine()) != null)
  130.             {
  131.                 response_string += line2 + "\n";
  132.             }
  133.         }
  134.     }
  135.     catch (Exception e)
  136.     {
  137.         Console.WriteLine(e.Message);
  138.     }
  139.     Task.Delay(1000).Wait();
  140.     // отправляем сообщение
  141.     writer.WriteLine(response_string);
  142.     writer.Write((char)0);
  143.  
  144.     writer.Flush();
  145.     server.WaitForPipeDrain();
  146.     line = "";
  147.     client_request[0] = null;
  148. }
  149. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement