Advertisement
Guest User

Untitled

a guest
Mar 30th, 2015
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. static ConcurrentDictionary<string, JoystickProcess> clientToDevice;
  2. private Thread listenThread;
  3.  
  4. public MainForm()
  5. {
  6. InitializeComponent();
  7.  
  8. Network network = new Network(this);
  9. clientToDevice = new ConcurrentDictionary<string, JoystickProcess>();
  10.  
  11. listenThread = new Thread(network.receive);
  12.  
  13. // Start threads in background
  14. listenThread.IsBackground = true;
  15. listenThread.Start();
  16. }
  17.  
  18. public JoystickProcess getClient(string client)
  19. {
  20. if (clientList.InvokeRequired)
  21. {
  22. // Causes System.ObjectDisposedException
  23. return (JoystickProcess) Invoke(new Func<JoystickProcess>(() => getClient(client)));
  24. }
  25. else
  26. {
  27. JoystickProcess process;
  28. if (clientToDevice.TryGetValue(client, out process))
  29. return process;
  30. return null;
  31. }
  32. }
  33.  
  34. public void receive()
  35. {
  36. int port = 1608;
  37. UdpClient client = new UdpClient(port);
  38. IPEndPoint end = new IPEndPoint(IPAddress.Any, port);
  39.  
  40. String addr;
  41. byte[] recv;
  42. JoystickProcess process;
  43.  
  44. while (true)
  45. {
  46. // Wait for data to be received
  47. recv = client.Receive(ref end);
  48.  
  49. addr = end.Address.ToString();
  50.  
  51. if (ipList.Contains(addr))
  52. {
  53. // Causes System.ObjectDisposedException
  54. process = main.getClient(addr);
  55.  
  56. if (process != null && process.getDevice() != -1)
  57. {
  58. Task.Run(() => process.updateDevice(recv));
  59. }
  60. }
  61. else
  62. {
  63. ipList.Add(addr);
  64. main.addClient(addr);
  65. }
  66. }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement