Search in sources :

Example 11 with Message

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

the class ConnectorAdapter method run.

public void run() {
    MessageProtocol protocol = new MessageProtocol();
    try {
        while (go.get()) {
            ByteBuffer buffer = connector.read();
            if (buffer == null) {
                if (go.get()) {
                    throw new ConnectorException("Connector closed.");
                } else {
                    // from a closed/closing connector
                    break;
                }
            }
            protocol.addBuffer(buffer);
            Message m = null;
            while ((m = protocol.getMessage()) != null) {
                m.setReliable(reliable);
                dispatch(m);
            }
        }
    } catch (Exception e) {
        handleError(e);
    }
}
Also used : Message(com.jme3.network.Message) ConnectorException(com.jme3.network.kernel.ConnectorException) ByteBuffer(java.nio.ByteBuffer) ConnectorException(com.jme3.network.kernel.ConnectorException)

Example 12 with Message

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

the class DefaultClient method start.

@Override
public void start() {
    if (isRunning)
        throw new IllegalStateException("Client is already started.");
    // connectors that we have
    for (ConnectorAdapter ca : channels) {
        if (ca == null)
            continue;
        ca.start();
    }
    // Send our connection message with a generated ID until
    // we get one back from the server.  We'll hash time in
    // millis and time in nanos.
    // This is used to match the TCP and UDP endpoints up on the
    // other end since they may take different routes to get there.
    // Behind NAT, many game clients may be coming over the same
    // IP address from the server's perspective and they may have
    // their UDP ports mapped all over the place.
    //
    // Since currentTimeMillis() is absolute time and nano time
    // is roughtly related to system start time, adding these two
    // together should be plenty unique for our purposes.  It wouldn't
    // hurt to reconcile with IP on the server side, though.
    long tempId = System.currentTimeMillis() + System.nanoTime();
    // Set it true here so we can send some messages.
    isRunning = true;
    ClientRegistrationMessage reg;
    reg = new ClientRegistrationMessage();
    reg.setId(tempId);
    reg.setGameName(getGameName());
    reg.setVersion(getVersion());
    reg.setReliable(true);
    send(CH_RELIABLE, reg, false);
    // Send registration messages to any other configured
    // connectors
    reg = new ClientRegistrationMessage();
    reg.setId(tempId);
    reg.setReliable(false);
    for (int ch = CH_UNRELIABLE; ch < channels.size(); ch++) {
        if (channels.get(ch) == null)
            continue;
        send(ch, reg, false);
    }
}
Also used : ClientRegistrationMessage(com.jme3.network.message.ClientRegistrationMessage)

Example 13 with Message

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

the class KernelAdapter method dispatch.

/**
     *  Note on threading for those writing their own server 
     *  or adapter implementations.  The rule that a single connection be 
     *  processed by only one thread at a time is more about ensuring that
     *  the messages are delivered in the order that they are received
     *  than for any user-code safety.  99% of the time the user code should
     *  be writing for multithreaded access anyway.
     *
     *  <p>The issue with the messages is that if a an implementation is
     *  using a general thread pool then it would be possible for a 
     *  naive implementation to have one thread grab an Envelope from
     *  connection 1's and another grab the next Envelope.  Since an Envelope
     *  may contain several messages, delivering the second thread's messages
     *  before or during the first's would be really confusing and hard
     *  to code for in user code.</p>
     *
     *  <p>And that's why this note is here.  DefaultServer does a rudimentary
     *  per-connection locking but it couldn't possibly guard against
     *  out of order Envelope processing.</p>    
     */
protected void dispatch(Endpoint p, Message m) {
    // here.
    if (m instanceof ClientRegistrationMessage) {
        server.registerClient(this, p, (ClientRegistrationMessage) m);
        return;
    }
    try {
        HostedConnection source = getConnection(p);
        if (source == null) {
            if (reliable) {
                // If it's a reliable connection then it's slightly more
                // concerning but this can happen all the time for a UDP endpoint.
                log.log(Level.WARNING, "Recieved message from unconnected endpoint:" + p + "  message:" + m);
            }
            return;
        }
        messageDispatcher.messageReceived(source, m);
    } catch (Exception e) {
        reportError(p, m, e);
    }
}
Also used : HostedConnection(com.jme3.network.HostedConnection) ClientRegistrationMessage(com.jme3.network.message.ClientRegistrationMessage)

Example 14 with Message

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

the class RpcConnection method callAsync.

/**
     *  Performs a remote procedure call with the specified arguments but does
     *  not wait for a response.  The outbound message is sent on the specified channel.
     *  There is no inbound response message. 
     */
public void callAsync(byte channel, short objId, short procId, Object... args) {
    RpcCallMessage msg = new RpcCallMessage(-1, channel, objId, procId, args);
    if (log.isLoggable(Level.FINEST)) {
        log.log(Level.FINEST, "Sending:{0}  on channel:{1}", new Object[] { msg, channel });
    }
    connection.send(channel, msg);
}
Also used : RpcCallMessage(com.jme3.network.service.rpc.msg.RpcCallMessage)

Example 15 with Message

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

the class ClientSerializerRegistrationsService method messageReceived.

@Override
public void messageReceived(Client source, Message m) {
    // We only wait for one kind of message...
    SerializerRegistrationsMessage msg = (SerializerRegistrationsMessage) m;
    msg.registerAll();
}
Also used : SerializerRegistrationsMessage(com.jme3.network.message.SerializerRegistrationsMessage)

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