public static ClientBridge createClientConnection(
      String ip, int port, SeedValidator validator, Class... registeredClasses) {

    if (AbstractServer.getInstance() == null) {
      try {
        throw new Exception("Cannot create a client connection if AbstractServer is null.");
      } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }

    ClientBridge bridge = ClientBridge.create(validator);
    Client client = new Client(8192, 8192);

    NetworkManager networkManager =
        new NetworkManager(ConnectionType.CLIENT, ClientBridge.getInstance());
    client.addListener(new NetworkListener(validator, networkManager));
    ClientConnectionProvider provider =
        new ClientConnectionProvider(client, 1000, ip, port, registeredClasses);
    client.start();
    provider.connect(); // client.connect()

    AbstractServer.getInstance().setBridge(bridge);
    return bridge;
  }
Ejemplo n.º 2
0
 private void startNetworkClient() {
   client.start();
   client.addListener(new NetworkListener(this));
   try {
     client.connect(5000, ipAddress, PORT1, PORT2);
   } catch (IOException e) {
     System.out.print(e);
     Gdx.app.debug("Test", e.toString());
     showDCdialog("Can not connect to server!");
   }
 }
  // Connect to the server
  protected Client doInBackground(final String... arg0) {
    client = new Client();
    client.start();

    Network.register(client);

    client.addListener(
        new Listener() {
          public void connected(Connection connection) { // send the player id to the server
            Connect player = new Connect();
            player.id = arg0[0];
            client.sendTCP(player);

            return;
          }

          public void received(
              Connection connection,
              Object
                  object) { // found a game, save the player color and game id and finish the task
            if (object instanceof GameData) {

              GameData data = (GameData) object;
              playerColor = data.playerColor;
              gameId = data.gameId;

              finished = true;

              return;
            }
          }
        });

    try { // attempt to connect to the server
      client.connect(5000, Network.IP, Network.PORT);
    } catch (IOException e) {
      e.printStackTrace();
    }

    // wait untill a game has been made
    while (!finished) {
      try {
        Thread.sleep(1000);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }

    return client;
  }
  public void connet(String host) {

    client = new Client();
    client.start();

    config.register(client);
    networkMessageAdapter.init(client);

    if (sync) {
      queuedListener =
          new WaitingQueuedListener(new NetworkListener(config, networkMessageAdapter));
      client.addListener(queuedListener);
    } else
      client.addListener(
          new Listener.ThreadedListener(new NetworkListener(config, networkMessageAdapter)));
    try {
      client.connect(5000, host, config.getServerTCPPort(), config.getServerUDPPort());
    } catch (IOException e) {
      logger.log(Level.SEVERE, "Can not bind port", e);
      // e.printStackTrace();
    }
  }
Ejemplo n.º 5
0
  public RemoteMinecraft(String address) {
    client = new Client(65536, 65536);
    Kryo kryo = client.getKryo();
    kryo.register(NetCreateWorldRequest.class);
    kryo.register(NetGetBiomeDataRequest.class);
    kryo.register(NetGetBiomeDataResult.class);
    kryo.register(NetBiome.class);
    kryo.register(NetBiome[].class);
    kryo.register(NetInfoRequest.class);
    kryo.register(int[].class);

    client.addListener(
        new Listener() {
          @Override
          public void received(Connection connection, Object object) {
            if (object instanceof NetGetBiomeDataResult) {
              currentResults = (NetGetBiomeDataResult) object;
              // Log.i("Received NetGetBiomeDataResult: " + currentResults);
            } else if (object instanceof NetBiome[]) {
              NetBiome[] biomes = (NetBiome[]) object;
              for (int i = 0; i < biomes.length; i++) {
                if (biomes[i] != null) {
                  new Biome(biomes[i].name, biomes[i].id, biomes[i].color | 0xFF000000, true);
                }
              }
            }
          }
        });

    client.start();
    try {
      client.connect(5000, address, 54580, 54580);
    } catch (IOException e) {
      e.printStackTrace();
    }

    client.sendTCP(new NetInfoRequest());
  }