Beispiel #1
0
        public VoidResult BeginStart()
        {
            VoidResult result = new VoidResult();

            if (!IsStopped)
            {
                result.AddException(new Exception("Is started now"));
                return result;
            }

            if (_threadPortOccupations == null)
                _threadPortOccupations = new List<ThreadPortOccupation>();

            if (_activeClients == null)
                _activeClients = new List<IPAddress>();

            IsStopped = false;
            _prepareToStop = false;

            _threadPortDistribution = ThreadHelper.AlterThread(() =>
            {
                _listenerPortDistribution = new TcpListenerEx(Settings.DistributionPort);
                _listenerPortDistribution.Start();
                while (!_prepareToStop)
                {
                    try
                    {
                        using (var client = _listenerPortDistribution.AcceptTcpClient())
                        {
                            var address = ((IPEndPoint)client.Client.RemoteEndPoint).Address;
                            if (!_activeClients.Any(x => x.Equals(address)))
                                _activeClients.Add(address);

                            ThreadPortOccupation occupation = null;

                            while (occupation == null)
                            {
                                occupation = _threadPortOccupations.FirstOrDefault(x => !x.IsOccupiedByClient);
                                if (occupation == null)
                                    Thread.Sleep(10);
                            }

                            SendString(client.GetStream(), occupation.Port.ToString());
                        }
                    }
                    catch (Exception e)
                    {
                        if (e is SocketException && e.Message.Contains("WSACancelBlockingCall"))
                        {
                            //do nothing
                        }
                        else
                            Log.Write(e);
                    }
                }
            });

            for (int i = 0; i < Settings.ActionsPorts.Count; i++)
            {
                try
                {
                    var port = Settings.ActionsPorts[i];

                    var listener = new TcpListenerEx(port);

                    listener.Server.SendTimeout = (int)ServerThreadingSettings.Defaults.SendTimout;
                    listener.Server.ReceiveTimeout = (int)ServerThreadingSettings.Defaults.ReceiveTimout;
                    ThreadPortOccupation portOccupation = null;
                    Thread t = new Thread(() =>
                    {
                        while (true)
                        {
                            try
                            {
                                using (TcpClient client = listener.AcceptTcpClient())
                                {
                                    portOccupation.IsOccupiedByClient = true;

                                    bool resolved = true;
                                    if (Settings.ResolvedIp != null && Settings.ResolvedIp.Any() && !Settings.ResolveAllIp)
                                    {
                                        lock (Settings.ResolvedIp)
                                        {
                                            var ip = ((IPEndPoint)client.Client.RemoteEndPoint).Address;
                                            if (!Settings.ResolvedIp.Where(x => x.Equals(ip)).Any())
                                            {
                                                client.Close();
                                                resolved = false;
                                            }
                                        }
                                    }

                                    if (resolved)
                                    {
                                        var stream = client.GetStream();
                                        var command = GetNextString(stream);

                                        CommandHandling(client, stream, command);
                                    }
                                }
                            }
                            catch (Exception e)
                            {
                                if (e is SocketException && e.Message.Contains("WSACancelBlockingCall"))
                                {
                                    //do nothing
                                }
                                else
                                    Log.Write(e);
                            }

                            portOccupation.IsOccupiedByClient = false;

                            if (!listener.IsActive)
                            {
                                lock (_threadPortOccupations)
                                {
                                    _threadPortOccupations.RemoveAll(x => x.Port == port);

                                    if (!_threadPortOccupations.Any())
                                        IsStopped = true;
                                }
                                break;
                            }
                        }
                    });

                    _threadPortOccupations.Add(portOccupation = new ThreadPortOccupation(t, listener, port));
                    t.IsBackground = false;
                    listener.Start();
                    t.Start();
                }
                catch (Exception e)
                {
                    result.AddException(e);
                }
            }

            if (ServerStarted != null)
                ServerStarted();

            return result;
        }
Beispiel #2
0
        internal VoidResult Initialize()
        {
            var result = new VoidResult();

            try
            {
                if (_customActions == null)
                    _customActions = new List<Type>();
                else _customActions.Clear();

                if (_customCheckers == null)
                    _customCheckers = new List<Type>();
                else _customCheckers.Clear();

                RegisterAction(new Uri(typeof(PyriteStandartActions.DoNotDeleteThisClass).Assembly.CodeBase).LocalPath);
                RegisterChecker(new Uri(typeof(PyriteStandartActions.DoNotDeleteThisClass).Assembly.CodeBase).LocalPath);
            }
            catch (Exception e)
            {
                result.AddException(e);
            }
            return result;
        }
Beispiel #3
0
        public VoidResult BeginStop(Action callback)
        {
            _whenStoppedCallback = callback;

            var result = new VoidResult();
            try
            {
                _prepareToStop = true;

                _listenerPortDistribution.Stop();

                _threadPortOccupations.ToList().ForEach(x =>
                {
                    x.Listener.Stop();
                });
            }
            catch (Exception e)
            {
                result.AddException(e);
            }

            return result;
        }
Beispiel #4
0
        public VoidResult ReIntialize(Action<VoidResult> callback)
        {
            var result = new VoidResult();
            ModulesControl.Clear();
            ScenariosPool.Clear();

            result.AddExceptions(SaveAndLoad.Load().Exceptions);

            Stop(() =>
            {
                try
                {
                    ServerThreading.BeginStart();
                    Log.Write("ServerThreading started");
                    ServerThreading.ServerStarted += () => ScenariosPool.StartActiveScenarios();
                    Log.Write("ScenariosPool items started");
                    callback(result);
                }
                catch (Exception e)
                {
                    result.AddException(e);
                }
            });

            return result;
        }