private void OnReceive(IAsyncResult ar)
{
try
{
IPEndPoint ipeSender = new IPEndPoint(IPAddress.Any, 0);
EndPoint epSender = (EndPoint)ipeSender;
serverSocket.EndReceiveFrom(ar, ref epSender);
//Transform the array of bytes received from the user into an
//intelligent form of object Data
Data msgReceived = new Data(byteData);
//We will send this object in response the users request
Data msgToSend = new Data();
byte[] message;
//If the message is to login, logout, or simple text message
//then when send to others the type of the message remains the same
msgToSend.cmdCommand = msgReceived.cmdCommand;
msgToSend.strName = msgReceived.strName;
switch (msgReceived.cmdCommand)
{
case Command.Login:
//When a user logs in to the server then we add her to our
//list of clients
if (playercount == 0)
{
player player1 = new player();
ClientInfo clientInfo = new ClientInfo();
//player player1 = new player();
clientInfo.endpoint = epSender;
clientInfo.strName = msgReceived.strName;
msgToSend.cmdCommand = Command.Login;
msgToSend.playerID = playercount;
clientList.Add(clientInfo);
player1.playerx = 100;
player1.playery = 100;
player1.playername = msgReceived.strName;
player1.playerid = playercount;
playerList.Add(player1);
playercount = playercount + 1;
//Set the text of the message that we will broadcast to all users
msgToSend.strMessage = "<<<" + msgReceived.strName + " has joined the room>>>";
msgToSend.cmdCommand = Command.Login;
}
else
{
player player2 = new player();
ClientInfo clientInfo = new ClientInfo();
clientInfo.endpoint = epSender;
clientInfo.strName = msgReceived.strName;
msgToSend.playerID = playercount;
player2.playerid = playercount;
clientList.Add(clientInfo);
player2.playerx = 300;
player2.playery = 300;
player2.playername = msgReceived.strName;
player2.playerid = playercount;
playerList.Add(player2);
playercount = playercount + 1;
//Set the text of the message that we will broadcast to all users
msgToSend.strMessage = "<<<" + msgReceived.strName + " has joined the room>>>";
msgToSend.cmdCommand = Command.Login;
}
break;
case Command.Create:
break;
case Command.Logout:
//When a user wants to log out of the server then we search for her
//in the list of clients and close the corresponding connection
int nIndex = 0;
foreach (ClientInfo client in clientList)
{
if (client.endpoint == epSender)
{
clientList.RemoveAt(nIndex);
break;
}
++nIndex;
}
msgToSend.strMessage = "<<<" + msgReceived.strName + " has left the room>>>";
break;
case Command.Message:
//Set the text of the message that we will broadcast to all users
msgToSend.strMessage = msgReceived.strName + ": " + msgReceived.strMessage;
break;
case Command.Move:
msgToSend = MovePlayer(msgReceived);
msgToSend.cmdCommand = msgReceived.cmdCommand;
break;
case Command.List:
//Send the names of all users in the chat room to the new user
msgToSend.cmdCommand = Command.List;
msgToSend.strName = null;
msgToSend.strMessage = null;
//Collect the names of the user in the chat room
foreach (ClientInfo client in clientList)
{
//To keep things simple we use asterisk as the marker to separate the user names
msgToSend.strMessage += client.strName + "*";
}
message = msgToSend.ToByte(playerList, playercount);
//Send the name of the users in the chat room
serverSocket.BeginSendTo(message, 0, message.Length, SocketFlags.None, epSender,
new AsyncCallback(OnSend), epSender);
break;
}
//TODO: Add check to see if it is a login, if it is send the players ID to the client
//When the client receives the login response it will set the users ID to the correct value
//
if (msgToSend.cmdCommand == Command.Login)
{
message = msgToSend.ToByte(playerList, playercount);
serverSocket.BeginSendTo(message, 0, message.Length, SocketFlags.None, epSender,
new AsyncCallback(OnSend), epSender);
msgToSend.cmdCommand = Command.Create;
message = msgToSend.ToByte(playerList, playercount);
serverSocket.BeginSendTo(message, 0, message.Length, SocketFlags.None, epSender,
new AsyncCallback(OnSend), epSender);
msgToSend.cmdCommand = Command.Login;
}
if (msgToSend.cmdCommand != Command.List) //List messages are not broadcasted
{
message = message = msgToSend.ToByte(playerList, playercount);
foreach (ClientInfo clientInfo in clientList)
{
//Send the message to all users
serverSocket.BeginSendTo(message, 0, message.Length, SocketFlags.None, clientInfo.endpoint,
new AsyncCallback(OnSend), clientInfo.endpoint);
}
}
//If the user is logging out then we need not listen from her
//Start listening to the message send by the user
serverSocket.BeginReceiveFrom(byteData, 0, byteData.Length, SocketFlags.None, ref epSender,
new AsyncCallback(OnReceive), epSender);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "SGSServerUDP", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}