com.badlogic.gdx.net.Socket Java Examples

The following examples show how to use com.badlogic.gdx.net.Socket. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: LoginScreen.java    From riiablo with Apache License 2.0 6 votes vote down vote up
private void process(Socket socket, BNLS packet) throws IOException {
  switch (packet.dataType()) {
    case BNLSData.ConnectionClosed:
      Gdx.app.debug(TAG, "Connection closed :(");
      break;
    case BNLSData.ConnectionAccepted:
      Gdx.app.debug(TAG, "Connection accepted!");
      break;
    case BNLSData.LoginResponse:
      Gdx.app.debug(TAG, "Login successful!");
      LoginResponse response = (LoginResponse) packet.data(new LoginResponse());
      final Account account = new Account(response);
      Gdx.app.postRunnable(new Runnable() {
        @Override
        public void run() {
          Riiablo.client.pushScreen(new SelectCharacterScreen2(account));
        }
      });
      break;
    default:
      Gdx.app.error(TAG, "Unknown packet type: " + packet.dataType());
  }
}
 
Example #2
Source File: MSI.java    From riiablo with Apache License 2.0 6 votes vote down vote up
@Override
public void render() {
  Socket socket = null;
  try {
    Gdx.app.log(TAG, "waiting...");
    socket = server.accept(null);
    Gdx.app.log(TAG, "connection from " + socket.getRemoteAddress());

    buffer.clear();
    buffer.mark();
    ReadableByteChannel in = Channels.newChannel(socket.getInputStream());
    in.read(buffer);
    buffer.limit(buffer.position());
    buffer.reset();

    com.riiablo.net.packet.msi.MSI packet = com.riiablo.net.packet.msi.MSI.getRootAsMSI(buffer);
    Gdx.app.log(TAG, "packet type " + MSIData.name(packet.dataType()));
    process(socket, packet);
  } catch (Throwable t) {
    if (socket != null) socket.dispose();
  }
}
 
Example #3
Source File: Commands.java    From riiablo with Apache License 2.0 6 votes vote down vote up
@Override
public void onExecuted(Command.Instance instance) {
  int port = instance.numArgs() == 1 ? 6114 : NumberUtils.toInt(instance.getArg(1), 6114);
  Socket socket = null;
  try {
    socket = Gdx.net.newClientSocket(Net.Protocol.TCP, instance.getArg(0), port, null);
    final Socket socketRef = socket;
    Gdx.app.postRunnable(new Runnable() {
      @Override
      public void run() {
        Riiablo.client.clearAndSet(new SelectCharacterScreen3(socketRef));
      }
    });
  } catch (Throwable t) {
    Gdx.app.error("Command", t.getMessage(), t);
    if (socket != null) socket.dispose();
  }
}
 
Example #4
Source File: BNLS.java    From riiablo with Apache License 2.0 6 votes vote down vote up
private boolean QueryRealms(Socket socket) throws IOException {
  FlatBufferBuilder builder = new FlatBufferBuilder();

  int[] realms = new int[REALMS.length];
  for (int i = 0; i < REALMS.length; i++) {
    realms[i] = Realm.createRealm(builder, builder.createString(REALMS[i][0]), builder.createString(REALMS[i][1]));
  }
  int realmsVec = QueryRealms.createRealmsVector(builder, realms);

  QueryRealms.startQueryRealms(builder);
  QueryRealms.addRealms(builder, realmsVec);
  int realmId = QueryRealms.endQueryRealms(builder);

  int id = com.riiablo.net.packet.bnls.BNLS.createBNLS(builder, BNLSData.QueryRealms, realmId);

  builder.finish(id);

  ByteBuffer data = builder.dataBuffer();

  OutputStream out = socket.getOutputStream();
  WritableByteChannel channel = Channels.newChannel(out);
  channel.write(data);
  Gdx.app.log(TAG, "returning realms list...");
  return false;
}
 
Example #5
Source File: MCP.java    From riiablo with Apache License 2.0 5 votes vote down vote up
private void process(Socket socket, com.riiablo.net.packet.mcp.MCP packet) throws IOException {
  switch (packet.dataType()) {
    case MCPData.CreateGame:
      CreateGame(socket, packet);
      break;
    case MCPData.JoinGame:
      JoinGame(socket, packet);
      break;
    case MCPData.ListGames:
      ListGames(socket, packet);
      break;
    default:
      Gdx.app.error(TAG, "Unknown packet type: " + packet.dataType());
  }
}
 
