//Generates a request to send to connected clients to add newly connected client to their list. static public void ListAddClient(Client client) { List <object> request = new List <object>(); List <int> dataType = new List <int>(); byte[] requestByteArray = new byte[17]; String StrIPAddr; IPEndPoint endPoint; request.Add(RequestTypes.ListAddClient); dataType.Add(VarTypes.typeByte); //conver the ip address to a long and add to the list endPoint = (IPEndPoint)client.clientSocket.RemoteEndPoint; IPAddress addr = endPoint.Address; StrIPAddr = addr.ToString(); request.Add(Server.IPtoLong(StrIPAddr)); dataType.Add(VarTypes.typeLong); request.Add(endPoint.Port); dataType.Add(VarTypes.typeInt32); request.Add(client.clientID); dataType.Add(VarTypes.typeInt32); requestByteArray = RequestHandler.byteConverter(request, dataType, 17); RequestQueueMutex.WaitOne(); RequestQueue.Enqueue(requestByteArray); RequestQueueMutex.ReleaseMutex(); }
//Generates a request to send to connected clients to remove disconnected client to their list. static public void ListRemoveClient(Client client) { List <object> request = new List <object>(); List <int> dataType = new List <int>(); byte[] requestByteArray = new byte[5]; request.Add(RequestTypes.ListRemoveClient); dataType.Add(VarTypes.typeByte); request.Add(client.clientID); dataType.Add(VarTypes.typeInt32); requestByteArray = RequestHandler.byteConverter(request, dataType, 5); RequestQueueMutex.WaitOne(); RequestQueue.Enqueue(requestByteArray); RequestQueueMutex.ReleaseMutex(); }
//sned newly connected client information about all other connected clients. public static void sendClientAll(byte[] clientData) { List <object> request = new List <object>(); List <int> dataType = new List <int>(); byte[] requestByteArray = new byte[17]; String StrIPAddr; IPEndPoint endPoint; int clientID, clientIndex; //find whether the client is a buggy or a controller client before sending anything clientID = BitConverter.ToInt32(clientData, 13); clientIndex = clientManager.Clients.FindIndex(x => x.clientID == clientID); //if client that we want to sent buggy data to exists then continue if (clientIndex != -1) { foreach (Client clients in clientManager.Clients) { //send data to the newly connected client if the client we are inspecting now is a buggy. if (clients.buggyOrClient == 0) { try { //Add request type to the request object list and the request's type to the dataType list request.Add(RequestTypes.ListAddClient); dataType.Add(VarTypes.typeByte); //extract ipaddress of the client and convert it to a long and add it to the request list. The add the its datatype to the dataType list endPoint = (IPEndPoint)clients.clientSocket.RemoteEndPoint; IPAddress addr = endPoint.Address; StrIPAddr = addr.ToString(); request.Add(Server.IPtoLong(StrIPAddr)); dataType.Add(VarTypes.typeLong); //add the port request.Add(endPoint.Port); dataType.Add(VarTypes.typeInt32); //add client id request.Add(clients.clientID); dataType.Add(VarTypes.typeInt32); //convert the data from the request list to a byte array for sending over the network requestByteArray = RequestHandler.byteConverter(request, dataType, 17); if (clients.clientID != clientManager.Clients[clientManager.Clients.Count - 1].clientID) //Do not send information about the newest client to itself { clientManager.Clients[clientIndex].clientSocket.BeginSend(requestByteArray, 0, requestByteArray.Length, SocketFlags.None, clients.SendCallback, clients.clientSocket); //send data to the other clients } //clear the lists for the next iteration request.Clear(); dataType.Clear(); } catch (Exception e) { Console.WriteLine("Exception in sendAllClients"); //remove client that was disconnected RemoveClient(clients.clientID); } } } } }