Clean up more of the JDWP request handling.

There are no longer any naked uint8_t*s; it's JDWP::Requests all the way.

Also remove a bunch of the adb/socket transport duplication.

Change-Id: I0a33e393b54924d4ab9cc9f76fb346661931284f
diff --git a/src/jdwp/jdwp_socket.cc b/src/jdwp/jdwp_socket.cc
index 6f6fbe5..43906ef 100644
--- a/src/jdwp/jdwp_socket.cc
+++ b/src/jdwp/jdwp_socket.cc
@@ -28,14 +28,11 @@
 
 #include "base/logging.h"
 #include "base/stringprintf.h"
-#include "jdwp/jdwp_handler.h"
 #include "jdwp/jdwp_priv.h"
 
 #define kBasePort           8000
 #define kMaxPort            8040
 
-#define kInputBufferSize    8192
-
 namespace art {
 
 namespace JDWP {
@@ -59,10 +56,6 @@
 
   bool    awaitingHandshake;  /* waiting for "JDWP-Handshake" */
 
-  /* pending data from the network; would be more efficient as circular buf */
-  unsigned char  inputBuffer[kInputBufferSize];
-  size_t inputCount;
-
   JdwpNetState() {
     listenPort  = 0;
     listenSock  = -1;
@@ -70,8 +63,6 @@
     wakePipe[1] = -1;
 
     awaitingHandshake = false;
-
-    inputCount = 0;
   }
 };
 
@@ -444,75 +435,6 @@
 }
 
 /*
- * Consume bytes from the buffer.
- *
- * This would be more efficient with a circular buffer.  However, we're
- * usually only going to find one packet, which is trivial to handle.
- */
-static void consumeBytes(JdwpNetState* netState, size_t count) {
-  CHECK_GT(count, 0U);
-  CHECK_LE(count, netState->inputCount);
-
-  if (count == netState->inputCount) {
-    netState->inputCount = 0;
-    return;
-  }
-
-  memmove(netState->inputBuffer, netState->inputBuffer + count, netState->inputCount - count);
-  netState->inputCount -= count;
-}
-
-/*
- * Handle a packet.  Returns "false" if we encounter a connection-fatal error.
- */
-static bool handlePacket(JdwpState* state) {
-  JdwpNetState* netState = state->netState;
-  const unsigned char* buf = netState->inputBuffer;
-  uint8_t cmdSet, cmd;
-
-  cmd = cmdSet = 0;       // shut up gcc
-
-  uint32_t length = Read4BE(&buf);
-  uint32_t id = Read4BE(&buf);
-  int8_t flags = Read1(&buf);
-  if ((flags & kJDWPFlagReply) != 0) {
-    LOG(FATAL) << "reply?!";
-  } else {
-    cmdSet = Read1(&buf);
-    cmd = Read1(&buf);
-  }
-
-  CHECK_LE(length, netState->inputCount);
-  int dataLen = length - (buf - netState->inputBuffer);
-
-  ExpandBuf* pReply = expandBufAlloc();
-
-  JdwpReqHeader hdr;
-  hdr.length = length;
-  hdr.id = id;
-  hdr.cmdSet = cmdSet;
-  hdr.cmd = cmd;
-  state->ProcessRequest(&hdr, buf, dataLen, pReply);
-  if (expandBufGetLength(pReply) > 0) {
-    ssize_t cc = netState->writePacket(pReply);
-
-    if (cc != (ssize_t) expandBufGetLength(pReply)) {
-      PLOG(ERROR) << "Failed sending reply to debugger";
-      expandBufFree(pReply);
-      return false;
-    }
-  } else {
-    LOG(WARNING) << "No reply created for set=" << cmdSet << " cmd=" << cmd;
-  }
-  expandBufFree(pReply);
-
-  VLOG(jdwp) << "----------";
-
-  consumeBytes(netState, length);
-  return true;
-}
-
-/*
  * Process incoming data.  If no data is available, this will block until
  * some arrives.
  *
@@ -665,7 +587,7 @@
       goto fail;
     }
 
-    consumeBytes(netState, kMagicHandshakeLen);
+    netState->ConsumeBytes(kMagicHandshakeLen);
     netState->awaitingHandshake = false;
     VLOG(jdwp) << "+++ handshake complete";
     return true;
@@ -674,7 +596,7 @@
   /*
    * Handle this packet.
    */
-  return handlePacket(state);
+  return state->HandlePacket();
 
  fail:
   closeConnection(state);
@@ -700,7 +622,7 @@
   }
 
   errno = 0;
-  ssize_t cc = netState->writePacket(pReq);
+  ssize_t cc = netState->WritePacket(pReq);
 
   if (cc != (ssize_t) expandBufGetLength(pReq)) {
     PLOG(ERROR) << "Failed sending req to debugger (" << cc << " of " << expandBufGetLength(pReq) << ")";
@@ -732,7 +654,7 @@
     expected += iov[i].iov_len;
   }
 
-  ssize_t actual = netState->writeBufferedPacket(iov, iov_count);
+  ssize_t actual = netState->WriteBufferedPacket(iov, iov_count);
 
   if ((size_t)actual != expected) {
     PLOG(ERROR) << "Failed sending b-req to debugger (" << actual << " of " << expected << ")";