Search in sources :

Example 1 with MessageHeader

use of games.strategy.net.MessageHeader in project triplea by triplea-game.

the class Decoder method loop.

private void loop() {
    while (running) {
        try {
            final SocketReadData data = reader.take();
            if (data == null || !running) {
                continue;
            }
            try {
                final MessageHeader header = IoUtils.readFromMemory(data.getData(), is -> {
                    try {
                        return readMessageHeader(data.getChannel(), objectStreamFactory.create(is));
                    } catch (final ClassNotFoundException e) {
                        throw new IOException(e);
                    }
                });
                // make sure we are still open
                final Socket s = data.getChannel().socket();
                if (!running || s == null || s.isInputShutdown()) {
                    continue;
                }
                final QuarantineConversation converstation = quarantine.get(data.getChannel());
                if (converstation != null) {
                    sendQuarantine(data.getChannel(), converstation, header);
                } else {
                    if (nioSocket.getLocalNode() == null) {
                        throw new IllegalStateException("we are writing messages, but no local node");
                    }
                    if (header.getFrom() == null) {
                        throw new IllegalArgumentException("Null from:" + header);
                    }
                    nioSocket.messageReceived(header, data.getChannel());
                }
            } catch (final IOException | RuntimeException e) {
                // we are reading from memory here
                // there should be no network errors, something
                // is odd
                logger.log(Level.SEVERE, "error reading object", e);
                errorReporter.error(data.getChannel(), e);
            }
        } catch (final InterruptedException e) {
            // otherwise re-interrupt this thread and keep running
            if (running) {
                Thread.currentThread().interrupt();
            }
        }
    }
}
Also used : MessageHeader(games.strategy.net.MessageHeader) IOException(java.io.IOException) Socket(java.net.Socket)

Example 2 with MessageHeader

use of games.strategy.net.MessageHeader in project triplea by triplea-game.

the class ServerQuarantineConversation method send.

private void send(final Serializable object) {
    // this messenger is quarantined, so to and from dont matter
    final MessageHeader header = new MessageHeader(Node.NULL_NODE, Node.NULL_NODE, object);
    socket.send(channel, header);
}
Also used : MessageHeader(games.strategy.net.MessageHeader)

Example 3 with MessageHeader

use of games.strategy.net.MessageHeader in project triplea by triplea-game.

the class Decoder method readMessageHeader.

private MessageHeader readMessageHeader(final SocketChannel channel, final ObjectInputStream objectInput) throws IOException, ClassNotFoundException {
    final INode to;
    if (objectInput.read() == 1) {
        to = null;
    } else {
        if (objectInput.read() == 1) {
            // this may be null if we
            // have not yet fully joined the network
            to = nioSocket.getLocalNode();
        } else {
            to = new Node();
            ((Node) to).readExternal(objectInput);
        }
    }
    final INode from;
    final int readMark = objectInput.read();
    if (readMark == 1) {
        from = nioSocket.getRemoteNode(channel);
    } else if (readMark == 2) {
        from = null;
    } else {
        from = new Node();
        ((Node) from).readExternal(objectInput);
    }
    final Serializable message;
    final byte type = (byte) objectInput.read();
    if (type != Byte.MAX_VALUE) {
        final Externalizable template = getTemplate(type);
        template.readExternal(objectInput);
        message = template;
    } else {
        message = (Serializable) objectInput.readObject();
    }
    return new MessageHeader(to, from, message);
}
Also used : INode(games.strategy.net.INode) Serializable(java.io.Serializable) INode(games.strategy.net.INode) Node(games.strategy.net.Node) Externalizable(java.io.Externalizable) MessageHeader(games.strategy.net.MessageHeader)

Example 4 with MessageHeader

use of games.strategy.net.MessageHeader in project triplea by triplea-game.

the class ClientQuarantineConversation method send.

private void send(final Serializable object) {
    // this messenger is quarantined, so to and from dont matter
    final MessageHeader header = new MessageHeader(Node.NULL_NODE, Node.NULL_NODE, object);
    socket.send(channel, header);
}
Also used : MessageHeader(games.strategy.net.MessageHeader)

Aggregations

MessageHeader (games.strategy.net.MessageHeader)4 INode (games.strategy.net.INode)1 Node (games.strategy.net.Node)1 Externalizable (java.io.Externalizable)1 IOException (java.io.IOException)1 Serializable (java.io.Serializable)1 Socket (java.net.Socket)1