Search in sources :

Example 1 with StreamEngine

use of zmq.io.StreamEngine in project jeromq by zeromq.

the class TcpConnecter method connectEvent.

@Override
public void connectEvent() {
    ioObject.removeHandle(handle);
    handle = null;
    SocketChannel channel = connect();
    if (channel == null) {
        // Handle the error condition by attempt to reconnect.
        close();
        addReconnectTimer();
        return;
    }
    try {
        TcpUtils.tuneTcpSocket(channel);
        TcpUtils.tuneTcpKeepalives(channel, options.tcpKeepAlive, options.tcpKeepAliveCnt, options.tcpKeepAliveIdle, options.tcpKeepAliveIntvl);
    } catch (IOException e) {
        throw new ZError.IOException(e);
    }
    // remember our fd for ZMQ_SRCFD in messages
    // socket.setFd(channel);
    // Create the engine object for this connection.
    StreamEngine engine;
    try {
        engine = new StreamEngine(channel, options, addr.toString());
    } catch (ZError.InstantiationException e) {
        // TODO V4 socket.eventConnectDelayed(addr.toString(), -1);
        return;
    }
    this.fd = null;
    // Attach the engine to the corresponding session object.
    sendAttach(session, engine);
    // Shut the connecter down.
    terminate();
    socket.eventConnected(addr.toString(), channel);
}
Also used : SocketChannel(java.nio.channels.SocketChannel) StreamEngine(zmq.io.StreamEngine) ZError(zmq.ZError) IOException(java.io.IOException)

Example 2 with StreamEngine

use of zmq.io.StreamEngine in project jeromq by zeromq.

the class TcpListener method acceptEvent.

@Override
public void acceptEvent() {
    SocketChannel channel;
    try {
        channel = accept();
        // If connection was reset by the peer in the meantime, just ignore it.
        if (channel == null) {
            socket.eventAcceptFailed(endpoint, ZError.EADDRNOTAVAIL);
            return;
        }
        TcpUtils.tuneTcpSocket(channel);
        TcpUtils.tuneTcpKeepalives(channel, options.tcpKeepAlive, options.tcpKeepAliveCnt, options.tcpKeepAliveIdle, options.tcpKeepAliveIntvl);
    } catch (IOException e) {
        // If connection was reset by the peer in the meantime, just ignore it.
        // TODO: Handle specific errors like ENFILE/EMFILE etc.
        socket.eventAcceptFailed(endpoint, ZError.exccode(e));
        return;
    }
    // remember our fd for ZMQ_SRCFD in messages
    // socket.setFd(channel);
    // Create the engine object for this connection.
    StreamEngine engine = null;
    try {
        engine = new StreamEngine(channel, options, endpoint);
    } catch (ZError.InstantiationException e) {
        socket.eventAcceptFailed(endpoint, ZError.EINVAL);
        return;
    }
    // Choose I/O thread to run connecter in. Given that we are already
    // running in an I/O thread, there must be at least one available.
    IOThread ioThread = chooseIoThread(options.affinity);
    assert (ioThread != null);
    // Create and launch a session object.
    SessionBase session = Sockets.createSession(ioThread, false, socket, options, null);
    assert (session != null);
    session.incSeqnum();
    launchChild(session);
    sendAttach(session, engine, false);
    socket.eventAccepted(endpoint, channel);
}
Also used : ServerSocketChannel(java.nio.channels.ServerSocketChannel) SocketChannel(java.nio.channels.SocketChannel) StreamEngine(zmq.io.StreamEngine) ZError(zmq.ZError) IOThread(zmq.io.IOThread) IOException(java.io.IOException) SessionBase(zmq.io.SessionBase)

Aggregations

IOException (java.io.IOException)2 SocketChannel (java.nio.channels.SocketChannel)2 ZError (zmq.ZError)2 StreamEngine (zmq.io.StreamEngine)2 ServerSocketChannel (java.nio.channels.ServerSocketChannel)1 IOThread (zmq.io.IOThread)1 SessionBase (zmq.io.SessionBase)1