/// <summary>
        /// Send a list of results to server
        /// </summary>
        /// <param name="ip">Server IP</param>
        /// <param name="port">Server Port</param>
        /// <param name="listResults">List of results to send</param>
        public DataResult(string ip, int port, List<Task> listResults)
        {
            List<byte[]> listWorksToSend = new List<byte[]>();
            listResults.Sort();
            /*Connect to client*/
            Sender s = new Sender(ip, Convert.ToInt32(port));
            foreach (Task item in listResults)
            {
                /*Add info of data*/
                if (item.data != null)
                {
                    PacketInfo packetInfo = new PacketInfo(Command.Result, item.noPacket, "", item.data.Length,item.secondSince1970,item.issueNumber);
                    listWorksToSend.Add(packetInfo.ToByte());
                    /*Add data*/
                    listWorksToSend.Add(item.data);
                }
            }
            /*Assembly the packets*/
            PacketAssembler packetAssembler = new PacketAssembler();
            byte[] dataToSend = packetAssembler.Assemble(listWorksToSend);
            listResults.Clear();

            /*Send data to client*/
            s.send(dataToSend);
        }
        /// <summary>
        /// Receive data from broadcast receiver
        /// </summary>
        /// <param name="data">data received</param>
        void broadcastReceiver_OnReceived(byte[] data)
        {
            Command cmd = (Command)BitConverter.ToInt32(data, 0);

            /*Start the data receiver and send client data*/
            if (cmd == Command.Alive)
            {
                if (firstExecution)
                {
                    startReceiver();
                    firstExecution = false;
                }
                /*Test IP adrresse*/
                PacketInfo packetInfo = new PacketInfo(data, 0);

                string[] listIP = packetInfo.ipAddress.Split(new char[] { ';' });
                for (int i = 0; i < listIP.Length; i++)
                {
                    /*Set fisrt true IP to IP of server*/
                    if (listIP[i].IsIpReachable(serverPort))
                    {
                        serverIP = listIP[i];
                    }
                }

                /*Send init data each broadcast from server*/
                sendInitData();
            }
        }
Exemple #3
0
        /// <summary>
        /// Send a list of results to server
        /// </summary>
        /// <param name="ip">Server IP</param>
        /// <param name="port">Server Port</param>
        /// <param name="listResults">List of results to send</param>
        public DataResult(string ip, int port, List <Task> listResults)
        {
            List <byte[]> listWorksToSend = new List <byte[]>();

            listResults.Sort();
            /*Connect to client*/
            Sender s = new Sender(ip, Convert.ToInt32(port));

            foreach (Task item in listResults)
            {
                /*Add info of data*/
                if (item.data != null)
                {
                    PacketInfo packetInfo = new PacketInfo(Command.Result, item.noPacket, "", item.data.Length, item.secondSince1970, item.issueNumber);
                    listWorksToSend.Add(packetInfo.ToByte());
                    /*Add data*/
                    listWorksToSend.Add(item.data);
                }
            }
            /*Assembly the packets*/
            PacketAssembler packetAssembler = new PacketAssembler();

            byte[] dataToSend = packetAssembler.Assemble(listWorksToSend);
            listResults.Clear();

            /*Send data to client*/
            s.send(dataToSend);
        }
 /// <summary>
 /// Send alive packet
 /// </summary>
 private void sendAlivePacket()
 {
     if (working)
     {
         PacketInfo packetInfo = new PacketInfo(Command.AliveClient, 0, "", 0, (int)timer.Interval, 0);
         byte[]     data       = packetInfo.ToByte();
         Sender     s          = new Sender(serverIP, serverPort);
         s.send(data);
     }
 }
        /// <summary>
        /// Send initialized data
        /// </summary>
        /// <param name="ip">Server IP</param>
        /// <param name="port">Server port</param>
        public static void sendInitialized(string ip, int port)
        {
            /*Connect to client*/
            Sender s = new Sender(ip, Convert.ToInt32(port));

            /*Add info of data*/
            PacketInfo packetInfo = new PacketInfo(Command.Initialized, 0, "", 0, 0, 0);

            /*Send data to client*/
            s.send(packetInfo.ToByte());
        }
        /// <summary>
        /// Send initialized data
        /// </summary>
        /// <param name="ip">Server IP</param>
        /// <param name="port">Server port</param>
        public static void sendInitialized(string ip, int port)
        {
            /*Connect to client*/
            Sender s = new Sender(ip, Convert.ToInt32(port));

            /*Add info of data*/
            PacketInfo packetInfo = new PacketInfo(Command.Initialized, 0, "", 0,0,0);

            /*Send data to client*/
            s.send(packetInfo.ToByte());
        }
