Exemple #1
0
        /// <summary>
        /// ПОддерживает соединение хоста с мастером
        /// </summary>
        /// <param name="Params"></param>
        private void MaintaintHostConnection(Object Params)
        {
            TcpClient Connection = Params as TcpClient;

            try
            {
                Connection.Connect(Master);
                if (state != ControllerState.Suspending)
                {
                    state = ControllerState.Suspending;
                }
                UpdateData("Connection to the wizard is installed");
                Connection.ReceiveBufferSize = 10000;
                Connection.ReceiveTimeout    = 100;
                NetworkStream Stream = Connection.GetStream();
                bool          DataCollected;
                bool          Finished     = false;
                UInt32        oldHttpCount = 0;
                UInt32        oldUdpCount  = 0;
                UInt32        newHttpCount;
                UInt32        newUdpCount;
                while (Connection.Connected && !Finished)
                {
                    DataCollected = false;
                    if (Connection.Available >= 1)
                    {
                        int code = Stream.ReadByte();
                        switch (code)
                        {
                        case (int)MessageCode.Params:
                            while (Connection.Connected && !DataCollected)
                            {
                                if (Connection.Available >= 2)
                                {
                                    byte[] buuf = new byte[2];
                                    Stream.Read(buuf, 0, 2);                                  //df to add length in sendservicemsg
                                    UInt16 len = BitConverter.ToUInt16(buuf, 0);
                                    while (Connection.Connected && !DataCollected)
                                    {
                                        if (Connection.Available >= len)
                                        {
                                            byte[] buffer = new byte[len];
                                            Stream.Read(buffer, 0, len);
                                            InitParams(AttackParams.parseFromArray(buffer));
                                            state         = ControllerState.Suspending;
                                            DataCollected = true;
                                            UpdateData("Received settings from the master");
                                        }
                                    }
                                }
                            }
                            break;

                        case (int)MessageCode.StartAttack:
                            AttackIsAllowed = true;
                            Start(); break;

                        case (int)MessageCode.StopAttack:
                            AttackIsAllowed = false;
                            Stop(); break;

                        case (int)MessageCode.StatisticMessageRequest:
                            List <byte> msgs = new List <byte>();
                            msgs.Add(3);
                            newHttpCount = Core.HttpCounter;
                            newUdpCount  = Core.UdpCounter;
                            msgs.AddRange(BitConverter.GetBytes(newHttpCount - oldHttpCount));
                            msgs.AddRange(BitConverter.GetBytes(newUdpCount - oldUdpCount));
                            oldHttpCount = newHttpCount;
                            oldUdpCount  = newUdpCount;
                            Stream.Write(msgs.ToArray(), 0, msgs.Count);
                            break;

                        case (int)MessageCode.Finish:
                            Finished = true;
                            break;
                        }
                    }
                }
                UpdateData("Connection with the master is broken");
                state = ControllerState.Error;
                Stream.Close();
                HostClient.Close();
            }
            catch (SocketException)
            {
                UpdateData("Unable to connect to master"); //1060 exception
                state = ControllerState.Error;
            }
            catch (ObjectDisposedException)
            {
                return;
            }
        }
Exemple #2
0
        private void MaintaintHostConnection(Object Params)
        {
            TcpClient Connection = Params as TcpClient;

            try
            {
                Connection.Connect(Master);
                if (state != ControllerState.Suspending)
                {
                    state = ControllerState.Suspending;
                }
                UpdateData("Соединение с мастером установлено");
                Connection.ReceiveBufferSize = 10000;
                Connection.ReceiveTimeout    = 100;
                NetworkStream Stream = Connection.GetStream();
                bool          DataCollected;
                UInt32        oldHttpCount = 0;
                UInt32        oldUdpCount  = 0;
                UInt32        newHttpCount;
                UInt32        newUdpCount;
                while (Connection.Connected)
                {
                    DataCollected = false;
                    if (Connection.Available >= 1)
                    {
                        int code = Stream.ReadByte();
                        switch (code)
                        {
                        case 0:
                            while (Connection.Connected && !DataCollected)
                            {
                                if (Connection.Available >= 2)
                                {
                                    byte[] buuf = new byte[2];
                                    Stream.Read(buuf, 0, 2);                                  //df to add length in sendservicemsg
                                    UInt16 len = BitConverter.ToUInt16(buuf, 0);
                                    while (Connection.Connected && !DataCollected)
                                    {
                                        if (Connection.Available >= len)
                                        {
                                            byte[] buffer = new byte[len];
                                            Stream.Read(buffer, 0, len);
                                            InitParams(AttackParams.parseFromArray(buffer));
                                            state         = ControllerState.Suspending;
                                            DataCollected = true;
                                            UpdateData("Получены настройки от мастера");
                                        }
                                    }
                                }
                            }

                            break;

                        case 1:
                            AttackIsAllowed = true;
                            Start(); break;

                        case 2:
                            AttackIsAllowed = false;
                            Stop(); break;

                        case 3:
                            List <byte> msgs = new List <byte>();
                            msgs.Add(3);
                            newHttpCount = Core.HttpCounter;
                            newUdpCount  = Core.UdpCounter;
                            msgs.AddRange(BitConverter.GetBytes(newHttpCount - oldHttpCount));
                            msgs.AddRange(BitConverter.GetBytes(newUdpCount - oldUdpCount));
                            oldHttpCount = newHttpCount;
                            oldUdpCount  = newUdpCount;
                            Stream.Write(msgs.ToArray(), 0, msgs.Count);
                            //stats
                            break;
                        }
                    }
                }
                UpdateData("Соединение с мастером разорвано");
            }
            catch (SocketException err)
            {
                UpdateData("Невозможно подключиться к мастеру"); //1060 exception
                state = ControllerState.Error;
            }
            catch (ObjectDisposedException)
            {
                return;
            }
        }