Renderer/src/main/java/electrosphere/net/server/protocol/ServerProtocol.java
austin 8c0a7697d0
All checks were successful
studiorailgun/Renderer/pipeline/head This commit looks good
block synchronization
2024-06-18 18:41:33 -04:00

166 lines
6.6 KiB
Java

package electrosphere.net.server.protocol;
import electrosphere.logger.LoggerInterface;
import electrosphere.net.parser.net.message.AuthMessage;
import electrosphere.net.parser.net.message.CharacterMessage;
import electrosphere.net.parser.net.message.EntityMessage;
import electrosphere.net.parser.net.message.InventoryMessage;
import electrosphere.net.parser.net.message.LoreMessage;
import electrosphere.net.parser.net.message.NetworkMessage;
import electrosphere.net.parser.net.message.PlayerMessage;
import electrosphere.net.parser.net.message.ServerMessage;
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;
/**
* The server's protocol for handling a message
*/
public class ServerProtocol {
//the connection handler associated with this protocol object
ServerConnectionHandler connectionHandler;
//if set to true, will log ping messages
boolean echoPings = false;
/**
* Constructor
* @param connectionHandler the associated connection handler
*/
public ServerProtocol(ServerConnectionHandler connectionHandler){
this.connectionHandler = connectionHandler;
}
/**
* Handles a given network message
* @param message The network message to handle
* @return Returns the message if it should be synchronously handled with the main server thread instead
*/
public NetworkMessage handleAsyncMessage(NetworkMessage message){
//print out message
printMessage(message);
//actually handle message
switch(message.getType()){
case ENTITY_MESSAGE: {
EntityProtocol.handleEntityMessage(connectionHandler, (EntityMessage)message);
} break;
case TERRAIN_MESSAGE: {
TerrainProtocol.handleTerrainMessage(connectionHandler, (TerrainMessage)message);
} break;
case PLAYER_MESSAGE: {
PlayerProtocol.handlePlayerMessage(connectionHandler, (PlayerMessage)message);
} break;
case AUTH_MESSAGE: {
AuthProtocol.handleAuthenticationMessage(connectionHandler, (AuthMessage)message);
} break;
case SERVER_MESSAGE: {
handleServerMessage((ServerMessage)message);
} break;
case CHARACTER_MESSAGE: {
CharacterProtocol.handleCharacterMessage(connectionHandler, (CharacterMessage)message);
} break;
case LORE_MESSAGE: {
LoreProtocol.handleLoreMessage(connectionHandler, (LoreMessage)message);
} break;
case INVENTORY_MESSAGE: {
return InventoryProtocol.handleAsyncInventoryMessage(connectionHandler, (InventoryMessage)message);
}
case SYNCHRONIZATION_MESSAGE:
//silently ignore sync messages from client
break;
}
return null;
}
/**
* Synchronously handles a network message
* @param message The network message
*/
public void handleSynchronousMessage(NetworkMessage message){
//print out message
printMessage(message);
//actually handle message
switch(message.getType()){
case ENTITY_MESSAGE: {
EntityProtocol.handleSynchronousEntityMessage(connectionHandler, (EntityMessage)message);
} break;
case TERRAIN_MESSAGE: {
TerrainProtocol.handleSynchronousServerMessage(connectionHandler, (TerrainMessage)message);
} break;
case PLAYER_MESSAGE: {
PlayerProtocol.handleSynchronousPlayerMessage(connectionHandler, (PlayerMessage)message);
} break;
case AUTH_MESSAGE: {
AuthProtocol.handleSynchronousAuthMessage(connectionHandler, (AuthMessage)message);
} break;
case SERVER_MESSAGE: {
ServerProtocol.handleSynchronousServerMessage(connectionHandler, (ServerMessage)message);
} break;
case CHARACTER_MESSAGE: {
CharacterProtocol.handleSynchronousCharacterMessage(connectionHandler, (CharacterMessage)message);
} break;
case LORE_MESSAGE: {
LoreProtocol.handleSynchronousLoreMessage(connectionHandler, (LoreMessage)message);
} break;
case INVENTORY_MESSAGE: {
InventoryProtocol.handleSynchronousInventoryMessage(connectionHandler, (InventoryMessage)message);
} break;
case SYNCHRONIZATION_MESSAGE:
//silently ignore sync messages from client
break;
}
}
/**
* Handles a server-type network message
* @param message The server message
*/
void handleServerMessage(ServerMessage message){
switch(message.getMessageSubtype()){
case PING:
connectionHandler.addMessagetoOutgoingQueue(ServerMessage.constructPongMessage());
break;
case PONG:
connectionHandler.markReceivedPongMessage();
break;
}
}
/**
* Handles the network message from the context of the main server simulation thread
* @param connectionHandler The connection handler
* @param message The network message
*/
protected static void handleSynchronousServerMessage(ServerConnectionHandler connectionHandler, ServerMessage message){
switch(message.getMessageSubtype()){
case PING:
case PONG:
//silently ignore
break;
}
}
/**
* 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());
}
}
}