Search in sources :

Example 21 with CompletedCallback

use of com.koushikdutta.async.callback.CompletedCallback in project AndroidAsync by koush.

the class AsyncSSLSocketWrapper method handshake.

public static void handshake(AsyncSocket socket, String host, int port, SSLEngine sslEngine, TrustManager[] trustManagers, HostnameVerifier verifier, boolean clientMode, final HandshakeCallback callback) {
    AsyncSSLSocketWrapper wrapper = new AsyncSSLSocketWrapper(socket, host, port, sslEngine, trustManagers, verifier, clientMode);
    wrapper.handshakeCallback = callback;
    socket.setClosedCallback(new CompletedCallback() {

        @Override
        public void onCompleted(Exception ex) {
            if (ex != null)
                callback.onHandshakeCompleted(ex, null);
            else
                callback.onHandshakeCompleted(new SSLException("socket closed during handshake"), null);
        }
    });
    try {
        wrapper.engine.beginHandshake();
        wrapper.handleHandshakeStatus(wrapper.engine.getHandshakeStatus());
    } catch (SSLException e) {
        wrapper.report(e);
    }
}
Also used : CompletedCallback(com.koushikdutta.async.callback.CompletedCallback) SSLException(javax.net.ssl.SSLException) SSLException(javax.net.ssl.SSLException) GeneralSecurityException(java.security.GeneralSecurityException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 22 with CompletedCallback

use of com.koushikdutta.async.callback.CompletedCallback in project AndroidAsync by koush.

the class Util method pump.

public static void pump(final InputStream is, final long max, final DataSink ds, final CompletedCallback callback) {
    final CompletedCallback wrapper = new CompletedCallback() {

        boolean reported;

        @Override
        public void onCompleted(Exception ex) {
            if (reported)
                return;
            reported = true;
            callback.onCompleted(ex);
        }
    };
    final WritableCallback cb = new WritableCallback() {

        int totalRead = 0;

        private void cleanup() {
            ds.setClosedCallback(null);
            ds.setWriteableCallback(null);
            pending.recycle();
            StreamUtility.closeQuietly(is);
        }

        ByteBufferList pending = new ByteBufferList();

        Allocator allocator = new Allocator();

        @Override
        public void onWriteable() {
            try {
                do {
                    if (!pending.hasRemaining()) {
                        ByteBuffer b = allocator.allocate();
                        long toRead = Math.min(max - totalRead, b.capacity());
                        int read = is.read(b.array(), 0, (int) toRead);
                        if (read == -1 || totalRead == max) {
                            cleanup();
                            wrapper.onCompleted(null);
                            return;
                        }
                        allocator.track(read);
                        totalRead += read;
                        b.position(0);
                        b.limit(read);
                        pending.add(b);
                    }
                    ds.write(pending);
                } while (!pending.hasRemaining());
            } catch (Exception e) {
                cleanup();
                wrapper.onCompleted(e);
            }
        }
    };
    ds.setWriteableCallback(cb);
    ds.setClosedCallback(wrapper);
    cb.onWriteable();
}
Also used : Allocator(com.koushikdutta.async.util.Allocator) CompletedCallback(com.koushikdutta.async.callback.CompletedCallback) WritableCallback(com.koushikdutta.async.callback.WritableCallback) ByteBuffer(java.nio.ByteBuffer) IOException(java.io.IOException)

Example 23 with CompletedCallback

use of com.koushikdutta.async.callback.CompletedCallback in project AndroidAsync by koush.

the class Util method pump.

public static void pump(final File file, final DataSink ds, final CompletedCallback callback) {
    try {
        if (file == null || ds == null) {
            callback.onCompleted(null);
            return;
        }
        final InputStream is = new FileInputStream(file);
        pump(is, ds, new CompletedCallback() {

            @Override
            public void onCompleted(Exception ex) {
                try {
                    is.close();
                    callback.onCompleted(ex);
                } catch (IOException e) {
                    callback.onCompleted(e);
                }
            }
        });
    } catch (Exception e) {
        callback.onCompleted(e);
    }
}
Also used : CompletedCallback(com.koushikdutta.async.callback.CompletedCallback) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException)

Example 24 with CompletedCallback

use of com.koushikdutta.async.callback.CompletedCallback in project AndroidAsync by koush.

the class AsyncSSLSocketMiddleware method wrapCallback.

