Merge remote-tracking branch 'origin/master'
This commit is contained in:
commit
8916d7f8b9
BIN
java_pid21340.hprof
Normal file
BIN
java_pid21340.hprof
Normal file
Binary file not shown.
@ -5,62 +5,64 @@ import java.io.IOException;
|
|||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sends a ping to the server ("CPING") every 2 seconds and checks if it has gotten a pingback.
|
* Sends a ping to the server ("CPING") every 2 seconds and checks if it has gotten a pingback. The
|
||||||
* The actual logging of the pingback (via the gotPingBack boolean) has to be done elsewhere,
|
* actual logging of the pingback (via the gotPingBack boolean) has to be done elsewhere, depends on
|
||||||
* depends on how the client receives and parses messages.
|
* how the client receives and parses messages.
|
||||||
*/
|
*/
|
||||||
public class ClientPinger implements Runnable{
|
public class ClientPinger implements Runnable {
|
||||||
private boolean gotPingBack; //should be set to true (via setGotPingBack) as soon as the client gets a pingback.
|
|
||||||
private boolean isConnected; //set to true unless the ClientPinger detects a connection loss.
|
|
||||||
BufferedWriter out; //the output of this client through which the pings are sent
|
|
||||||
private Socket socket;
|
|
||||||
|
|
||||||
/**
|
private boolean gotPingBack; //should be set to true when client gets a pingback.
|
||||||
* @param socket the socket the Client is connected to which is used to end the thread if the connection is lost.
|
private boolean isConnected; //set to true unless the ClientPinger detects a connection loss.
|
||||||
* @param out the output through which the pings are sent.
|
BufferedWriter out; //the output of this client through which the pings are sent
|
||||||
*/
|
private Socket socket;
|
||||||
public ClientPinger(BufferedWriter out, Socket socket) {
|
|
||||||
|
/**
|
||||||
|
* @param socket the socket the Client is connected to which is used to end the thread if the
|
||||||
|
* connection is lost.
|
||||||
|
* @param out the output through which the pings are sent.
|
||||||
|
*/
|
||||||
|
public ClientPinger(BufferedWriter out, Socket socket) {
|
||||||
|
gotPingBack = false;
|
||||||
|
isConnected = true;
|
||||||
|
this.out = out;
|
||||||
|
this.socket = socket;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
try {
|
||||||
|
while (socket.isConnected()) {
|
||||||
gotPingBack = false;
|
gotPingBack = false;
|
||||||
isConnected = true;
|
out.write("CPING");
|
||||||
this.out = out;
|
out.newLine();
|
||||||
this.socket = socket;
|
out.flush();
|
||||||
}
|
Thread.sleep(2000);
|
||||||
|
if (gotPingBack) {
|
||||||
@Override
|
if (!isConnected) { //if !isConnected, then the connection had been lost before.
|
||||||
public void run() {
|
isConnected = true;
|
||||||
try {
|
System.out.println("Connection regained!");
|
||||||
while (socket.isConnected()) {
|
}
|
||||||
gotPingBack = false;
|
} else {
|
||||||
out.write("CPING");
|
isConnected = false;
|
||||||
out.newLine();
|
System.out.println("Lost connection. Waiting to reconnect...");
|
||||||
out.flush();
|
|
||||||
Thread.sleep(2000);
|
|
||||||
if (gotPingBack) {
|
|
||||||
if (!isConnected) { //if !isConnected, then the connection had been lost before.
|
|
||||||
isConnected = true;
|
|
||||||
System.out.println("Connection regained!");
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
isConnected = false;
|
|
||||||
System.out.println("Lost connection. Waiting to reconnect...");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
isConnected = false; //in case the socket accidentally disconnects (can this happen?)
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public void setGotPingBack(boolean gotPingBack) {
|
}
|
||||||
this.gotPingBack = gotPingBack;
|
isConnected = false; //in case the socket accidentally disconnects (can this happen?)
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public boolean isConnected() {
|
public void setGotPingBack(boolean gotPingBack) {
|
||||||
return isConnected;
|
this.gotPingBack = gotPingBack;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isConnected() {
|
||||||
|
return isConnected;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -5,61 +5,63 @@ import java.io.IOException;
|
|||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sends a ping to the client ("SPING") every 2 seconds and checks if it has gotten a pingback.
|
* Sends a ping to the client ("SPING") every 2 seconds and checks if it has gotten a pingback. The
|
||||||
* The actual logging of the pingback (via the gotPingBack boolean) has to be done elsewhere,
|
* actual logging of the pingback (via the gotPingBack boolean) has to be done elsewhere, depends on
|
||||||
* depends on how the server receives and parses messages.
|
* how the server receives and parses messages.
|
||||||
*/
|
*/
|
||||||
public class ServerPinger implements Runnable{
|
public class ServerPinger implements Runnable {
|
||||||
private boolean gotPingBack; //should be set to true (via setGotPingBack) as soon as the server gets a pingback.
|
|
||||||
private boolean isConnected; //set to true unless the ServerPinger detects a connection loss.
|
|
||||||
BufferedWriter out; //the output of this client through which the pings are sent
|
|
||||||
private Socket socket;
|
|
||||||
|
|
||||||
/**
|
private boolean gotPingBack; //should be set to true (via setGotPingBack) as soon as the server gets a pingback.
|
||||||
* @param socket the socket the ClientHandler is connected to; used to end the thread if the connection is lost.
|
private boolean isConnected; //set to true unless the ServerPinger detects a connection loss.
|
||||||
* @param out the output through which the pings are sent.
|
BufferedWriter out; //the output of this client through which the pings are sent
|
||||||
*/
|
private Socket socket;
|
||||||
public ServerPinger(BufferedWriter out, Socket socket) {
|
|
||||||
|
/**
|
||||||
|
* @param socket the socket the ClientHandler is connected to; used to end the thread if the
|
||||||
|
* connection is lost.
|
||||||
|
* @param out the output through which the pings are sent.
|
||||||
|
*/
|
||||||
|
public ServerPinger(BufferedWriter out, Socket socket) {
|
||||||
|
gotPingBack = false;
|
||||||
|
isConnected = true;
|
||||||
|
this.out = out;
|
||||||
|
this.socket = socket;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
try {
|
||||||
|
while (socket.isConnected()) {
|
||||||
gotPingBack = false;
|
gotPingBack = false;
|
||||||
isConnected = true;
|
out.write("SPING");
|
||||||
this.out = out;
|
out.newLine();
|
||||||
this.socket = socket;
|
out.flush();
|
||||||
}
|
Thread.sleep(2000);
|
||||||
|
if (gotPingBack) {
|
||||||
@Override
|
if (!isConnected) { //if !isConnected, then the connection had been lost before.
|
||||||
public void run() {
|
isConnected = true;
|
||||||
try {
|
System.out.println("Connection regained!");
|
||||||
while (socket.isConnected()) {
|
}
|
||||||
gotPingBack = false;
|
} else {
|
||||||
out.write("SPING");
|
isConnected = false;
|
||||||
out.newLine();
|
System.out.println("Lost connection. Waiting to reconnect...");
|
||||||
out.flush();
|
|
||||||
Thread.sleep(2000);
|
|
||||||
if (gotPingBack) {
|
|
||||||
if (!isConnected) { //if !isConnected, then the connection had been lost before.
|
|
||||||
isConnected = true;
|
|
||||||
System.out.println("Connection regained!");
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
isConnected = false;
|
|
||||||
System.out.println("Lost connection. Waiting to reconnect...");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
isConnected = false; //in case the socket accidentally disconnects (can this happen?)
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
isConnected = false; //in case the socket accidentally disconnects (can this happen?)
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void setGotPingBack(boolean gotPingBack) {
|
public void setGotPingBack(boolean gotPingBack) {
|
||||||
this.gotPingBack = gotPingBack;
|
this.gotPingBack = gotPingBack;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isConnected() {
|
public boolean isConnected() {
|
||||||
return isConnected;
|
return isConnected;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -7,14 +7,14 @@
|
|||||||
* LEAVG: leave a game
|
* LEAVG: leave a game
|
||||||
* JOING: join a game
|
* JOING: join a game
|
||||||
* VOTEG: ghost voting who to infect
|
* VOTEG: ghost voting who to infect
|
||||||
* VOTEH: humans voting whos the ghost
|
* VOTEH: humans voting who is the ghost
|
||||||
* QUITS: quit server/ leave servr
|
* QUITS: quit server/ leave server
|
||||||
* LISTP: list players/clients in session with the Server
|
* LISTP: list players/clients in session with the Server
|
||||||
* CPING: Ping from client to server.
|
* CPING: Ping from client to server.
|
||||||
*/
|
*/
|
||||||
/**
|
/**
|
||||||
Server Commands:
|
Server Commands:
|
||||||
* MSGRS: "Message recieved": Paramaters: a string detailing to the client that and what the server recieved as command.
|
* MSGRS: "Message received": Paramaters: a string detailing to the client that and what the server received as command.
|
||||||
* SEROR: Server had an error. (used for debugging)
|
* SEROR: Server had an error. (used for debugging)
|
||||||
* SPING: Ping from server to client;
|
* SPING: Ping from server to client;
|
||||||
*/
|
*/
|
||||||
@ -8,54 +8,54 @@ import java.util.Scanner;
|
|||||||
|
|
||||||
public class Server {
|
public class Server {
|
||||||
|
|
||||||
private static final int gamePort = 42069;
|
private static final int gamePort = 42069;
|
||||||
private HashSet<ClientHandler> connectedClients = new HashSet<>();
|
private HashSet<ClientHandler> connectedClients = new HashSet<>();
|
||||||
private ServerSocket serverSocket;
|
private ServerSocket serverSocket;
|
||||||
Scanner sc = new Scanner(System.in);
|
Scanner sc = new Scanner(System.in);
|
||||||
|
|
||||||
|
public Server(ServerSocket serverSocket) {
|
||||||
|
this.serverSocket = serverSocket;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void startServer() {
|
||||||
|
try {
|
||||||
|
System.out.println("Port 42069 is open on " + this.serverSocket.getInetAddress());
|
||||||
|
while (!serverSocket.isClosed()) {
|
||||||
|
Socket socket = serverSocket.accept();
|
||||||
|
ClientHandler nextClient = new ClientHandler(socket);
|
||||||
|
Thread th = new Thread(nextClient);
|
||||||
|
connectedClients.add(nextClient);
|
||||||
|
th.start();
|
||||||
|
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
|
||||||
public Server(ServerSocket serverSocket) {
|
|
||||||
this.serverSocket = serverSocket;
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void startServer() {
|
public void closeServerSocket() {
|
||||||
try {
|
try {
|
||||||
System.out.println("Port 42069 is open on " + this.serverSocket.getInetAddress());
|
if (serverSocket != null) {
|
||||||
while (!serverSocket.isClosed()) {
|
serverSocket.close();
|
||||||
Socket socket = serverSocket.accept();
|
}
|
||||||
ClientHandler nextClient = new ClientHandler(socket);
|
} catch (IOException e) {
|
||||||
Thread th = new Thread(nextClient);
|
e.printStackTrace();
|
||||||
connectedClients.add(nextClient);
|
|
||||||
th.start();
|
|
||||||
|
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void closeServerSocket() {
|
public static void main(String[] args) {
|
||||||
try {
|
ServerSocket serverSocket = null;
|
||||||
if (serverSocket != null){
|
try {
|
||||||
serverSocket.close();
|
serverSocket = new ServerSocket(gamePort);
|
||||||
}
|
} catch (IOException e) {
|
||||||
} catch (IOException e) {
|
e.printStackTrace();
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Server server = new Server(serverSocket);
|
||||||
|
server.startServer();
|
||||||
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void broadcast(String msg) {
|
||||||
ServerSocket serverSocket = null;
|
//TODO
|
||||||
try {
|
}
|
||||||
serverSocket = new ServerSocket(gamePort);
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
Server server = new Server(serverSocket);
|
|
||||||
server.startServer();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void broadcast(String msg){
|
|
||||||
//TODO
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user