conditional ping and pong echo to log
This commit is contained in:
parent
ab5abb019f
commit
3e44bfbb79
@ -11,6 +11,8 @@ import electrosphere.net.parser.net.message.EntityMessage;
|
|||||||
import electrosphere.net.parser.net.message.NetworkMessage;
|
import electrosphere.net.parser.net.message.NetworkMessage;
|
||||||
import electrosphere.net.parser.net.message.PlayerMessage;
|
import electrosphere.net.parser.net.message.PlayerMessage;
|
||||||
import electrosphere.net.parser.net.message.ServerMessage;
|
import electrosphere.net.parser.net.message.ServerMessage;
|
||||||
|
import electrosphere.net.parser.net.message.NetworkMessage.MessageType;
|
||||||
|
import electrosphere.net.parser.net.message.ServerMessage.ServerMessageType;
|
||||||
import electrosphere.net.parser.net.raw.NetworkParser;
|
import electrosphere.net.parser.net.raw.NetworkParser;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
@ -49,8 +51,13 @@ public class ClientNetworking implements Runnable{
|
|||||||
|
|
||||||
static final int MAX_CONNECTION_ATTEMPTS = 10;
|
static final int MAX_CONNECTION_ATTEMPTS = 10;
|
||||||
|
|
||||||
|
//set to true to also get ping and pong messages in debug logging
|
||||||
|
boolean echoPings = false;
|
||||||
|
|
||||||
|
//thresholds for when to send pings and to determine when we've disconnected
|
||||||
static final long SEND_PING_THRESHOLD = 3000;
|
static final long SEND_PING_THRESHOLD = 3000;
|
||||||
static final long PING_DISCONNECT_THRESHOLD = 20000;
|
static final long PING_DISCONNECT_THRESHOLD = 20000;
|
||||||
|
//times for calculating ping-pong
|
||||||
long lastPingTime = 0;
|
long lastPingTime = 0;
|
||||||
long lastPongTime = 0;
|
long lastPongTime = 0;
|
||||||
|
|
||||||
@ -151,13 +158,34 @@ public class ClientNetworking implements Runnable{
|
|||||||
if(initialized){
|
if(initialized){
|
||||||
while(parser.hasIncomingMessaage()){
|
while(parser.hasIncomingMessaage()){
|
||||||
NetworkMessage message = parser.popIncomingMessage();
|
NetworkMessage message = parser.popIncomingMessage();
|
||||||
LoggerInterface.loggerNetworking.DEBUG("[Client] New message " + message.getType());
|
//print network message
|
||||||
|
printMessage(message);
|
||||||
//do something
|
//do something
|
||||||
clientProtocol.handleMessage(message);
|
clientProtocol.handleMessage(message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Print out the network message type, this only prints ping and pong if echoPings is true
|
||||||
|
*/
|
||||||
|
void printMessage(NetworkMessage message){
|
||||||
|
//only print ping and pong if echoPings is true
|
||||||
|
if(message.getType() == MessageType.SERVER_MESSAGE){
|
||||||
|
if((((ServerMessage)message).getMessageSubtype()) == ServerMessageType.PING ||
|
||||||
|
(((ServerMessage)message).getMessageSubtype()) == ServerMessageType.PONG
|
||||||
|
){
|
||||||
|
if(this.echoPings == true){
|
||||||
|
LoggerInterface.loggerNetworking.DEBUG("[Server] New message " + message.getType());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
LoggerInterface.loggerNetworking.DEBUG("[Server] New message " + message.getType());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
LoggerInterface.loggerNetworking.DEBUG("[Server] New message " + message.getType());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public void queueOutgoingMessage(NetworkMessage message){
|
public void queueOutgoingMessage(NetworkMessage message){
|
||||||
parser.addOutgoingMessage(message);
|
parser.addOutgoingMessage(message);
|
||||||
|
|||||||
@ -23,19 +23,24 @@ import electrosphere.net.parser.net.message.NetworkMessage;
|
|||||||
import electrosphere.net.parser.net.message.PlayerMessage;
|
import electrosphere.net.parser.net.message.PlayerMessage;
|
||||||
import electrosphere.net.parser.net.message.ServerMessage;
|
import electrosphere.net.parser.net.message.ServerMessage;
|
||||||
import electrosphere.net.parser.net.message.TerrainMessage;
|
import electrosphere.net.parser.net.message.TerrainMessage;
|
||||||
|
import electrosphere.net.parser.net.message.NetworkMessage.MessageType;
|
||||||
|
import electrosphere.net.parser.net.message.ServerMessage.ServerMessageType;
|
||||||
import electrosphere.net.server.ServerConnectionHandler;
|
import electrosphere.net.server.ServerConnectionHandler;
|
||||||
import electrosphere.net.server.player.Player;
|
import electrosphere.net.server.player.Player;
|
||||||
|
|
||||||
public class ServerProtocol {
|
public class ServerProtocol {
|
||||||
|
|
||||||
ServerConnectionHandler connectionHandler;
|
ServerConnectionHandler connectionHandler;
|
||||||
|
boolean echoPings = false;
|
||||||
|
|
||||||
public ServerProtocol(ServerConnectionHandler connectionHandler){
|
public ServerProtocol(ServerConnectionHandler connectionHandler){
|
||||||
this.connectionHandler = connectionHandler;
|
this.connectionHandler = connectionHandler;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void handleMessage(NetworkMessage message){
|
public void handleMessage(NetworkMessage message){
|
||||||
LoggerInterface.loggerNetworking.DEBUG("[Server] New message " + message.getType());
|
//print out message
|
||||||
|
printMessage(message);
|
||||||
|
//actually handle message
|
||||||
switch(message.getType()){
|
switch(message.getType()){
|
||||||
case ENTITY_MESSAGE:
|
case ENTITY_MESSAGE:
|
||||||
EntityProtocol.handleEntityMessage(connectionHandler, (EntityMessage)message);
|
EntityProtocol.handleEntityMessage(connectionHandler, (EntityMessage)message);
|
||||||
@ -74,4 +79,24 @@ public class ServerProtocol {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Print out the network message type, this only prints ping and pong if echoPings is true
|
||||||
|
*/
|
||||||
|
void printMessage(NetworkMessage message){
|
||||||
|
//only print ping and pong if echoPings is true
|
||||||
|
if(message.getType() == MessageType.SERVER_MESSAGE){
|
||||||
|
if((((ServerMessage)message).getMessageSubtype()) == ServerMessageType.PING ||
|
||||||
|
(((ServerMessage)message).getMessageSubtype()) == ServerMessageType.PONG
|
||||||
|
){
|
||||||
|
if(this.echoPings == true){
|
||||||
|
LoggerInterface.loggerNetworking.DEBUG("[Server] New message " + message.getType());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
LoggerInterface.loggerNetworking.DEBUG("[Server] New message " + message.getType());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
LoggerInterface.loggerNetworking.DEBUG("[Server] New message " + message.getType());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user