base.d 10.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
module thrift.transport.base;

import core.stdc.string : strerror;
import std.conv : text;
import thrift.base;

/**
 * An entity data can be read from and/or written to.
 *
 * A TTransport implementation may capable of either reading or writing, but
 * not necessarily both.
 */
interface TTransport {
  /**
   * Whether this transport is open.
   *
   * If a transport is closed, it can be opened by calling open(), and vice
   * versa for close().
   *
   * While a transport should always be open when trying to read/write data,
   * the related functions do not necessarily fail when called for a closed
   * transport. Situations like this could occur e.g. with a wrapper
   * transport which buffers data when the underlying transport has already
   * been closed (possibly because the connection was abruptly closed), but
   * there is still data left to be read in the buffers. This choice has been
   * made to simplify transport implementations, in terms of both  code
   * complexity and runtime overhead.
   */
  bool isOpen() @property;

  /**
   * Tests whether there is more data to read or if the remote side is
   * still open.
   *
   * A typical use case would be a server checking if it should process
   * another request on the transport.
   */
  bool peek();

  /**
   * Opens the transport for communications.
   *
   * If the transport is already open, nothing happens.
   *
   * Throws: TTransportException if opening fails.
   */
  void open();

  /**
   * Closes the transport.
   *
   * If the transport is not open, nothing happens.
   *
   * Throws: TTransportException if closing fails.
   */
  void close();

  /**
   * Attempts to fill the given buffer by reading data.
   *
   * For potentially blocking data sources (e.g. sockets), read() will only
   * block if no data is available at all. If there is some data available,
   * but waiting for new data to arrive would be required to fill the whole
   * buffer, the readily available data will be immediately returned – use
   * readAll() if you want to wait until the whole buffer is filled.
   *
   * Params:
   *   buf = Slice to use as buffer.
   *
   * Returns: How many bytes were actually read
   *
   * Throws: TTransportException if an error occurs.
   */
  size_t read(ubyte[] buf);

  /**
   * Fills the given buffer by reading data into it, failing if not enough
   * data is available.
   *
   * Params:
   *   buf = Slice to use as buffer.
   *
   * Throws: TTransportException if insufficient data is available or reading
   *   fails altogether.
   */
  void readAll(ubyte[] buf);

  /**
   * Must be called by clients when read is completed.
   *
   * Implementations can choose to perform a transport-specific action, e.g.
   * logging the request to a file.
   *
   * Returns: The number of bytes read if available, 0 otherwise.
   */
  size_t readEnd();

  /**
   * Writes the passed slice of data.
   *
   * Note: You must call flush() to ensure the data is actually written,
   * and available to be read back in the future.  Destroying a TTransport
   * object does not automatically flush pending data – if you destroy a
   * TTransport object with written but unflushed data, that data may be
   * discarded.
   *
   * Params:
   *   buf = Slice of data to write.
   *
   * Throws: TTransportException if an error occurs.
   */
  void write(in ubyte[] buf);

  /**
   * Must be called by clients when write is completed.
   *
   * Implementations can choose to perform a transport-specific action, e.g.
   * logging the request to a file.
   *
   * Returns: The number of bytes written if available, 0 otherwise.
   */
  size_t writeEnd();

  /**
   * Flushes any pending data to be written.
   *
   * Must be called before destruction to ensure writes are actually complete,
   * otherwise pending data may be discarded. Typically used with buffered
   * transport mechanisms.
   *
   * Throws: TTransportException if an error occurs.
   */
  void flush();

  /**
   * Attempts to return a slice of <code>len</code> bytes of incoming data,
   * possibly copied into buf, not consuming them (i.e.: a later read will
   * return the same data).
   *
   * This method is meant to support protocols that need to read variable-
   * length fields. They can attempt to borrow the maximum amount of data that
   * they will need, then <code>consume()</code> what they actually use. Some
   * transports will not support this method and others will fail occasionally,
   * so protocols must be prepared to fall back to <code>read()</code> if
   * borrow fails.
   *
   * The transport must be open when calling this.
   *
   * Params:
   *   buf = A buffer where the data can be stored if needed, or null to
   *     indicate that the caller is not supplying storage, but would like a
   *     slice of an internal buffer, if available.
   *   len = The number of bytes to borrow.
   *
   * Returns: If the borrow succeeds, a slice containing the borrowed data,
   *   null otherwise. The slice will be at least as long as requested, but
   *   may be longer if the returned slice points into an internal buffer
   *   rather than buf.
   *
   * Throws: TTransportException if an error occurs.
   */
  const(ubyte)[] borrow(ubyte* buf, size_t len) out (result) {
    // FIXME: Commented out because len gets corrupted in
    // thrift.transport.memory borrow() unittest.
    version(none) assert(result is null || result.length >= len,
       "Buffer returned by borrow() too short.");
  }

