Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //send join request to server and wait for response.
- //the initial join should be blocking since if we fail to connect we should not listing for packets
- var joinRequest = new byte[2];
- joinRequest = BitConverter.GetBytes((short) SystemMessage.Join);
- _udpService.Send(joinRequest, joinRequest.Length, _joinedSessionEndPoint);
- int timeOut = 1000;
- //we should give the server some time to respond.
- //Lets say a max length of 1000ms before dropping the connect
- var asyncResult = _udpService.BeginReceive(null, null);
- asyncResult.AsyncWaitHandle.WaitOne(timeOut);
- if (asyncResult.IsCompleted)
- {
- IPEndPoint host = null;
- var hostResponse = _udpService.EndReceive(asyncResult, ref host);
- if (hostResponse.Length == 2)
- {
- var message = (SystemMessage) BitConverter.ToInt16(hostResponse, 0);
- if (message == SystemMessage.JoinAccepted)
- {
- // Now we should retrieve the session member list.
- _udpService.Send(BitConverter.GetBytes((short) SystemMessage.RequestMemberList), 2,
- _joinedSessionEndPoint);
- asyncResult = _udpService.BeginReceive(null, null);
- asyncResult.AsyncWaitHandle.WaitOne(timeOut);
- if (asyncResult.IsCompleted)
- {
- hostResponse = _udpService.EndReceive(asyncResult, ref host);
- if (hostResponse.Length >= 6)
- {
- message = (SystemMessage) BitConverter.ToInt16(hostResponse, 0);
- if (message == SystemMessage.MemberList)
- {
- int length = BitConverter.ToInt32(hostResponse, 2);
- var memList = new List<SessionMember>();
- byte[] ipBytes = new byte[4];
- for (int i = 0; i < length; i++)
- {
- int offsetIp = 8 * i + 6;
- Array.Copy(hostResponse, offsetIp, ipBytes, 0, 4);
- var ip = new IPAddress(ipBytes);
- int port = BitConverter.ToInt32(hostResponse, offsetIp + 4);
- memList.Add(new SessionMember(new IPEndPoint(ip, port)));
- }
- _members.Clear();
- _members.AddRange(memList.ToArray());
- _sessionActive = true;
- }
- else
- {
- throw new NetworkSessionException(
- "The remote host responded with an unexpected packet.");
- }
- }
- else
- {
- throw new NetworkSessionException(
- "The remote host responded with an invalid packet size.");
- }
- }
- else
- {
- throw new NetworkSessionException("The remote host took to long to respond.");
- }
- }
- else if(message == SystemMessage.JoinRefused)
- {
- throw new NetworkSessionException("Connection refused by remote host.");
- }
- }
- else
- {
- throw new NetworkSessionException("The remote host responded with an invalid packet size.");
- }
- }
- else
- {
- //the server took to long to respond.
- throw new NetworkSessionException("The remote host took to long to respond.");
- }
- //connection is confirmed by host, we can now listing for packets.
- _checkActivityTask.Start();
- var remoteEndPoint = new IPEndPoint(IPAddress.Any, 0);
- _udpService.BeginReceive(AsynClientReceiveCallback, new AsyncReceiveState(_udpService, remoteEndPoint));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement