/// <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);
            }
        }
Esempio n. 2
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);
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Send a broadcast
        /// </summary>
        /// <param name="ip">IP of server</param>
        private void sendBroadcast(string ip)
        {
            /*Send clients the server is alive*/
            int        packetInfoLenght = 28;
            PacketInfo packetInfo       = new PacketInfo(Command.Alive, 0, ip, packetInfoLenght, 0, 0);

            BroadcastSender.sendBroadcast(packetInfo.ToByte(), sendBroadcastPort);
        }
        /// <summary>
        /// Disassemble a byte array on a list of DataReceived
        /// </summary>
        /// <param name="data">Data to disassemble</param>
        /// <returns></returns>
        public static List <DataReceived> Disassemble(byte[] data)
        {
            byte[]              hash             = SHAHash.calculate(1687933897132637812);
            List <byte>         listByte         = data.ToList();
            List <DataReceived> listDataReveived = new List <DataReceived>();
            Command             cmd;
            int    noPacket;
            string ipAddress;
            int    sizeData;
            int    currentPosition  = 0;
            int    previousPosition = 0;

            /*Read each packet into data array*/
            while (currentPosition + SIZEPACKETINFO <= data.Length)
            {
                previousPosition = currentPosition;
                /*Test if next data is a real data*/
                /*Get packet info*/
                PacketInfo packetInfo = new PacketInfo(data, previousPosition);
                cmd = packetInfo.cmd;
                if (SHAHash.verifiy(packetInfo.Hash, hash) && (cmd == Command.Init || cmd == Command.Result || cmd == Command.State || cmd == Command.Initialized || cmd == Command.CorrectIP || cmd == Command.AliveClient || cmd == Command.Interval)) //Correct command?
                {
                    noPacket  = packetInfo.noPacket;
                    ipAddress = packetInfo.ipAddress;

                    /*Get size of packet data*/
                    sizeData = packetInfo.dataLength;
                    /*First data position*/
                    currentPosition += SIZEPACKETINFO + ipAddress.Length + packetInfo.Hash.Length;
                    List <byte> tempListByte = new List <byte>();
                    int         i            = currentPosition;
                    /*Get data from previous position to the current position and data lenght*/
                    for (; i < (currentPosition + sizeData); i++)
                    {
                        tempListByte.Add(listByte[i]);
                    }
                    currentPosition = i;
                    DataReceived dataReceived;
                    if (cmd == Command.Result || cmd == Command.AliveClient) //Add time
                    {
                        dataReceived = new DataReceived(cmd, noPacket, packetInfo.secondSince1970, packetInfo.issueNumber, ipAddress, tempListByte.ToArray());
                    }
                    else
                    {
                        dataReceived = new DataReceived(cmd, noPacket, 0, packetInfo.issueNumber, ipAddress, tempListByte.ToArray());
                    }

                    listDataReveived.Add(dataReceived);
                }
                else
                {
                    break;
                }
            }
            return(listDataReveived);
        }
        /// <summary>
        /// Disassemble a byte array on a list of DataReceived
        /// </summary>
        /// <param name="data">Data to disassemble</param>
        /// <returns></returns>
        public static List<DataReceived> Disassemble(byte[] data)
        {
            byte[] hash = SHAHash.calculate(1687933897132637812);
            List<byte> listByte = data.ToList();
            List<DataReceived> listDataReveived = new List<DataReceived>();
            Command cmd;
            int noPacket;
            string ipAddress;
            int sizeData;
            int currentPosition = 0;
            int previousPosition = 0;
            /*Read each packet into data array*/
            while (currentPosition + SIZEPACKETINFO <= data.Length)
            {
                previousPosition = currentPosition;
                /*Test if next data is a real data*/
                /*Get packet info*/
                PacketInfo packetInfo = new PacketInfo(data, previousPosition);
                cmd = packetInfo.cmd;
                if (SHAHash.verifiy(packetInfo.Hash,hash) && (cmd == Command.Init || cmd == Command.Result || cmd == Command.State || cmd == Command.Initialized || cmd == Command.CorrectIP || cmd == Command.AliveClient || cmd==Command.Interval)) //Correct command?
                {
                    noPacket = packetInfo.noPacket;
                    ipAddress = packetInfo.ipAddress;

                    /*Get size of packet data*/
                    sizeData = packetInfo.dataLength;
                    /*First data position*/
                    currentPosition += SIZEPACKETINFO + ipAddress.Length + packetInfo.Hash.Length;
                    List<byte> tempListByte = new List<byte>();
                    int i = currentPosition;
                    /*Get data from previous position to the current position and data lenght*/
                    for (; i < (currentPosition + sizeData); i++)
                    {
                        tempListByte.Add(listByte[i]);
                    }
                    currentPosition = i;
                    DataReceived dataReceived;
                    if(cmd == Command.Result || cmd == Command.AliveClient) //Add time
                        dataReceived = new DataReceived(cmd, noPacket,packetInfo.secondSince1970,packetInfo.issueNumber, ipAddress, tempListByte.ToArray());
                    else
                        dataReceived = new DataReceived(cmd, noPacket,0, packetInfo.issueNumber,ipAddress, tempListByte.ToArray());

                    listDataReveived.Add(dataReceived);
                }
                else
                    break;
            }
            return listDataReveived;
        }
        /// <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>
 /// 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;
 }
Esempio n. 8
0
        /// <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);
            }
        }
Esempio n. 10
0
 /// <summary>
 /// Send a broadcast 
 /// </summary>
 /// <param name="ip">IP of server</param>
 private void sendBroadcast(string ip)
 {
     /*Send clients the server is alive*/
     int packetInfoLenght = 28;
     PacketInfo packetInfo = new PacketInfo(Command.Alive, 0, ip, packetInfoLenght, 0, 0);
     BroadcastSender.sendBroadcast(packetInfo.ToByte(), sendBroadcastPort);
 }