Exemple #1
0
        void BeginService()
        {
            try
            {
                string service_base_dir = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
                System.Environment.CurrentDirectory = service_base_dir;

                XLog.statuslog("Started");

                {
                    try
                    {
                        XLog.statuslog("Starting QizmtEC2ServiceInit.exe");
                        System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics
                            .ProcessStartInfo("QizmtEC2ServiceInit.exe");
                        psi.UseShellExecute = false;
                        psi.CreateNoWindow = true;
                        System.Diagnostics.Process initproc = System.Diagnostics.Process.Start(psi);
#if RESETEC2SERVICEPASSWORD
                        initproc.Dispose();
                        this.Stop(); // Note! QizmtEC2ServiceInit will restart the service with new creds.
                        XLog.statuslog("Stopping this service");
                        return;
#else
                        initproc.WaitForExit();
                        initproc.Dispose();
                        XLog.statuslog("Waited on QizmtEC2ServiceInit.exe");
#endif
                    }
                    catch (System.Threading.ThreadAbortException)
                    {
                        return;
                    }
                    catch (System.Threading.ThreadInterruptedException)
                    {
                        return;
                    }
                    catch (Exception e)
                    {
                        XLog.errorlog("OnStart exception while running QizmtEC2ServiceInit: " + e.ToString());
                        return;
                    }
                }

                {
                    lthd = new System.Threading.Thread(new System.Threading.ThreadStart(ListenThreadProc));
                    lthd.IsBackground = true;
                    lthd.Start();
                }
            }
            catch (Exception e)
            {
                XLog.errorlog("OnStart exception: " + e.ToString());
            }
        }