@Override
protected ConnectCallback wrapCallback(final GetSocketData data, final Uri uri, final int port, final boolean proxied, final ConnectCallback callback) {
    return new ConnectCallback() {

        @Override
        public void onConnectCompleted(Exception ex, final AsyncSocket socket) {
            if (ex != null) {
                callback.onConnectCompleted(ex, socket);
                return;
            }
            if (!proxied) {
                tryHandshake(socket, data, uri, port, callback);
                return;
            }
            // this SSL connection is proxied, must issue a CONNECT request to the proxy server
            // http://stackoverflow.com/a/6594880/704837
            // some proxies also require 'Host' header, it should be safe to provide it every time
            String connect = String.format(Locale.ENGLISH, "CONNECT %s:%s HTTP/1.1\r\nHost: %s\r\n\r\n", uri.getHost(), port, uri.getHost());
            data.request.logv("Proxying: " + connect);
            Util.writeAll(socket, connect.getBytes(), new CompletedCallback() {

                @Override
                public void onCompleted(Exception ex) {
                    if (ex != null) {
                        callback.onConnectCompleted(ex, socket);
                        return;
                    }
                    LineEmitter liner = new LineEmitter();
                    liner.setLineCallback(new LineEmitter.StringCallback() {

                        String statusLine;

                        @Override
                        public void onStringAvailable(String s) {
                            data.request.logv(s);
                            if (statusLine == null) {
                                statusLine = s.trim();
                                if (!statusLine.matches("HTTP/1.\\d 2\\d\\d .*")) {
                                    // connect response is allowed to have any 2xx status code
                                    socket.setDataCallback(null);
                                    socket.setEndCallback(null);
                                    callback.onConnectCompleted(new IOException("non 2xx status line: " + statusLine), socket);
                                }
                            } else if (TextUtils.isEmpty(s.trim())) {
                                // skip all headers, complete handshake once empty line is received
                                socket.setDataCallback(null);
                                socket.setEndCallback(null);
                                tryHandshake(socket, data, uri, port, callback);
                            }
                        }
                    });
                    socket.setDataCallback(liner);
                    socket.setEndCallback(new CompletedCallback() {

                        @Override
                        public void onCompleted(Exception ex) {
                            if (!socket.isOpen() && ex == null)
                                ex = new IOException("socket closed before proxy connect response");
                            callback.onConnectCompleted(ex, socket);
                        }
                    });
                }
            });
        }
    };
}
Also used : AsyncSocket(com.koushikdutta.async.AsyncSocket) CompletedCallback(com.koushikdutta.async.callback.CompletedCallback) LineEmitter(com.koushikdutta.async.LineEmitter) IOException(java.io.IOException) ConnectCallback(com.koushikdutta.async.callback.ConnectCallback) IOException(java.io.IOException)

Example 25 with CompletedCallback

use of com.koushikdutta.async.callback.CompletedCallback in project AndroidAsync by koush.

the class AsyncSocketMiddleware method idleSocket.

private void idleSocket(final AsyncSocket socket) {
    // must listen for socket close, otherwise log will get spammed.
    socket.setEndCallback(new CompletedCallback() {

        @Override
        public void onCompleted(Exception ex) {
            socket.setClosedCallback(null);
            socket.close();
        }
    });
    socket.setWriteableCallback(null);
    // should not get any data after this point...
    // if so, eat it and disconnect.
    socket.setDataCallback(new DataCallback.NullDataCallback() {

        @Override
        public void onDataAvailable(DataEmitter emitter, ByteBufferList bb) {
            super.onDataAvailable(emitter, bb);
            bb.recycle();
            socket.setClosedCallback(null);
            socket.close();
        }
    });
}
Also used : CompletedCallback(com.koushikdutta.async.callback.CompletedCallback) ByteBufferList(com.koushikdutta.async.ByteBufferList) DataEmitter(com.koushikdutta.async.DataEmitter) DataCallback(com.koushikdutta.async.callback.DataCallback)

Aggregations

CompletedCallback (com.koushikdutta.async.callback.CompletedCallback)46 IOException (java.io.IOException)12 TimeoutException (java.util.concurrent.TimeoutException)11 ByteBufferList (com.koushikdutta.async.ByteBufferList)10 DataEmitter (com.koushikdutta.async.DataEmitter)10 DataCallback (com.koushikdutta.async.callback.DataCallback)10 Semaphore (java.util.concurrent.Semaphore)9 ContinuationCallback (com.koushikdutta.async.callback.ContinuationCallback)8 Continuation (com.koushikdutta.async.future.Continuation)8 CancellationException (java.util.concurrent.CancellationException)8 ExecutionException (java.util.concurrent.ExecutionException)8 AsyncHttpServer (com.koushikdutta.async.http.server.AsyncHttpServer)7 AsyncHttpServerRequest (com.koushikdutta.async.http.server.AsyncHttpServerRequest)7 FileNotFoundException (java.io.FileNotFoundException)7 AsyncHttpServerResponse (com.koushikdutta.async.http.server.AsyncHttpServerResponse)6 HttpServerRequestCallback (com.koushikdutta.async.http.server.HttpServerRequestCallback)6 AsyncSocket (com.koushikdutta.async.AsyncSocket)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)5 Uri (android.net.Uri)4 WritableCallback (com.koushikdutta.async.callback.WritableCallback)4