技术头条 - 一个快速在微博传播文章的方式     搜索本站
您现在的位置首页 --> 系统架构 --> 游戏程序守护进程-Windows版

游戏程序守护进程-Windows版

浏览:2580次  出处信息

    游戏程序守护进程-Windows版,是用VBS写的,适用于Windows 2003 x86 和 Windows 2003 x64.

    游戏程序的开启一般是需要按照指定的顺序开启的,这里以标准的db-gs-gg的架构为例,正常的开启顺序为db-gs-gg,如果gg程序挂了,一般我们手动的判断流程是先判断db程序是否正常,然后判断gs程序是否正常,都没有问题,然后再开启gg程序,该脚本就是按照该关联关系判断的原则进行处理的。

    所有的配置信息存放在setting.ini

    ‘说明

    ‘name项配置服务或程序名称,设置为设置为任务管理器中的image name

    ‘type项配置需要守护的进程为s(service)或是p(program)

    ‘process项配置程序的绝对路径或服务名称,如果守护进程和程序放在同一目录可配置相对路径

    ‘relate项配置是(y)否(n)有关联程序

    ‘logfile项配置log存放路径,默认deamon.log,存放守护进程目录

    [setting]

    name=EMeditor.exe

    type=S

    process=zhudongfangyu

    relate=y

    logfile=

    ‘relate_ip项配置关联程序/服务的ip,本机可配置localhost

    ‘relate_port项配置关联程序/服务的端口号,目前只支持tcp端口

    [relate]

    relate_ip=192.168.30.109

    relate_port=139

    如果配置了关联关系,即relate=y,则会程序开启前会自动判断关联程序是否正常,需使用check_tcp,是一个第三方判断tcp端口的程序,将check_tcp.zip中的check_tcp.exe放在setting.ini同一目录中即可。

    守护进程主程序源代码如下:

    On Error Resume Next

    path=Left(Wscript.ScriptFullName,len(Wscript.ScriptFullName)-len(Wscript.ScriptName))

    ‘定义函数

    function GetFile(strPath,contect)

    Set objFSO = CreateObject(“Scripting.FileSystemObject”)

    Set objFile = objFSO.OpenTextFile(strpath, 1)

    Do Until objFile.AtEndOfStream

    strLine = objFile.ReadLine

    If InStr(strLine, contect) Then

    GetFile=strLine

    end if

    Loop

    objFile.Close

    end function

    ‘函数结束

    Set objFS = CreateObject (“Scripting.FileSystemObject”)

    ‘读取配置文件

    settingfile=”setting.ini”

    name=GetFile(settingfile,”name=”)

    ptype=GetFile(settingfile,”type=”)

    process=GetFile(settingfile,”process=”)

    relate=GetFile(settingfile,”relate=”)

    logfile=GetFile(settingfile,”logfile=”)

    rname=lcase(right(name,len(name)-5))

    rtype=lcase(right(ptype,len(ptype)-5))

    rprocess=lcase(right(process,len(process)-8))

    rrelate=right(relate,len(relate)-7)

    rlog=right(logfile,len(logfile)-8)

    ‘判断log目录是否设置

    if rlog=”" then

    rlog=”deamon.log”

    end if

    ‘判断log是否存在

    if objFS.fileExists(rlog)=Flase then

    objFS.CreateTextFile(rlog)

    Set objNF1 = objFS.OpenTextFile(rlog,8)

    else

    Set objNF1 = objFS.OpenTextFile(rlog,8)

    end if

    Set objShell = CreateObject(“Wscript.Shell”)

    ‘判断setting是否配置关联

    if rrelate=”y” or rrelate=”Y” then

     if objFS.fileExists(path&”check_tcp.exe”)=Flase then

     objshell.popup “check_tcp.exe does not exist,can not check relate”,10,”Usage”

     objNF1.writeline “Usage: “&now&” check_tcp.exe does not exist,can not check stauts”

     rrelate=0

     else

     relate_ip=GetFile(settingfile,”relate_ip=”)

     relate_port=GetFile(settingfile,”relate_port=”)

     r_ip=right(relate_ip,len(relate_ip)-10)

     r_port=right(relate_port,len(relate_port)-12)

     rrelate=1

     ’判断关联服务ip&端口

     if r_ip=”" then

     r_ip=”localhost”

     end if

     if r_port=”" then

     objshell.popup “relate_port does not exist”,10,”Usage”

     rrelate=0

     end if

     end if

    else

     rrelate=0

    end if

    ‘读配置文件结束

    Set objFS = CreateObject (“Scripting.FileSystemObject”)

    Set WshShell = WScript.CreateObject(“WScript.Shell”)

     ’判断进程类型

    ‘program

    if rtype=”p” then

     ’判断程序是否存在

     if objFS.fileExists(rprocess)=Flase then

     objshell.popup “Program “&rprocess &” does not exist”,10,”Usage”

     objNF1.writeline “Usage: “&now&” Program “&rprocess &” does not exist”

     wscript.quit

     end if

    ‘program的守护

    Do While 1

    strComputer = “.”

    Set objWMIService = GetObject _

     (“winmgmts:\\\\” & strComputer & “\\root\\cimv2″)

    Set colItems = objWMIService.ExecQuery _

     (“SELECT * FROM Win32_process”)

     For Each objItem in colItems

     pname=lcase(objItem.Name)

     if objitem.ExecutablePath<>”" and pname=rname then

     wscript.sleep 10000

     exit for

     else

     pname=”No Process”

     end if

     ’进程判断结束

     Next

     ’无关联程序的进程

     if pname=”No Process” and rrelate=0 then

     objNF1.writeline “Info: “&now&” Program “&rprocess &” restart”

     return=WshShell.Run(chr(34)&rprocess&chr(34),1, true)

     ’有关联程序的进程

     elseif pname=”No Process” and rrelate=1 then

     relatestatus = WshShell.run (chr(34)&path&”check_tcp.exe”&chr(34)&” -H “&r_ip&” -p “&r_port&” -w 200 -c 1000″,0,true)

     ’判断关联程序是否运行

     if relatestatus=0 then

     objNF1.writeline “Info: “&now&” Program “&rprocess &” restart”

     return=WshShell.Run(chr(34)&rprocess&chr(34),1, true)

     else

     objshell.popup ” relate ip(“&r_ip&”) port(“&r_port&”) can’t connect,please check it”,10,”Usage”

     objNF1.writeline “Usage: “&now&” relate ip(“&r_ip&”) port(“&r_port&”) can’t connect,please check it”

     wscript.sleep 30000

     end if

     ’关联程序运行判断结束

     end if

     ’关联程序的进程结束

     Loop

    ‘service

    elseif rtype=”s” then

     Do While 1

     ’读取服务状态

     strComputer = “.”

     Set objWMIService = GetObject(“winmgmts:” _

     & “{impersonationLevel=impersonate}!\\\\” & strComputer & “\\root\\cimv2″)

     Set colListOfServices = objWMIService.ExecQuery _

     (“Select * from Win32_Service”)

     For Each objService in colListOfServices

     ’判断服务是否存在

     pname=lcase(objService.name)

     if pname=rprocess then

     pstatus=objService.state

     pmode=objService.startmode

     if pstatus=”running” or pstatus=”Running” then

     pstatus=”running”

     wscript.sleep 10000

     end If

     exit for

     else

     pname=”No Service”

     end if

     Next

     ’判断服务是否停止

     if pname=”No Service” then

     objshell.popup “service: “&rprocess&” does not exist,please check it”,10,”Usage”

     objNF1.writeline “Usage: “&now&” service: “&rprocess&” does not exist,please check it”

     wscript.quit

     end if

     if pstatus=”stopped” or pstatus=”Stopped” then

     ’判断服务模式

     if pmode=”Disabled” or pmode=”disabled” then

     objshell.popup rprocess&” mode is disabled,can’t start it”,30,”Usage”

     objNF1.writeline “Usage: “&now&” “&rprocess&” mode is disabled,can’t start it”

     wscript.quit

     elseif rrelate=0 then

     ’启动服务及其依存关系

     objNF1.writeline “Info: “&now&” Service:”&rprocess&” restart”

     Set colServiceList = objWMIService.ExecQuery (“Select * from Win32_Service where

    Name=’”&rprocess&”‘”)

     For each objService in colServiceList

     errReturn = objService.StartService()

     Next

     Wscript.Sleep 20000

     Set colServiceList = objWMIService.ExecQuery(“Associators of ” _

     & “{Win32_Service.Name=’”&rprocess&”‘} Where ” _

     & “AssocClass=Win32_DependentService ” & “Role=Dependent” )

     For each objService in colServiceList

     objService.StartService()

     Next

     elseif rrelate=1 then

     relatestatus = WshShell.run (chr(34)&path&”check_tcp.exe”&chr(34)&” -H “&r_ip&” -p “&r_port&” -w

    200 -c 1000″,0,true)

     ’判断关联程序是否运行

     if relatestatus=0 then

     objNF1.writeline “Info: “&now&” Service:”&rprocess&” restart”

     Set colServiceList = objWMIService.ExecQuery (“Select * from Win32_Service where

    Name=’”&rprocess&”‘”)

     For each objService in colServiceList

     errReturn = objService.StartService()

     Next

     Wscript.Sleep 20000

     Set colServiceList = objWMIService.ExecQuery(“Associators of ” _

     & “{Win32_Service.Name=’”&rprocess&”‘} Where ” _

     & “AssocClass=Win32_DependentService ” & “Role=Dependent” )

     For each objService in colServiceList

     objService.StartService()

     Next

     else

     objshell.popup ” relate ip(“&r_ip&”) port(“&r_port&”) can’t connect,please check

    it”,10,”Usage”

     objNF1.writeline “Usage: “&now&” relate ip(“&r_ip&”) port(“&r_port&”) can’t connect,please check it”

     wscript.sleep 30000

     end if

     end if

     ’判断服务是否非运行状态

     elseif pstatus=”running” or pstatus=”Running” then

     wscript.sleep 10000

     else

     objshell.popup rprocess&” status error,please check it”,10,”Usage”

     objNF1.writeline “Usage: “&now&” “&rprocess&” status error,please check it”

     end if

     Loop

    else

     objshell.popup “Setting of the type set incorrectly”,10,”Usage”

     objNF1.writeline “Usage: “&now&” setting of the type set incorrectly”

     wscript.quit

    end if

    objNF1.close

    其中的wscript.sleep 的值可以根据实际情况自定义修改,需要注意的是要合理设置该值,如果该值设置的过小会是判断过于频繁,导致守护进程占用服务器系统资源。另外如果关联程序不在同一台服务器上,需考虑网络延时等因素,否则有一定概率因网络原因造成无法正常探测关联程序,从而导致程序无法正常开启。

建议继续学习:

  1. 《部落冲突》的设计    (阅读:3673)
  2. 用python编写Linux守护进程    (阅读:3216)
  3. 手机游戏设计初体验    (阅读:3166)
  4. 游戏多服务器架构的一点想法    (阅读:2915)
  5. 使用JavaScript和Canvas开发游戏    (阅读:2756)
  6. 游戏动作感设计初探    (阅读:2528)
  7. 网络游戏的社会化    (阅读:2390)
  8. 如何让玩家相信游戏是公平的    (阅读:2415)
  9. Web设计师能从游戏中学到些什么    (阅读:2291)
  10. 使用JavaScript和Canvas开发游戏(三)    (阅读:2275)
QQ技术交流群:445447336,欢迎加入!
扫一扫订阅我的微信号:IT技术博客大学习
© 2009 - 2024 by blogread.cn 微博:@IT技术博客大学习

京ICP备15002552号-1