public static void main(String[] args) throws IOException, InterruptedException {
    Serializer.registerClass(SomeObject.class);
    Serializer.registerClass(TestSerializationMessage.class);

    Server server = Network.createServer(5110);
    server.start();

    Client client = Network.connectToServer("localhost", 5110);
    client.start();

    server.addMessageListener(new TestSerialization(), TestSerializationMessage.class);
    client.send(new TestSerializationMessage(true));

    Thread.sleep(10000);
  }
  @Override
  public void simpleInitApp() {
    try {
      server =
          Network.createServer(
              Heraldry.VERSION, Heraldry.PROTOCOL_VERSION,
              Heraldry.DEFAULT_PORT_TCP, Heraldry.DEFAULT_PORT_UDP);
      server.start();
    } catch (IOException e) {
      Logger.getLogger(HeraldryServer.class.getName())
          .log(Level.SEVERE, "Cannot start server: {0}", e);
      return;
    }
    bulletState = new BulletAppState();
    getStateManager().attach(bulletState);
    bulletState.getPhysicsSpace().setAccuracy(Heraldry.PHYSICS_FPS);
    syncManager = new ServerPhysicsSyncManager(app, server);
    syncManager.setSyncFrequency(Heraldry.NETWORK_SYNC_FREQUENCY);
    syncManager.setMessageTypes();
    stateManager.attach(syncManager);

    worldManager = new ServerWorldManager(this, rootNode);
    stateManager.attach(worldManager);
    // register world manager with sync manager so that messages can apply their data
    syncManager.addObject(-1, worldManager);
    // create server side game manager
    gameManager = new ServerGameManager();
    stateManager.attach(gameManager);
    listenerManager = new ServerNetListener(this, server, worldManager, gameManager);
  }
Exemple #3
0
  @Override
  public void simpleInitApp() {
    MessageUtils.registerDataClasses();
    MessageUtils.registerMessages();

    Serializer.registerClass(ReplayHeader.class);
    Serializer.registerClass(ReplayCmdData.class);
    Serializer.registerClass(ReplayData.class);

    Globals.assets = getAssetManager();
    Globals.app = this;
    world = new World();
    game = new ServerGame();
    physics = new BulletAppState();
    physics.setThreadingType(BulletAppState.ThreadingType.PARALLEL);
    flyCam.setEnabled(false);

    int port = Settings.get().General().getPort();
    if (port <= 0 || port > 65535) {
      System.out.println("Port must be between 0 and 65535");
      System.exit(1);
    }

    try {
      server = Network.createServer(port, port);
      server.start();
    } catch (IOException ex) {
      System.exit(1);
    }

    receiver = new DefaultReceiver();
    server.addMessageListener(receiver, OneTrueMessage.class);

    sender = new RecordingServerSender(server);
    AbstractBuff.setSender(sender);

    receiver.registerCommandHandler(sender);

    listenerManager = new ServerNetListener(this, server);
    sync = new Sync(this);

    ServerInput input = ServerInput.get();

    receiver.registerCommandHandler(listenerManager);
    receiver.registerCommandHandler(input);
    receiver.registerCommandHandler(sync);

    stateManager.attach(sender);
    stateManager.attach(receiver);
    stateManager.attach(sync);
    stateManager.attach(world);
    stateManager.attach(game);
    stateManager.attach(physics);

    sender.setWorld(world);
    // Accuracy should be > 45 or projectiles might "disappear" before
    // exploding. This is because of FogOfWar
    physics.getPhysicsSpace().setAccuracy(1f / Settings.get().General().getPhysicsTicksPerSecond());
  }
Exemple #4
0
  public ServerTest() throws IOException, InterruptedException {
    DiffClassRegistration.registerClasses();
    Serializer.registerClass(GameStateMessage.class);

    myServer = Network.createServer(6143);
    diffHandler = new ServerDiffHandler<GameStateMessage>(myServer);
    myServer.start();

    myServer.addConnectionListener(
        new ConnectionListener() {

          @Override
          public void connectionRemoved(Server server, HostedConnection conn) {
            System.out.println("Connection closed from " + conn.getAddress());
          }

          @Override
          public void connectionAdded(Server server, HostedConnection conn) {
            System.out.println("New connection from " + conn.getAddress());
          }
        });

    List<Float> or = Arrays.asList(new Float[] {0.5f, 0.6f, 0.7f});

    while (true) {
      if (myServer.hasConnections()) {
        List<Float> newPos =
            new ArrayList<Float>(Arrays.asList(new Float[] {(float) serverTicks, 8.0f, 3.0f}));

        GameStateMessage newMessage = new GameStateMessage("test", newPos, or, (byte) 0);

        /* Dispatch same message to all clients */
        diffHandler.dispatchMessage(myServer, Filters.in(myServer.getConnections()), newMessage);

        // send a message that is old (id=1), see what happens
        // myServer.broadcast(new LabeledMessage((short)1, newMessage));

        /* Check if the connection is lagging badly */
        for (HostedConnection conn : myServer.getConnections()) {
          if (diffHandler.getLag(conn) > 20) {
            System.out.println("Client " + conn.getAddress() + " is lagging badly.");
          }
        }
      }
      try {
        Thread.sleep(1000);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }

      serverTicks++;
    }
  }
  public void Init_server() throws IOException {
    Serializer.registerClass(HelloMessage.class);
    Serializer.registerClass(SMObjectShare.class);
    try {
      Server myServer = Network.createServer(NAME, VERSION, PORT, UDP_PORT);
      SpiderMonkeyObjPlayerServerListener objlistener = new SpiderMonkeyObjPlayerServerListener();
      objlistener.SetApp(this);

      myServer.addMessageListener(new SpiderMonkeyServerListener(), HelloMessage.class);
      myServer.addMessageListener(objlistener, SMObjectShare.class);
      myServer.start();
    } catch (Exception e) {

    }
  }
 /* (non-Javadoc)
  * @see com.jme3.app.SimpleApplication#simpleInitApp()
  */
 @Override
 public void simpleInitApp() {
   try {
     gameServer = Network.createServer(SERVER_NAME, SERVER_VERSION, tcpPort, udpPort);
     gameServer.start();
     gameServer.addMessageListener(
         serverListener, StringMessage.class, ChatMessage.class, PlayerLeavingMessage.class);
     for (ServerRegisterListener rl : registerListeners) {
       rl.registerServerListener(gameServer);
     }
     serverState = ServerState.LOBBY;
     // NetworkManager.getInstance().registerServerListeners();
     // addClientMessageListener(createServer.serverConListener, PlayerConnectingMessage.class,
     // PlayerLeavingMessage.class);
     // gameServer.addConnectionListener(connections);
   } catch (IOException ex) {
     logger.log(Level.SEVERE, ex.getMessage(), ex);
   }
 }