Advertisement
Guest User

Untitled

a guest
Apr 25th, 2015
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.57 KB | None | 0 0
  1. //send join request to server and wait for response.
  2. //the initial join should be blocking since if we fail to connect we should not listing for packets
  3.  
  4. var joinRequest = new byte[2];
  5. joinRequest = BitConverter.GetBytes((short) SystemMessage.Join);
  6. _udpService.Send(joinRequest, joinRequest.Length, _joinedSessionEndPoint);
  7.  
  8. int timeOut = 1000;
  9.  
  10. //we should give the server some time to respond.
  11. //Lets say a max length of 1000ms before dropping the connect
  12.  
  13. var asyncResult = _udpService.BeginReceive(null, null);
  14. asyncResult.AsyncWaitHandle.WaitOne(timeOut);
  15. if (asyncResult.IsCompleted)
  16. {
  17. IPEndPoint host = null;
  18. var hostResponse = _udpService.EndReceive(asyncResult, ref host);
  19. if (hostResponse.Length == 2)
  20. {
  21. var message = (SystemMessage) BitConverter.ToInt16(hostResponse, 0);
  22. if (message == SystemMessage.JoinAccepted)
  23. {
  24. // Now we should retrieve the session member list.
  25. _udpService.Send(BitConverter.GetBytes((short) SystemMessage.RequestMemberList), 2,
  26. _joinedSessionEndPoint);
  27. asyncResult = _udpService.BeginReceive(null, null);
  28. asyncResult.AsyncWaitHandle.WaitOne(timeOut);
  29. if (asyncResult.IsCompleted)
  30. {
  31. hostResponse = _udpService.EndReceive(asyncResult, ref host);
  32. if (hostResponse.Length >= 6)
  33. {
  34. message = (SystemMessage) BitConverter.ToInt16(hostResponse, 0);
  35. if (message == SystemMessage.MemberList)
  36. {
  37. int length = BitConverter.ToInt32(hostResponse, 2);
  38. var memList = new List<SessionMember>();
  39. byte[] ipBytes = new byte[4];
  40. for (int i = 0; i < length; i++)
  41. {
  42. int offsetIp = 8 * i + 6;
  43.  
  44. Array.Copy(hostResponse, offsetIp, ipBytes, 0, 4);
  45. var ip = new IPAddress(ipBytes);
  46. int port = BitConverter.ToInt32(hostResponse, offsetIp + 4);
  47.  
  48. memList.Add(new SessionMember(new IPEndPoint(ip, port)));
  49. }
  50. _members.Clear();
  51. _members.AddRange(memList.ToArray());
  52. _sessionActive = true;
  53. }
  54. else
  55. {
  56. throw new NetworkSessionException(
  57. "The remote host responded with an unexpected packet.");
  58. }
  59. }
  60. else
  61. {
  62. throw new NetworkSessionException(
  63. "The remote host responded with an invalid packet size.");
  64. }
  65. }
  66. else
  67. {
  68. throw new NetworkSessionException("The remote host took to long to respond.");
  69. }
  70. }
  71. else if(message == SystemMessage.JoinRefused)
  72. {
  73. throw new NetworkSessionException("Connection refused by remote host.");
  74. }
  75. }
  76. else
  77. {
  78. throw new NetworkSessionException("The remote host responded with an invalid packet size.");
  79. }
  80. }
  81. else
  82. {
  83. //the server took to long to respond.
  84. throw new NetworkSessionException("The remote host took to long to respond.");
  85. }
  86.  
  87. //connection is confirmed by host, we can now listing for packets.
  88. _checkActivityTask.Start();
  89. var remoteEndPoint = new IPEndPoint(IPAddress.Any, 0);
  90. _udpService.BeginReceive(AsynClientReceiveCallback, new AsyncReceiveState(_udpService, remoteEndPoint));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement