Example #1
0
        /// <summary>
        /// Connects with the remote module application for first time
        /// </summary>
        protected override void MainThreadFirstTimeConnect()
        {
            int       addressIndex = 0;
            Stopwatch sw           = new Stopwatch();

            while (running && !stopMainThread)
            {
                if (!client.TryConnect())
                {
                    addressIndex         = (addressIndex + 1) % this.serverAddresses.Count;
                    client.ServerAddress = this.ServerAddresses[addressIndex];
                    if (stopMainThread)
                    {
                        break;
                    }
                    AditionalActions();
                    sw.Start();
                    while ((sw.ElapsedMilliseconds < 100) && running && !stopMainThread)
                    {
                        Thread.Sleep((int)Math.Max((100 - sw.ElapsedMilliseconds) / 2, 0));
                    }
                    sw.Reset();
                    continue;
                }
                else
                {
                    IsAlive = true;
                    Ready   = !aliveCheck;
                    break;
                }
            }
        }
Example #2
0
        /// <summary>
        /// Synchronously sends the command to the module.
        /// </summary>
        /// <returns>true if the action was executed successfully. false otherwise</returns>
        private bool ExecuteSync()
        {
            SocketTcpClient client;

            try
            {
                client = new SocketTcpClient(address, port);
                if (!client.TryConnect())
                {
                    return(false);
                }
                client.Send(textToSend);
                client.Disconnect();
                return(true);
            }
            catch
            {
                return(false);
            }
        }
Example #3
0
        /// <summary>
        /// Request to execute the module startup on remote computer
        /// </summary>
        /// <param name="mc">The module to start</param>
        /// <param name="method">The startup sequence method</param>
        private bool RemoteStartup(IModuleClientTcp mc, ModuleStartupMethod method)
        {
            RemoteStartupRequest  request;
            RemoteStartupResponse response;
            SocketTcpClient       client;
            AutoResetEvent        dataReceivedEvent;
            string serialized;

            Log.WriteLine(5, "Starting module '" + mc.Name + "': on remote computer.");
            client = null;
            foreach (IPAddress ip in mc.ServerAddresses)
            {
                client = new SocketTcpClient(ip, 2300);
                if (client.TryConnect())
                {
                    break;
                }
            }
            if ((client == null) || !client.IsConnected)
            {
                Log.WriteLine(5, "Can not start module '" + mc.Name + "': unable to connect to remote computer.");
                return(false);
            }

            dataReceivedEvent    = new AutoResetEvent(false);
            client.DataReceived += new TcpDataReceivedEventHandler(delegate(TcpPacket packet)
            {
                response = RemoteStartupResponse.FromXml(packet.DataString);
                dataReceivedEvent.Set();
            });

            try
            {
                request    = new RemoteStartupRequest(mc.Name, method, mc.ProcessInfo);
                serialized = RemoteStartupRequest.ToXml(request);
                client.Send(serialized);
                response = null;
                dataReceivedEvent.WaitOne(10000);
                if (response == null)
                {
                    Log.WriteLine(5, "Can not start module '" + mc.Name + "': no response received");
                    client.Disconnect();
                    return(false);
                }
                if ((response.ModuleName != request.ModuleName) ||
                    (response.Method != request.Method))
                {
                    Log.WriteLine(5, "Can not start module '" + mc.Name + "': invalid response");
                    client.Disconnect();
                    return(false);
                }
                if (!response.Success)
                {
                    Log.WriteLine(5, "Can not start module '" + mc.Name + "': " + response.Message);
                    client.Disconnect();
                    return(false);
                }
                Log.WriteLine(5, "Start module '" + mc.Name + "': Success");
                client.Disconnect();
                return(true);
            }
            catch (Exception ex)
            {
                Log.WriteLine(5, "Can not start module '" + mc.Name + "': " + ex.Message);
                return(false);
            }
            finally
            {
                if ((client != null) && client.IsConnected)
                {
                    client.Disconnect();
                }
                if (client.Socket != null)
                {
                    client.Socket.Close();
                }
            }
        }