Search in sources :

Example 1 with Endpoint

use of com.jme3.network.kernel.Endpoint 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 2 with Endpoint

use of com.jme3.network.kernel.Endpoint in project jmonkeyengine by jMonkeyEngine.

the class UdpEndpoint method send.

public void send(ByteBuffer data) {
    if (!isConnected()) {
        throw new KernelException("Endpoint is not connected:" + this);
    }
    try {
        DatagramPacket p = new DatagramPacket(data.array(), data.position(), data.remaining(), address);
        // Just queue it up for the kernel threads to write
        // out
        kernel.enqueueWrite(this, p);
    //socket.send(p);
    } catch (Exception e) {
        if (e instanceof SocketException) {
            throw new KernelException("Error sending datagram to:" + address, e);
        } else if (e instanceof RuntimeException) {
            throw (RuntimeException) e;
        } else {
            throw new RuntimeException(e);
        }
    }
}
Also used : SocketException(java.net.SocketException) DatagramPacket(java.net.DatagramPacket) KernelException(com.jme3.network.kernel.KernelException) SocketException(java.net.SocketException) KernelException(com.jme3.network.kernel.KernelException) IOException(java.io.IOException)

Example 3 with Endpoint

use of com.jme3.network.kernel.Endpoint in project jmonkeyengine by jMonkeyEngine.

the class DefaultServer method registerClient.

protected void registerClient(KernelAdapter ka, Endpoint p, ClientRegistrationMessage m) {
    Connection addedConnection = null;
    // important enough I won't take chances
    synchronized (this) {
        // Grab the random ID that the client created when creating
        // its two registration messages
        long tempId = m.getId();
        // See if we already have one
        Connection c = connecting.remove(tempId);
        if (c == null) {
            c = new Connection(channels.size());
            log.log(Level.FINE, "Registering client for endpoint, pass 1:{0}.", p);
        } else {
            log.log(Level.FINE, "Refining client registration for endpoint:{0}.", p);
        }
        // Fill in what we now know
        int channel = getChannel(ka);
        c.setChannel(channel, p);
        log.log(Level.FINE, "Setting up channel:{0}", channel);
        // and we will send the connection information
        if (channel == CH_RELIABLE) {
            // over the reliable connection at this point.
            if (!getGameName().equals(m.getGameName()) || getVersion() != m.getVersion()) {
                log.log(Level.FINE, "Kicking client due to name/version mismatch:{0}.", c);
                // Need to kick them off... I may regret doing this from within
                // the sync block but the alternative is more code
                c.close("Server client mismatch, server:" + getGameName() + " v" + getVersion() + "  client:" + m.getGameName() + " v" + m.getVersion());
                return;
            }
            // Else send the extra channel information to the client
            if (!alternatePorts.isEmpty()) {
                ChannelInfoMessage cim = new ChannelInfoMessage(m.getId(), alternatePorts);
                c.send(cim);
            }
        }
        if (c.isComplete()) {
            // Then we are fully connected
            if (connections.put(c.getId(), c) == null) {
                for (Endpoint cp : c.channels) {
                    if (cp == null)
                        continue;
                    endpointConnections.put(cp, c);
                }
                addedConnection = c;
            }
        } else {
            // Need to keep getting channels so we'll keep it in
            // the map
            connecting.put(tempId, c);
        }
    }
    // over synchronizing which is the path to deadlocks
    if (addedConnection != null) {
        log.log(Level.FINE, "Client registered:{0}.", addedConnection);
        // Send the ID back to the client letting it know it's
        // fully connected.
        m = new ClientRegistrationMessage();
        m.setId(addedConnection.getId());
        m.setReliable(true);
        addedConnection.send(m);
        // Now we can notify the listeners about the
        // new connection.
        fireConnectionAdded(addedConnection);
        // Send a second registration message with an invalid ID
        // to let the connection know that it can start its services
        m = new ClientRegistrationMessage();
        m.setId(-1);
        m.setReliable(true);
        addedConnection.send(m);
    }
}
Also used : Endpoint(com.jme3.network.kernel.Endpoint) ChannelInfoMessage(com.jme3.network.message.ChannelInfoMessage) Endpoint(com.jme3.network.kernel.Endpoint) ClientRegistrationMessage(com.jme3.network.message.ClientRegistrationMessage)

Example 4 with Endpoint

use of com.jme3.network.kernel.Endpoint in project jmonkeyengine by jMonkeyEngine.

the class BasicProfiler method createMesh.

protected final void createMesh() {
    if (mesh == null) {
        mesh = new Mesh();
        mesh.setMode(Mesh.Mode.Lines);
    }
    mesh.setBuffer(Type.Position, 3, BufferUtils.createFloatBuffer(size * 4 * 3));
    FloatBuffer cb = BufferUtils.createFloatBuffer(size * 4 * 4);
    for (int i = 0; i < size; i++) {
        // For each index we add 4 colors, one for each line
        // endpoint for two layers.
        cb.put(0.5f).put(0.5f).put(0).put(1);
        cb.put(1).put(1).put(0).put(1);
        cb.put(0).put(0.5f).put(0.5f).put(1);
        cb.put(0).put(1).put(1).put(1);
    }
    mesh.setBuffer(Type.Color, 4, cb);
}
Also used : Mesh(com.jme3.scene.Mesh) FloatBuffer(java.nio.FloatBuffer)

Example 5 with Endpoint

use of com.jme3.network.kernel.Endpoint 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)

Aggregations

KernelException (com.jme3.network.kernel.KernelException)3 IOException (java.io.IOException)3 ClientRegistrationMessage (com.jme3.network.message.ClientRegistrationMessage)2 HostedConnection (com.jme3.network.HostedConnection)1 Endpoint (com.jme3.network.kernel.Endpoint)1 ChannelInfoMessage (com.jme3.network.message.ChannelInfoMessage)1 Mesh (com.jme3.scene.Mesh)1 DatagramPacket (java.net.DatagramPacket)1 SocketException (java.net.SocketException)1 FloatBuffer (java.nio.FloatBuffer)1