-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
56 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 41 additions & 42 deletions
83
src/main/java/mpo/dayon/common/network/message/NetworkMessage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,47 @@ | ||
package mpo.dayon.common.network.message; | ||
|
||
import java.io.*; | ||
import java.util.Arrays; | ||
|
||
public abstract class NetworkMessage { | ||
private static final byte MAGIC_NUMBER = (byte) 170; | ||
|
||
NetworkMessage() { | ||
} | ||
|
||
public abstract NetworkMessageType getType(); | ||
|
||
/** | ||
* Take into account some extra-info sent over the network with the actual | ||
* payload ... | ||
*/ | ||
public abstract int getWireSize(); | ||
|
||
public abstract void marshall(ObjectOutputStream out) throws IOException; | ||
|
||
public static void marshallMagicNumber(ObjectOutputStream out) throws IOException { | ||
out.writeByte(NetworkMessage.MAGIC_NUMBER); | ||
} | ||
|
||
public static void unmarshallMagicNumber(ObjectInputStream in) throws IOException { | ||
if (NetworkMessage.MAGIC_NUMBER != in.readByte()) { | ||
throw new IOException("Protocol error!"); | ||
} | ||
} | ||
|
||
static <T extends Enum<T>> void marshallEnum(ObjectOutputStream out, Enum<T> value) throws IOException { | ||
out.write(value.ordinal()); | ||
} | ||
|
||
public static <T extends Enum<T>> T unmarshallEnum(ObjectInputStream in, Class<T> enumClass) throws IOException { | ||
final byte ordinal = in.readByte(); | ||
final T[] xenums = enumClass.getEnumConstants(); | ||
for (final T xenum : xenums) { | ||
if (xenum.ordinal() == ordinal) { | ||
return xenum; | ||
} | ||
} | ||
throw new IllegalArgumentException("Unknown " + enumClass.getSimpleName() + " [" + ordinal + "] enum!"); | ||
} | ||
|
||
@Override | ||
public abstract String toString(); | ||
private static final byte MAGIC_NUMBER = (byte) 170; | ||
|
||
NetworkMessage() { | ||
} | ||
|
||
public abstract NetworkMessageType getType(); | ||
|
||
/** | ||
* Take into account some extra-info sent over the network with the actual | ||
* payload ... | ||
*/ | ||
public abstract int getWireSize(); | ||
|
||
public abstract void marshall(ObjectOutputStream out) throws IOException; | ||
|
||
public static void marshallMagicNumber(ObjectOutputStream out) throws IOException { | ||
out.writeByte(NetworkMessage.MAGIC_NUMBER); | ||
} | ||
|
||
public static void unmarshallMagicNumber(ObjectInputStream in) throws IOException { | ||
if (NetworkMessage.MAGIC_NUMBER != in.readByte()) { | ||
throw new IOException("Protocol error!"); | ||
} | ||
} | ||
|
||
static <T extends Enum<T>> void marshallEnum(ObjectOutputStream out, Enum<T> value) throws IOException { | ||
out.write(value.ordinal()); | ||
} | ||
|
||
public static <T extends Enum<T>> T unmarshallEnum(ObjectInputStream in, Class<T> enumClass) throws IOException { | ||
final byte ordinal = in.readByte(); | ||
final T[] xenums = enumClass.getEnumConstants(); | ||
return Arrays.stream(xenums) | ||
.filter(xenum -> xenum.ordinal() == ordinal) | ||
.findFirst() | ||
.orElseThrow(() -> new IllegalArgumentException("Unknown " + enumClass.getSimpleName() + " [" + ordinal + "] enum!")); | ||
} | ||
|
||
@Override | ||
public abstract String toString(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters