【发布时间】:2011-09-23 10:37:41
【问题描述】:
我对写游戏服务器有一些疑问,希望有人有实践经验,可以帮助我。
我正在使用 python 和 twisted 为 Flash 游戏(MMO 头像游戏)开发服务器。这是我第一次使用twisted,也是我第一次写游戏服务器,关于服务器设计和twisted实现我几乎没有疑问。
有没有人有关于“游戏服务器情况”中扭曲性能的实践经验或案例研究(互相交谈、与房间里的每个人交谈、走路等 - 每个房间中的头像按 50 分组。那里也是其他动作,但这是最常见的)。扭曲的用户可以处理多少? (预计每台服务器有 10K 用户是真的)
Epoll 反应器?这是MMO游戏服务器的好选择吗?
如何建立服务器监控和管理?如果我想断开某些用户的连接,或者在服务器已经运行时采取任何措施?一种解决方案是在 memcache 服务器中从 Protocol 和 Factory 写入数据,然后在 Web 界面中处理和显示,但这是一种通信方式,而且它“昂贵”,我需要根据请求提供的信息,但并非总是如此。 有没有什么好的方法来构建用于监控和管理的“控制台”?我在网上搜索,但没有找到任何示例或文字,这是我的想法:
用一个工厂和两个协议创建服务器?一种游戏协议,一种管理协议。 (一个工厂协议监听端口 1234,第二个工厂协议监听 1235)。我拥有工厂所需的所有信息(用户数量、活动房间数量等),管理员也可以轻松阅读这些信息,因为他们共享同一个工厂。但是一厂一协议,所以我做了一些修改:
两个工厂两个协议,一个工厂作为参考传递给另一个工厂。 在实践中是这样的:
from twisted.internet.protocol import Factory, Protocol
from twisted.protocols import basic
from twisted.internet import reactor
from twisted.application import service, internet
class Game(Protocol):
def connectionMade(self):
self.factory.users.append(self)
def dataReceiver(self, data):
for user in self.factory.users:
user.transport.write(data+"\n")
def connectionLost(self, why):
self.transport.write("You are off: {0}".format(why))
self.factory.users.remove(self)
class GameFactory(Factory):
users = []
protocol = Game
class Admin(basic.LineReceiver):
def lineReceived(self, line):
if line == 'stats':
self.transport.write("{0} users online\n".format(self.factory.stats()))
if line[0:4] == 'kill':
self.factory.kill(int(line[5:6]))
def connectionMade(self):
self.transport.write("hello fanta\n")
class AdminFactory(Factory):
protocol = Admin
def __init__(self, GameFactory):
self.GameFactory = GameFactory
def stats(self):
return len(self.GameFactory.users)
def kill(self, id):
self.GameFactory.users[id].connectionLost('die')
application = service.Application("game")
gf = GameFactory()
internet.TCPServer(1234, gf).setServiceParent(application)
internet.TCPServer(1235, AdminFactory(gf)).setServiceParent(application)
这是一个好的解决方案吗?不影响 GameFactory 性能?有没有更好的解决方案,建议?正如我所说,我是编写游戏服务器(任何类型的服务器)的新手,所以我需要有关组织和设计方面的帮助。
【问题讨论】:
标签: python monitoring twisted