Example #6
Source File: ServerSocketMonitor.java    From Cubes with MIT License 5 votes vote down vote up
@Override
public void run() {
  Side.setSide(Side.Server);
  while (running.get()) {
    try {
      Socket accept = serverSocket.accept(Networking.socketHints);
      ServerConnectionInitializer.check(accept);
    } catch (Exception e) {
      if (running.get()) Log.error(e);
    }
  }
  dispose();
}
 
Example #7
Source File: ClientNetworking.java    From Cubes with MIT License 5 votes vote down vote up
public static PingResult ping(ClientNetworkingParameter clientNetworkingParameter) {
  Log.debug("Pinging Host:" + clientNetworkingParameter.host + " Port:" + clientNetworkingParameter.port);
  Socket socket;
  try {
    socket = Gdx.net.newClientSocket(Protocol.TCP, clientNetworkingParameter.host, clientNetworkingParameter.port, socketHints);
    return ClientConnectionInitializer.ping(socket);
  } catch (Exception e) {
    PingResult pingResult = new PingResult();
    pingResult.failure = true;
    pingResult.exception = e;
    return pingResult;
  }
}
 
Example #8
Source File: SocketMonitor.java    From Cubes with MIT License 5 votes vote down vote up
public SocketMonitor(Socket socket, Networking networking, Side side) {
  this.socket = socket;
  this.networking = networking;
  this.side = side;
  this.remoteAddress = socket.getRemoteAddress();
  this.packetIDDatabase = new PacketIDDatabase();
  running = new AtomicBoolean(true);
  socketInput = new SocketInput(this);
  socketOutput = new SocketOutput(this);
  socketInput.start("Socket Input: " + remoteAddress);
  socketOutput.start("Socket Output: " + remoteAddress);
}
 
Example #9
Source File: LobbyScreen.java    From riiablo with Apache License 2.0 5 votes vote down vote up
private void process(Socket socket, MCP packet) throws IOException {
  switch (packet.dataType()) {
    case MCPData.ConnectionClosed:
      Gdx.app.debug(TAG, "Connection closed :(");
      break;
    case MCPData.ConnectionAccepted:
      Gdx.app.debug(TAG, "Connection accepted!");
      break;
    default:
      Gdx.app.error(TAG, "Unknown packet type: " + packet.dataType());
  }
}
 
Example #10
Source File: BNLS.java    From riiablo with Apache License 2.0 5 votes vote down vote up
private boolean ConnectionAccepted(Socket socket) throws IOException {
  Gdx.app.debug(TAG, "Connection accepted!");
  FlatBufferBuilder builder = new FlatBufferBuilder();
  ConnectionAccepted.startConnectionAccepted(builder);
  int connectionAcceptedId = ConnectionAccepted.endConnectionAccepted(builder);
  int id = com.riiablo.net.packet.bnls.BNLS.createBNLS(builder, BNLSData.ConnectionAccepted, connectionAcceptedId);
  builder.finish(id);

  ByteBuffer data = builder.dataBuffer();
  OutputStream out = socket.getOutputStream();
  WritableByteChannel channel = Channels.newChannel(out);
  channel.write(data);
  return false;
}
 
Example #11
Source File: BNLS.java    From riiablo with Apache License 2.0 5 votes vote down vote up
private boolean ConnectionDenied(Socket socket, String reason) throws IOException {
  FlatBufferBuilder builder = new FlatBufferBuilder();
  int reasonOffset = builder.createString(reason);
  int connectionDeniedId = ConnectionClosed.createConnectionClosed(builder, reasonOffset);
  int id = com.riiablo.net.packet.bnls.BNLS.createBNLS(builder, BNLSData.ConnectionClosed, connectionDeniedId);
  builder.finish(id);

  ByteBuffer data = builder.dataBuffer();
  OutputStream out = socket.getOutputStream();
  WritableByteChannel channel = Channels.newChannel(out);
  channel.write(data);
  return true;
}
 
Example #12
Source File: BNLS.java    From riiablo with Apache License 2.0 5 votes vote down vote up
private void process(Socket socket, com.riiablo.net.packet.bnls.BNLS packet) throws IOException {
  switch (packet.dataType()) {
    case BNLSData.QueryRealms:
      QueryRealms(socket);
      break;
    case BNLSData.LoginResponse:
      LoginResponse(socket, packet);
      break;
    default:
      Gdx.app.error(TAG, "Unknown packet type: " + packet.dataType());
  }
}
 
