Search in sources :

Example 1 with Message

use of com.jme3.network.Message in project jmonkeyengine by jMonkeyEngine.

the class GZIPSerializer method readObject.

@SuppressWarnings("unchecked")
public <T> T readObject(ByteBuffer data, Class<T> c) throws IOException {
    try {
        GZIPCompressedMessage result = new GZIPCompressedMessage();
        byte[] byteArray = new byte[data.remaining()];
        data.get(byteArray);
        GZIPInputStream in = new GZIPInputStream(new ByteArrayInputStream(byteArray));
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte[] tmp = new byte[9012];
        int read;
        while (in.available() > 0 && ((read = in.read(tmp)) > 0)) {
            out.write(tmp, 0, read);
        }
        result.setMessage((Message) Serializer.readClassAndObject(ByteBuffer.wrap(out.toByteArray())));
        return (T) result;
    } catch (Exception e) {
        e.printStackTrace();
        throw new IOException(e.toString());
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) GZIPCompressedMessage(com.jme3.network.message.GZIPCompressedMessage) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) IOException(java.io.IOException)

Example 2 with Message

use of com.jme3.network.Message in project jmonkeyengine by jMonkeyEngine.

the class MessageProtocol method createMessage.

/**
     *  Creates a message from the properly sized byte buffer
     *  and adds it to the messages queue.
     */
protected void createMessage(ByteBuffer buffer) {
    try {
        Object obj = Serializer.readClassAndObject(buffer);
        Message m = (Message) obj;
        messages.add(m);
    } catch (IOException e) {
        throw new RuntimeException("Error deserializing object, class ID:" + buffer.getShort(0), e);
    }
}
Also used : Message(com.jme3.network.Message) IOException(java.io.IOException)

Example 3 with Message

use of com.jme3.network.Message in project jmonkeyengine by jMonkeyEngine.

the class NioEndpoint method close.

public void close(boolean flushData) {
    if (flushData) {
        closing = true;
        // Enqueue a close marker message to let the server
        // know we should close
        send(CLOSE_MARKER, false, true);
        return;
    }
    try {
        // Note: even though we may be disconnected from the socket.isConnected()
        // standpoint, it's still safest to tell the kernel so that it can be sure
        // to stop managing us gracefully.
        kernel.closeEndpoint(this);
    } catch (IOException e) {
        throw new KernelException("Error closing endpoint for socket:" + socket, e);
    }
}
Also used : IOException(java.io.IOException) KernelException(com.jme3.network.kernel.KernelException)

Example 4 with Message

use of com.jme3.network.Message in project jmonkeyengine by jMonkeyEngine.

the class RpcConnection method callAndWait.

/**
     *  Performs a remote procedure call with the specified arguments and waits
     *  for the response.  Both the outbound message and inbound response will
     *  be sent on the specified channel.
     */
public Object callAndWait(byte channel, short objId, short procId, Object... args) {
    RpcCallMessage msg = new RpcCallMessage(sequenceNumber.getAndIncrement(), channel, objId, procId, args);
    // Need to register an object so we can wait for the response.
    // ...before we send it.  Just in case.
    ResponseHolder holder = new ResponseHolder(msg);
    responses.put(msg.getMessageId(), holder);
    if (log.isLoggable(Level.FINEST)) {
        log.log(Level.FINEST, "Sending:{0}  on channel:{1}", new Object[] { msg, channel });
    }
    // so it doesn't do the check.
    if (channel >= 0) {
        connection.send(channel, msg);
    } else {
        connection.send(msg);
    }
    return holder.getResponse();
}
Also used : RpcCallMessage(com.jme3.network.service.rpc.msg.RpcCallMessage)

Example 5 with Message

use of com.jme3.network.Message in project jmonkeyengine by jMonkeyEngine.

the class RpcHostedService method onInitialize.

/**
     *  Used internally to setup the message delegator that will
     *  handle HostedConnection specific messages and forward them
     *  to that connection's RpcConnection.
     */
@Override
protected void onInitialize(HostedServiceManager serviceManager) {
    Server server = serviceManager.getServer();
    // A general listener for forwarding the messages
    // to the client-specific handler
    this.delegator = new SessionDataDelegator(RpcConnection.class, ATTRIBUTE_NAME, true);
    server.addMessageListener(delegator, delegator.getMessageTypes());
    if (log.isLoggable(Level.FINEST)) {
        log.log(Level.FINEST, "Registered delegator for message types:{0}", Arrays.asList(delegator.getMessageTypes()));
    }
}
Also used : Server(com.jme3.network.Server) SessionDataDelegator(com.jme3.network.util.SessionDataDelegator)

Aggregations

IOException (java.io.IOException)6 Message (com.jme3.network.Message)5 ClientRegistrationMessage (com.jme3.network.message.ClientRegistrationMessage)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 ByteBuffer (java.nio.ByteBuffer)4 GhostControl (com.jme3.bullet.control.GhostControl)2 Server (com.jme3.network.Server)2 Endpoint (com.jme3.network.kernel.Endpoint)2 ChannelInfoMessage (com.jme3.network.message.ChannelInfoMessage)2 GZIPCompressedMessage (com.jme3.network.message.GZIPCompressedMessage)2 ZIPCompressedMessage (com.jme3.network.message.ZIPCompressedMessage)2 RpcCallMessage (com.jme3.network.service.rpc.msg.RpcCallMessage)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 BulletAppState (com.jme3.bullet.BulletAppState)1 BoxCollisionShape (com.jme3.bullet.collision.shapes.BoxCollisionShape)1 CompoundCollisionShape (com.jme3.bullet.collision.shapes.CompoundCollisionShape)1 SphereCollisionShape (com.jme3.bullet.collision.shapes.SphereCollisionShape)1 RigidBodyControl (com.jme3.bullet.control.RigidBodyControl)1 BitmapText (com.jme3.font.BitmapText)1 KeyTrigger (com.jme3.input.controls.KeyTrigger)1