use of com.generallycloud.baseio.codec.protobase.future.ProtobaseFuture in project baseio by generallycloud.
the class TestListenSimpleServlet method doAccept.
@Override
protected void doAccept(SocketSession session, ParamedProtobaseFuture future) throws Exception {
String test = future.getReadText();
if (StringUtil.isNullOrBlank(test)) {
test = "test";
}
future.write(test);
future.write("$");
session.flush(future);
for (int i = 0; i < 5; i++) {
ProtobaseFuture f = new ProtobaseFutureImpl(session.getContext(), future.getFutureId(), future.getFutureName());
f.write(test);
f.write("$");
session.flush(f);
}
}
use of com.generallycloud.baseio.codec.protobase.future.ProtobaseFuture in project baseio by generallycloud.
the class FileSendUtil method sendFile.
public void sendFile(SocketSession session, String serviceName, File file, int cacheSize) throws Exception {
FileInputStream inputStream = new FileInputStream(file);
int available = inputStream.available();
int time = (available + cacheSize) / cacheSize - 1;
byte[] cache = new byte[cacheSize];
JSONObject json = new JSONObject();
json.put(FileReceiveUtil.FILE_NAME, file.getName());
json.put(FileReceiveUtil.IS_END, false);
String jsonString = json.toJSONString();
for (int i = 0; i < time; i++) {
FileUtil.readInputStream(inputStream, cache);
ProtobaseFuture f = new ProtobaseFutureImpl(session.getContext(), serviceName);
f.write(jsonString);
f.writeBinary(cache);
session.flush(f);
}
int r = FileUtil.readInputStream(inputStream, cache);
json.put(FileReceiveUtil.IS_END, true);
ProtobaseFuture f = new ProtobaseFutureImpl(session.getContext(), serviceName);
f.write(json.toJSONString());
f.writeBinary(cache, 0, r);
session.flush(f);
CloseUtil.close(inputStream);
}
use of com.generallycloud.baseio.codec.protobase.future.ProtobaseFuture in project baseio by generallycloud.
the class ProtobaseProtocolEncoder method encode.
@Override
public void encode(SocketChannel channel, ChannelFuture future) throws IOException {
ByteBufAllocator allocator = channel.getByteBufAllocator();
if (future.isHeartbeat()) {
ByteBuf buf = future.isPING() ? PING.duplicate() : PONG.duplicate();
future.setByteBuf(buf);
return;
}
ProtobaseFuture f = (ProtobaseFuture) future;
String futureName = f.getFutureName();
if (StringUtil.isNullOrBlank(futureName)) {
throw new ProtocolException("future name is empty");
}
byte[] futureNameBytes = futureName.getBytes(channel.getEncoding());
if (futureNameBytes.length > Byte.MAX_VALUE) {
throw new ProtocolException("future name max length 127");
}
byte futureNameLength = (byte) futureNameBytes.length;
int allLen = 6 + futureNameLength;
int textWriteSize = f.getWriteSize();
int binaryWriteSize = f.getWriteBinarySize();
byte h1 = 0b01000000;
if (f.isBroadcast()) {
h1 |= 0b00100000;
}
if (f.getFutureId() > 0) {
h1 |= 0b00010000;
allLen += 4;
}
if (f.getSessionId() > 0) {
h1 |= 0b00001000;
allLen += 4;
}
if (f.getHashCode() > 0) {
h1 |= 0b00000100;
allLen += 4;
}
if (f.getWriteBinarySize() > 0) {
h1 |= 0b00000010;
allLen += 4;
allLen += f.getWriteBinarySize();
}
if (textWriteSize > 0) {
allLen += textWriteSize;
}
ByteBuf buf = allocator.allocate(allLen);
buf.putByte(h1);
buf.putByte(futureNameLength);
buf.putInt(textWriteSize);
if (f.getFutureId() > 0) {
buf.putInt(f.getFutureId());
}
if (f.getSessionId() > 0) {
buf.putInt(f.getSessionId());
}
if (f.getHashCode() > 0) {
buf.putInt(f.getHashCode());
}
if (binaryWriteSize > 0) {
buf.putInt(binaryWriteSize);
}
buf.put(futureNameBytes);
if (textWriteSize > 0) {
buf.put(f.getWriteBuffer(), 0, textWriteSize);
}
if (binaryWriteSize > 0) {
buf.put(f.getWriteBinary(), 0, binaryWriteSize);
}
future.setByteBuf(buf.flip());
}
use of com.generallycloud.baseio.codec.protobase.future.ProtobaseFuture in project baseio by generallycloud.
the class SimpleTestProtobaseClient method main.
public static void main(String[] args) throws Exception {
IoEventHandleAdaptor eventHandleAdaptor = new IoEventHandleAdaptor() {
@Override
public void accept(SocketSession session, Future future) throws Exception {
System.out.println();
System.out.println("____________________" + future.getReadText());
System.out.println();
}
};
SocketChannelContext context = new NioSocketChannelContext(new ServerConfiguration("localhost", 18300));
context.getServerConfiguration().setSERVER_ENABLE_MEMORY_POOL_DIRECT(true);
SocketChannelConnector connector = new SocketChannelConnector(context);
connector.setTimeout(99999999);
context.setIoEventHandleAdaptor(eventHandleAdaptor);
context.addSessionEventListener(new LoggerSocketSEListener());
context.setProtocolFactory(new ProtobaseProtocolFactory());
SocketSession session = connector.connect();
ProtobaseFuture future = new ProtobaseFutureImpl(context, "test222");
future.write("hello server!");
session.flush(future);
ThreadUtil.sleep(100);
CloseUtil.close(connector);
}
use of com.generallycloud.baseio.codec.protobase.future.ProtobaseFuture in project baseio by generallycloud.
the class Test404 method main.
public static void main(String[] args) throws Exception {
String serviceKey = "22";
LoggerFactory.configure();
SimpleIoEventHandle eventHandle = new SimpleIoEventHandle();
ServerConfiguration configuration = new ServerConfiguration(8300);
SocketChannelContext context = new NioSocketChannelContext(configuration);
SocketChannelConnector connector = new SocketChannelConnector(context);
context.setIoEventHandleAdaptor(eventHandle);
context.setProtocolFactory(new ParamedProtobaseProtocolFactory());
context.addSessionEventListener(new LoggerSocketSEListener());
FixedSession session = new FixedSession(connector.connect());
ProtobaseFuture future = session.request(serviceKey, null);
System.out.println(future.getReadText());
Future future1 = new ProtobaseFutureImpl(connector.getContext()).setPING();
session.getSession().flush(future1);
CloseUtil.close(connector);
}
Aggregations