Example #13
Source File: Main.java    From riiablo with Apache License 2.0 5 votes vote down vote up
private boolean ConnectionAccepted(Socket socket) throws IOException {
//    Gdx.app.debug(TAG, "Connection accepted!");
//    FlatBufferBuilder builder = new FlatBufferBuilder();
//    ConnectionAccepted.startConnectionAccepted(builder);
//    int connectionAcceptedId = ConnectionAccepted.endConnectionAccepted(builder);
//    int id = com.riiablo.net.packet.d2gs.D2GS.createD2GS(builder, D2GSData.ConnectionAccepted, connectionAcceptedId);
//    builder.finish(id);
//
//    ByteBuffer data = builder.dataBuffer();
//    OutputStream out = socket.getOutputStream();
//    WritableByteChannel channel = Channels.newChannel(out);
//    channel.write(data);
    return false;
  }
 
Example #14
Source File: Main.java    From riiablo with Apache License 2.0 5 votes vote down vote up
private boolean ConnectionDenied(Socket socket, String reason) throws IOException {
//    FlatBufferBuilder builder = new FlatBufferBuilder();
//    int reasonOffset = builder.createString(reason);
//    int connectionDeniedId = ConnectionClosed.createConnectionClosed(builder, reasonOffset);
//    int id = com.riiablo.net.packet.d2gs.D2GS.createD2GS(builder, D2GSData.ConnectionClosed, connectionDeniedId);
//    builder.finish(id);
//
//    ByteBuffer data = builder.dataBuffer();
//    OutputStream out = socket.getOutputStream();
//    WritableByteChannel channel = Channels.newChannel(out);
//    channel.write(data);
    return true;
  }
 
Example #15
Source File: MSI.java    From riiablo with Apache License 2.0 5 votes vote down vote up
private boolean StartInstance(Socket socket, com.riiablo.net.packet.msi.MSI packet) throws IOException {
  Gdx.app.debug(TAG, "Starting instance...");

  try {
    File outFile = new File("D2GS.tmp");
    ProcessBuilder processBuilder = new ProcessBuilder("java", "-jar", "server/d2gs/build/libs/d2gs-1.0.jar");
    processBuilder.redirectOutput(ProcessBuilder.Redirect.to(outFile));
    processBuilder.redirectError(ProcessBuilder.Redirect.to(outFile));
    Process process = processBuilder.start();
    instances.add(process);
  } catch (Throwable t) {
    Gdx.app.error(TAG, t.getMessage(), t);
  }

  int ip     = 2130706433; // 127.0.0.1
  short port = 6114;

  FlatBufferBuilder builder = new FlatBufferBuilder();
  StartInstance.startStartInstance(builder);
  StartInstance.addResult(builder, Result.SUCCESS);
  StartInstance.addIp(builder, ip);
  StartInstance.addPort(builder, port);
  int startInstanceOffset = StartInstance.endStartInstance(builder);
  int id = com.riiablo.net.packet.msi.MSI.createMSI(builder, MSIData.StartInstance, startInstanceOffset);
  builder.finish(id);

  ByteBuffer data = builder.dataBuffer();
  OutputStream out = socket.getOutputStream();
  WritableByteChannel channel = Channels.newChannel(out);
  channel.write(data);
  Gdx.app.debug(TAG, "Returning instance at " + InetAddress.getByAddress(ByteBuffer.allocate(4).putInt(ip).array()));
  return true;
}
 
Example #16
Source File: MCP.java    From riiablo with Apache License 2.0 5 votes vote down vote up
private boolean ConnectionDenied(Socket socket, String reason) throws IOException {
  FlatBufferBuilder builder = new FlatBufferBuilder();
  int reasonOffset = builder.createString(reason);
  int connectionDeniedId = ConnectionClosed.createConnectionClosed(builder, reasonOffset);
  int id = com.riiablo.net.packet.mcp.MCP.createMCP(builder, MCPData.ConnectionClosed, connectionDeniedId);
  builder.finish(id);

  ByteBuffer data = builder.dataBuffer();
  OutputStream out = socket.getOutputStream();
  WritableByteChannel channel = Channels.newChannel(out);
  channel.write(data);
  return true;
}
 
Example #17
Source File: MSI.java    From riiablo with Apache License 2.0 5 votes vote down vote up
private void process(Socket socket, com.riiablo.net.packet.msi.MSI packet) throws IOException {
  switch (packet.dataType()) {
    case MSIData.StartInstance:
      StartInstance(socket, packet);
      break;
    default:
      Gdx.app.error(TAG, "Unknown packet type: " + packet.dataType());
  }
}
 
