hotspot/agent/src/os/win32/IOBuf.cpp
author never
Mon, 04 May 2009 22:06:47 -0700
changeset 2744 57f0579fbe09
parent 1 489c9b5090e2
child 5547 f4b087cbb361
permissions -rw-r--r--
6837224: libsaproc.so on linux needs version of 6799141 Reviewed-by: kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
489c9b5090e2 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2000-2003 Sun Microsystems, Inc.  All Rights Reserved.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     4
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
489c9b5090e2 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     8
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
489c9b5090e2 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    14
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
489c9b5090e2 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    18
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
489c9b5090e2 Initial load
duke
parents:
diff changeset
    25
#include <stdio.h>
489c9b5090e2 Initial load
duke
parents:
diff changeset
    26
489c9b5090e2 Initial load
duke
parents:
diff changeset
    27
// This file is currently used for os/solaris/agent too.  At some point in time
489c9b5090e2 Initial load
duke
parents:
diff changeset
    28
// the source will be reorganized to avoid these ifdefs.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
#ifdef __sun
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
  #include <string.h>
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
  #include <inttypes.h>
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
  #include <sys/byteorder.h>
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
#include "IOBuf.hpp"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
// Formats for printing pointers
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
#ifdef _LP64
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
#  define INTPTR_FORMAT "0x%016lx"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
#else /* ! _LP64 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
#  define INTPTR_FORMAT "0x%08lx"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
#endif /* _LP64 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
// Uncomment the #define below to get messages on stderr
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
// #define DEBUGGING
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
IOBuf::IOBuf(int inLen, int outLen) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
  inBuf = new Buffer(inLen);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
  outBuf = new Buffer(outLen);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
  fd = INVALID_SOCKET;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
  outHandle = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
  usingSocket = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
  reset();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
IOBuf::~IOBuf() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
  delete inBuf;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
  delete outBuf;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
void
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
IOBuf::setSocket(SOCKET sock) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
  fd = sock;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
  usingSocket = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
// Reading/writing files is only needed and used on windows.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
#ifdef WIN32
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
void
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
IOBuf::setOutputFileHandle(HANDLE handle) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
  outHandle = handle;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
  usingSocket = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
void
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
IOBuf::reset() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
  gotDataLastTime = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
  state          = TEXT_STATE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
  binPos         = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
  binLength      = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
