(模块的介绍方法都是先说大体功能,在捡一些细节详细讨论。)

master 类很简单,就3个函数,一个init,设置配置信息,并调用masterapp,然后还有一个循环启动子进程的start函数。

这里只有masterapp函数值得我们关注。

代码如下:

36     defmasterapp(self):

 37         config = json.load(open(self.configpath,'r'))

 38         mastercnf = config.get('master')

 39         rootport = mastercnf.get('rootport')

 40         webport = mastercnf.get('webport')

 41         masterlog = mastercnf.get('log')

 42         self.root = PBRoot()

 43         rootservice = services.Service("rootservice")

 44         self.root.addServiceChannel(rootservice)

 45         self.webroot = vhost.NameVirtualHost()

 46         self.webroot.addHost('0.0.0.0','./')                                                                       

 47         GlobalObject().root = self.root

 48         GlobalObject().webroot = self.webroot

 49         ifmasterlog:

 50             log.addObserver(loogoo(masterlog))#日志处理

 51         log.startLogging(sys.stdout)

 52         import webapp

 53         import rootapp

 54         reactor.listenTCP(webport,DelaySite(self.webroot))

 55         reactor.listenTCP(rootport,BilateralFactory(self.root))

实际上我不喜欢这种编码风格,感觉有点乱,有些过度使用import和python的修饰符。

仔细看,这里首先通过config.json读取配置信息,然后根据配置信息,起一个pb.root,和一个webserver,然后给pb.root 加一个services,这个services类是个非常重要的类,贯穿整个系统。我们下面会详细介绍它。这里还通过import  webapp 和修饰符@xxx的方法来实现给webserver添加stop 和reload 2个child。实现的功能,我前面其实已经是说过。就是在浏览器里面输入http://localhost:9998/stop或者http://localhost:9998/reload来调用对于的类。具体实现的方法是:

webroot = vhost.NameVirtualHost()

webroot.putChild(cls.__name__, cls()) ;

这个vhost.NameVirtualHost().putChild()函数也是twisted的函数,和前面pb.root一样,大家如果等不及我后面的解说可以自己google到twisted网站,上面有详细的doc、samples。

