hotspot/agent/src/os/win32/IOBuf.hpp
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
#ifndef _IO_BUF_
489c9b5090e2 Initial load
duke
parents:
diff changeset
    26
#define _IO_BUF_
489c9b5090e2 Initial load
duke
parents:
diff changeset
    27
489c9b5090e2 Initial load
duke
parents:
diff changeset
    28
// This file is currently used for os/solaris/agent/ too.  At some point in time
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
// the source will be reorganized to avoid these ifdefs.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
// Note that this class can read/write from a file as well as a socket.  This
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
// file capability is only implemented on win32.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
#ifdef WIN32
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
  #include <winsock2.h>
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
#else
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
  #include <sys/types.h>
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
  #include <sys/socket.h>
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
  // These are from win32 winsock2.h
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
  typedef unsigned int SOCKET;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
  typedef void * HANDLE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
  typedef unsigned long DWORD;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
  #define INVALID_SOCKET (SOCKET)(~0)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
#include <vector>
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
#include "Buffer.hpp"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
/** Manages an input/output buffer pair for a socket or file handle. */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
class IOBuf {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
  IOBuf(int inBufLen, int outBufLen);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
  ~IOBuf();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
  enum ReadLineResult {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
    RL_GOT_DATA,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
    RL_NO_DATA,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
    RL_ERROR
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
  };
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
  /** Change the socket with which this buffer is associated */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
  void setSocket(SOCKET sock);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
  // Reading/writing files is only supported on windows.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
#ifdef WIN32
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
  /** Change the output file handle with which this buffer is
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
      associated. Currently IOBufs can not be used to read from a file
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
      handle. */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
  void setOutputFileHandle(HANDLE handle);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
  /** Reset the input and output buffers, without flushing the output
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
      data to the socket */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
  void reset();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
  /** Try to read a line of data from the given socket without
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
      blocking. If was able to read a complete line of data, returns a
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
      character pointer to the beginning of the (null-terminated)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
      string. If not, returns NULL, but maintains enough state that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
      subsequent calls to tryReadLine() will not ignore the data
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
      already read. NOTE: this skips end-of-line characters (typically
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
      CR/LF) as defined by "isEOL()". When switching back and forth
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
      between binary and text modes, to be sure no data is lost, pad
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
      the beginning and end of the binary transmission with bytes
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
      which can not be confused with these characters. */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
  ReadLineResult tryReadLine();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
  /** Read a line of data from the given socket, blocking until a
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
      line, including EOL, appears.  Return the line, or NULL if
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
      something goes wrong. */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
  char *readLine();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
  /** Get the pointer to the beginning of the (null-terminated) line.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
      This should only be called if tryReadLine() has returned
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
      RL_GOT_DATA. This sets the "parsing cursor" to the beginning of
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
      the line. */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
  char* getLine();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
  // NOTE: any further data-acquisition routines must ALWAYS call
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
  // fixupData() at the beginning!
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
  //----------------------------------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
  // Output routines
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
  //
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
  /** Flush the output buffer to the socket. Returns true if
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
      succeeded, false if write error occurred. */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
  bool flush();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
  /** Write the given string to the output buffer. May flush if output
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
      buffer becomes too full to store the data. Not guaranteed to
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
      work if string is longer than the size of the output buffer.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
      Does not include the null terminator of the string. Returns true
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
      if succeeded, false if write error occurred. */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
  bool writeString(const char* str);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
  /** Write the given int to the output buffer. May flush if output
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
      buffer becomes too full to store the data. Returns true if
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
      succeeded, false if write error occurred. */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
  bool writeInt(int val);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
  /** Write the given unsigned int to the output buffer. May flush if
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
      output buffer becomes too full to store the data. Returns true
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
      if succeeded, false if write error occurred. */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
  bool writeUnsignedInt(unsigned int val);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
  /** Write the given boolean to the output buffer. May flush if
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
      output buffer becomes too full to store the data. Returns true
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
      if succeeded, false if write error occurred. */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
  bool writeBoolAsInt(bool val);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
  /** Write the given address to the output buffer. May flush if
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
      output buffer becomes too full to store the data. Returns true
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
      if succeeded, false if write error occurred. */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
  bool writeAddress(void* val);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
  /** Writes a space to the output buffer. May flush if output buffer
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
      becomes too full to store the data. Returns true if succeeded,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
      false if write error occurred. */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
  bool writeSpace();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
  /** Writes an end-of-line sequence to the output buffer. May flush
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
      if output buffer becomes too full to store the data. Returns
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
      true if succeeded, false if write error occurred. */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
  bool writeEOL();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
  /** Writes a binary character to the output buffer. */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
  bool writeBinChar(char c);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
  /** Writes a binary unsigned short in network (big-endian) byte
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
      order to the output buffer. */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
  bool writeBinUnsignedShort(unsigned short i);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
  /** Writes a binary unsigned int in network (big-endian) byte order
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
      to the output buffer. */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
  bool writeBinUnsignedInt(unsigned int i);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
  /** Writes a binary buffer to the output buffer. */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
  bool writeBinBuf(char* buf, int size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
#ifdef WIN32
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
  enum FillState {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
    DONE = 1,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
    MORE_DATA_PENDING = 2,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
    FAILED = 3
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
  };
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
  /** Very specialized routine; fill the output buffer from the given
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
      file handle. Caller is responsible for ensuring that there is
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
      data to be read on the file handle. */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
  FillState fillFromFileHandle(HANDLE fh, DWORD* numRead);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
  /** Binary utility routine (for poke) */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
  static bool isBinEscapeChar(char c);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   177
  IOBuf(const IOBuf&);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   178
  IOBuf& operator=(const IOBuf&);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
  // Returns -1 if non-blocking and no data available
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
  int readChar(bool block);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
  // Line-oriented reading
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
  std::vector<char> curLine;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
  bool gotDataLastTime;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
  ReadLineResult doReadLine(bool);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   187
489c9b5090e2 Initial load
duke
parents:
diff changeset
   188
  bool flushImpl(bool moreDataToCome);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   189
489c9b5090e2 Initial load
duke
parents:
diff changeset
   190
  SOCKET fd;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   191
  HANDLE outHandle;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   192
  bool usingSocket;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   193
489c9b5090e2 Initial load
duke
parents:
diff changeset
   194
  // Buffers
489c9b5090e2 Initial load
duke
parents:
diff changeset
   195
  Buffer* inBuf;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   196
  Buffer* outBuf;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   197
489c9b5090e2 Initial load
duke
parents:
diff changeset
   198
  // Simple finite-state machine to handle binary data
489c9b5090e2 Initial load
duke
parents:
diff changeset
   199
  enum State {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   200
    TEXT_STATE,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   201
    BIN_STATE,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   202
    EOL_STATE
489c9b5090e2 Initial load
duke
parents:
diff changeset
   203
  };
489c9b5090e2 Initial load
duke
parents:
diff changeset
   204
  enum Action {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   205
    NO_ACTION,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   206
    GOT_LINE,     // TEXT_STATE -> EOL_STATE transition
489c9b5090e2 Initial load
duke
parents:
diff changeset
   207
    SKIP_EOL_CHAR // EOL_STATE -> EOL_STATE transition
489c9b5090e2 Initial load
duke
parents:
diff changeset
   208
  };
489c9b5090e2 Initial load
duke
parents:
diff changeset
   209
489c9b5090e2 Initial load
duke
parents:
diff changeset
   210
  State state;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   211
  Action processChar(char c);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   212
489c9b5090e2 Initial load
duke
parents:
diff changeset
   213
  // Handling incoming binary buffers (poke command)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   214
  int   binPos;    // Number of binary characters read so far;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   215
                   // total number to read is binLength + 4
489c9b5090e2 Initial load
duke
parents:
diff changeset
   216
  int   binLength; // Number of binary characters in message;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   217
                   // not valid until binPos >= 4
489c9b5090e2 Initial load
duke
parents:
diff changeset
   218
489c9b5090e2 Initial load
duke
parents:
diff changeset
   219
  bool isEOL(char c);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   220
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   221
489c9b5090e2 Initial load
duke
parents:
diff changeset
   222
#endif  // #defined _IO_BUF_