Example #18
Source File: MCP.java    From riiablo with Apache License 2.0 5 votes vote down vote up
private boolean ConnectionAccepted(Socket socket) throws IOException {
  Gdx.app.debug(TAG, "Connection accepted!");
  FlatBufferBuilder builder = new FlatBufferBuilder();
  ConnectionAccepted.startConnectionAccepted(builder);
  int connectionAcceptedId = ConnectionAccepted.endConnectionAccepted(builder);
  int id = com.riiablo.net.packet.mcp.MCP.createMCP(builder, MCPData.ConnectionAccepted, connectionAcceptedId);
  builder.finish(id);

  ByteBuffer data = builder.dataBuffer();
  OutputStream out = socket.getOutputStream();
  WritableByteChannel channel = Channels.newChannel(out);
  channel.write(data);
  return false;
}
 
Example #19
Source File: MCP.java    From riiablo with Apache License 2.0 5 votes vote down vote up
private void StartInstance(CreateGame createGame, ResponseListener listener) {
  Gdx.app.debug(TAG, "Requesting game instance for " + createGame);
  FlatBufferBuilder builder = new FlatBufferBuilder();
  StartInstance.startStartInstance(builder);
  int startInstanceOffset = StartInstance.endStartInstance(builder);
  int id = MSI.createMSI(builder, MSIData.StartInstance, startInstanceOffset);
  builder.finish(id);
  ByteBuffer data = builder.dataBuffer();

  Socket socket = null;
  try {
    socket = Gdx.net.newClientSocket(Net.Protocol.TCP, "localhost", com.riiablo.server.mcp.MSI.PORT, null);

    OutputStream out = socket.getOutputStream();
    WritableByteChannel channelOut = Channels.newChannel(out);
    channelOut.write(data);

    buffer.clear();
    buffer.mark();
    InputStream in = socket.getInputStream();
    ReadableByteChannel channelIn = Channels.newChannel(in);
    channelIn.read(buffer);
    buffer.limit(buffer.position());
    buffer.reset();

    MSI packet = MSI.getRootAsMSI(buffer);
    Gdx.app.log(TAG, "packet type " + MCPData.name(packet.dataType()));
    listener.handleResponse(packet);
  } catch (Throwable t) {
    listener.failed(t);
  } finally {
    if (socket != null) socket.dispose();
  }
}
 
Example #20
Source File: LobbyScreen.java    From riiablo with Apache License 2.0 4 votes vote down vote up
MCPConnection(Socket socket) {
  super(MCPConnection.class.getName());
  this.socket = socket;
}
 
Example #21
Source File: IOSMini2DxNet.java    From mini2Dx with Apache License 2.0 4 votes vote down vote up
@Override
public Socket newClientSocket (Protocol protocol, String host, int port, SocketHints hints) {
	return new NetJavaSocketImpl(protocol, host, port, hints);
}
 
Example #22
Source File: ServerNetworking.java    From Cubes with MIT License 4 votes vote down vote up
protected synchronized void accepted(Socket socket) {
  sockets.add(new SocketMonitor(socket, this, Side.Server));
  Log.info("Successfully connected to " + socket.getRemoteAddress());
}
 
Example #23
Source File: MCP.java    From riiablo with Apache License 2.0 4 votes vote down vote up
@Override
public void create() {
  Gdx.app.setLogLevel(Application.LOG_DEBUG);

  final Calendar calendar = Calendar.getInstance();
  DateFormat format = DateFormat.getDateTimeInstance();
  Gdx.app.log(TAG, format.format(calendar.getTime()));

  try {
    InetAddress address = InetAddress.getLocalHost();
    Gdx.app.log(TAG, "IP Address: " + address.getHostAddress() + ":" + PORT);
    Gdx.app.log(TAG, "Host Name: " + address.getHostName());
  } catch (UnknownHostException e) {
    Gdx.app.error(TAG, e.getMessage(), e);
  }

  clientThreads = new ThreadGroup("MCPClients");

  Gdx.app.log(TAG, "Starting server...");
  server = Gdx.net.newServerSocket(Net.Protocol.TCP, PORT, null);
  buffer = BufferUtils.newByteBuffer(4096);
  kill = new AtomicBoolean(false);
  main = new Thread(new Runnable() {
    @Override
    public void run() {
      while (!kill.get()) {
        Gdx.app.log(TAG, "waiting...");
        Socket socket = server.accept(null);
        Gdx.app.log(TAG, "connection from " + socket.getRemoteAddress());
        if (CLIENTS.size() >= MAX_CLIENTS) {
          try {
            ConnectionDenied(socket, "Server is Full");
          } catch (Throwable ignored) {
          } finally {
            socket.dispose();
          }
        } else {
          try {
            ConnectionAccepted(socket);
            new Client(socket).start();
          } catch (Throwable ignored) {
            socket.dispose();
          }
        }
      }

      Gdx.app.log(TAG, "killing child threads...");
      for (Client client : CLIENTS) {
        if (client != null) {
          client.kill.set(true);
        }
      }

      Gdx.app.log(TAG, "killing thread...");
    }
  });
  main.setName("MCP");
  main.start();

  cli = new Thread(new Runnable() {
    @Override
    public void run() {
      BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
      while (!kill.get()) {
        try {
          if (!reader.ready()) continue;
          String in = reader.readLine();
          if (in.equalsIgnoreCase("exit")) {
            Gdx.app.exit();
          } else if (in.equalsIgnoreCase("games")) {
            Gdx.app.log(TAG, "games:");
            for (GameSession session : sessions.values()) {
              Gdx.app.log(TAG, "  " + session);
            }
          }
        } catch (Throwable t) {
          Gdx.app.log(TAG, t.getMessage());
        }
      }
    }
  });
  cli.setName("CLI");
  cli.start();
}
 
