use of com.generallycloud.baseio.protocol.DatagramPacket 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