Search in sources :

Example 1 with DatagramPacket

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");
    }
}
Also used : ClosedChannelException(com.generallycloud.baseio.ClosedChannelException) Authority(com.generallycloud.baseio.container.authority.Authority) OnFuture(com.generallycloud.baseio.container.OnFuture) IOException(java.io.IOException) JSONObject(com.alibaba.fastjson.JSONObject) SocketSession(com.generallycloud.baseio.component.SocketSession) DatagramPacket(com.generallycloud.baseio.protocol.DatagramPacket) ProtobaseFuture(com.generallycloud.baseio.codec.protobase.future.ProtobaseFuture) Future(com.generallycloud.baseio.protocol.Future) OnFuture(com.generallycloud.baseio.container.OnFuture) Waiter(com.generallycloud.baseio.concurrent.Waiter)

Aggregations

JSONObject (com.alibaba.fastjson.JSONObject)1 ClosedChannelException (com.generallycloud.baseio.ClosedChannelException)1 ProtobaseFuture (com.generallycloud.baseio.codec.protobase.future.ProtobaseFuture)1 SocketSession (com.generallycloud.baseio.component.SocketSession)1 Waiter (com.generallycloud.baseio.concurrent.Waiter)1 OnFuture (com.generallycloud.baseio.container.OnFuture)1 Authority (com.generallycloud.baseio.container.authority.Authority)1 DatagramPacket (com.generallycloud.baseio.protocol.DatagramPacket)1 Future (com.generallycloud.baseio.protocol.Future)1 IOException (java.io.IOException)1