예제 #1
0
    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."))
예제 #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")
예제 #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");
예제 #4
0
    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")
예제 #5
0
	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))