somebodyy
September 21st, 2010, 00:35
This tutorial will allow you to have the most powerful RSPS EVER.
Finished product: Only the registered members can see the link.
This is the same networking used on InnovationX. We have tested it and our server has held 1208 REAL players (because thats the max we ever had online and it DID NOT crash) and 2000 bots.
First add these to your source.
MultiThreadedCoreExecutor
public class MultiThreadedCoreExecutor implements Runnable {
public void run() {
try {
synchronized(Server.s) {
Server.s.playerHandler.process();
}
} catch(Exception e) {
System.out.println("[MULTITHREADED]: Error occured!");
}
}
}
MultiThreader
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
public class MultiThreader {
public static ScheduledExecutorService serverLogicExecutor;
public static ThreadPoolExecutor serverThreadPool;
static {
System.out.println("[MULTITHREADER] Executors starting up...");
setServerLogicExecutor(Executors.newSingleThreadSc heduledExecutor());
setServerThreadPool((ThreadPoolExecutor) Executors.newCachedThreadPool());
System.out.println("[MULTITHREADER] Executors running!");
}
public static void setServerLogicExecutor(ScheduledExecutorService serverLogicExecutor) {
MultiThreader.serverLogicExecutor = serverLogicExecutor;
}
public static ScheduledExecutorService getServerLogicExecutor() {
return serverLogicExecutor;
}
public static void setServerThreadPool(ThreadPoolExecutor serverThreadPool) {
MultiThreader.serverThreadPool = serverThreadPool;
}
public static ThreadPoolExecutor getServerThreadPool() {
return serverThreadPool;
}
}
Network
import java.net.Socket;
import java.net.ServerSocket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Network implements Runnable {
/**
* The number of acceptors the server will have.
* Higher the number more stable server will be.
* Higher the number more memory usage server will use.
*/
public int ACCEPTORS = 300;
public void run() {
try {
System.out.println("[NETWORK]: Starting...");
int counter = 0;
for (int i = 0; i < ACCEPTORS; i ++) {
serverConnectionAcceptorService.submit(new ServerConnectionAcceptRunnable());
counter++;
if(counter >= ACCEPTORS) {
System.out.println("[NETWORK]: Started.");
}
}
listener = new ServerSocket();
listener.bind(new InetSocketAddress(InetAddress.getByName("0.0.0.0"), 43594), 250);
while(true) {
Socket s = listener.accept();
String connectingHost = s.getInetAddress().getHostName();
String connector = connectingHost.replace("/", "");
System.out.println("[MONITOR]: Accepted connection from " + connector);
Server.s.playerHandler.newPlayerClient(s, connector);
}
}
catch(Exception e) {}
}
private static class ServerConnectionAcceptRunnable implements Runnable
{
@SuppressWarnings("static-access")
public void run() {
Socket acceptedSocket = null;
String acceptedSocketHost = null;
while(true) {
synchronized(listener) {
try {
acceptedSocket = listener.accept();
}
catch(Exception e) {
}
}
try {
acceptedSocket.setTcpNoDelay(true);
acceptedSocketHost = acceptedSocket.getInetAddress().toString();
String connector = acceptedSocketHost.replace("/", "");
synchronized(Server.s.playerHandler) {
System.out.println("[MONITOR]: Accepted connection from " + connector);
Server.s.playerHandler.newPlayerClient(acceptedSoc ket, connector);
}
} catch(Exception e) {
}
}
}
}
public static ServerSocket listener = null;
public static ExecutorService serverConnectionAcceptorService = Executors.newFixedThreadPool(10);
}
In class Server, find
while(true)
it may also be
while (true)
Inside that bracket (if there is a try { then look inside that) remove
Server.s.playerHandler.process();
Add this above the method run()
public static MultiThreader MultiThreader = new MultiThreader();
public static MultiThreader getMultiThreaderCore() {
return MultiThreader;
}
In the main(String[] args) method add:
(new Thread(Network())).start();
s.MultiThreader = new MultiThreader();
s.getMultiThreadedCore().getServerLogicExecutor(). scheduleAtFixedRate(new MultiThreadedCoreExecutor(), 0, S_CYCLE_TIME, TimeUnit.MILLISECONDS);
Add this at the top with your other imports.
import java.util.concurrent.TimeUnit;
import java.io.*;
public final int CYCLE_TIME = 600;
public final static int S_CYCLE_TIME = CYCLE_TIME;
^ Add if you don't have already.
That's it. Winterlove with stability like Hyperion (but even better!)
Credits to me (Canownueasy): tgpn1996@hotmail.com
Finished product: Only the registered members can see the link.
This is the same networking used on InnovationX. We have tested it and our server has held 1208 REAL players (because thats the max we ever had online and it DID NOT crash) and 2000 bots.
First add these to your source.
MultiThreadedCoreExecutor
public class MultiThreadedCoreExecutor implements Runnable {
public void run() {
try {
synchronized(Server.s) {
Server.s.playerHandler.process();
}
} catch(Exception e) {
System.out.println("[MULTITHREADED]: Error occured!");
}
}
}
MultiThreader
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
public class MultiThreader {
public static ScheduledExecutorService serverLogicExecutor;
public static ThreadPoolExecutor serverThreadPool;
static {
System.out.println("[MULTITHREADER] Executors starting up...");
setServerLogicExecutor(Executors.newSingleThreadSc heduledExecutor());
setServerThreadPool((ThreadPoolExecutor) Executors.newCachedThreadPool());
System.out.println("[MULTITHREADER] Executors running!");
}
public static void setServerLogicExecutor(ScheduledExecutorService serverLogicExecutor) {
MultiThreader.serverLogicExecutor = serverLogicExecutor;
}
public static ScheduledExecutorService getServerLogicExecutor() {
return serverLogicExecutor;
}
public static void setServerThreadPool(ThreadPoolExecutor serverThreadPool) {
MultiThreader.serverThreadPool = serverThreadPool;
}
public static ThreadPoolExecutor getServerThreadPool() {
return serverThreadPool;
}
}
Network
import java.net.Socket;
import java.net.ServerSocket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Network implements Runnable {
/**
* The number of acceptors the server will have.
* Higher the number more stable server will be.
* Higher the number more memory usage server will use.
*/
public int ACCEPTORS = 300;
public void run() {
try {
System.out.println("[NETWORK]: Starting...");
int counter = 0;
for (int i = 0; i < ACCEPTORS; i ++) {
serverConnectionAcceptorService.submit(new ServerConnectionAcceptRunnable());
counter++;
if(counter >= ACCEPTORS) {
System.out.println("[NETWORK]: Started.");
}
}
listener = new ServerSocket();
listener.bind(new InetSocketAddress(InetAddress.getByName("0.0.0.0"), 43594), 250);
while(true) {
Socket s = listener.accept();
String connectingHost = s.getInetAddress().getHostName();
String connector = connectingHost.replace("/", "");
System.out.println("[MONITOR]: Accepted connection from " + connector);
Server.s.playerHandler.newPlayerClient(s, connector);
}
}
catch(Exception e) {}
}
private static class ServerConnectionAcceptRunnable implements Runnable
{
@SuppressWarnings("static-access")
public void run() {
Socket acceptedSocket = null;
String acceptedSocketHost = null;
while(true) {
synchronized(listener) {
try {
acceptedSocket = listener.accept();
}
catch(Exception e) {
}
}
try {
acceptedSocket.setTcpNoDelay(true);
acceptedSocketHost = acceptedSocket.getInetAddress().toString();
String connector = acceptedSocketHost.replace("/", "");
synchronized(Server.s.playerHandler) {
System.out.println("[MONITOR]: Accepted connection from " + connector);
Server.s.playerHandler.newPlayerClient(acceptedSoc ket, connector);
}
} catch(Exception e) {
}
}
}
}
public static ServerSocket listener = null;
public static ExecutorService serverConnectionAcceptorService = Executors.newFixedThreadPool(10);
}
In class Server, find
while(true)
it may also be
while (true)
Inside that bracket (if there is a try { then look inside that) remove
Server.s.playerHandler.process();
Add this above the method run()
public static MultiThreader MultiThreader = new MultiThreader();
public static MultiThreader getMultiThreaderCore() {
return MultiThreader;
}
In the main(String[] args) method add:
(new Thread(Network())).start();
s.MultiThreader = new MultiThreader();
s.getMultiThreadedCore().getServerLogicExecutor(). scheduleAtFixedRate(new MultiThreadedCoreExecutor(), 0, S_CYCLE_TIME, TimeUnit.MILLISECONDS);
Add this at the top with your other imports.
import java.util.concurrent.TimeUnit;
import java.io.*;
public final int CYCLE_TIME = 600;
public final static int S_CYCLE_TIME = CYCLE_TIME;
^ Add if you don't have already.
That's it. Winterlove with stability like Hyperion (but even better!)
Credits to me (Canownueasy): tgpn1996@hotmail.com