use of com.generallycloud.baseio.component.SocketSession in project baseio by generallycloud.
the class SimpleTestProtobaseServer method main.
public static void main(String[] args) throws Exception {
DebugUtil.setEnableDebug(true);
IoEventHandleAdaptor eventHandleAdaptor = new IoEventHandleAdaptor() {
@Override
public void accept(SocketSession session, Future future) throws Exception {
DebugUtil.debug("receive:" + future.getReadText());
future.write("yes server already accept your message:");
future.write(future.getReadText());
session.flush(future);
}
};
SocketChannelContext context = new NioSocketChannelContext(new ServerConfiguration(18300));
context.getServerConfiguration().setSERVER_ENABLE_MEMORY_POOL_DIRECT(true);
context.getServerConfiguration().setSERVER_SESSION_IDLE_TIME(60 * 60 * 1000);
SocketChannelAcceptor acceptor = new SocketChannelAcceptor(context);
context.addSessionEventListener(new LoggerSocketSEListener());
context.setBeatFutureFactory(new ProtobaseBeatFutureFactory());
context.addSessionIdleEventListener(new SocketSessionAliveSEListener());
context.setIoEventHandleAdaptor(eventHandleAdaptor);
context.setProtocolFactory(new ProtobaseProtocolFactory());
acceptor.bind();
}
use of com.generallycloud.baseio.component.SocketSession in project baseio by generallycloud.
the class TestSessionDisconnect method main.
public static void main(String[] args) throws Exception {
String serviceName = "TestSessionDisconnectServlet";
String param = "ttt";
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 ProtobaseProtocolFactory());
context.addSessionEventListener(new LoggerSocketSEListener());
FixedSession session = new FixedSession(connector.connect());
session.login("admin", "admin100");
ProtobaseFuture future = session.request(serviceName, param);
System.out.println(future.getReadText());
session.listen(serviceName, new OnFuture() {
@Override
public void onResponse(SocketSession session, Future future) {
ProtobaseFuture f = (ProtobaseFuture) future;
System.out.println(f.getReadText());
}
});
session.write(serviceName, param);
ThreadUtil.sleep(9999);
CloseUtil.close(connector);
}
use of com.generallycloud.baseio.component.SocketSession in project baseio by generallycloud.
the class TestSimple method main.
public static void main(String[] args) throws Exception {
String serviceKey = "/test-simple";
String param = "ttt";
IoEventHandleAdaptor eventHandle = new IoEventHandleAdaptor() {
@Override
public void accept(SocketSession session, Future future) throws Exception {
System.out.println("________________________" + future.getReadText());
}
};
SocketChannelContext context = new NioSocketChannelContext(new ServerConfiguration(18300));
SocketChannelConnector connector = new SocketChannelConnector(context);
context.setProtocolFactory(new ProtobaseProtocolFactory());
context.addSessionEventListener(new LoggerSocketSEListener());
context.setIoEventHandleAdaptor(eventHandle);
SocketSession session = connector.connect();
ProtobaseFuture f = new ProtobaseFutureImpl(connector.getContext(), serviceKey);
f.write(param);
session.flush(f);
ThreadUtil.sleep(500);
CloseUtil.close(connector);
}
use of com.generallycloud.baseio.component.SocketSession in project baseio by generallycloud.
the class ServerDatagramPacketAcceptor method accept.
@Override
public void accept(DatagramSession session, DatagramPacket packet) throws IOException {
if (packet.getType() == DatagramPacket.TYPE_ACTION) {
execute(session, new DatagramRequest(packet.getDataString()));
return;
}
// logger.debug("___________________server receive,packet:{}",packet);
SocketSession socketSession = session.getSocketSession();
if (socketSession == null) {
logger.debug("___________________null session,packet:{}", packet);
return;
}
// FIXME UDP
doAccept(session, packet, socketSession);
}
use of com.generallycloud.baseio.component.SocketSession in project baseio by generallycloud.
the class RTPClient method bindTCPSession.
public void bindTCPSession() throws IOException {
if (connector == null) {
throw new IllegalArgumentException("null udp connector");
}
Authority authority = session.getAuthority();
if (authority == null) {
throw new IllegalArgumentException("not login");
}
JSONObject json = new JSONObject();
json.put("serviceName", RTPServerDPAcceptor.BIND_SESSION);
json.put("username", authority.getUsername());
json.put("password", authority.getPassword());
final DatagramPacket packet = DatagramPacket.createSendPacket(json.toJSONString().getBytes(context.getEncoding()));
final String BIND_SESSION_CALLBACK = RTPServerDPAcceptor.BIND_SESSION_CALLBACK;
final Waiter<Integer> waiter = new Waiter<>();
session.listen(BIND_SESSION_CALLBACK, new OnFuture() {
@Override
public void onResponse(SocketSession session, Future future) {
waiter.setPayload(0);
}
});
final byte[] shortWaiter = new byte[] {};
ThreadUtil.execute(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
try {
connector.sendDatagramPacket(packet);
} catch (IOException e) {
DebugUtil.debug(e);
}
if (waiter.isDnoe()) {
break;
}
synchronized (shortWaiter) {
try {
shortWaiter.wait(300);
} catch (InterruptedException e) {
break;
}
}
}
}
});
if (waiter.await(3000)) {
CloseUtil.close(connector);
throw new ClosedChannelException("disconnected");
}
}
Aggregations