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);
}
}
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();
}
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);
}
}
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);
}
});
}
});
}
};
}
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();
}
});
}
Aggregations