use of zmq.Msg in project jeromq by zeromq.
the class StreamEngine method inEvent.
@Override
public void inEvent() {
assert (!ioError);
// If still handshaking, receive and process the greeting message.
if (handshaking) {
if (!handshake()) {
return;
}
}
assert (decoder != null);
// If there has been an I/O error, stop polling.
if (inputStopped) {
ioObject.removeHandle(handle);
handle = null;
ioError = true;
return;
}
// If there's no data to process in the buffer...
if (insize == 0) {
// Retrieve the buffer and read as much data as possible.
// Note that buffer can be arbitrarily large. However, we assume
// the underlying TCP layer has fixed buffer size and thus the
// number of bytes read will be always limited.
inpos = decoder.getBuffer();
int rc = read(inpos);
if (rc == 0) {
error(ErrorReason.CONNECTION);
}
if (rc == -1) {
if (!errno.is(ZError.EAGAIN)) {
error(ErrorReason.CONNECTION);
}
return;
}
// Adjust input size
inpos.flip();
insize = rc;
}
boolean rc = false;
ValueReference<Integer> processed = new ValueReference<>(0);
while (insize > 0) {
// Push the data to the decoder.
Step.Result result = decoder.decode(inpos, insize, processed);
assert (processed.get() <= insize);
insize -= processed.get();
if (result == Step.Result.MORE_DATA) {
rc = true;
break;
}
if (result == Step.Result.ERROR) {
rc = false;
break;
}
Msg msg = decoder.msg();
rc = processMsg.apply(msg);
if (!rc) {
break;
}
}
// or the session has rejected the message.
if (!rc) {
if (!errno.is(ZError.EAGAIN)) {
error(ErrorReason.PROTOCOL);
return;
}
inputStopped = true;
ioObject.resetPollIn(handle);
}
// Flush all messages the decoder may have produced.
session.flush();
}
use of zmq.Msg in project jeromq by zeromq.
the class StreamEngine method identityMsg.
private Msg identityMsg() {
Msg msg = new Msg(options.identitySize);
if (options.identitySize > 0) {
msg.put(options.identity, 0, options.identitySize);
}
nextMsg = pullMsgFromSession;
return msg;
}
use of zmq.Msg in project jeromq by zeromq.
the class StreamEngine method producePongMessage.
private Msg producePongMessage(byte[] pingContext) {
assert (mechanism != null);
assert (pingContext != null);
Msg msg = new Msg(5 + pingContext.length);
msg.setFlags(Msg.COMMAND);
msg.putShortString("PONG");
msg.put(pingContext);
msg = mechanism.encode(msg);
nextMsg = pullAndEncode;
return msg;
}
use of zmq.Msg 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();
}
use of zmq.Msg in project jeromq by zeromq.
the class StreamEngine method processIdentityMsg.
private boolean processIdentityMsg(Msg msg) {
if (options.recvIdentity) {
msg.setFlags(Msg.IDENTITY);
boolean rc = session.pushMsg(msg);
assert (rc);
}
if (subscriptionRequired) {
// Inject the subscription message, so that also
// ZMQ 2.x peers receive published messages.
Msg subscription = new Msg(1);
subscription.put((byte) 1);
boolean rc = session.pushMsg(subscription);
assert (rc);
}
processMsg = pushMsgToSession;
return true;
}
Aggregations