Search in sources :

Example 1 with Protocol

use of com.koushikdutta.async.http.Protocol in project AndroidAsync by koush.

the class SpdyMiddleware method createHandshakeCallback.

@Override
protected AsyncSSLSocketWrapper.HandshakeCallback createHandshakeCallback(final GetSocketData data, final ConnectCallback callback) {
    final String key = data.state.get("spdykey");
    if (key == null)
        return super.createHandshakeCallback(data, callback);
    return new AsyncSSLSocketWrapper.HandshakeCallback() {

        @Override
        public void onHandshakeCompleted(Exception e, AsyncSSLSocket socket) {
            data.request.logv("checking spdy handshake");
            if (e != null || nativeGetAlpnNegotiatedProtocol == null) {
                invokeConnect(key, callback, e, socket);
                noSpdy(key);
                return;
            }
            String protoString;
            try {
                long ptr = (Long) sslNativePointer.get(socket.getSSLEngine());
                byte[] proto = (byte[]) nativeGetAlpnNegotiatedProtocol.invoke(null, ptr);
                if (proto == null) {
                    invokeConnect(key, callback, null, socket);
                    noSpdy(key);
                    return;
                }
                protoString = new String(proto);
                Protocol p = Protocol.get(protoString);
                if (p == null || !p.needsSpdyConnection()) {
                    invokeConnect(key, callback, null, socket);
                    noSpdy(key);
                    return;
                }
            } catch (Exception ex) {
                throw new AssertionError(ex);
            }
            final AsyncSpdyConnection connection = new AsyncSpdyConnection(socket, Protocol.get(protoString)) {

                boolean hasReceivedSettings;

                @Override
                public void settings(boolean clearPrevious, Settings settings) {
                    super.settings(clearPrevious, settings);
                    if (!hasReceivedSettings) {
                        hasReceivedSettings = true;
                        SpdyConnectionWaiter waiter = connections.get(key);
                        if (waiter.originalCancellable.setComplete()) {
                            data.request.logv("using new spdy connection for host: " + data.request.getUri().getHost());
                            newSocket(data, this, callback);
                        }
                        waiter.setComplete(this);
                    }
                }
            };
            try {
                connection.sendConnectionPreface();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    };
}
Also used : AsyncSSLSocket(com.koushikdutta.async.AsyncSSLSocket) IOException(java.io.IOException) IOException(java.io.IOException) Protocol(com.koushikdutta.async.http.Protocol)

Example 2 with Protocol

use of com.koushikdutta.async.http.Protocol in project AndroidAsync by koush.

the class SpdyMiddleware method concatLengthPrefixed.

static byte[] concatLengthPrefixed(Protocol... protocols) {
    ByteBuffer result = ByteBuffer.allocate(8192);
    for (Protocol protocol : protocols) {
        // No HTTP/1.0 for NPN.
        if (protocol == Protocol.HTTP_1_0)
            continue;
        result.put((byte) protocol.toString().length());
        result.put(protocol.toString().getBytes(Charsets.UTF_8));
    }
    result.flip();
    byte[] ret = new ByteBufferList(result).getAllByteArray();
    return ret;
}
Also used : ByteBufferList(com.koushikdutta.async.ByteBufferList) Protocol(com.koushikdutta.async.http.Protocol) ByteBuffer(java.nio.ByteBuffer)

Aggregations

Protocol (com.koushikdutta.async.http.Protocol)2 AsyncSSLSocket (com.koushikdutta.async.AsyncSSLSocket)1 ByteBufferList (com.koushikdutta.async.ByteBufferList)1 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1