Add Network
This commit is contained in:
parent
9505d4e887
commit
a00f214911
15 changed files with 782 additions and 5 deletions
|
@ -1,11 +1,13 @@
|
|||
package mrdev023;
|
||||
|
||||
import mrdev023.gameengine.*;
|
||||
import mrdev023.network.client.*;
|
||||
import mrdev023.network.common.*;
|
||||
import mrdev023.network.packet.*;
|
||||
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
GameEngine.start("2D Game UDP", 800, 800);
|
||||
public static void main(String[] args) throws Exception{
|
||||
MainClient.startClient();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,17 +1,20 @@
|
|||
package mrdev023.gameengine;
|
||||
|
||||
import mrdev023.gameengine.gamestate.main.*;
|
||||
import mrdev023.network.client.*;
|
||||
import mrdev023.network.packet.*;
|
||||
import mrdev023.opengl.*;
|
||||
|
||||
public class GameEngine {
|
||||
|
||||
private static Frame frame;
|
||||
|
||||
private static long current,previous,elapsedInfo,elapsedTicks;
|
||||
private static long current,previous,elapsedInfo,elapsedTicks,elapsedPing;
|
||||
private static int countFPS = 0,countTICKS = 0,FPS,TICKS;
|
||||
private static boolean IsRunning = false;
|
||||
private static GameState state = GameState.MAIN;
|
||||
private static String title;
|
||||
private static long ping = 0;
|
||||
|
||||
public static void start(String title,int width,int height){
|
||||
try{
|
||||
|
@ -36,6 +39,7 @@ public class GameEngine {
|
|||
current = System.nanoTime();
|
||||
elapsedInfo += current - previous;
|
||||
elapsedTicks += current - previous;
|
||||
elapsedPing += current - previous;
|
||||
|
||||
if(frame.isClosedRequested()){
|
||||
IsRunning = false;
|
||||
|
@ -47,6 +51,7 @@ public class GameEngine {
|
|||
state.updateKeyboard();
|
||||
state.updateMouse();
|
||||
state.update();
|
||||
MainClient.client.update();
|
||||
countTICKS++;
|
||||
elapsedTicks -= 1000000000/60;
|
||||
}else{
|
||||
|
@ -59,12 +64,17 @@ public class GameEngine {
|
|||
countFPS++;
|
||||
}
|
||||
|
||||
if(elapsedPing >= 1000000000){
|
||||
MainClient.send(new PingClientPacket(System.currentTimeMillis()));
|
||||
elapsedPing = 0;
|
||||
}
|
||||
|
||||
if(elapsedInfo >= 1000000000){
|
||||
FPS = countFPS;
|
||||
countFPS = 0;
|
||||
TICKS = countTICKS;
|
||||
countTICKS = 0;
|
||||
frame.setTitle(title + " | FPS:" + FPS + " | TICKS:" + TICKS);
|
||||
frame.setTitle(title + " | FPS:" + FPS + " | TICKS:" + TICKS + " | PING:" + ping + "ms" + " | " + MainClient.pseudo + ((MainClient.client.isConnect())?" connected !":" disconnected !"));
|
||||
elapsedInfo -= 1000000000;
|
||||
}
|
||||
}
|
||||
|
@ -74,6 +84,7 @@ public class GameEngine {
|
|||
public static void destroy(){
|
||||
state.destroy();
|
||||
frame.destroy();
|
||||
MainClient.client.destroy();
|
||||
}
|
||||
|
||||
public static Frame getFrame() {
|
||||
|
@ -98,6 +109,9 @@ public class GameEngine {
|
|||
GameEngine.state.init();
|
||||
}
|
||||
|
||||
public static void setPing(long ping){
|
||||
GameEngine.ping = ping;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,38 @@
|
|||
package mrdev023.network.client;
|
||||
import java.net.*;
|
||||
import java.nio.*;
|
||||
|
||||
import javax.swing.*;
|
||||
|
||||
import mrdev023.gameengine.*;
|
||||
import mrdev023.network.common.*;
|
||||
import mrdev023.network.packet.*;
|
||||
import mrdev023.network.packet.main.*;
|
||||
|
||||
public class MainClient {
|
||||
|
||||
public static Client client;
|
||||
public static String pseudo = "";
|
||||
|
||||
public static void startClient() {
|
||||
try {
|
||||
String input = JOptionPane.showInputDialog(null,"Entrer ip:port du serveur");
|
||||
String[] i = input.split(":");
|
||||
String pseudo = JOptionPane.showInputDialog(null,"Pseudo:");
|
||||
client = new Client(InetAddress.getByName(i[0]),Integer.parseInt(i[1]));
|
||||
client.send(new ChangePseudoPacket(pseudo));
|
||||
GameEngine.start("2D Game UDP", 800, 800);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static void send(IPacket packet){
|
||||
try {
|
||||
client.send(packet);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,192 @@
|
|||
package mrdev023.network.common;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
|
||||
import mrdev023.network.packet.main.*;
|
||||
|
||||
public class Client extends Thread{
|
||||
|
||||
private DatagramSocket client;
|
||||
private boolean isRunning = false;
|
||||
private boolean isConnect = false;
|
||||
private InetAddress address;
|
||||
private int port;
|
||||
private int TIME_OUT = 15000;
|
||||
private long current,elapsed,previous;
|
||||
private String pseudo = "Default";
|
||||
private String state = "";
|
||||
private boolean isAdmin = false;
|
||||
|
||||
public Client(InetAddress address,int port) throws SocketException{
|
||||
client = new DatagramSocket();
|
||||
this.address = address;
|
||||
this.port = port;
|
||||
isRunning = true;
|
||||
current = System.currentTimeMillis();
|
||||
this.start();
|
||||
}
|
||||
|
||||
public void update(){
|
||||
previous = current;
|
||||
current = System.currentTimeMillis();
|
||||
elapsed += current - previous;
|
||||
}
|
||||
|
||||
public void setPseudo(String pseudo){
|
||||
this.pseudo = pseudo;
|
||||
}
|
||||
|
||||
public void run(){
|
||||
while(isRunning){
|
||||
up();
|
||||
byte[] data = new byte[DataBuffer.SIZE];
|
||||
DatagramPacket packet = new DatagramPacket(data, DataBuffer.SIZE);
|
||||
try {
|
||||
client.receive(packet);
|
||||
} catch (IOException e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
if(packet.getAddress() != null){
|
||||
isConnect = true;
|
||||
if(is(packet.getAddress(),packet.getPort()) || true){
|
||||
DataBuffer dataBuff = new DataBuffer();
|
||||
dataBuff.setData(data);
|
||||
try {
|
||||
String name = dataBuff.getString();
|
||||
IPacket obj = (IPacket)Class.forName(name).newInstance();
|
||||
if(obj == null)continue;
|
||||
obj.read(dataBuff);
|
||||
obj.manage(this, obj);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
isConnect = false;
|
||||
destroy();
|
||||
}
|
||||
|
||||
public void up(){
|
||||
|
||||
}
|
||||
|
||||
public void destroy(){
|
||||
isRunning = false;
|
||||
isConnect = false;
|
||||
client.close();
|
||||
}
|
||||
|
||||
public boolean isTimeOut(){
|
||||
return elapsed > TIME_OUT;
|
||||
}
|
||||
|
||||
public boolean is(InetAddress address,int port){
|
||||
return this.address.getHostAddress().equals(address.getHostAddress()) && this.port == port;
|
||||
}
|
||||
|
||||
public void send(IPacket packet) throws Exception{
|
||||
DataBuffer data = new DataBuffer();
|
||||
data.put(packet.getClass().getName());
|
||||
packet.write(data);
|
||||
DatagramPacket sendPacket = new DatagramPacket(data.getData(),data.getData().length,address,port);
|
||||
client.send(sendPacket);
|
||||
elapsed = 0;
|
||||
}
|
||||
|
||||
public DatagramSocket getClient() {
|
||||
return client;
|
||||
}
|
||||
|
||||
public void setClient(DatagramSocket client) {
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
public boolean isRunning() {
|
||||
return isRunning;
|
||||
}
|
||||
|
||||
public void setRunning(boolean isRunning) {
|
||||
this.isRunning = isRunning;
|
||||
}
|
||||
|
||||
public boolean isAdmin() {
|
||||
return isAdmin;
|
||||
}
|
||||
|
||||
public void setAdmin(boolean isAdmin) {
|
||||
this.isAdmin = isAdmin;
|
||||
}
|
||||
|
||||
public boolean isConnect() {
|
||||
return isConnect;
|
||||
}
|
||||
|
||||
public void setConnect(boolean isConnect) {
|
||||
this.isConnect = isConnect;
|
||||
}
|
||||
|
||||
public InetAddress getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public void setAddress(InetAddress address) {
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
public int getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
public void setPort(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public int getTIME_OUT() {
|
||||
return TIME_OUT;
|
||||
}
|
||||
|
||||
public void setTIME_OUT(int tIME_OUT) {
|
||||
TIME_OUT = tIME_OUT;
|
||||
}
|
||||
|
||||
public long getCurrent() {
|
||||
return current;
|
||||
}
|
||||
|
||||
public void setCurrent(long current) {
|
||||
this.current = current;
|
||||
}
|
||||
|
||||
public long getElapsed() {
|
||||
return elapsed;
|
||||
}
|
||||
|
||||
public void setElapsed(long elapsed) {
|
||||
this.elapsed = elapsed;
|
||||
}
|
||||
|
||||
public long getPrevious() {
|
||||
return previous;
|
||||
}
|
||||
|
||||
public void setPrevious(long previous) {
|
||||
this.previous = previous;
|
||||
}
|
||||
|
||||
public String getStatee() {
|
||||
return state;
|
||||
}
|
||||
|
||||
public void setState(String state) {
|
||||
this.state = state;
|
||||
}
|
||||
|
||||
public String getPseudo() {
|
||||
return pseudo;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,100 @@
|
|||
package mrdev023.network.common;
|
||||
|
||||
import java.nio.*;
|
||||
|
||||
public class DataBuffer {
|
||||
|
||||
public static final int SIZE = 1024;
|
||||
|
||||
public byte[] data = new byte[SIZE];
|
||||
public int p = 0;
|
||||
public int used = 0;
|
||||
|
||||
public void setData(byte[] data){
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public void put(byte a) throws Exception{
|
||||
if(p >= SIZE)throw new Exception("Overflow !");
|
||||
data[p++] = a;
|
||||
if(p>used)used = p;
|
||||
}
|
||||
|
||||
public void put(short a) throws Exception{
|
||||
put((byte)((a >> 8) & 0xFF));
|
||||
put((byte)((a) & 0xFF));
|
||||
}
|
||||
|
||||
public void put(int a) throws Exception{
|
||||
put((byte)((a >> 24) & 0xFF));
|
||||
put((byte)((a >> 16) & 0xFF));
|
||||
put((byte)((a >> 8) & 0xFF));
|
||||
put((byte)((a) & 0xFF));
|
||||
}
|
||||
|
||||
public void put(long a) throws Exception{
|
||||
put((byte)((a >> 54) & 0xFF));
|
||||
put((byte)((a >> 48) & 0xFF));
|
||||
put((byte)((a >> 40) & 0xFF));
|
||||
put((byte)((a >> 32) & 0xFF));
|
||||
put((byte)((a >> 24) & 0xFF));
|
||||
put((byte)((a >> 16) & 0xFF));
|
||||
put((byte)((a >> 8) & 0xFF));
|
||||
put((byte)((a) & 0xFF));
|
||||
}
|
||||
|
||||
public void put(char a) throws Exception{
|
||||
put((byte)a);
|
||||
}
|
||||
|
||||
public void put(String a) throws Exception{
|
||||
put(a.length());
|
||||
for(int i = 0;i < a.length();i++)put(a.charAt(i));
|
||||
}
|
||||
|
||||
public byte getByte() throws Exception{
|
||||
if(p >= SIZE)throw new Exception("Overflow !");
|
||||
if(p + 1>used)used = p + 1;
|
||||
return data[p++];
|
||||
}
|
||||
|
||||
public short getShort() throws Exception{
|
||||
return ByteBuffer.wrap(new byte[]{getByte(),getByte()}).getShort();
|
||||
}
|
||||
|
||||
public int getInt() throws Exception{
|
||||
return ByteBuffer.wrap(new byte[]{getByte(),getByte(),getByte(),getByte()}).getInt();
|
||||
}
|
||||
|
||||
public long getLong() throws Exception{
|
||||
return ByteBuffer.wrap(new byte[]{getByte(),getByte(),getByte(),getByte(),getByte(),getByte(),getByte(),getByte()}).getLong();
|
||||
}
|
||||
|
||||
public char getChar() throws Exception{
|
||||
return (char)getByte();
|
||||
}
|
||||
|
||||
public String getString() throws Exception{
|
||||
int length = getInt();
|
||||
char[] b = new char[length];
|
||||
for(int i = 0;i < length;i++)b[i] = getChar();
|
||||
return new String(b);
|
||||
}
|
||||
|
||||
public void flip(){
|
||||
p = 0;
|
||||
}
|
||||
|
||||
public int available(){
|
||||
return SIZE - used;
|
||||
}
|
||||
|
||||
public int limit(){
|
||||
return SIZE;
|
||||
}
|
||||
|
||||
public byte[] getData(){
|
||||
return this.data;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
package mrdev023.network.packet;
|
||||
|
||||
import java.net.*;
|
||||
|
||||
import mrdev023.*;
|
||||
import mrdev023.network.client.*;
|
||||
import mrdev023.network.common.*;
|
||||
import mrdev023.network.packet.main.*;
|
||||
|
||||
public class ChangePseudoPacket implements IPacket{
|
||||
|
||||
private String pseudo;
|
||||
|
||||
public ChangePseudoPacket(){}
|
||||
public ChangePseudoPacket(String pseudo){
|
||||
this.pseudo = pseudo;
|
||||
}
|
||||
|
||||
public void write(DataBuffer buff) throws Exception {
|
||||
buff.put(pseudo);
|
||||
}
|
||||
|
||||
public void read(DataBuffer buff) throws Exception {
|
||||
this.pseudo = buff.getString();
|
||||
}
|
||||
|
||||
public void manage(Client client, IPacket packet) throws Exception{
|
||||
MainClient.pseudo = pseudo;
|
||||
}
|
||||
|
||||
public void manage(Client client, IPacket packet, DatagramSocket server) throws Exception {
|
||||
client.setPseudo(pseudo);
|
||||
client.send(packet);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
package mrdev023.network.packet;
|
||||
|
||||
import java.net.*;
|
||||
|
||||
import mrdev023.network.common.*;
|
||||
import mrdev023.network.packet.main.*;
|
||||
|
||||
public class ClientConnect implements IPacket{
|
||||
|
||||
public String pseudo;
|
||||
|
||||
public ClientConnect(){}
|
||||
public ClientConnect(String pseudo){this.pseudo = pseudo;}
|
||||
|
||||
public void write(DataBuffer buff) throws Exception {
|
||||
buff.put(pseudo);
|
||||
}
|
||||
|
||||
public void read(DataBuffer buff) throws Exception {
|
||||
this.pseudo = buff.getString();
|
||||
}
|
||||
|
||||
public void manage(Client client, IPacket packet) throws Exception {
|
||||
|
||||
}
|
||||
|
||||
public void manage(Client client, IPacket packet, DatagramSocket server) throws Exception {
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
package mrdev023.network.packet;
|
||||
|
||||
import java.net.*;
|
||||
|
||||
import mrdev023.network.common.*;
|
||||
import mrdev023.network.packet.main.*;
|
||||
|
||||
public class ClientDisconnect implements IPacket{
|
||||
|
||||
public String pseudo;
|
||||
|
||||
public ClientDisconnect(){}
|
||||
public ClientDisconnect(String pseudo){this.pseudo = pseudo;}
|
||||
|
||||
public void write(DataBuffer buff) throws Exception {
|
||||
buff.put(pseudo);
|
||||
}
|
||||
|
||||
public void read(DataBuffer buff) throws Exception {
|
||||
this.pseudo = buff.getString();
|
||||
}
|
||||
|
||||
public void manage(Client client, IPacket packet) throws Exception {
|
||||
|
||||
}
|
||||
|
||||
public void manage(Client client, IPacket packet, DatagramSocket server) throws Exception {
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,66 @@
|
|||
package mrdev023.network.packet;
|
||||
|
||||
import java.net.*;
|
||||
|
||||
import mrdev023.network.common.*;
|
||||
import mrdev023.network.packet.main.*;
|
||||
import mrdev023.network.server.*;
|
||||
|
||||
public class MessagePacket implements IPacket{
|
||||
|
||||
public String message,pseudo;
|
||||
|
||||
public MessagePacket(){}
|
||||
|
||||
public MessagePacket(String message,String pseudo){this.message = message;this.pseudo = pseudo;}
|
||||
|
||||
public void write(DataBuffer buff) throws Exception {
|
||||
buff.put(message);
|
||||
buff.put(pseudo);
|
||||
}
|
||||
|
||||
public void read(DataBuffer buff) throws Exception {
|
||||
this.message = buff.getString();
|
||||
this.pseudo = buff.getString();
|
||||
}
|
||||
|
||||
public void manage(Client client, IPacket packet) throws Exception {
|
||||
System.out.println(pseudo + ": " + message);
|
||||
}
|
||||
|
||||
public void manage(Client client, IPacket packet, DatagramSocket server) throws Exception {
|
||||
if(message.startsWith("/")){
|
||||
if(message.split(" ")[0].equals("/login") && message.split(" ").length == 2){
|
||||
if(message.split(" ")[1].equals(MainServer.password)){
|
||||
MainServer.sendToClient(client, new MessagePacket("login successful!", "Server"));
|
||||
client.setAdmin(true);
|
||||
}else{
|
||||
MainServer.sendToClient(client, new MessagePacket("password incorrect!", "Server"));
|
||||
}
|
||||
}else{
|
||||
if(client.isAdmin()){
|
||||
String[] cmd = message.split(" ");
|
||||
if(cmd[0].equals("/say") && cmd.length >= 2){
|
||||
MainServer.sendToClients(new MessagePacket(Utils.getStringByStringArray(cmd, 1, cmd.length - 1), "Server"));
|
||||
}else if(cmd[0].equals("/logout")){
|
||||
MainServer.sendToClient(client, new MessagePacket("logout successful!", "Server"));
|
||||
client.setAdmin(false);
|
||||
}else if(cmd[0].equals("/help")){
|
||||
MainServer.sendToClient(client, new MessagePacket("Help", "Server"));
|
||||
MainServer.sendToClient(client, new MessagePacket("/login [password]", "Server"));
|
||||
MainServer.sendToClient(client, new MessagePacket("/logout", "Server"));
|
||||
MainServer.sendToClient(client, new MessagePacket("/say", "Server"));
|
||||
MainServer.sendToClient(client, new MessagePacket("/help", "Server"));
|
||||
}else{
|
||||
MainServer.sendToClient(client, new MessagePacket("No command", "Server"));
|
||||
}
|
||||
}else{
|
||||
MainServer.sendToClient(client, new MessagePacket("You are not Admin!", "Server"));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
else MainServer.sendToClients(packet);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
package mrdev023.network.packet;
|
||||
|
||||
import java.net.*;
|
||||
|
||||
import mrdev023.network.common.*;
|
||||
import mrdev023.network.packet.main.*;
|
||||
|
||||
public class MessageTestPacket implements IPacket{
|
||||
|
||||
private String message;
|
||||
|
||||
public MessageTestPacket(){}
|
||||
|
||||
public MessageTestPacket(String message){
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public void write(DataBuffer buff) throws Exception{
|
||||
buff.put(message);
|
||||
}
|
||||
|
||||
public void read(DataBuffer buff) throws Exception{
|
||||
this.message = buff.getString();
|
||||
}
|
||||
|
||||
public void manage(Client client,IPacket packet)throws Exception{
|
||||
|
||||
}
|
||||
|
||||
public void manage(Client client,IPacket packet,DatagramSocket server)throws Exception{
|
||||
|
||||
}
|
||||
|
||||
public String getMessage(){
|
||||
return this.message;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
package mrdev023.network.packet;
|
||||
|
||||
import java.net.*;
|
||||
|
||||
import javax.swing.*;
|
||||
|
||||
import mrdev023.network.common.*;
|
||||
import mrdev023.network.packet.main.*;
|
||||
|
||||
public class OutOfSocketPacket implements IPacket{
|
||||
|
||||
public void write(DataBuffer buff) throws Exception {
|
||||
|
||||
}
|
||||
|
||||
public void read(DataBuffer buff) throws Exception {
|
||||
|
||||
}
|
||||
|
||||
public void manage(Client client, IPacket packet) throws Exception {
|
||||
JOptionPane.showMessageDialog(null,"OutOfSocket","Error",JOptionPane.ERROR_MESSAGE);
|
||||
client.setRunning(false);
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
public void manage(Client client, IPacket packet, DatagramSocket server) throws Exception {
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
package mrdev023.network.packet;
|
||||
|
||||
import java.net.*;
|
||||
|
||||
import mrdev023.gameengine.*;
|
||||
import mrdev023.network.common.*;
|
||||
import mrdev023.network.packet.main.*;
|
||||
|
||||
public class PingClientPacket implements IPacket{
|
||||
|
||||
public long current;
|
||||
|
||||
public PingClientPacket(){
|
||||
|
||||
}
|
||||
|
||||
public PingClientPacket(long current){
|
||||
this.current = current;
|
||||
}
|
||||
|
||||
|
||||
public void write(DataBuffer buff) throws Exception {
|
||||
buff.put(current);
|
||||
}
|
||||
|
||||
public void read(DataBuffer buff) throws Exception {
|
||||
this.current = buff.getLong();
|
||||
}
|
||||
|
||||
public void manage(Client client, IPacket packet) throws Exception {
|
||||
GameEngine.setPing((System.currentTimeMillis() - current));
|
||||
}
|
||||
|
||||
public void manage(Client client, IPacket packet, DatagramSocket server) throws Exception {
|
||||
client.send(packet);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
package mrdev023.network.packet.main;
|
||||
|
||||
import java.net.*;
|
||||
|
||||
import mrdev023.network.common.*;
|
||||
|
||||
public interface IPacket {
|
||||
|
||||
public void write(DataBuffer buff) throws Exception;
|
||||
public void read(DataBuffer buff) throws Exception;
|
||||
public void manage(Client client,IPacket packet)throws Exception;
|
||||
public void manage(Client client,IPacket packet,DatagramSocket server)throws Exception;
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
package mrdev023.network.packet.main;
|
||||
|
||||
public class Utils {
|
||||
|
||||
public static String getStringByStringArray(String[] cmd,int begin,int end){
|
||||
String data = "";
|
||||
for(int i = begin;i <= end;i++){
|
||||
if(i == end){
|
||||
data += cmd[i];
|
||||
break;
|
||||
}
|
||||
data += cmd[i] + " ";
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,125 @@
|
|||
package mrdev023.network.server;
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.util.*;
|
||||
|
||||
import mrdev023.network.common.*;
|
||||
import mrdev023.network.packet.*;
|
||||
import mrdev023.network.packet.main.*;
|
||||
|
||||
public class MainServer extends Thread{
|
||||
|
||||
public static DatagramSocket server;
|
||||
public static ArrayList<Client> clients = new ArrayList<Client>();
|
||||
public static boolean isRunning = false;
|
||||
public static final int PORT = 9999;
|
||||
public static final int SLOTS = 64;
|
||||
public static final String password = "florian";
|
||||
|
||||
public void run(){
|
||||
while(isRunning){
|
||||
try{
|
||||
for(Client cl : clients){
|
||||
if(cl != null){
|
||||
cl.update();
|
||||
if(cl.isTimeOut()){
|
||||
System.out.println(cl.getPseudo() + " has diconnected by TIME OUT " + cl.getAddress().getHostName() + ":" + cl.getPort());
|
||||
sendToClients(new ClientDisconnect(cl.getPseudo()));
|
||||
cl.setRunning(false);
|
||||
clients.remove(cl);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}catch(Exception e){e.printStackTrace();}
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
server = new DatagramSocket(9999);
|
||||
System.out.println("Server binding " + InetAddress.getLocalHost().getHostAddress() + ":" + PORT);
|
||||
isRunning = true;
|
||||
(new Thread(new MainServer())).start();
|
||||
while(isRunning){
|
||||
byte[] data = new byte[DataBuffer.SIZE];
|
||||
DatagramPacket p = new DatagramPacket(data,DataBuffer.SIZE);
|
||||
try {
|
||||
server.receive(p);
|
||||
if(p.getAddress() != null){
|
||||
DataBuffer buffer = new DataBuffer();
|
||||
buffer.setData(data);
|
||||
IPacket packet = (IPacket)Class.forName(buffer.getString()).newInstance();
|
||||
packet.read(buffer);
|
||||
Client c = getClient(p.getAddress(), p.getPort());
|
||||
if(c == null){
|
||||
if(clients.size() >= SLOTS){
|
||||
send(new OutOfSocketPacket(), p.getAddress(), p.getPort());
|
||||
}else{
|
||||
c = new Client(p.getAddress(), p.getPort());
|
||||
packet.manage(c, packet,server);
|
||||
clients.add(c);
|
||||
System.out.println(c.getPseudo() + " has connected " + c.getAddress().getHostName() + ":" + c.getPort());
|
||||
sendToClients(new ClientConnect(c.getPseudo()));
|
||||
}
|
||||
}else{
|
||||
packet.manage(c, packet,server);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static Client getClient(InetAddress address,int port){
|
||||
for(Client cl : clients){
|
||||
if(cl.getAddress().getHostAddress().equals(address.getHostAddress()) && port == cl.getPort())return cl;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static boolean isEquals(Client c,Client c2){
|
||||
if(c.getAddress().getHostAddress().equals(c2.getAddress().getHostAddress()) && c.getPort() == c2.getPort())return true;
|
||||
else return false;
|
||||
}
|
||||
|
||||
public static void sendToClient(Client cl,IPacket packet){
|
||||
for(Client c : clients){
|
||||
if(isEquals(cl, c))send(packet, cl.getAddress(), c.getPort());
|
||||
}
|
||||
}
|
||||
|
||||
public static void sendToClientsWithoutClient(Client[] cl,IPacket packet){
|
||||
for(Client c : clients){
|
||||
for(Client c2 : cl){
|
||||
if(!isEquals(c2, c)){
|
||||
sendToClient(c, packet);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void sendToClients(IPacket packet){
|
||||
for(Client c : clients){
|
||||
sendToClient(c, packet);
|
||||
}
|
||||
}
|
||||
|
||||
public static void send(IPacket packet,InetAddress ip,int port){
|
||||
try {
|
||||
DataBuffer buffer = new DataBuffer();
|
||||
buffer.put(packet.getClass().getName());
|
||||
packet.write(buffer);
|
||||
DatagramPacket p = new DatagramPacket(buffer.getData(), buffer.getData().length,ip,port);
|
||||
server.send(p);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
Reference in a new issue