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()); } }
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()); } }