Exemple #2
0
        static void ListenThreadProc()
        {
            try
            {
                lsock = new System.Net.Sockets.Socket(System.Net.Sockets.AddressFamily.InterNetwork,
                    System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp);
                System.Net.IPEndPoint ipep = new System.Net.IPEndPoint(System.Net.IPAddress.Any, 55907);
                for (int i = 0; ; i++)
                {
                    try
                    {
                        lsock.Bind(ipep);
                        break;
                    }
                    catch (Exception e)
                    {
                        if (i >= 5)
                        {
                            throw;
                        }
                        System.Threading.Thread.Sleep(1000 * 4);
                        continue;
                    }
                }

                lsock.Listen(30);

                XLog.statuslog("Accepting connections on port " + ipep.Port);

                for (; ; )
                {
                    System.Net.Sockets.Socket dllclientSock = lsock.Accept();

                    System.Threading.Thread cthd = new System.Threading.Thread(
                        new System.Threading.ParameterizedThreadStart(ClientThreadProc));
                    cthd.Name = "ClientThread" + (++userhit).ToString();
                    cthd.IsBackground = true;
                    cthd.Start(dllclientSock);
                }
            }
            catch (System.Threading.ThreadAbortException e)
            {
            }
            catch (Exception e)
            {
                XLog.errorlog("ListenThreadProc exception: " + e.ToString());
            }
        }
        protected override void OnStart(string[] args)
        {
            try
            {
                string service_base_dir = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
                System.Environment.CurrentDirectory = service_base_dir;

                lthd = new System.Threading.Thread(new System.Threading.ThreadStart(ListenThreadProc));
                lthd.IsBackground = true;
                lthd.Start();
            }
            catch (Exception e)
            {
                XLog.errorlog("OnStart exception: " + e.ToString());
            }
        }
        static void ClientThreadProc(object obj)
        {
            System.Net.Sockets.Socket dllclientSock = null;
            byte[] buf = new byte[0x400 * 4];
            try
            {
                dllclientSock = (System.Net.Sockets.Socket)obj;
                System.Net.Sockets.NetworkStream netstm = new System.Net.Sockets.NetworkStream(dllclientSock);
                for (bool run = true; run;)
                {
                    int ich = -1;
                    try
                    {
                        ich = netstm.ReadByte();
                    }
                    catch
                    {
                    }
                    if (ich == -1)
                    {
                        break;
                    }
                    switch (ich)
                    {
                    case 'p':     // Pin shared memory.
                    {
                        try
                        {
                            string smname = XContent.ReceiveXString(netstm, buf);

                            IntPtr hf        = INVALID_HANDLE_VALUE;
                            IntPtr hmap      = CreateFileMapping(hf, IntPtr.Zero, PAGE_READWRITE, 1, @"Global\" + smname);
                            int    lasterror = Marshal.GetLastWin32Error();
                            if (IntPtr.Zero == hmap)
                            {
                                if (8 == lasterror)
                                {
                                    throw new Exception("Shared memory segment named '" + smname + "' cannot be allocated; CreateFileMapping failed with ERROR_NOT_ENOUGH_MEMORY",
                                                        new OutOfMemoryException());
                                }
                                throw new Exception("Shared memory segment named '" + smname + "' cannot be allocated; CreateFileMapping failed with GetLastWin32Error=" + lasterror);
                            }
                            if (ERROR_ALREADY_EXISTS != lasterror)
                            {
                                throw new Exception("Shared memory segment named '" + smname + "' not found");
                            }
                            IntPtr ipview = MapViewOfFile(hmap, FILE_MAP_ALL_ACCESS, 0, 0, 0);
                            if (IntPtr.Zero == ipview)
                            {
                                lasterror = Marshal.GetLastWin32Error();
                                CloseHandle(hmap);
                                if (8 == lasterror)
                                {
                                    throw new Exception("Shared memory segment named '" + smname + "' cannot be mapped into memory; MapViewOfFile failed with ERROR_NOT_ENOUGH_MEMORY",
                                                        new OutOfMemoryException());
                                }
                                throw new Exception("Shared memory segment named '" + smname + "' cannot be mapped into memory; MapViewOfFile failed with GetLastWin32Error=" + lasterror);
                            }

                            PSM psm;
                            psm.hmap   = hmap;
                            psm.ipview = ipview;
                            psm.name   = smname;
                            lock (pins)
                            {
                                pins.Add(psm);
                            }

                            netstm.WriteByte((byte)'+');
                        }
                        catch (Exception e)
                        {
                            try
                            {
                                netstm.WriteByte((byte)'-');
                                XContent.SendXContent(netstm, e.ToString());
                            }
                            catch
                            {
                                throw new Exception("Unable to report exception to caller (pin shared memory)", e);
                            }
                        }
                    }
                    break;

                    case 'u':     // Unpin shared memory.
                    {
                        try
                        {
                            string smname = XContent.ReceiveXString(netstm, buf);

                            bool found = false;
                            PSM  psm   = new PSM();
                            lock (pins)
                            {
                                int ipsm = IndexOfPSM_unlocked(smname);
                                if (-1 != ipsm)
                                {
                                    psm = pins[ipsm];
                                    pins.RemoveAt(ipsm);
                                    found = true;
                                }
                            }
                            if (found)
                            {
                                UnmapViewOfFile(psm.ipview);
                                CloseHandle(psm.hmap);
                                netstm.WriteByte((byte)'+');
                            }
                            else
                            {
                                netstm.WriteByte((byte)'-');
                                XContent.SendXContent(netstm, "Cannot unpin; shared memory segment not pinned: " + smname);
                            }
                        }
                        catch (Exception e)
                        {
                            try
                            {
                                netstm.WriteByte((byte)'-');
                                XContent.SendXContent(netstm, e.ToString());
                            }
                            catch
                            {
                                throw new Exception("Unable to report exception to caller (unpin shared memory)", e);
                            }
                        }
                    }
                    break;

                    case 'c':     // Close.
                        run = false;
                        break;
                    }
                }
                netstm.Close();
                dllclientSock.Close();
            }
            catch (Exception e)
            {
                XLog.errorlog("ClientThreadProc exception: " + e.ToString());

                try
                {
                    dllclientSock.Close();
                }
                catch (Exception e2)
                {
                }
            }
        }
Exemple #5
0
        static void ClientThreadProc(object obj)
        {
            System.Net.Sockets.Socket dllclientSock = null;
            byte[] buf = new byte[0x400 * 4];
            try
            {
                dllclientSock = (System.Net.Sockets.Socket)obj;
                System.Net.Sockets.NetworkStream netstm = new System.Net.Sockets.NetworkStream(dllclientSock);
                for (bool run = true; run; )
                {
                    int ich = -1;
                    try
                    {
                        ich = netstm.ReadByte();
                    }
                    catch
                    {
                    }
                    if (ich == -1)
                    {
                        break;
                    }
                    switch (ich)
                    {
                        case 'r': // Run!
                            {
                                Exception outputexception = null;
                                string output = "";
                                try
                                {
                                    string cmd = XContent.ReceiveXString(netstm, buf);
#if QEC2S_RUNTHREAD
                                    System.Threading.Thread thd = new System.Threading.Thread(
                                        new System.Threading.ThreadStart(
                                            delegate()
                                            {
#endif
                                                try
                                                {
                                                    output = Exec.Shell(cmd, false);
                                                }
                                                catch (Exception einner)
                                                {
                                                    outputexception = einner;
                                                }
#if QEC2S_RUNTHREAD
                                            }));
                                    thd.IsBackground = true;
                                    thd.Start();
                                    thd.Join();
#endif
                                    if (null == outputexception)
                                    {
                                        netstm.WriteByte((byte)'+');
                                        XContent.SendXContent(netstm, output);
                                    }
                                }
                                catch (Exception eouter)
                                {
                                    if (null == outputexception)
                                    {
                                        outputexception = eouter;
                                    }
                                }
                                if (null != outputexception)
                                {
                                    try
                                    {
                                        netstm.WriteByte((byte)'-');
                                        XContent.SendXContent(netstm, outputexception.ToString());
                                    }
                                    catch (Exception e)
                                    {
                                        XLog.errorlog("Error while reporting error: " + outputexception.ToString());
                                    }
                                }
                            }
                            break;

                        case 'c': // Close.
                            run = false;
                            break;
                    }
                }
                netstm.Close();
                dllclientSock.Close();
            }
            catch (Exception e)
            {
                XLog.errorlog("ClientThreadProc exception: " + e.ToString());

                try
                {
                    dllclientSock.Close();
                }
                catch (Exception e2)
                {
                }
            }
        }