本文整理汇总了Python中horizons.network.networkinterface.NetworkInterface类的典型用法代码示例。如果您正苦于以下问题:Python NetworkInterface类的具体用法?Python NetworkInterface怎么用?Python NetworkInterface使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了NetworkInterface类的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _on_NetworkPort_changed
def _on_NetworkPort_changed(self, old, new):
"""Sets a new value for client network port"""
# port is saved as string due to pychan limitations
try:
# 0 is not a valid port, but a valid value here (used for default)
parse_port(new)
except ValueError:
headline = _("Invalid network port")
descr = _("The port you specified is not valid. It must be a number between 1 and 65535.")
advice = _("Please check the port you entered and make sure it is in the specified range.")
self._windows.open_error_popup(headline, descr, advice)
# reset value and reshow settings dlg
self._settings.set(SETTINGS.UH_MODULE, "NetworkPort", u"0")
else:
# port is valid
try:
if NetworkInterface() is None:
NetworkInterface.create_instance()
NetworkInterface().network_data_changed()
except Exception as e:
headline = _("Failed to apply new network settings.")
descr = _("Network features could not be initialized with the current configuration.")
advice = _("Check the settings you specified in the network section.")
if 0 < parse_port(new) < 1024:
# i18n This is advice for players seeing a network error with the current config
advice += u" " + _(
"Low port numbers sometimes require special access privileges, try 0 or a number greater than 1024."
)
details = unicode(e)
self._windows.open_error_popup(headline, descr, advice, details)
示例2: set_network_port
def set_network_port(self, port):
"""Sets a new value for client network port"""
# port is saved as string due to pychan limitations
try:
# 0 is not a valid port, but a valid value here (used for default)
parse_port(port, allow_zero=True)
except ValueError:
headline = _("Invalid network port")
descr = _("The port you specified is not valid. It must be a number between 1 and 65535.")
advice = _("Please check the port you entered and make sure it is in the specified range.")
horizons.main._modules.gui.show_error_popup(headline, descr, advice)
# reset value and reshow settings dlg
self.engine.set_uh_setting("NetworkPort", u"0")
ExtScheduler().add_new_object(self._setting.onOptionsPress, self.engine, 0)
else:
# port is valid
try:
if NetworkInterface() is None:
NetworkInterface.create_instance()
NetworkInterface().network_data_changed(connect=False)
except Exception as e:
headline = _(u"Failed to apply new network settings.")
descr = _("Network features could not be initialized with the current configuration.")
advice = _("Check the settings you specified in the network section.")
if 0 < parse_port(port, allow_zero=True) < 1024:
# i18n This is advice for players seeing a network error with the current config
advice += u" " + _(
"Low port numbers sometimes require special access privileges, try 0 or a number greater than 1024."
)
details = unicode(e)
horizons.main._modules.gui.show_error_popup(headline, descr, advice, details)
ExtScheduler().add_new_object(self._setting.onOptionsPress, self.engine, 0)
示例3: show_multi
def show_multi(self):
"""Shows main multiplayer menu"""
if enet == None:
headline = _(u"Unable to find pyenet")
descr = _(
u'The multiplayer feature requires the library "pyenet", which couldn\'t be found on your system.'
)
advice = (
_(u"Linux users: Try to install pyenet through your package manager.")
+ "\n"
+ _(u"Windows users: There is currently no reasonable support for Windows.")
)
self.show_error_popup(headline, descr, advice)
return
if NetworkInterface() is None:
try:
NetworkInterface.create_instance()
except RuntimeError as e:
headline = _(u"Failed to initialize networking.")
descr = _("Network features could not be initialized with the current configuration.")
advice = _("Check the settings you specified in the network section.")
self.show_error_popup(headline, descr, advice, unicode(e))
return
if not NetworkInterface().isconnected():
connected = self.__connect_to_server()
if not connected:
return
if NetworkInterface().isjoined():
if not NetworkInterface().leavegame():
return
event_map = {
"cancel": self.__cancel,
"join": self.__join_game,
"create": self.__show_create_game,
"load": self.__show_load_game,
"refresh": self.__refresh,
}
# store old name and color
self.__apply_new_nickname()
# reload because changing modes (not yet implemented) will need it
self.widgets.reload("multiplayermenu")
self._switch_current_widget("multiplayermenu", center=True, event_map=event_map, hide_old=True)
refresh_worked = self.__refresh()
if not refresh_worked:
self.show_main()
return
self.current.findChild(name="gamelist").capture(self.__update_game_details)
self.current.findChild(name="showonlyownversion").capture(self.__show_only_own_version_toggle)
self.current.playerdata = PlayerDataSelection(self.current, self.widgets)
self.current.show()
self.on_escape = event_map["cancel"]
示例4: show_multi
def show_multi(self):
"""Shows main multiplayer menu"""
if enet is None:
headline = _(u"Unable to find pyenet")
descr = _(u'The multiplayer feature requires the library "pyenet", '
u"which could not be found on your system.")
advice = _(u"Linux users: Try to install pyenet through your package manager.")
self.mainmenu.show_error_popup(headline, descr, advice)
return
if NetworkInterface() is None:
try:
NetworkInterface.create_instance()
except RuntimeError as e:
headline = _(u"Failed to initialize networking.")
descr = _("Network features could not be initialized with the current configuration.")
advice = _("Check the settings you specified in the network section.")
self.mainmenu.show_error_popup(headline, descr, advice, unicode(e))
return
if not NetworkInterface().is_connected:
connected = self.__connect_to_server()
if not connected:
return
if NetworkInterface().is_joined:
if not NetworkInterface().leavegame():
return
event_map = {
'cancel' : self.__cancel,
'join' : self.__join_game,
'create' : self.__show_create_game,
'load' : self.__show_load_game,
'refresh' : Callback(self.__refresh, play_sound=True)
}
# store old name and color
self.__apply_new_nickname()
self.__apply_new_color()
# reload because changing modes (not yet implemented) will need it
self.widgets.reload('multiplayermenu')
self.mainmenu._switch_current_widget('multiplayermenu', center=True, event_map=event_map, hide_old=True)
self.current = self.mainmenu.current
refresh_worked = self.__refresh()
if not refresh_worked:
self.mainmenu.show_main()
return
self.current.findChild(name='gamelist').capture(self.__update_game_details)
self.current.findChild(name='showonlyownversion').capture(self.__show_only_own_version_toggle)
self.current.playerdata = PlayerDataSelection(self.current, self.widgets)
self.current.show()
self.mainmenu.on_escape = event_map['cancel']
示例5: _check_connection
def _check_connection(self):
"""
Check if all dependencies for multiplayer games are met and we can connect to
the master server. If any dependency is not met, the window is closed.
"""
# It is important to close this window before showing the error popup.
# Otherwise closing the popup will trigger `show` again, a new attempt
# to connect is made, which ends up in an endless loop.
if enet is None:
self._windows.close()
headline = T("Unable to find pyenet")
descr = T('The multiplayer feature requires the library "pyenet", '
"which could not be found on your system.")
advice = T("For instructions on installing pyenet see:"
"https://github.com/unknown-horizons/unknown-horizons/wiki/Installing-PyEnet")
self._windows.open_error_popup(headline, descr, advice)
return False
if NetworkInterface() is None:
try:
NetworkInterface.create_instance()
except RuntimeError as e:
self._windows.close()
headline = T("Failed to initialize networking.")
descr = T("Network features could not be initialized with the current configuration.")
advice = T("Check the settings you specified in the network section.")
self._windows.open_error_popup(headline, descr, advice, str(e))
return False
if not NetworkInterface().is_connected:
try:
NetworkInterface().connect()
except TypeError as terr:
self._windows.close()
headline = T("Pyenet type error")
descr = T("You are probably using an incompatible pyenet installation")
advice = T("For instructions on properly installing pyenet see: "
"https://github.com/unknown-horizons/unknown-horizons/wiki/Installing-PyEnet")
self._windows.open_error_popup(headline, descr, advice, str(terr))
except Exception as err:
self._windows.close()
headline = T("Fatal Network Error")
descr = T("Could not connect to master server.")
advice = T("Please check your Internet connection. If it is fine, "
"it means our master server is temporarily down.")
self._windows.open_error_popup(headline, descr, advice, str(err))
return False
if NetworkInterface().is_joined:
if not NetworkInterface().leavegame():
self._windows.close()
return False
return True
示例6: _check_connection
def _check_connection(self):
"""
Check if all dependencies for multiplayer games are met and we can connect to
the master server. If any dependency is not met, the window is closed.
"""
# It is important to close this window before showing the error popup.
# Otherwise closing the popup will trigger `show` again, a new attempt
# to connect is made, which ends up in an endless loop.
if enet is None:
self._windows.close()
headline = _("Unable to find pyenet")
descr = _('The multiplayer feature requires the library "pyenet", '
"which could not be found on your system.")
advice = _("Linux users: Try to install pyenet through your package manager.")
self._windows.show_error_popup(headline, descr, advice)
return False
if NetworkInterface() is None:
try:
NetworkInterface.create_instance()
except RuntimeError as e:
self._windows.close()
headline = _("Failed to initialize networking.")
descr = _("Network features could not be initialized with the current configuration.")
advice = _("Check the settings you specified in the network section.")
self._windows.show_error_popup(headline, descr, advice, unicode(e))
return False
if not NetworkInterface().is_connected:
try:
NetworkInterface().connect()
except Exception as err:
self._windows.close()
headline = _("Fatal Network Error")
descr = _("Could not connect to master server.")
advice = _("Please check your Internet connection. If it is fine, "
"it means our master server is temporarily down.")
self._windows.show_error_popup(headline, descr, advice, unicode(err))
return False
if NetworkInterface().is_joined:
if not NetworkInterface().leavegame():
self._windows.close()
return False
return True
示例7: _check_connection
def _check_connection(self):
"""
Check if all dependencies for multiplayer games are met and we can connect to
the master server.
"""
if enet is None:
headline = _(u"Unable to find pyenet")
descr = _(u'The multiplayer feature requires the library "pyenet", '
u"which could not be found on your system.")
advice = _(u"Linux users: Try to install pyenet through your package manager.")
self._mainmenu.show_error_popup(headline, descr, advice)
return False
if NetworkInterface() is None:
try:
NetworkInterface.create_instance()
except RuntimeError as e:
headline = _(u"Failed to initialize networking.")
descr = _("Network features could not be initialized with the current configuration.")
advice = _("Check the settings you specified in the network section.")
self._mainmenu.show_error_popup(headline, descr, advice, unicode(e))
return False
if not NetworkInterface().is_connected:
try:
NetworkInterface().connect()
except Exception as err:
headline = _(u"Fatal Network Error")
descr = _(u"Could not connect to master server.")
advice = _(u"Please check your Internet connection. If it is fine, "
u"it means our master server is temporarily down.")
self._mainmenu.show_error_popup(headline, descr, advice, unicode(err))
return False
if NetworkInterface().is_joined:
if not NetworkInterface().leavegame():
return False
return True
示例8: start
def start(command_line_arguments):
"""Starts the horizons.
@param command_line_arguments: options object from optparse.OptionParser. see run_uh.py.
"""
global fife, db, debug, preloading
# NOTE: globals are designwise the same thing as singletons. they don't look pretty.
# here, we only have globals that are either trivial, or only one instance may ever exist.
from engine import Fife
# handle commandline globals
debug = command_line_arguments.debug
if command_line_arguments.restore_settings:
# just delete the file, Settings ctor will create a new one
os.remove( PATHS.USER_CONFIG_FILE )
if command_line_arguments.mp_master:
try:
mpieces = command_line_arguments.mp_master.partition(':')
NETWORK.SERVER_ADDRESS = mpieces[0]
# only change port if port is specified
if len(mpieces[2]) > 0:
NETWORK.SERVER_PORT = int(mpieces[2])
if NETWORK.SERVER_PORT < 1 or NETWORK.SERVER_PORT > 65535:
raise ValueError
except ValueError:
print _("Error: Invalid syntax in --mp-master commandline option. Port must be a number between 0 and 65535.")
return False
if command_line_arguments.mp_bind:
try:
mpieces = command_line_arguments.mp_bind.partition(':')
NETWORK.CLIENT_ADDRESS = mpieces[0]
NETWORK.CLIENT_PORT = int(mpieces[2])
if NETWORK.CLIENT_PORT < 1 or NETWORK.CLIENT_PORT > 65535:
raise ValueError
except ValueError:
print _("Error: Invalid syntax in --mp-bind commandline option. Port must be a number between 0 and 65535.")
return False
db = _create_db()
# init game parts
fife = Fife()
_init_gettext(fife)
client_id = fife.get_uh_setting("ClientID")
if client_id is None or len(client_id) == 0:
# We need a new client id
client_id = "".join("-" if c in (8, 13, 18, 23) else \
random.choice("0123456789abcdef") for c in xrange(0, 36))
from engine import UH_MODULE
fife.settings.set(UH_MODULE, "ClientID", client_id)
fife.settings.saveSettings()
ExtScheduler.create_instance(fife.pump)
fife.init()
_modules.gui = Gui()
SavegameManager.init()
try:
NetworkInterface.create_instance()
NetworkInterface().add_to_extscheduler()
except RuntimeError, e:
print "Error during network initialization: %s" % (e)
return False
示例9: start
def start(command_line_arguments):
"""Starts the horizons.
@param command_line_arguments: options object from optparse.OptionParser. see run_uh.py.
"""
global fife, db, debug, preloading
# NOTE: globals are designwise the same thing as singletons. they don't look pretty.
# here, we only have globals that are either trivial, or only one instance may ever exist.
from engine import Fife
# handle commandline globals
debug = command_line_arguments.debug
if command_line_arguments.restore_settings:
# just delete the file, Settings ctor will create a new one
os.remove( PATHS.USER_CONFIG_FILE )
if command_line_arguments.mp_master:
try:
mpieces = command_line_arguments.mp_master.partition(':')
NETWORK.SERVER_ADDRESS = mpieces[0]
# only change port if port is specified
if len(mpieces[2]) > 0:
NETWORK.SERVER_PORT = parse_port(mpieces[2], allow_zero=True)
except ValueError:
print _("Error: Invalid syntax in --mp-master commandline option. Port must be a number between 1 and 65535.")
return False
# init fife before mp_bind is parsed, since it's needed there
fife = Fife()
if command_line_arguments.mp_bind:
try:
mpieces = command_line_arguments.mp_bind.partition(':')
NETWORK.CLIENT_ADDRESS = mpieces[0]
fife.set_uh_setting("NetworkPort", parse_port(mpieces[2], allow_zero=True))
print 'asdf', fife.get_uh_setting("NetworkPort2")
except ValueError:
print _("Error: Invalid syntax in --mp-bind commandline option. Port must be a number between 1 and 65535.")
return False
db = _create_db()
# init game parts
_init_gettext(fife)
client_id = fife.get_uh_setting("ClientID")
if client_id is None or len(client_id) == 0:
# We need a new client id
client_id = "".join("-" if c in (8, 13, 18, 23) else \
random.choice("0123456789abcdef") for c in xrange(0, 36))
from engine import UH_MODULE
fife.settings.set(UH_MODULE, "ClientID", client_id)
fife.settings.saveSettings()
ExtScheduler.create_instance(fife.pump)
fife.init()
_modules.gui = Gui()
SavegameManager.init()
try:
NetworkInterface.create_instance()
except RuntimeError, e:
headline = _(u"Failed to initialize networking.")
descr = _(u"Networking couldn't be initialised with the current configuration.")
advice = _(u"Check the data you entered in the Network section in the settings dialogue.")
_modules.gui.show_error_popup(headline, descr, advice, unicode(e))