本文整理汇总了Java中com.jme3.network.serializing.Serializer类的典型用法代码示例。如果您正苦于以下问题:Java Serializer类的具体用法?Java Serializer怎么用?Java Serializer使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Serializer类属于com.jme3.network.serializing包,在下文中一共展示了Serializer类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: main
import com.jme3.network.serializing.Serializer; //导入依赖的package包/类
public static void main(String[] args) throws IOException, InterruptedException{
Serializer.registerClass(PingMessage.class);
Serializer.registerClass(PongMessage.class);
Server server = Network.createServer(5110);
server.start();
Client client = Network.connectToServer("localhost", 5110);
client.start();
server.addMessageListener(new ServerPingResponder(), PingMessage.class);
client.addMessageListener(new ClientPingResponder(), PongMessage.class);
System.out.println("Client: Sending ping message..");
client.send(new PingMessage());
Object obj = new Object();
synchronized (obj){
obj.wait();
}
}
示例2: main
import com.jme3.network.serializing.Serializer; //导入依赖的package包/类
public static void main(String[] args) throws IOException, InterruptedException {
Serializer.registerClass(TestMessage.class);
// Use this to test the client/server name version check
//Server server = Network.createServer( "bad name", 42, 5110, 5110 );
Server server = Network.createServer(5110, 5110);
server.start();
Client client = Network.connectToServer("localhost", 5110);
client.start();
client.addMessageListener(new TestThroughput(false), TestMessage.class);
server.addMessageListener(new TestThroughput(true), TestMessage.class);
Thread.sleep(1);
TestMessage test = new TestMessage();
// for( int i = 0; i < 10; i++ ) {
while (true) {
//System.out.println( "sending." );
client.send(test);
}
//Thread.sleep(5000);
}
示例3: messageToBuffer
import com.jme3.network.serializing.Serializer; //导入依赖的package包/类
/**
* Converts a message to a ByteBuffer using the Serializer
* and the (short length) + data protocol. If target is null
* then a 32k byte buffer will be created and filled.
*/
public static ByteBuffer messageToBuffer( Message message, ByteBuffer target )
{
// Could let the caller pass their own in
ByteBuffer buffer = target == null ? ByteBuffer.allocate( 32767 + 2 ) : target;
try {
buffer.position( 2 );
Serializer.writeClassAndObject( buffer, message );
buffer.flip();
short dataLength = (short)(buffer.remaining() - 2);
buffer.putShort( dataLength );
buffer.position( 0 );
return buffer;
} catch( IOException e ) {
throw new RuntimeException( "Error serializing message", e );
}
}
示例4: writeMethodCall
import com.jme3.network.serializing.Serializer; //导入依赖的package包/类
private void writeMethodCall(ByteBuffer buffer, RemoteMethodCallMessage call) throws IOException{
buffer.putShort((short)call.objectId);
buffer.putShort(call.methodId);
buffer.putShort(call.invocationId);
if (call.args == null){
buffer.put((byte)0);
}else{
buffer.put((byte)call.args.length);
// Right now it writes 0 for every null argument
// and 1 for every non-null argument followed by the serialized
// argument. For the future, using a bit set should be considered.
for (Object obj : call.args){
if (obj != null){
buffer.put((byte)0x01);
Serializer.writeClassAndObject(buffer, obj);
}else{
buffer.put((byte)0x00);
}
}
}
}
示例5: readMethodCall
import com.jme3.network.serializing.Serializer; //导入依赖的package包/类
private RemoteMethodCallMessage readMethodCall(ByteBuffer buffer) throws IOException{
RemoteMethodCallMessage call = new RemoteMethodCallMessage();
call.objectId = buffer.getShort();
call.methodId = buffer.getShort();
call.invocationId = buffer.getShort();
int numArgs = buffer.get() & 0xff;
if (numArgs > 0){
Object[] args = new Object[numArgs];
for (int i = 0; i < numArgs; i++){
if (buffer.get() == (byte)0x01){
args[i] = Serializer.readClassAndObject(buffer);
}
}
call.args = args;
}
return call;
}
示例6: writeObject
import com.jme3.network.serializing.Serializer; //导入依赖的package包/类
public void writeObject(ByteBuffer buffer, Object object) throws IOException {
if (!(object instanceof ZIPCompressedMessage)) return;
ZIPCompressedMessage zipMessage = (ZIPCompressedMessage)object;
Message message = zipMessage.getMessage();
ByteBuffer tempBuffer = ByteBuffer.allocate(512000);
Serializer.writeClassAndObject(tempBuffer, message);
ByteArrayOutputStream byteArrayOutput = new ByteArrayOutputStream();
ZipOutputStream zipOutput = new ZipOutputStream(byteArrayOutput);
zipOutput.setLevel(zipMessage.getLevel());
ZipEntry zipEntry = new ZipEntry("zip");
zipOutput.putNextEntry(zipEntry);
zipOutput.write(tempBuffer.array());
zipOutput.flush();
zipOutput.closeEntry();
zipOutput.close();
buffer.put(byteArrayOutput.toByteArray());
}
示例7: writeObject
import com.jme3.network.serializing.Serializer; //导入依赖的package包/类
public void writeObject(ByteBuffer buffer, Object object) throws IOException {
if (!(object instanceof GZIPCompressedMessage)) return;
Message message = ((GZIPCompressedMessage)object).getMessage();
ByteBuffer tempBuffer = ByteBuffer.allocate(512000);
Serializer.writeClassAndObject(tempBuffer, message);
ByteArrayOutputStream byteArrayOutput = new ByteArrayOutputStream();
GZIPOutputStream gzipOutput = new GZIPOutputStream(byteArrayOutput);
gzipOutput.write(tempBuffer.array());
gzipOutput.flush();
gzipOutput.finish();
gzipOutput.close();
buffer.put(byteArrayOutput.toByteArray());
}
示例8: readObject
import com.jme3.network.serializing.Serializer; //导入依赖的package包/类
public <T> T readObject(ByteBuffer data, Class<T> c) throws IOException {
byte dimensionCount = data.get();
if (dimensionCount == 0)
return null;
int[] dimensions = new int[dimensionCount];
for (int i = 0; i < dimensionCount; i++)
dimensions[i] = data.getInt();
Serializer elementSerializer = null;
Class elementClass = c;
while (elementClass.getComponentType() != null)
elementClass = elementClass.getComponentType();
if (Modifier.isFinal(elementClass.getModifiers())) elementSerializer = Serializer.getSerializer(elementClass);
// Create array and read in the data.
T array = (T)Array.newInstance(elementClass, dimensions);
readArray(elementSerializer, elementClass, data, array, 0, dimensions);
return array;
}
示例9: writeObject
import com.jme3.network.serializing.Serializer; //导入依赖的package包/类
public void writeObject(ByteBuffer buffer, Object object) throws IOException {
if (object == null){
buffer.put((byte)0);
return;
}
int[] dimensions = getDimensions(object);
buffer.put((byte)dimensions.length);
for (int dimension : dimensions) buffer.putInt(dimension);
Serializer elementSerializer = null;
Class elementClass = object.getClass();
while (elementClass.getComponentType() != null) {
elementClass = elementClass.getComponentType();
}
if (Modifier.isFinal(elementClass.getModifiers())) elementSerializer = Serializer.getSerializer(elementClass);
writeArray(elementSerializer, buffer, object, 0, dimensions.length);
}
示例10: writeArray
import com.jme3.network.serializing.Serializer; //导入依赖的package包/类
private void writeArray(Serializer elementSerializer, ByteBuffer buffer, Object array, int dimension, int dimensionCount) throws IOException {
int length = Array.getLength(array);
if (dimension > 0) {
buffer.putInt(length);
}
// Write array data.
boolean elementsAreArrays = dimension < dimensionCount - 1;
for (int i = 0; i < length; i++) {
Object element = Array.get(array, i);
if (elementsAreArrays) {
if (element != null) writeArray(elementSerializer, buffer, element, dimension + 1, dimensionCount);
} else if (elementSerializer != null) {
elementSerializer.writeObject(buffer, element);
} else {
// Each element could be a different type. Store the class with the object.
Serializer.writeClassAndObject(buffer, element);
}
}
}
示例11: read
import com.jme3.network.serializing.Serializer; //导入依赖的package包/类
public void read(Object entity, ByteBuffer in, boolean init){
FieldTable table = getTable(entity.getClass());
for (SyncFieldInfo fieldInfo : table){
if ( (init && !fieldInfo.init)
|| (!init && !fieldInfo.sync) )
continue;
Field field = fieldInfo.field;
try {
Object obj = Serializer.readClassAndObject(in);
field.set(entity, obj);
} catch (Exception ex){
ex.printStackTrace();
}
}
}
示例12: GameServer
import com.jme3.network.serializing.Serializer; //导入依赖的package包/类
/**
* @param gameData 游戏数据
* @param gameName 游戏名称
* @param versionName 游戏的版本
* @param versionCode 游戏版本代码
* @param serverPort 端服端运行端口
* @throws IOException
*/
public GameServer(GameData gameData, String gameName, String versionName, int versionCode, int serverPort) throws IOException {
// 不要设置为true,这会导致在Network.createServer创建Server后,第二次再创建时报异常:
// java.lang.RuntimeException: Serializer registry locked trying to register class:class com.jme3.network.message.SerializerRegistrationsMessage
// 这个问题在JME3.1发生,在3.0时没有问题。
Serializer.setReadOnly(false);
this.gameData = gameData;
this.gameName = gameName;
this.versionName = versionName;
this.versionCode = versionCode;
this.serverPort = serverPort;
server = Network.createServer(gameName, versionCode , serverPort, serverPort);
server.addConnectionListener(this);
server.addMessageListener(this);
}
示例13: convert
import com.jme3.network.serializing.Serializer; //导入依赖的package包/类
public void convert(String path) throws IOException {
byte[] bytes = Files.readAllBytes(Paths.get(path));
ByteBuffer buffer = ByteBuffer.wrap(bytes);
buffer.position(2);
ReplayData data = (ReplayData) Serializer.readClassAndObject(buffer);
ExtraMetadata extra = findExtraMetadata(data);
Path outputPath = Paths.get("converted",
Paths.get(path).getFileName().toString());
ByteBuffer outBuffer = ByteBuffer.allocate(314572800); // 300 MB
// serializer.convertFromOld(outBuffer, data, extra);
outBuffer.flip();
Path filePath = Files.createFile(outputPath);
writeToFile(outBuffer, filePath);
}
示例14: main
import com.jme3.network.serializing.Serializer; //导入依赖的package包/类
public static void main(String[] args) throws IOException, InterruptedException{
Serializer.registerClass(Savable.class, new SavableSerializer());
createServer();
Client client = Network.connectToServer("localhost", 5110);
client.start();
ObjectStore store = new ObjectStore(client);
ServerAccess access = store.getExposedObject("access", ServerAccess.class, true);
boolean result = access.attachChild("Models/Oto/Oto.mesh.xml");
System.out.println(result);
}
示例15: main
import com.jme3.network.serializing.Serializer; //导入依赖的package包/类
public static void main(String[] args) throws IOException, InterruptedException{
Serializer.registerClass(SomeObject.class);
Serializer.registerClass(TestSerializationMessage.class);
Server server = Network.createServer( 5110 );
server.start();
Client client = Network.connectToServer( "localhost", 5110 );
client.start();
server.addMessageListener(new TestSerialization(), TestSerializationMessage.class);
client.send(new TestSerializationMessage(true));
Thread.sleep(10000);
}