由于看的实在不习惯(可能自己是python、server的新手),所以我就自己按照功能实现改了一下结构,如下,希望大家对比可以更加清晰。(我改动后的所有代码都会抽空上传到github。地址为: https://github.com/chenee如果没有说明我还没来得及上传,在等等,或者直接M我要。)

22classMaster:

 23     def__init__(self, configpath, mainpath):

 24         """

 25         """

 26         self.configpath = configpath

 27         self.mainpath = mainpath

 28

 29         config = json.load(open(self.configpath, 'r'))

 30         mastercnf = config.get('master')

 31         self.rootport = mastercnf.get('rootport')

 32         self.webport = mastercnf.get('webport')

 33         self.masterlog = mastercnf.get('log')

 34

 35     def__startRoot(self):

 36         root = PBRoot("rootservice")

 37         GlobalObject().root = root

 38         reactor.listenTCP(self.rootport,BilateralFactory(root))

 39

 40

 41     def__startWeb(self):

 42         webroot = vhost.NameVirtualHost()

 43         webroot.addHost('0.0.0.0', './')

 44         GlobalObject().webroot = webroot

 45         webapp.initWebChildren()                                                                                    

 46         reactor.listenTCP(self.webport,DelaySite(webroot))

 47

 48

 49     defstartMaster(self):

50

 51         self.__startRoot()

 52         self.__startWeb()

 53

 54         if self.masterlog:

 55             log.addObserver(loogoo(self.masterlog))#日志处理

 56         log.startLogging(sys.stdout)

 57

 58         #reactor.run()

 59

 60     defstartChildren(self):

 61         """

 62         """

 63         print"startchildren ......"

 64         config = json.load(open(self.configpath, 'r'))

 65         sersconf = config.get('servers')

 66         for sername in sersconf.keys():

 67             cmds = 'python %s %s %s' % (self.mainpath,sername, self.configpath)

 68             subprocess.Popen(cmds, shell=True)

 69         reactor.run() 

我把原先通过addServiceChannel()添加services的过程放到PBRoot类的__init__里面了,这样改动也适合后面其它模块,反正root逻辑上肯定是需要一个services的。而且这个services就是普通services。(后面还会提到一些services的子类)

另外,把原先通过import webapp 加用修饰类实现的putChild()功能,直接写到一个注册函数里面。

45   webapp.initWebChildren()                                                                                     

addToWebRoot(stop)

addToWebRoot(reloadmodule)

改动以后的功能和原先一模一样,改动后的代码对我等新手来说可以清晰的看到master模块的结构

OK,下面我们来看刚才提到的services。客户端所有的命令最终都是通过services的

callTarget(self,targetKey,*args,**kw) 函数来分发。

比如client端发一条编号为01的命令,或者一条“login”命令,server端到底执行什么处理函数,就是通过services来实现的,具体实现实际上就是在services类里面通过

self._targets= {} # Keeps track of targets internally

这个字典来保存命令ID/名称 和具体命令实现函数的对应关系。

注册、和注销这个对应关系的函数为services类的:mapTarget() 、unMapTarget()。

每个模块(master,gate,net。。。)都有对应的services,但是可能不止一个。

模块之间提供服务,也是通过实现一个services实例,并注册一批相应处理函数来实现的。

OK,到这里master基本介绍完毕。

由于master的webserver功能比较简单,而且和系统的其它模块基本无关。大家可以通过twisted官网的DOC和sample来了解,我就不赘述了。

API:

http://twistedmatrix.com/documents/10.2.0/api/twisted.web.vhost.NameVirtualHost.html

Twisted Web In 60 Seconds:

https://twistedmatrix.com/documents/current/web/howto/web-in-60/index.html

下篇文章我尽力介绍twisted的PB(Perspective Broker,透明代理)

游戏服务器学习笔记 4———— master 模块介绍的更多相关文章

  1. 游戏服务器学习笔记 5———— twisted Perspective Broker 透明代理

    实际上这章压根不需要我来说,twisted官网的Doc里面有专门介绍的章节.写的非常详细. http://twistedmatrix.com/documents/current/core/howto/ ...

  2. 游戏服务器学习笔记 3———— firefly 的代码结构,逻辑

    注:以下所有代码都是拿暗黑来举例,由于本人能力有限很多地方还没有看透彻,所以建议大家只是参考.有不对的地方非常欢迎指正. 一.结构     系统启动命令是,python statmaster.py,启 ...

  3. PYQT5学习笔记之各模块介绍

    Qtwidgets模块包含创造经典桌面风格的用户界面提供了一套UI元素的类 Qtwidegts下还有以下常用对象,所以一般使用Qtwidegts时会使用面向对象式编程 QApplication: ap ...

  4. Python3学习笔记(urllib模块的使用)转http://www.cnblogs.com/Lands-ljk/p/5447127.html

    Python3学习笔记(urllib模块的使用)   1.基本方法 urllib.request.urlopen(url, data=None, [timeout, ]*, cafile=None,  ...

  5. 【Visual C++】游戏编程学习笔记之四:透明动画实现

    本系列文章由@二货梦想家张程 所写,转载请注明出处. 本文章链接:http://blog.csdn.net/terence1212/article/details/44224963 作者:ZeeCod ...

  6. 【Visual C++】游戏编程学习笔记之八:鼠标输入消息(小demo)

     本系列文章由@二货梦想家张程 所写,转载请注明出处. 作者:ZeeCoder  微博链接:http://weibo.com/zc463717263 我的邮箱:michealfloyd@126.c ...

  7. 【Visual C++】游戏编程学习笔记之七:键盘输入消息

     本系列文章由@二货梦想家张程 所写,转载请注明出处. 作者:ZeeCoder  微博链接:http://weibo.com/zc463717263 我的邮箱:michealfloyd@126.c ...

  8. DirectX 11游戏编程学习笔记之6: 第5章The Rendering Pipeline(渲染管线)

            本文由哈利_蜘蛛侠原创,转载请注明出处.有问题欢迎联系2024958085@qq.com         注:我给的电子版是700多页,而实体书是800多页,所以我在提到相关概念的时候 ...

  9. DirectX 11游戏编程学习笔记之8: 第6章Drawing in Direct3D(在Direct3D中绘制)(习题解答)

            本文由哈利_蜘蛛侠原创,转载请注明出处.有问题欢迎联系2024958085@qq.com         注:我给的电子版是700多页,而实体书是800多页,所以我在提到相关概念的时候 ...

随机推荐

  1. C# 在EF中直接运行SQL命令

    相信不少使用EF的同志们已经知道如何在EF中运行SQL命令了.我在这里简单总结下,希望对大家学习EF有所帮助! 在 EF第一个版本(.NET 3.5 SP1)中,我们只能通过将ObjectContex ...

  2. Linux 系统强制踢掉登录用户并禁止用户再次登录系统

    标注:创建一个test测试用户,test用户使用Xshel工具ssh远程登录linux操作系统. 强制踢掉登录用户方法一: [root@cloucentos6 ~]# w               ...

  3. Redis 缓存 + Spring 的集成示例(转载)

    1. 依赖包安装 pom.xml 加入: <dependency> <groupId>org.springframework.data</groupId> < ...

  4. tp5的学习

    1.安装,官网下载 2.访问配置:http://localhost/App/public/ 3.入口文件,项目目录/public // 定义应用目录 define('APP_PATH', __DIR_ ...

  5. 写一个方法,用一个for循环打印九九乘法表

    public class MultiplicationTable { /**  * @description 写一个方法,用一个for循环打印九九乘法表   * @author  wangkun  * ...

  6. 基于struts2的ajaxfileupload异步上传插件的使用

    服务器端采用struts2来处理文件上传. 所需环境: jquery.js ajaxfileupload.js struts2所依赖的jar包 及struts2-json-plugin-2.1.8.1 ...

  7. Libertarian

    Libertarians as the real god-son has the consistent faith of humanity freedom. The super libertarian ...

  8. semi-global matching 算法总结

    semi-global matching(缩写SGM)是一种用于计算双目视觉中disparity的半全局匹配算法.在OpenCV中的实现为semi-global block matching(SGBM ...

  9. .NET 获取Get方式URL中的参数键值

    在Web开发中,我们常常会涉及到需要获取Get方式URL中的参数键值的情况,这里简单介绍三种方法: 第一种:常用的做法有使用JavaScript获取location.href后用正则表达式匹配获取此U ...

  10. Hessian资料

    introduction http://www.cnblogs.com/hzmark/archive/2012/11/27/Hessian.html 超时时间设置 http://www.tuicool ...