IOBuf::ReadLineResult
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
IOBuf::tryReadLine() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
  return doReadLine(false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
char*
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
IOBuf::readLine() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
  ReadLineResult rr = doReadLine(true);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
  if (rr != RL_GOT_DATA) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
    return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
  return getLine();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
IOBuf::ReadLineResult
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
IOBuf::doReadLine(bool shouldWait) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
  if (!usingSocket) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
    return IOBuf::RL_ERROR;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
  if (gotDataLastTime) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
    curLine.clear();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
  int c;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
  do {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
    c = readChar(shouldWait);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
    if (c >= 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
      Action act = processChar((char) c);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
      if (act == GOT_LINE) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
        curLine.push_back('\0');
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
        gotDataLastTime = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
        return IOBuf::RL_GOT_DATA;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
      } else if (act == SKIP_EOL_CHAR) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
        // Do nothing
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
      } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
        curLine.push_back((char) c);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
  } while (shouldWait || c >= 0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
  gotDataLastTime = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
  return IOBuf::RL_NO_DATA;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
bool
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
IOBuf::flushImpl(bool moreDataToCome) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
  int numWritten = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
#ifdef WIN32
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
  // When running on Windows and using IOBufs for inter-process
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
  // communication, we need to write metadata into the stream
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
  // indicating how many bytes are coming down. Five bytes are written
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
  // per flush() call, four containing the integer number of bytes
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
  // coming (not including the five-byte header) and one (a 0 or 1)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
  // indicating whether there is more data coming.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
  if (!usingSocket) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
    int numToWrite = outBuf->drainRemaining();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
    char moreToCome = (moreDataToCome ? 1 : 0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
    DWORD numBytesWritten;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
    if (!WriteFile(outHandle, &numToWrite, sizeof(int), &numBytesWritten, NULL)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
      return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
    if (numBytesWritten != sizeof(int)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
      return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
    if (!WriteFile(outHandle, &moreToCome, 1, &numBytesWritten, NULL)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
      return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
    if (numBytesWritten != 1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
      return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
  while (outBuf->drainRemaining() != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
#ifdef DEBUGGING
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
      fprintf(stderr, "Flushing %d bytes\n", outBuf->drainRemaining());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
    if (usingSocket) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
      numWritten = send(fd, outBuf->drainPos(), outBuf->drainRemaining(), 0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
#ifdef WIN32
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
      DWORD numBytesWritten;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
      if (!WriteFile(outHandle, outBuf->drainPos(), outBuf->drainRemaining(), &numBytesWritten, NULL)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
        numWritten = -1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
      } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
        numWritten = numBytesWritten;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   177
    if (numWritten != -1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   178
#ifdef DEBUGGING
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
      fprintf(stderr, "Flushed %d bytes\n", numWritten);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
      outBuf->incrDrainPos(numWritten);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
      return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
489c9b5090e2 Initial load
duke
parents:
diff changeset
   187
  outBuf->compact();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   188
489c9b5090e2 Initial load
duke
parents:
diff changeset
   189
  return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   190
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   191
489c9b5090e2 Initial load
duke
parents:
diff changeset
   192
int
489c9b5090e2 Initial load
duke
parents:
diff changeset
   193
IOBuf::readChar(bool block) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   194
  do {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   195
    int c = inBuf->readByte();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   196
    if (c >= 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   197
      return c;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   198
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   199
    // See whether we need to compact the input buffer
489c9b5090e2 Initial load
duke
parents:
diff changeset
   200
    if (inBuf->remaining() < inBuf->size() / 2) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   201
      inBuf->compact();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   202
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   203
    // See whether socket is ready
489c9b5090e2 Initial load
duke
parents:
diff changeset
   204
    fd_set fds;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   205
    FD_ZERO(&fds);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   206
    FD_SET(fd, &fds);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   207
    struct timeval timeout;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   208
    timeout.tv_sec = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   209
    timeout.tv_usec = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   210
    if (block || select(1 + fd, &fds, NULL, NULL, &timeout) > 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   211
      if (block || FD_ISSET(fd, &fds)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   212
#ifdef DEBUGGING
489c9b5090e2 Initial load
duke
parents:
diff changeset
   213
        int b = (block ? 1 : 0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   214
        fprintf(stderr, "calling recv: block = %d\n", b);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   215
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   216
        // Read data from socket
489c9b5090e2 Initial load
duke
parents:
diff changeset
   217
        int numRead = recv(fd, inBuf->fillPos(), inBuf->remaining(), 0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   218
        if (numRead < 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   219
#ifdef DEBUGGING
489c9b5090e2 Initial load
duke
parents:
diff changeset
   220
          fprintf(stderr, "recv failed\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   221
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   222
          return -1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   223
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   224
        inBuf->incrFillPos(numRead);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   225
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   226
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   227
  } while (block);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   228
489c9b5090e2 Initial load
duke
parents:
diff changeset
   229
  return inBuf->readByte();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   230
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   231
489c9b5090e2 Initial load
duke
parents:
diff changeset
   232
char*
489c9b5090e2 Initial load
duke
parents:
diff changeset
   233
IOBuf::getLine() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   234
#ifdef DEBUGGING
489c9b5090e2 Initial load
duke
parents:
diff changeset
   235
  fprintf(stderr, "Returning (first 10 chars) \"%.10s\"\n", curLine.begin());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   236
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   237
  return curLine.begin();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   238
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   239
489c9b5090e2 Initial load
duke
parents:
diff changeset
   240
bool
489c9b5090e2 Initial load
duke
parents:
diff changeset
   241
IOBuf::flush() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   242
  return flushImpl(false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   243
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   244
489c9b5090e2 Initial load
duke
parents:
diff changeset
   245
bool
489c9b5090e2 Initial load
duke
parents:
diff changeset
   246
IOBuf::writeString(const char* str) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   247
  int len = strlen(str);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   248
489c9b5090e2 Initial load
duke
parents:
diff changeset
   249
  if (len > outBuf->size()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   250
    return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   251
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   252
489c9b5090e2 Initial load
duke
parents:
diff changeset
   253
  if (len > outBuf->remaining()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   254
    if (!flushImpl(true)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   255
      return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   256
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   257
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   258
489c9b5090e2 Initial load
duke
parents:
diff changeset
   259
  // NOTE we do not copy the null terminator of the string.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   260
489c9b5090e2 Initial load
duke
parents:
diff changeset
   261
  strncpy(outBuf->fillPos(), str, len);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   262
  outBuf->incrFillPos(len);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   263
  return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   264
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   265
489c9b5090e2 Initial load
duke
parents:
diff changeset
   266
bool
489c9b5090e2 Initial load
duke
parents:
diff changeset
   267
IOBuf::writeInt(int val) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   268
  char buf[128];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   269
  sprintf(buf, "%d", val);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   270
  return writeString(buf);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   271
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   272
489c9b5090e2 Initial load
duke
parents:
diff changeset
   273
bool
489c9b5090e2 Initial load
duke
parents:
diff changeset
   274
IOBuf::writeUnsignedInt(unsigned int val) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   275
  char buf[128];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   276
  sprintf(buf, "%u", val);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   277
  return writeString(buf);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   278
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   279
489c9b5090e2 Initial load
duke
parents:
diff changeset
   280
bool
489c9b5090e2 Initial load
duke
parents:
diff changeset
   281
IOBuf::writeBoolAsInt(bool val) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   282
  if (val) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   283
    return writeString("1");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   284
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   285
    return writeString("0");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   286
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   287
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   288
489c9b5090e2 Initial load
duke
parents:
diff changeset
   289
bool
489c9b5090e2 Initial load
duke
parents:
diff changeset
   290
IOBuf::writeAddress(void* val) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   291
  char buf[128];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   292
  sprintf(buf, INTPTR_FORMAT, val);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   293
  return writeString(buf);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   294
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   295
489c9b5090e2 Initial load
duke
parents:
diff changeset
   296
bool
489c9b5090e2 Initial load
duke
parents:
diff changeset
   297
IOBuf::writeSpace() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   298
  return writeString(" ");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   299
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   300
489c9b5090e2 Initial load
duke
parents:
diff changeset
   301
bool
489c9b5090e2 Initial load
duke
parents:
diff changeset
   302
IOBuf::writeEOL() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   303
  return writeString("\n\r");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   304
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   305
489c9b5090e2 Initial load
duke
parents:
diff changeset
   306
bool
489c9b5090e2 Initial load
duke
parents:
diff changeset
   307
IOBuf::writeBinChar(char c) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   308
  return writeBinBuf((char*) &c, sizeof(c));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   309
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   310
489c9b5090e2 Initial load
duke
parents:
diff changeset
   311
bool
489c9b5090e2 Initial load
duke
parents:
diff changeset
   312
IOBuf::writeBinUnsignedShort(unsigned short i) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   313
  i = htons(i);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   314
  return writeBinBuf((char*) &i, sizeof(i));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   315
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   316
489c9b5090e2 Initial load
duke
parents:
diff changeset
   317
bool
489c9b5090e2 Initial load
duke
parents:
diff changeset
   318
IOBuf::writeBinUnsignedInt(unsigned int i) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   319
  i = htonl(i);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   320
  return writeBinBuf((char*) &i, sizeof(i));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   321
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   322
489c9b5090e2 Initial load
duke
parents:
diff changeset
   323
bool
489c9b5090e2 Initial load
duke
parents:
diff changeset
   324
IOBuf::writeBinBuf(char* buf, int size) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   325
  while (size > 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   326
    int spaceRemaining = outBuf->remaining();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   327
    if (spaceRemaining == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   328
      if (!flushImpl(true)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   329
        return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   330
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   331
      spaceRemaining = outBuf->remaining();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   332
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   333
    int toCopy = (size > spaceRemaining) ? spaceRemaining : size;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   334
    memcpy(outBuf->fillPos(), buf, toCopy);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   335
    outBuf->incrFillPos(toCopy);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   336
    buf += toCopy;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   337
    size -= toCopy;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   338
    if (size > 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   339
      if (!flushImpl(true)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   340
        return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   341
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   342
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   343
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   344
  return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   345
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   346
489c9b5090e2 Initial load
duke
parents:
diff changeset
   347
#ifdef WIN32
489c9b5090e2 Initial load
duke
parents:
diff changeset
   348
IOBuf::FillState
489c9b5090e2 Initial load
duke
parents:
diff changeset
   349
IOBuf::fillFromFileHandle(HANDLE fh, DWORD* numBytesRead) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   350
  int totalToRead;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   351
  char moreToCome;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   352
489c9b5090e2 Initial load
duke
parents:
diff changeset
   353
  outBuf->compact();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   354
489c9b5090e2 Initial load
duke
parents:
diff changeset
   355
  DWORD numRead;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   356
  if (!ReadFile(fh, &totalToRead, sizeof(int), &numRead, NULL)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   357
    return FAILED;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   358
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   359
  if (numRead != sizeof(int)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   360
    return FAILED;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   361
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   362
  if (!ReadFile(fh, &moreToCome, 1, &numRead, NULL)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   363
    return FAILED;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   364
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   365
  if (numRead != 1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   366
    return FAILED;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   367
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   368
  if (outBuf->remaining() < totalToRead) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   369
    return FAILED;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   370
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   371
489c9b5090e2 Initial load
duke
parents:
diff changeset
   372
  int tmp = totalToRead;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   373
489c9b5090e2 Initial load
duke
parents:
diff changeset
   374
  while (totalToRead > 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   375
    if (!ReadFile(fh, outBuf->fillPos(), totalToRead, &numRead, NULL)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   376
      return FAILED;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   377
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   378
    outBuf->incrFillPos((int) numRead);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   379
    totalToRead -= numRead;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   380
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   381
489c9b5090e2 Initial load
duke
parents:
diff changeset
   382
  *numBytesRead = tmp;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   383
  return ((moreToCome == 0) ? DONE : MORE_DATA_PENDING);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   384
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   385
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   386
489c9b5090e2 Initial load
duke
parents:
diff changeset
   387
bool
489c9b5090e2 Initial load
duke
parents:
diff changeset
   388
IOBuf::isBinEscapeChar(char c) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   389
  return (c == '|');
489c9b5090e2 Initial load
duke
parents:
diff changeset
   390
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   391
489c9b5090e2 Initial load
duke
parents:
diff changeset
   392
IOBuf::Action
489c9b5090e2 Initial load
duke
parents:
diff changeset
   393
IOBuf::processChar(char c) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   394
  Action action = NO_ACTION;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   395
  switch (state) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   396
  case TEXT_STATE: {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   397
    // Looking for text char, bin escape char, or EOL
489c9b5090e2 Initial load
duke
parents:
diff changeset
   398
    if (isBinEscapeChar(c)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   399
#ifdef DEBUGGING
489c9b5090e2 Initial load
duke
parents:
diff changeset
   400
      fprintf(stderr, "[a: '%c'] ", inBuf[0]);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   401
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   402
      binPos = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   403
#ifdef DEBUGGING
489c9b5090e2 Initial load
duke
parents:
diff changeset
   404
      fprintf(stderr, "[b: '%c'] ", inBuf[0]);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   405
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   406
      binLength = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   407
#ifdef DEBUGGING
489c9b5090e2 Initial load
duke
parents:
diff changeset
   408
      fprintf(stderr, "[c: '%c'] ", inBuf[0]);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   409
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   410
      state = BIN_STATE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   411
#ifdef DEBUGGING
489c9b5090e2 Initial load
duke
parents:
diff changeset
   412
      fprintf(stderr, "[d: '%c'] ", inBuf[0]);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   413
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   414
#ifdef DEBUGGING
489c9b5090e2 Initial load
duke
parents:
diff changeset
   415
      fprintf(stderr, "\nSwitching to BIN_STATE\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   416
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   417
    } else if (isEOL(c)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   418
      state = EOL_STATE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   419
      action = GOT_LINE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   420
#ifdef DEBUGGING
489c9b5090e2 Initial load
duke
parents:
diff changeset
   421
      fprintf(stderr, "\nSwitching to EOL_STATE (GOT_LINE)\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   422
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   423
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   424
#ifdef DEBUGGING
489c9b5090e2 Initial load
duke
parents:
diff changeset
   425
    else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   426
      fprintf(stderr, "'%c' ", c);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   427
      fflush(stderr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   428
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   429
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   430
    break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   431
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   432
489c9b5090e2 Initial load
duke
parents:
diff changeset
   433
  case BIN_STATE: {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   434
    // Seeking to finish read of input
489c9b5090e2 Initial load
duke
parents:
diff changeset
   435
    if (binPos < 4) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   436
      int cur = c & 0xFF;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   437
      binLength <<= 8;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   438
      binLength |= cur;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   439
      ++binPos;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   440
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   441
#ifdef DEBUGGING
489c9b5090e2 Initial load
duke
parents:
diff changeset
   442
      fprintf(stderr, "Reading binary byte %d of %d\n",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   443
              binPos - 4, binLength);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   444
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   445
      ++binPos;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   446
      if (binPos == 4 + binLength) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   447
        state = TEXT_STATE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   448
#ifdef DEBUGGING
489c9b5090e2 Initial load
duke
parents:
diff changeset
   449
        fprintf(stderr, "Switching to TEXT_STATE\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   450
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   451
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   452
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   453
    break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   454
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   455
489c9b5090e2 Initial load
duke
parents:
diff changeset
   456
  case EOL_STATE: {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   457
    // More EOL characters just cause us to re-enter this state
489c9b5090e2 Initial load
duke
parents:
diff changeset
   458
    if (isEOL(c)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   459
      action = SKIP_EOL_CHAR;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   460
    } else if (isBinEscapeChar(c)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   461
      binPos = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   462
      binLength = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   463
      state = BIN_STATE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   464
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   465
      state = TEXT_STATE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   466
#ifdef DEBUGGING
489c9b5090e2 Initial load
duke
parents:
diff changeset
   467
      fprintf(stderr, "'%c' ", c);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   468
      fflush(stderr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   469
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   470
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   471
    break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   472
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   473
489c9b5090e2 Initial load
duke
parents:
diff changeset
   474
  } // switch
489c9b5090e2 Initial load
duke
parents:
diff changeset
   475
489c9b5090e2 Initial load
duke
parents:
diff changeset
   476
  return action;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   477
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   478
489c9b5090e2 Initial load
duke
parents:
diff changeset
   479
489c9b5090e2 Initial load
duke
parents:
diff changeset
   480
bool
489c9b5090e2 Initial load
duke
parents:
diff changeset
   481
IOBuf::isEOL(char c) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   482
#ifdef WIN32
489c9b5090e2 Initial load
duke
parents:
diff changeset
   483
  return ((c == '\n') || (c == '\r'));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   484
#elif defined(__sun)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   485
  return c == '\n';
489c9b5090e2 Initial load
duke
parents:
diff changeset
   486
#else
489c9b5090e2 Initial load
duke
parents:
diff changeset
   487
  #error Please port isEOL() to your platform
489c9b5090e2 Initial load
duke
parents:
diff changeset
   488
  return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   489
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   490
}