Search in sources :

Example 1 with WritableCallback

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

the class BufferedDataSink method setDataSink.

public void setDataSink(DataSink datasink) {
    mDataSink = datasink;
    mDataSink.setWriteableCallback(new WritableCallback() {

        @Override
        public void onWriteable() {
            writePending();
        }
    });
}
Also used : WritableCallback(com.koushikdutta.async.callback.WritableCallback)

Example 2 with WritableCallback

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

the class Util method writeAll.

public static void writeAll(final DataSink sink, final ByteBufferList bb, final CompletedCallback callback) {
    WritableCallback wc;
    sink.setWriteableCallback(wc = new WritableCallback() {

        @Override
        public void onWriteable() {
            sink.write(bb);
            if (bb.remaining() == 0 && callback != null) {
                sink.setWriteableCallback(null);
                callback.onCompleted(null);
            }
        }
    });
    wc.onWriteable();
}
Also used : WritableCallback(com.koushikdutta.async.callback.WritableCallback)

Example 3 with WritableCallback

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

the class Util method pump.

public static void pump(final DataEmitter emitter, final DataSink sink, final CompletedCallback callback) {
    final DataCallback dataCallback = new DataCallback() {

        @Override
        public void onDataAvailable(DataEmitter emitter, ByteBufferList bb) {
            sink.write(bb);
            if (bb.remaining() > 0)
                emitter.pause();
        }
    };
    emitter.setDataCallback(dataCallback);
    sink.setWriteableCallback(new WritableCallback() {

        @Override
        public void onWriteable() {
            emitter.resume();
        }
    });
    final CompletedCallback wrapper = new CompletedCallback() {

        boolean reported;

        @Override
        public void onCompleted(Exception ex) {
            if (reported)
                return;
            reported = true;
            emitter.setDataCallback(null);
            emitter.setEndCallback(null);
            sink.setClosedCallback(null);
            sink.setWriteableCallback(null);
            callback.onCompleted(ex);
        }
    };
    emitter.setEndCallback(wrapper);
    sink.setClosedCallback(new CompletedCallback() {

        @Override
        public void onCompleted(Exception ex) {
            if (ex == null)
                ex = new IOException("sink was closed before emitter ended");
            wrapper.onCompleted(ex);
        }
    });
}
Also used : CompletedCallback(com.koushikdutta.async.callback.CompletedCallback) WritableCallback(com.koushikdutta.async.callback.WritableCallback) IOException(java.io.IOException) DataCallback(com.koushikdutta.async.callback.DataCallback) IOException(java.io.IOException)

Example 4 with WritableCallback

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

the class AsyncHttpServerResponseImpl method initFirstWrite.

void initFirstWrite() {
    if (headWritten)
        return;
    headWritten = true;
    final boolean isChunked;
    String currentEncoding = mRawHeaders.get("Transfer-Encoding");
    if ("".equals(currentEncoding))
        mRawHeaders.removeAll("Transfer-Encoding");
    boolean canUseChunked = ("Chunked".equalsIgnoreCase(currentEncoding) || currentEncoding == null) && !"close".equalsIgnoreCase(mRawHeaders.get("Connection"));
    if (mContentLength < 0) {
        String contentLength = mRawHeaders.get("Content-Length");
        if (!TextUtils.isEmpty(contentLength))
            mContentLength = Long.valueOf(contentLength);
    }
    if (mContentLength < 0 && canUseChunked) {
        mRawHeaders.set("Transfer-Encoding", "Chunked");
        isChunked = true;
    } else {
        isChunked = false;
    }
    String statusLine = String.format(Locale.ENGLISH, "%s %s %s", httpVersion, code, AsyncHttpServer.getResponseCodeDescription(code));
    String rh = mRawHeaders.toPrefixString(statusLine);
    Util.writeAll(mSocket, rh.getBytes(), ex -> {
        if (ex != null) {
            report(ex);
            return;
        }
        if (isChunked) {
            ChunkedOutputFilter chunked = new ChunkedOutputFilter(mSocket);
            chunked.setMaxBuffer(0);
            mSink = chunked;
        } else {
            mSink = mSocket;
        }
        mSink.setClosedCallback(closedCallback);
        closedCallback = null;
        mSink.setWriteableCallback(writable);
        writable = null;
        if (ended) {
            // the response ended while headers were written
            end();
            return;
        }
        getServer().post(() -> {
            WritableCallback wb = getWriteableCallback();
            if (wb != null)
                wb.onWriteable();
        });
    });
}
Also used : WritableCallback(com.koushikdutta.async.callback.WritableCallback) ChunkedOutputFilter(com.koushikdutta.async.http.filter.ChunkedOutputFilter)

Example 5 with WritableCallback

use of com.koushikdutta.async.callback.WritableCallback in project ion by koush.

the class RequestBodyUploadObserver method write.

@Override
public void write(AsyncHttpRequest request, final DataSink sink, final CompletedCallback completed) {
    final int length = body.length();
    body.write(request, new DataSink() {

        int totalWritten;

        @Override
        public void write(ByteBufferList bb) {
            int start = bb.remaining();
            sink.write(bb);
            int wrote = start - bb.remaining();
            totalWritten += wrote;
            callback.onProgress(totalWritten, length);
        }

        @Override
        public void setWriteableCallback(WritableCallback handler) {
            sink.setWriteableCallback(handler);
        }

        @Override
        public WritableCallback getWriteableCallback() {
            return sink.getWriteableCallback();
        }

        @Override
        public boolean isOpen() {
            return sink.isOpen();
        }

        @Override
        public void end() {
            sink.end();
        }

        @Override
        public void setClosedCallback(CompletedCallback handler) {
            sink.setClosedCallback(handler);
        }

        @Override
        public CompletedCallback getClosedCallback() {
            return sink.getClosedCallback();
        }

        @Override
        public AsyncServer getServer() {
            return sink.getServer();
        }
    }, completed);
}
Also used : DataSink(com.koushikdutta.async.DataSink) CompletedCallback(com.koushikdutta.async.callback.CompletedCallback) ByteBufferList(com.koushikdutta.async.ByteBufferList) AsyncServer(com.koushikdutta.async.AsyncServer) WritableCallback(com.koushikdutta.async.callback.WritableCallback)

Aggregations

WritableCallback (com.koushikdutta.async.callback.WritableCallback)6 CompletedCallback (com.koushikdutta.async.callback.CompletedCallback)3 IOException (java.io.IOException)2 AsyncServer (com.koushikdutta.async.AsyncServer)1 ByteBufferList (com.koushikdutta.async.ByteBufferList)1 DataSink (com.koushikdutta.async.DataSink)1 DataCallback (com.koushikdutta.async.callback.DataCallback)1 ChunkedOutputFilter (com.koushikdutta.async.http.filter.ChunkedOutputFilter)1 Allocator (com.koushikdutta.async.util.Allocator)1 ByteBuffer (java.nio.ByteBuffer)1