/// <summary> /// Send init data to client /// </summary> /// <param name="client">Client</param> /// <param name="port">Client port</param> /// <param name="src">Code to send</param> public static void initClient(Client client,int port, string src) { /*Send initilisation packet*/ if (!client.initialised) { /*Connect to client*/ Sender s = new Sender(client.ip, Convert.ToInt32(port)); /*Create init packet*/ DataInitClient dic = new DataInitClient(src); byte[] dicData = dic.ToByte(); /*Create packet info data*/ PacketInfo packetInfo = new PacketInfo(Command.ClientInit,0,"", dicData.Length,0,0); byte[] packetInfoData = packetInfo.ToByte(); /*Assembly the packets*/ List<byte[]> listPacket = new List<byte[]>(); listPacket.Add(packetInfoData); listPacket.Add(dicData); /*Send all packet*/ byte[] dataToSend = PacketAssembler.Assemble(listPacket); s.send(dataToSend); } }
/// <summary> /// Test own IP addresses on own computer /// </summary> /// <param name="port">Port to send</param> /// <returns>Finish?</returns> public bool GetIPAddress(int port) { List<IPAddressInformation> listInfo = new List<IPAddressInformation>(); foreach (NetworkInterface netif in NetworkInterface.GetAllNetworkInterfaces()) //Each network interface { IPInterfaceProperties properties = netif.GetIPProperties(); foreach (IPAddressInformation unicast in properties.UnicastAddresses) //Each ip adress { if (unicast.Address != IPAddress.Loopback) //No local host { Sender sender = new Sender(unicast.Address.ToString(), port); //Start tcp sender PacketInfo packetInfo = new PacketInfo(Command.CorrectIP, 0, "",0,0,0); sender.send(packetInfo.ToByte()); //Send own } } } Thread.Sleep(100); return true; }
/// <summary> /// Send a work to a client /// </summary> /// <param name="listClient">List of clients</param> /// <param name="listPacketSended">Packet already sended</param> /// <param name="ipAddress">Ip address of a client</param> /// <param name="port">Port of a client</param> /// <param name="nbrPackets">Number packet to send</param> /// <param name="issueNumber">Issu number</param> public void sendWork(ref SortedList<string, Client> listClient, ref SortedList<int, PacketInfo>listPacketSended, string ipAddress, int port, int nbrPackets,int issueNumber) { List<byte[]> listWorksToSend = new List<byte[]>(); /*Connect to client*/ Sender s = new Sender(ipAddress, Convert.ToInt32(port)); SortedList<int,byte[]> tempListTasks = new SortedList<int,byte[]>(tasks); for (int i = 0; i < nbrPackets; i++) { if (tempListTasks.Count > 0) //More tasks? { int firstKey = tempListTasks.Keys[0]; byte[] data = tempListTasks[firstKey]; tempListTasks.Remove(firstKey); //remove temporary data, if the sender doesn't work /*Add info of data*/ PacketInfo packetInfo = new PacketInfo(Command.Work, firstKey, "", data.Length, SecondSince1970.Get(),issueNumber); if(!listPacketSended.ContainsKey(firstKey)) listPacketSended.Add(firstKey, packetInfo); listWorksToSend.Add(packetInfo.ToByte()); /*Add data*/ listWorksToSend.Add(data); listClient[ipAddress].currentWork.Add(firstKey); //Add to current client work } else break; } /*Assembly the packets*/ byte[] dataToSend = PacketAssembler.Assemble(listWorksToSend); /*Send data to client*/ s.send(dataToSend); int nbrPaquetsToRemove = nbrPackets; if (nbrPackets > tasks.Count) //More packet to remove than number of task? nbrPaquetsToRemove = tasks.Count; for (int i = 0; i < nbrPaquetsToRemove; i++) //Remove real data { tasks.RemoveAt(0); } }
/// <summary> /// Send work to all clients /// </summary> /// <param name="list">List of clients</param> /// <returns>New list of client if someone offline </returns> private SortedList<string, Client> sendFirstWorkAllClient(SortedList<string, Client> list) { /*Send work work to all connected clients*/ SortedList<string, Client> tmpListClient = new SortedList<string, Client>(list); foreach (string item in listClient.Keys) { Sender s = new Sender(listClient[item].ip, sendPort); if (!s.testConnexion()) { tmpListClient.Remove(item); continue; } if (dataWork != null && listClient[item].initialised == true && dataWork.tasks.Count != 0 && listClient[item].currentWork.Count == 0) //No more work? dataWork.sendWork(ref listClient, ref listPacketSended, listClient[item].ip, sendPort, nbrPacketToSend, issueNumber); } return tmpListClient; }