Example #24
Source File: SocketMonitor.java    From Cubes with MIT License 4 votes vote down vote up
public Socket getSocket() {
  return socket;
}
 
Example #25
Source File: NetworkedGameScreen.java    From riiablo with Apache License 2.0 4 votes vote down vote up
public NetworkedGameScreen(CharData charData, Socket socket) {
  super(charData, socket);
  this.socket = socket;
}
 
Example #26
Source File: LoginScreen.java    From riiablo with Apache License 2.0 4 votes vote down vote up
Connection(Socket socket) {
  super(Connection.class.getName());
  this.socket = socket;
}
 
Example #27
Source File: Main.java    From riiablo with Apache License 2.0 4 votes vote down vote up
Client(int id, Socket socket) {
  super(clientThreads, generateClientName());
  this.id = id;
  this.socket = socket;
}
 
Example #28
Source File: MCP.java    From riiablo with Apache License 2.0 4 votes vote down vote up
Client(Socket socket) {
  super(clientThreads, generateClientName());
  this.socket = socket;
}
 
Example #29
Source File: BNLS.java    From riiablo with Apache License 2.0 4 votes vote down vote up
Client(Socket socket) {
  super(clientThreads, generateClientName());
  this.socket = socket;
}
 
Example #30
Source File: Main.java    From riiablo with Apache License 2.0 4 votes vote down vote up
@Override
public void create() {
  Gdx.app.setLogLevel(Application.LOG_DEBUG);

  final Calendar calendar = Calendar.getInstance();
  DateFormat format = DateFormat.getDateTimeInstance();
  Gdx.app.log(TAG, format.format(calendar.getTime()));

  try {
    InetAddress address = InetAddress.getLocalHost();
    Gdx.app.log(TAG, "IP Address: " + address.getHostAddress() + ":" + PORT);
    Gdx.app.log(TAG, "Host Name: " + address.getHostName());
  } catch (UnknownHostException e) {
    Gdx.app.error(TAG, e.getMessage(), e);
  }

  clientThreads = new ThreadGroup("D2CSClients");

  Gdx.app.log(TAG, "Starting server...");
  server = Gdx.net.newServerSocket(Net.Protocol.TCP, PORT, null);
  buffer = BufferUtils.newByteBuffer(4096);
  kill = new AtomicBoolean(false);
  main = new Thread(new Runnable() {
    @Override
    public void run() {
      while (!kill.get()) {
        Gdx.app.log(TAG, "waiting...");
        Socket socket = server.accept(null);
        Gdx.app.log(TAG, "connection from " + socket.getRemoteAddress());
        synchronized (clients) {
          if (clients.size >= MAX_CLIENTS) {
            try {
              ConnectionDenied(socket, "Server is Full");
            } catch (Throwable ignored) {
            } finally {
              socket.dispose();
            }
          } else {
            try {
              ConnectionAccepted(socket);
              int id = clients.size;
              Client client = new Client(id, socket);
              clients.add(client);
              client.start();
            } catch (Throwable ignored) {
              socket.dispose();
            }
          }
        }
      }

      Gdx.app.log(TAG, "killing child threads...");
      synchronized (clients) {
        for (Client client : clients) {
          if (client != null) {
            client.kill.set(true);
          }
        }

        clients.clear();
      }

      Gdx.app.log(TAG, "killing thread...");
    }
  });
  main.setName("D2CS");
  main.start();
}