void setSocket(AsyncSocket exchange) {
    mSocket = exchange;

    if (mSocket == null) return;

    mWriter = mRequest.getBody();
    if (mWriter != null) {
      mRequest.getHeaders().setContentType(mWriter.getContentType());
      if (mWriter.length() != -1) {
        mRequest.getHeaders().setContentLength(mWriter.length());
        mSink = mSocket;
      } else {
        mRequest.getHeaders().getHeaders().set("Transfer-Encoding", "Chunked");
        mSink = new ChunkedOutputFilter(mSocket);
      }
    } else {
      mSink = mSocket;
    }

    String rs = mRequest.getRequestString();
    com.koushikdutta.async.Util.writeAll(
        exchange,
        rs.getBytes(),
        new CompletedCallback() {
          @Override
          public void onCompleted(Exception ex) {
            if (mWriter != null) mWriter.write(mRequest, AsyncHttpResponseImpl.this);
          }
        });

    LineEmitter liner = new LineEmitter();
    exchange.setDataCallback(liner);
    liner.setLineCallback(mHeaderCallback);

    mSocket.setEndCallback(mReporter);
    mSocket.setClosedCallback(
        new CompletedCallback() {
          @Override
          public void onCompleted(Exception ex) {
            // TODO: do we care? throw if socket is still writing or something?
          }
        });
  }
Пример #2
0
 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.close();
         }
       });
   socket.setWriteableCallback(null);
   // should not get any data after this point...
   // if so, eat it and disconnect.
   socket.setDataCallback(
       new NullDataCallback() {
         @Override
         public void onDataAvailable(DataEmitter emitter, ByteBufferList bb) {
           super.onDataAvailable(emitter, bb);
           bb.recycle();
           socket.close();
         }
       });
 }