/// <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);
            }
        }
 void server_OnUpdateClient(Client client)
 {
     UpdateClient(client);
 }
 void UpdateClient(Client client)
 {
     if (InvokeRequired)
     {
         // We're not in the UI thread, so we need to call BeginInvoke
         BeginInvoke(new UpdateListbox(UpdateClient),new object[]{client});
         return;
     }
     // Must be on the UI thread if we've got this far
     lock (lockThis)
     {
         bool doesNodeAlreadyExist = false;
         foreach (TreeNode node in treeView1.Nodes)
         {
             if (node.Text == client.ip)
             {
                 doesNodeAlreadyExist = true;
                 break;
             }
         }
         if (!doesNodeAlreadyExist)
         {
             /*Display a new client*/
             TreeNode treeNode;
             treeNode = treeView1.Nodes.Add(client.ip);
             treeNode.Nodes.Add(string.Format("{0} core", client.nbrCore));
             //listBoxWorkInfo.Items.Add(string.Format("{0} interval", client.interval));
         }
     }
 }
        /// <summary>
        /// Event when as sended a data
        /// </summary>
        /// <param name="clientIP">Client IP address</param>
        /// <param name="data">Data received</param>
        private void r_OnReceived(string clientIP, byte[] data)
        {
            lock (lockThis)
            {
                /*Disassemble data*/
                List<DataReceived> listPackets = PacketDisassembler.Disassemble(data);
                /*Read each packets*/
                for (int i = 0; i < listPackets.Count; i++)
                {
                    DataReceived packet = listPackets[i];
                    Command cmd = (Command)BitConverter.ToInt32(data, 0);
                    switch (cmd)
                    {
                        case Command.Init:
                            Init dataInit = new Init(packet.data);
                            Client client = new Client(clientIP, dataInit.nbrCore, dataInit.memoryUsed, dataInit.listMethod, false, new List<int>(), 7000); //Create the client with her sended data
                            /*Init client*/
                            InitClient.initClient(client, sendPort, File.ReadAllText(calculatorClass));
                            if (!listClient.ContainsKey(clientIP)) //Client exist?
                            {
                                listClient.Add(client.ip, client);
                                if(OnUpdateClient!=null)
                                    OnUpdateClient(client); //Display client
                            }
                            else //Client already exist and not detected deconnexion
                            {
                                List<int> tmpCurrentWork = new List<int>(listClient[clientIP].currentWork);
                                foreach (int value in tmpCurrentWork)
                                {
                                    if (!dataWork.tasks.ContainsKey(value)) //Load packet non calculated
                                    {
                                        dataWork.tasks.Add(value, cuttedTask[value - 1]);
                                        listClient[clientIP].currentWork.Remove(value);
                                    }
                                }
                            }
                            break;
                        case Command.Result:
                            if (issueNumber == packet.issueNumber) //Working? Old sended packet?
                            {
                                if (!listResults.Contains(packet.noPacket)) //Task already calculated?
                                {
                                    listResults.Add(packet.noPacket);
                                }
                                if (listResults.Count == nbrPacket)
                                    timer.Stop();
                                listClient[clientIP].currentWork.Remove(packet.noPacket);
                                listClient[clientIP].interval = 7000;
                                int secondToCalculate = SecondSince1970.Get() - listPacketSended[packet.noPacket].secondSince1970;

                                if(OnUpdateWorkInfo!=null)
                                    OnUpdateWorkInfo(string.Format("{0} send result no {1}", clientIP, packet.noPacket));
                                OnUpdateResult((int)(((float)listResults.Count / cuttedTask.Length) * 100)); //Display numbre results

                                //Next works
                                if (listClient[clientIP].currentWork.Count == 0) //All the work has been done?
                                {
                                    if (dataWork != null && dataWork.tasks.Count != 0) //No more work?
                                    {
                                        avgCalculationTime = avgTime.calculateNewTime(SecondSince1970.Get() - listPacketSended[packet.noPacket].secondSince1970);  //Calculate average time from current time and sended packet
                                        timeRemaining = avgCalculationTime * (nbrPacket - listPacketSended.Count); //Calculate the time to remaining calculation
                                        dataWork.sendWork(ref listClient, ref listPacketSended, clientIP, sendPort, nbrPacketToSend, issueNumber);
                                    }
                                }
                                if (savePacketsToFile) //Save to file? or display fractale?
                                {
                                    string path = string.Format("{0}\\tmp\\result", Environment.CurrentDirectory); //Path of file data for this noPacket
                                    SaveToFile.save(path, FILE_EXTENTION, packet.noPacket.ToString(), packet.data);
                                }
                                else
                                {
                                    if(OnDisplayFractal!=null)
                                        OnDisplayFractal(fractale.createFractale(packet.noPacket, packet.data, nbrPacket));
                                }
                            }
                            break;
                        case Command.State:
                            break;
                        case Command.Initialized:
                            listClient[clientIP].initialised = true;
                            if (dataWork != null && dataWork.tasks.Count != 0) //No more work?
                                dataWork.sendWork(ref listClient, ref listPacketSended, clientIP, sendPort, nbrPacketToSend, issueNumber);
                            break;
                        case Command.CorrectIP:
                            if (clientIP != "127.0.0.1") //No local host
                                serverIPs += clientIP.ToString() + ";";
                            break;
                        //case Command.AliveClient:
                            //if (listClient != null && listClient.ContainsKey(clientIP))
                            //    listClient[clientIP].interval = packet.secondSince1970 * 7;
                            //break;
                    }
                }
                GC.Collect();
            }
        }