Search in sources :

Example 1 with RawDecoder

use of zmq.io.coder.raw.RawDecoder in project jeromq by zeromq.

the class StreamEngine method plug.

@Override
public void plug(IOThread ioThread, SessionBase session) {
    assert (!plugged);
    plugged = true;
    // Connect to session object.
    assert (this.session == null);
    assert (session != null);
    this.session = session;
    socket = session.getSocket();
    // Connect to I/O threads poller object.
    ioObject = new IOObject(ioThread, this);
    ioObject.plug();
    handle = ioObject.addFd(fd);
    ioError = false;
    // Make sure batch sizes match large buffer sizes
    final int inBatchSize = Math.max(options.rcvbuf, Config.IN_BATCH_SIZE.getValue());
    final int outBatchSize = Math.max(options.sndbuf, Config.OUT_BATCH_SIZE.getValue());
    if (options.rawSocket) {
        decoder = instantiate(options.decoder, inBatchSize, options.maxMsgSize);
        if (decoder == null) {
            decoder = new RawDecoder(inBatchSize);
        }
        encoder = instantiate(options.encoder, outBatchSize, options.maxMsgSize);
        if (encoder == null) {
            encoder = new RawEncoder(errno, outBatchSize);
        }
        // disable handshaking for raw socket
        handshaking = false;
        nextMsg = pullMsgFromSession;
        processMsg = pushRawMsgToSession;
        if (peerAddress != null && !peerAddress.address().isEmpty()) {
            assert (metadata == null);
            // Compile metadata
            metadata = new Metadata();
            metadata.set(Metadata.PEER_ADDRESS, peerAddress.address());
        }
        // For raw sockets, send an initial 0-length message to the
        // application so that it knows a peer has connected.
        Msg connector = new Msg();
        pushRawMsgToSession(connector);
        session.flush();
    } else {
        // start optional timer, to prevent handshake hanging on no input
        setHandshakeTimer();
        // Send the 'length' and 'flags' fields of the identity message.
        // The 'length' field is encoded in the long format.
        greetingSend.put((byte) 0xff);
        Wire.putUInt64(greetingSend, options.identitySize + 1);
        greetingSend.put((byte) 0x7f);
        outpos.set(greetingSend);
        outsize = greetingSend.position();
        greetingSend.flip();
    }
    ioObject.setPollIn(handle);
    ioObject.setPollOut(handle);
    // Flush all the data that may have been already received downstream.
    inEvent();
}
Also used : Msg(zmq.Msg) RawDecoder(zmq.io.coder.raw.RawDecoder) RawEncoder(zmq.io.coder.raw.RawEncoder)

Aggregations

Msg (zmq.Msg)1 RawDecoder (zmq.io.coder.raw.RawDecoder)1 RawEncoder (zmq.io.coder.raw.RawEncoder)1