byte[] b = { 0, 4, 0x31, 0x32, 0x33, 0x34 }; // length 4, message 1234
import io.vertx.core.AbstractVerticle;
import io.vertx.core.AsyncResult;
import io.vertx.core.Handler;
import io.vertx.core.net.NetServer;
import io.vertx.core.net.NetServerOptions;
import io.vertx.core.parsetools.RecordParser;
import io.vertx.example.util.Runner;
public class TCPServer2 extends AbstractVerticle {
private FrameToken expectedToken = FrameToken.SIZE;
RecordParser frameParser = null;
@Override
public void start() throws Exception {
NetServerOptions opt = new NetServerOptions();
opt.setIdleTimeout(500); // large to assist in debugging for now
NetServer server = vertx.createNetServer(opt);
server.connectHandler(socket -> {
frameParser = RecordParser.newFixed(2, buffer -> {
switch (expectedToken) {
case SIZE:
byte[] b = buffer.getBytes(0, 2);
int frameSize = (((b[0]) & 0xFF) << 8) | ((b[1]) & 0xFF);
System.out.println("size: " + frameSize);
frameParser.fixedSizeMode(frameSize);
expectedToken = FrameToken.PAYLOAD;
break;
case PAYLOAD:
System.out.println("Payload: |" + buffer + "|");
frameParser.fixedSizeMode(2);
expectedToken = FrameToken.SIZE;
break;
}
});
socket.handler(frameParser);
});
server.listen(44444, "localhost", new Handler<AsyncResult<NetServer>>() {
@Override
public void handle(AsyncResult<NetServer> res) {
if (res.succeeded()) {
System.out.println("Server is now listening!");
}
else {
System.out.println("Failed to bind!");
}
}
});
}
public static void main(String[] args) {
Runner.runExample(TCPServer2.class);
}
}
--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+un...@googlegroups.com.
Visit this group at https://groups.google.com/group/vertx.
To view this discussion on the web, visit https://groups.google.com/d/msgid/vertx/1d5b8b71-c54e-4753-8eda-ad761036eaf3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
To view this discussion on the web, visit https://groups.google.com/d/msgid/vertx/32efcfee-4074-4afd-8f3f-91ce07ba6a2c%40googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+unsubscribe@googlegroups.com.
Visit this group at https://groups.google.com/group/vertx.
To view this discussion on the web, visit https://groups.google.com/d/msgid/vertx/32efcfee-4074-4afd-8f3f-91ce07ba6a2c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to a topic in the Google Groups "vert.x" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/vertx/_8FBnsVqY_4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to vertx+unsubscribe@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/vertx/FC009C85-B1E2-4060-886A-A048488550B7%40julienviet.com.
package io.vertx.book.http;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.AsyncResult;
import io.vertx.core.Handler;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.net.NetServer;
import io.vertx.core.net.NetServerOptions;
import io.vertx.core.net.NetSocket;
import io.vertx.core.parsetools.RecordParser;
import io.vertx.example.util.Runner;
public class TCPServer2 extends AbstractVerticle {
private FrameToken expectedToken = FrameToken.SIZE;
RecordParser parser, frameParser = null;
private Handler<NetSocket> getServerHandler() {
return socket -> {
parser = RecordParser.newFixed(2, new Handler<Buffer>() {// this line varies from the original
int size = -1;
public void handle(Buffer buff) {
if (size == -1) { // this is no different than the earlier codes switch statement
byte[] b = buff.getBytes(0, 2);
size = (((b[0]) & 0xFF) << 8) | ((b[1]) & 0xFF);
System.out.println("size: " + size);
parser.fixedSizeMode(size);
}
else {
parser.fixedSizeMode(2);
size = -1;
}
}
});
// parser.setOutput(handler); // not sure what do I need to do here.
socket.handler(parser);
};
}
@Override
public void start() throws Exception {
NetServerOptions opt = new NetServerOptions();
opt.setIdleTimeout(500); // large to assist in debugging for now
NetServer server = vertx.createNetServer(opt);
server.connectHandler(getServerHandler());
// server.connectHandler(socket -> {
// frameParser = RecordParser.newFixed(2, buffer -> {
//
// switch (expectedToken) {
//
// case SIZE:
//
// byte[] b = buffer.getBytes(0, 2);
// int frameSize = (((b[0]) & 0xFF) << 8) | ((b[1]) & 0xFF);
// System.out.println("size: " + frameSize);
// frameParser.fixedSizeMode(frameSize);
// expectedToken = FrameToken.PAYLOAD;
// break;
//
// case PAYLOAD:
// System.out.println("Payload: |" + buffer + "|");
// frameParser.fixedSizeMode(2);
// expectedToken = FrameToken.SIZE;
// break;
// }
//
// });
//
// socket.handler(frameParser);
// });
server.listen(44444, "localhost", new Handler<AsyncResult<NetServer>>() {
@Override
public void handle(AsyncResult<NetServer> res) {
if (res.succeeded()) {
System.out.println("Server is now listening!");
}
else {
System.out.println("Failed to bind!");
}
}
});
}
public static void main(String[] args) {
Runner.runExample(TCPServer2.class);
}
}
To view this discussion on the web, visit https://groups.google.com/d/msgid/vertx/52372dc7-1abd-4319-8a52-3b51ccff2917%40googlegroups.com.
-chhil
To view this discussion on the web, visit https://groups.google.com/d/msgid/vertx/32efcfee-4074-4afd-8f3f-91ce07ba6a2c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to a topic in the Google Groups "vert.x" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/vertx/_8FBnsVqY_4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to vertx+un...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/vertx/FC009C85-B1E2-4060-886A-A048488550B7%40julienviet.com.
--
You received this message because you are subscribed to a topic in the Google Groups "vert.x" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/vertx/_8FBnsVqY_4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to vertx+un...@googlegroups.com.
private Handler<NetSocket> getServerHandler() {
return socket -> {
parser = RecordParser.newFixed(2, null);
Handler<Buffer> handler = new Handler<Buffer>() {
int size = -1;
public void handle(Buffer buff) {
if (size == -1) {
byte[] b = buff.getBytes(0, 2);
size = (((b[0]) & 0xFF) << 8) | ((b[1]) & 0xFF);
System.out.println("size: " + size);
parser.fixedSizeMode(size);
}
else {
parser.fixedSizeMode(2);
size = -1;
}
}
};
parser.setOutput(handler);
socket.handler(parser);
};
}
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+unsubscribe@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/vertx/e1c1cda9-e44c-40d6-8608-71436f7de5b6%40googlegroups.com.
To unsubscribe from this group and all its topics, send an email to vertx+unsubscribe@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/vertx/CAGDHiz%3DpTCtVEjUmjYHPCLmvZTyDR%2B0XUN2mwAkAc3-smrFG1Q%40mail.gmail.com.