Created
March 8, 2015 14:00
-
-
Save ThuCommix/159c74eadf486553014a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment