mirror of
https://github.com/evennia/evennia.git
synced 2026-04-01 21:47:17 +02:00
Converted to Twisted from asyncore. Not positive if this is just my local machine, but it seems like this backend is a bit faster.
This commit is contained in:
parent
82f46a2b69
commit
97cf1213e6
8 changed files with 113 additions and 145 deletions
90
server.py
90
server.py
|
|
@ -1,12 +1,15 @@
|
|||
from traceback import format_exc
|
||||
from asyncore import dispatcher
|
||||
from asynchat import async_chat
|
||||
import socket, asyncore, time
|
||||
import time
|
||||
import sys
|
||||
|
||||
from twisted.application import internet, service
|
||||
from twisted.internet import protocol, reactor, defer
|
||||
|
||||
from django.db import models
|
||||
from django.db import connection
|
||||
|
||||
from apps.config.models import CommandAlias
|
||||
import sys
|
||||
from session import SessionProtocol
|
||||
import scheduler
|
||||
import functions_general
|
||||
import session_mgr
|
||||
|
|
@ -15,22 +18,20 @@ import settings
|
|||
import cmdtable
|
||||
import initial_setup
|
||||
|
||||
class Server(dispatcher):
|
||||
"""
|
||||
The main server class from which everything branches.
|
||||
"""
|
||||
def __init__(self):
|
||||
class EvenniaService(service.Service):
|
||||
|
||||
def __init__(self, filename="blah"):
|
||||
self.cmd_alias_list = {}
|
||||
self.game_running = True
|
||||
|
||||
|
||||
# Database-specific startup optimizations.
|
||||
if settings.DATABASE_ENGINE == "sqlite3":
|
||||
self.sqlite3_prep()
|
||||
|
||||
|
||||
# Wipe our temporary flags on all of the objects.
|
||||
cursor = connection.cursor()
|
||||
cursor.execute("UPDATE objects_object SET nosave_flags=''")
|
||||
|
||||
|
||||
print '-'*50
|
||||
# Load command aliases into memory for easy/quick access.
|
||||
self.load_cmd_aliases()
|
||||
|
|
@ -40,21 +41,15 @@ class Server(dispatcher):
|
|||
print ' Game started for the first time, setting defaults.'
|
||||
initial_setup.handle_setup()
|
||||
|
||||
# Start accepting connections.
|
||||
dispatcher.__init__(self)
|
||||
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
self.set_reuse_addr()
|
||||
self.bind(('', int(self.port)))
|
||||
self.listen(100)
|
||||
self.start_time = time.time()
|
||||
|
||||
print ' %s started on port %s.' % (gameconf.get_configvalue('site_name'), self.port,)
|
||||
print '-'*50
|
||||
|
||||
scheduler.start_events()
|
||||
|
||||
"""
|
||||
BEGIN SERVER STARTUP METHODS
|
||||
"""
|
||||
|
||||
def load_cmd_aliases(self):
|
||||
"""
|
||||
Load up our command aliases.
|
||||
|
|
@ -63,17 +58,8 @@ class Server(dispatcher):
|
|||
for alias in alias_list:
|
||||
self.cmd_alias_list[alias.user_input] = alias.equiv_command
|
||||
print ' Command Aliases Loaded: %i' % (len(self.cmd_alias_list),)
|
||||
|
||||
def handle_accept(self):
|
||||
"""
|
||||
What to do when we get a connection.
|
||||
"""
|
||||
conn, addr = self.accept()
|
||||
session = session_mgr.new_session(self, conn, addr)
|
||||
session.game_connect_screen(session)
|
||||
print 'Connection:', str(session)
|
||||
print 'Sessions active:', len(session_mgr.get_session_list())
|
||||
|
||||
pass
|
||||
|
||||
def sqlite3_prep(self):
|
||||
"""
|
||||
Optimize some SQLite stuff at startup since we can't save it to the
|
||||
|
|
@ -84,14 +70,14 @@ class Server(dispatcher):
|
|||
cursor.execute("PRAGMA synchronous=OFF")
|
||||
cursor.execute("PRAGMA count_changes=OFF")
|
||||
cursor.execute("PRAGMA temp_store=2")
|
||||
|
||||
|
||||
"""
|
||||
BEGIN GENERAL METHODS
|
||||
"""
|
||||
"""
|
||||
def shutdown(self, message='The server has been shutdown. Please check back soon.'):
|
||||
functions_general.announce_all(message)
|
||||
self.game_running = False
|
||||
|
||||
session_mgr.disconnect_all_sessions()
|
||||
reactor.callLater(0, reactor.stop)
|
||||
|
||||
def command_list(self):
|
||||
"""
|
||||
|
|
@ -112,32 +98,26 @@ class Server(dispatcher):
|
|||
'events', 'functions_db', 'functions_general', 'functions_comsys',
|
||||
'functions_help', 'gameconf', 'session', 'apps.objects.models',
|
||||
'apps.helpsys.models', 'apps.config.models']
|
||||
|
||||
|
||||
for mod in reload_list:
|
||||
reload(sys.modules[mod])
|
||||
|
||||
session.msg("Modules reloaded.")
|
||||
functions_general.log_infomsg("Modules reloaded by %s." % (session,))
|
||||
|
||||
def getEvenniaServiceFactory(self):
|
||||
f = protocol.ServerFactory()
|
||||
f.protocol = SessionProtocol
|
||||
f.server = self
|
||||
return f
|
||||
|
||||
"""
|
||||
END Server CLASS
|
||||
"""
|
||||
"""
|
||||
|
||||
"""
|
||||
BEGIN MAIN APPLICATION LOGIC
|
||||
"""
|
||||
if __name__ == '__main__':
|
||||
server = Server()
|
||||
|
||||
try:
|
||||
while server.game_running:
|
||||
asyncore.loop(timeout=5, count=1)
|
||||
scheduler.heartbeat()
|
||||
|
||||
except KeyboardInterrupt:
|
||||
server.shutdown()
|
||||
print '--> Server killed by keystroke.'
|
||||
application = service.Application('Evennia')
|
||||
mud_service = EvenniaService('Evennia Server')
|
||||
|
||||
except:
|
||||
server.shutdown(message="The server has encountered a fatal error and has been shut down. Please check back soon.")
|
||||
functions_general.log_errmsg("Untrapped error: %s" %
|
||||
(format_exc()))
|
||||
# Sheet sheet, fire ze missiles!
|
||||
serviceCollection = service.IServiceCollection(application)
|
||||
internet.TCPServer(4000, mud_service.getEvenniaServiceFactory()).setServiceParent(serviceCollection)
|
||||
Loading…
Add table
Add a link
Reference in a new issue