def connect(self):
        """Connect to master server.

		After this, you can use `send_packet` and `receive_packet` to communicate
		with the server.
		"""
        if self.is_connected:
            raise network.AlreadyConnected(
                "We are already connected to a server")

        self.log.debug("[CONNECT] to server %s" % (self.server_address))
        try:
            if self.server_address is None:
                # can only construct address now, as it resolves the target and requires internet connection
                self.server_address = enet.Address(
                    *self.server_address_parameters)
            self.server_peer = self.host.connect(self.server_address, 1,
                                                 SERVER_PROTOCOL)
        except (IOError, MemoryError):
            raise network.NetworkException(
                _("Unable to connect to server.") + u" " +
                _("Maybe invalid or irresolvable server address."))

        event = self.host.service(SERVER_TIMEOUT)
        if event.type != enet.EVENT_TYPE_CONNECT:
            self._reset()
            raise network.UnableToConnect(_("Unable to connect to server."))
Exemple #2
0
 def connect(self):
     if self.serverpeer is not None:
         raise network.AlreadyConnected(
             "We are already connected to a server")
     if self.mode is None:
         self.mode = ClientMode.Server
     if self.mode is not ClientMode.Server:
         raise network.NotInServerMode(
             "You can't connect to a server while client is not in server mode"
         )
     self.log.debug("[CONNECT] to server %s" % (self.serveraddress))
     try:
         self.serverpeer = self.host.connect(
             enet.Address(self.serveraddress.host, self.serveraddress.port),
             1, 0)
     except (IOError, MemoryError):
         raise network.NetworkException(
             "Unable to connect to server. Maybe invalid or irresolvable server address."
         )
     self.mode = ClientMode.Server
     event = self.host.service(SERVER_TIMEOUT)
     if event.type != enet.EVENT_TYPE_CONNECT:
         self.reset()
         raise network.UnableToConnect("Unable to connect to server")
     self.log.debug("[CONNECT] done")
Exemple #3
0
	def connect(self):
		if self.serverpeer is not None:
			raise network.AlreadyConnected("We are already connected to a server")
		if self.mode is None:
			self.mode = ClientMode.Server
		if self.mode is not ClientMode.Server:
			raise network.NotInServerMode("You can't connect to a server while client is not in server mode")
		self.log.debug("[CONNECT] to server %s" % (self.serveraddress))
		try:
			self.serverpeer = self.host.connect(enet.Address(self.serveraddress.host, self.serveraddress.port), 1, 0)
		except (IOError, MemoryError):
			raise network.NetworkException("Unable to connect to server. Maybe invalid or irresolvable server address.")
		self.mode = ClientMode.Server
		event = self.host.service(SERVER_TIMEOUT)
		if event.type != enet.EVENT_TYPE_CONNECT:
			self.reset()
			raise network.UnableToConnect("Unable to connect to server")
		self.log.debug("[CONNECT] done")

		if self.localport is None and hasattr(socket, 'fromfd'):
			s = socket.fromfd(self.host.socket.fileno(), socket.AF_INET, socket.SOCK_DGRAM)
			self.localport = s.getsockname()[1]
		if self.localport is not None:
			self.log.debug("[CONNECT] localport=%s" % (self.localport))
			if self.extport is None:
				self.extport = self.upnp_connect(self.localport)
			if self.extport is None:
				self.extport = self.natpmp_connect(self.localport)
		else:
			self.log.debug("[CONNECT] Unable to determine local port");
Exemple #4
0
    def p2p_connect(self):
        if self.mode is not ClientMode.Peer2Peer:
            raise network.NotInServerMode(
                "You can't create the p2p network while client is not in p2p mode"
            )
        start = False
        for player in self.game.players:
            if self.name == player.name:
                start = True
                continue
            if not start:
                continue
            self.log.debug("[P2P CONNECT] to player %s (%s)" %
                           (player.name, player.address))
            player.peer = self.host.connect(
                enet.Address(player.address.host, player.address.port), 1, 0)

        self.log.debug("[P2P CONNECT] Waiting")
        waiting = len(self.game.players) - 1
        while waiting > 0:
            event = self.host.service(CLIENT_TIMEOUT)
            if event.type == enet.EVENT_TYPE_NONE:
                break
            elif event.type == enet.EVENT_TYPE_DISCONNECT:
                break
            elif event.type == enet.EVENT_TYPE_CONNECT:
                player = None
                for _player in self.game.players:
                    if _player.address == event.peer.address:
                        player = _player
                if player is None:
                    event.peer.disconnect()
                    self.flush()
                    event.peer.reset()
                    continue
                if player.peer is None:
                    player.peer = event.peer
                if not player.ready:
                    player.ready = True
                    waiting -= 1
                self.log.debug("[P2P CONNECT] Got connection from %s (%s)" %
                               (player.name, player.address))

        for player in self.game.players:
            if self.name == player.name:
                continue
            if not player.ready:
                self.reset()
                self.log.warning("Unable to connect to %s (%s)" %
                                 (player.name, player.address))
                raise network.UnableToConnect("Unable to connect to %s (%s)" %
                                              (player.name, player.address))
        self.log.debug("[P2P CONNECT] done")
    def connect(self):
        """Connect to master server.

		After this, you can use `send_packet` and `receive_packet` to communicate
		with the server.
		"""
        if self.is_connected:
            raise network.AlreadyConnected(
                "We are already connected to a server")

        self.log.debug("[CONNECT] to server %s" % (self.server_address))
        try:
            self.server_peer = self.host.connect(self.server_address, 1,
                                                 SERVER_PROTOCOL)
        except (IOError, MemoryError):
            raise network.NetworkException(
                "Unable to connect to server."
                "Maybe invalid or irresolvable server address.")

        event = self.host.service(SERVER_TIMEOUT)
        if event.type != enet.EVENT_TYPE_CONNECT:
            self._reset()
            raise network.UnableToConnect("Unable to connect to server")
	def connect(self):
		if self.serverpeer is not None:
			raise network.AlreadyConnected("We are already connected to a server")
		self.log.debug("[CONNECT] to server %s" % (self.serveraddress))
		try:
			self.serverpeer = self.host.connect(enet.Address(self.serveraddress.host,
			                                                 self.serveraddress.port),
			                                    1, SERVER_PROTOCOL)
		except (IOError, MemoryError):
			raise network.NetworkException("Unable to connect to server. Maybe invalid or irresolvable server address.")
		self.mode = ClientMode.Server
		event = self.host.service(SERVER_TIMEOUT)
		if event.type != enet.EVENT_TYPE_CONNECT:
			self.reset()
			raise network.UnableToConnect("Unable to connect to server")
		# wait for session id
		packet = self.recv_packet([packets.server.cmd_session])
		if packet is None:
			raise network.FatalError("No reply from server")
		elif not isinstance(packet[1], packets.server.cmd_session):
			raise network.CommandError("Unexpected packet")
		self.sid = packet[1].sid
		self.capabilities = packet[1].capabilities
		self.log.debug("[CONNECT] done (session=%s)" % (self.sid))