Exemple #7
0
        public 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.ClientInit || cmd == Command.Work)) //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 = new DataReceived(cmd, noPacket, packetInfo.secondSince1970, packetInfo.issueNumber, ipAddress, tempListByte.ToArray());
                    listDataReveived.Add(dataReceived);
                }
                else
                {
                    break;
                }
            }
            return(listDataReveived);
        }
        public 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.ClientInit || cmd == Command.Work)) //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 = new DataReceived(cmd, noPacket,packetInfo.secondSince1970,packetInfo.issueNumber, ipAddress, tempListByte.ToArray());
                    listDataReveived.Add(dataReceived);
                }
                else
                    break;
            }
            return listDataReveived;
        }
        /// <summary>
        /// Send data init to server
        /// </summary>
        /// <param name="ip">IP of server</param>
        /// <param name="port">Listen port of server</param>
        /// <param name="init">Packet Init</param>
        public DataInit(string ip, int port, Init init)
        {
            try
            {
                List<byte[]> listPacketInit = new List<byte[]>();
                /*Connect to client*/
                Sender s = new Sender(ip, Convert.ToInt32(port));

                byte[] dataToSend = init.ToByte();
                /*Add info of data*/
                PacketInfo packetInfo = new PacketInfo(Command.Init, 0, "", dataToSend.Length,0,0);
                listPacketInit.Add(packetInfo.ToByte());
                /*Add data*/
                listPacketInit.Add(dataToSend);
                /*Assembly the packets*/
                PacketAssembler packetAssembler = new PacketAssembler();
                dataToSend = packetAssembler.Assemble(listPacketInit);

                /*Send data to client*/
                s.send(dataToSend);
            }
            catch { }
        }
        /// <summary>
        /// Send data init to server
        /// </summary>
        /// <param name="ip">IP of server</param>
        /// <param name="port">Listen port of server</param>
        /// <param name="init">Packet Init</param>
        public DataInit(string ip, int port, Init init)
        {
            try
            {
                List <byte[]> listPacketInit = new List <byte[]>();
                /*Connect to client*/
                Sender s = new Sender(ip, Convert.ToInt32(port));

                byte[] dataToSend = init.ToByte();
                /*Add info of data*/
                PacketInfo packetInfo = new PacketInfo(Command.Init, 0, "", dataToSend.Length, 0, 0);
                listPacketInit.Add(packetInfo.ToByte());
                /*Add data*/
                listPacketInit.Add(dataToSend);
                /*Assembly the packets*/
                PacketAssembler packetAssembler = new PacketAssembler();
                dataToSend = packetAssembler.Assemble(listPacketInit);

                /*Send data to client*/
                s.send(dataToSend);
            }
            catch { }
        }
        /// <summary>
        /// Receive data from broadcast receiver
        /// </summary>
        /// <param name="data">data received</param>
        void broadcastReceiver_OnReceived(byte[] data)
        {
            Command cmd = (Command)BitConverter.ToInt32(data, 0);
            /*Start the data receiver and send client data*/
            if (cmd == Command.Alive)
            {
                if (firstExecution)
                {
                    startReceiver();
                    firstExecution = false;
                }
                /*Test IP adrresse*/
                PacketInfo packetInfo = new PacketInfo(data, 0);

                string[] listIP = packetInfo.ipAddress.Split(new char[] { ';' });
                for (int i = 0; i < listIP.Length; i++)
                {
                    /*Set fisrt true IP to IP of server*/
                    if (listIP[i].IsIpReachable(serverPort))
                        serverIP = listIP[i];
                }

                /*Send init data each broadcast from server*/
                sendInitData();
            }
        }
 /// <summary>
 /// Send alive packet
 /// </summary>
 private void sendAlivePacket()
 {
     if (working)
     {
         PacketInfo packetInfo = new PacketInfo(Command.AliveClient, 0, "", 0, (int)timer.Interval, 0);
         byte[] data = packetInfo.ToByte();
         Sender s = new Sender(serverIP, serverPort);
         s.send(data);
     }
 }