Exemplo n.º 1
0
        /// <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);
            }
        }
Exemplo n.º 2
0
        /// <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);
            }
        }