/// <summary> /// Sends the allocated id back to the client. /// </summary> /// <param name="senderSocket">Sender socket.</param> /// <param name="allocatedId">Allocated identifier.</param> private static void SendIdToClient(Socket senderSocket, int allocatedId) { Packet toSend = new Packet(-1, allocatedId, RequestType.Register); toSend.AddArgument(ServerConstants.ArgumentNames.Id, allocatedId.ToString()); String jsonString = JsonConvert.SerializeObject(toSend); jsonString += ServerConstants.endOfPacket; Send(senderSocket, jsonString); }
private static void HandleConnectToGameRequest(Packet receivedPacket) { ClientType clientType = (ClientType)((int)receivedPacket.Arguments[ServerConstants.ArgumentNames.SenderType]); switch (clientType) { case ClientType.Agent: { int idAgentTryingToConnect = receivedPacket.SenderId; bool didFindGame = false; foreach (var clientData in MyClients) { isConnecting.WaitOne(); if (clientData.ClientType == ClientType.GameMaster && clientData.NumberOfSpotsAvailable > 0) { clientData.NumberOfSpotsAvailable--; Packet forAgent = new Packet(-1, idAgentTryingToConnect, RequestType.ConnectToGame); forAgent.AddArgument("GameMasterId", clientData.Id); forAgent.AddArgument("DidFindGame", true); Packet forGameMaster = new Packet(-1, clientData.Id, RequestType.ConnectToGame); forGameMaster.AddArgument("NewPlayerId", idAgentTryingToConnect); HandleSendRequest(forAgent); HandleSendRequest(forGameMaster); didFindGame = true; isConnecting.ReleaseMutex(); break; } isConnecting.ReleaseMutex(); } if (!didFindGame) { Packet forAgent = new Packet(-1, idAgentTryingToConnect, RequestType.ConnectToGame); forAgent.AddArgument("DidFindGame", false); } break; } case ClientType.GameMaster: { int sendingGameMasterId = receivedPacket.SenderId; int indexInClientsList = GetIndexOfDestinationInClients(sendingGameMasterId); int newNumberOfPlayers = (int)receivedPacket.Arguments["UpdatedNumberOfPlayers"]; MyClients[indexInClientsList].NumberOfSpotsAvailable = newNumberOfPlayers; break; } default: Console.WriteLine("Invalid request received, do nothing."); break; } }