  /**
   * Remove len bytes from the transport. This must always follow a borrow
   * of at least len bytes, and should always succeed.
   *
   * The transport must be open when calling this.
   *
   * Params:
   *   len = Number of bytes to consume.
   *
   * Throws: TTransportException if an error occurs.
   */
  void consume(size_t len);
}

/**
 * Provides basic fall-back implementations of the TTransport interface.
 */
class TBaseTransport : TTransport {
  override bool isOpen() @property {
    return false;
  }

  override bool peek() {
    return isOpen;
  }

  override void open() {
    throw new TTransportException("Cannot open TBaseTransport.",
      TTransportException.Type.NOT_IMPLEMENTED);
  }

  override void close() {
    throw new TTransportException("Cannot close TBaseTransport.",
      TTransportException.Type.NOT_IMPLEMENTED);
  }

  override size_t read(ubyte[] buf) {
    throw new TTransportException("Cannot read from a TBaseTransport.",
      TTransportException.Type.NOT_IMPLEMENTED);
  }

  override void readAll(ubyte[] buf) {
    size_t have;
    while (have < buf.length) {
      size_t get = read(buf[have..$]);
      if (get <= 0) {
        throw new TTransportException(text("Could not readAll() ", buf.length,
          " bytes as no more data was available after ", have, " bytes."),
          TTransportException.Type.END_OF_FILE);
      }
      have += get;
    }
  }

  override size_t readEnd() {
    // Do nothing by default, not needed by all implementations.
    return 0;
  }

  override void write(in ubyte[] buf) {
    throw new TTransportException("Cannot write to a TBaseTransport.",
      TTransportException.Type.NOT_IMPLEMENTED);
  }

  override size_t writeEnd() {
    // Do nothing by default, not needed by all implementations.
    return 0;
  }

  override void flush() {
    // Do nothing by default, not needed by all implementations.
  }

  override const(ubyte)[] borrow(ubyte* buf, size_t len) {
    // borrow() is allowed to fail anyway, so just return null.
    return null;
  }

  override void consume(size_t len) {
    throw new TTransportException("Cannot consume from a TBaseTransport.",
      TTransportException.Type.NOT_IMPLEMENTED);
  }

protected:
  this() {}
}

/**
 * Makes a TTransport which wraps a given source transport in some way.
 *
 * A common use case is inside server implementations, where the raw client
 * connections accepted from e.g. TServerSocket need to be wrapped into
 * buffered or compressed transports.
 */
class TTransportFactory {
  /**
   * Default implementation does nothing, just returns the transport given.
   */
  TTransport getTransport(TTransport trans) {
    return trans;
  }
}

/**
 * Transport factory for transports which simply wrap an underlying TTransport
 * without requiring additional configuration.
 */
class TWrapperTransportFactory(T) if (
  is(T : TTransport) && __traits(compiles, new T(TTransport.init))
)  : TTransportFactory {
  override T getTransport(TTransport trans) {
    return new T(trans);
  }
}

/**
 * Transport-level exception.
 */
class TTransportException : TException {
  /**
   * Error codes for the various types of exceptions.
   */
  enum Type {
    UNKNOWN, ///
    NOT_OPEN, ///
    TIMED_OUT, ///
    END_OF_FILE, ///
    INTERRUPTED, ///
    BAD_ARGS, ///
    CORRUPTED_DATA, ///
    INTERNAL_ERROR, ///
    NOT_IMPLEMENTED ///
  }

  ///
  this(Type type, string file = __FILE__, size_t line = __LINE__, Throwable next = null) {
    static string msgForType(Type type) {
      switch (type) {
        case Type.UNKNOWN: return "Unknown transport exception";
        case Type.NOT_OPEN: return "Transport not open";
        case Type.TIMED_OUT: return "Timed out";
        case Type.END_OF_FILE: return "End of file";
        case Type.INTERRUPTED: return "Interrupted";
        case Type.BAD_ARGS: return "Invalid arguments";
        case Type.CORRUPTED_DATA: return "Corrupted Data";
        case Type.INTERNAL_ERROR: return "Internal error";
        case Type.NOT_IMPLEMENTED: return "Not implemented";
        default: return "(Invalid exception type)";
      }
    }
    this(msgForType(type), type, file, line, next);
  }

  ///
  this(string msg, string file = __FILE__, size_t line = __LINE__,
    Throwable next = null)
  {
    this(msg, Type.UNKNOWN, file, line, next);
  }

  ///
  this(string msg, Type type, string file = __FILE__, size_t line = __LINE__,
    Throwable next = null)
  {
    super(msg, file, line, next);
    type_ = type;
  }

  ///
  Type type() const nothrow @property {
    return type_;
  }

protected:
  Type type_;
}

/**
 * Meta-programming helper returning whether the passed type is a TTransport
 * implementation.
 */
template isTTransport(T) {
  enum isTTransport = is(T : TTransport);
}