Python扭曲游戏服务器监控
我对编写游戏服务器有一些疑问,希望有人有实践经验,并能帮助我。
我正在使用 python 和twisted 开发Flash 游戏(MMO 头像游戏)的服务器。这是我第一次使用twisted,也是我第一次编写游戏服务器,我对服务器设计和twisted实现没有什么疑问。
是否有人有关于“游戏服务器情况”中扭曲性能的实践经验或案例研究(互相交谈、与房间中的每个人交谈、行走等 - 每个房间中的头像按 50 个分组。还有其他动作,但这是最常见的)。 twisted 可以处理多少用户? (每台服务器有 10K 用户是真的)
Epoll 反应器?对于 MMO 游戏服务器来说这是一个好的选择吗?
如何构建服务器监控和管理?如果我想断开某些用户的连接,或者在服务器已经运行时采取任何操作?解决方案之一是将来自协议和工厂的数据写入内存缓存服务器,然后在 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的性能吗?有没有更好的解决方案、建议?正如我所说,我在编写游戏服务器(任何类型的服务器)方面是全新的,所以我需要有关组织和设计的帮助。
I have few question about writing game server, I hope that someone have experience from practice, and will help me.
I'm developing server for Flash game (MMO avatar game) using python and twisted. This is first time that I Use twisted and also first time that I write game server, and I have few question about server design, and twisted implementation.
Does anybody have practice experience or some case study about twisted performance in "game server situations" (talk to each other, talk to everyone in room, walking etc. - avatar are grouped by 50 in each room. There are also other actions but this is most common). How many users twisted can handle? (Is real to expect 10K users per server)
Epoll reactor? Is this good choice for MMO game server?
How to build server monitoring and administration? If i want to disconnect some users, or take any action when server is already running? One of solution is to write data from Protocol and Factory in memcache server, then processing and displaying in web interface, but this is one way communication and it's "expensive", I need that information on request, not always.
Is there some good way to build "console" for monitoring and administration? I was searching online but I didn't find any example or text about that, and here is my idea:
Create server with one Factory and two Protocol? One protocol for game, one for administration. (one factory<->protocol listen port 1234, second factory<->protocol listen 1235). I have all information that i need in factory (number of users, number of active rooms, etc), and also admin can easy read that information because they share same factory. But one factory one protocol so I made few modification:
Two factories two protocols, and one factory is passed to another as reference.
In practice something like this:
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)
Is this good solution? Doesn't affect GameFactory performances? Do any have better solution, proposition? As I said I'm totally new in writing game servers (any kind of servers) so i need help about organisation and design.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
twisted.conch.manhole
是一种可能性用于监控/管理。最终您可能需要多个用于不同目的的控制接口。不要害怕在另一个端口上设置一个 Web 服务器,在另一个端口上设置一个 IRC 服务器,也许是一个 SMTP 客户端,以便在一天结束时通过电子邮件向您发送统计信息 - 这就是 Twisted 的全部意义。您主要为连接数量付费,而不是为服务数量付费。尽量不要担心性能,直到您对第一次尝试进行负载测试并确认它太慢为止,您可能会发现当前的设置足以满足您的需要。如果没有,请尝试使用 UDP 进行玩家通信的某些方面(例如位置),并使用消息队列进行玩家聊天。 Second Life 编写了一些流行消息队列实现的概述。
祝你好运。
twisted.conch.manhole
is one possibility for monitoring/administration. Ultimately you'll probably want a number of control interfaces for different purposes. Don't be afraid to setup a web server on another port, an IRC server on another, perhaps an SMTP client to email you statistics at the end of the day - this is the whole point of Twisted. You'll mostly pay for the number of connections, rather than the number of services.Try not to worry about performance until you load test your first attempt and confirm it's too slow, you may find that your current setup is more than fast enough for what you need. If not, try using UDP for certain aspects of player communication (such as location), and a message queue for player chat. Second Life wrote an overview of some popular message queue implementations.
Good luck.