You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

PipeServer.cs 1.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using System;
  2. using System.IO;
  3. using System.IO.Pipes;
  4. using System.Net;
  5. using System.Text;
  6. namespace Shadowsocks.Controller
  7. {
  8. class RequestAddUrlEventArgs : EventArgs
  9. {
  10. public readonly string Url;
  11. public RequestAddUrlEventArgs(string url)
  12. {
  13. this.Url = url;
  14. }
  15. }
  16. internal class PipeServer
  17. {
  18. public event EventHandler<RequestAddUrlEventArgs> AddUrlRequested;
  19. public async void Run(string path)
  20. {
  21. byte[] buf = new byte[4096];
  22. while (true)
  23. {
  24. using (NamedPipeServerStream stream = new NamedPipeServerStream(path))
  25. {
  26. stream.WaitForConnection();
  27. await stream.ReadAsync(buf, 0, 4);
  28. int opcode = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(buf, 0));
  29. if (opcode == 1)
  30. {
  31. await stream.ReadAsync(buf, 0, 4);
  32. int strlen = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(buf, 0));
  33. await stream.ReadAsync(buf, 0, strlen);
  34. string url = Encoding.UTF8.GetString(buf, 0, strlen);
  35. AddUrlRequested?.Invoke(this, new RequestAddUrlEventArgs(url));
  36. }
  37. stream.Close();
  38. }
  39. }
  40. }
  41. }
  42. }