Пример #1
0
  public byte receive() throws EdbcEx {
    int msg_id;

    if (trace.enabled(3)) trace.write(title + ": reading next message");

    /*
     ** Check to see if message header is in the input
     ** buffer (handles message concatenation).
     */
    while (in.avail() < 8) {
      short tl_id;

      /*
       ** Headers are not split across continued messages.
       ** Data remaining in the current buffer indicates
       ** a message processing error.
       */
      if (in.avail() > 0) {
        if (trace.enabled(1))
          trace.write(title + ": invalid header length " + in.avail() + " bytes");
        disconnect();
        throw EdbcEx.get(E_IO0002_PROTOCOL_ERR);
      }

      try {
        tl_id = in.receive();
      } catch (EdbcEx ex) {
        disconnect();
        throw ex;
      }

      if (tl_id != JDBC_TL_DT) {
        if (trace.enabled(1))
          trace.write(title + ": invalid TL packet ID 0x" + Integer.toHexString(tl_id));
        disconnect();
        throw EdbcEx.get(E_IO0002_PROTOCOL_ERR);
      }
    }

    /*
     ** Read and validate message header.
     */
    if ((msg_id = in.readInt()) != JDBC_MSG_ID) {
      if (trace.enabled(1))
        trace.write(title + ": invalid header ID 0x" + Integer.toHexString(msg_id));
      disconnect();
      throw EdbcEx.get(E_IO0002_PROTOCOL_ERR);
    }

    in_msg_len = in.readShort();
    in_msg_id = in.readByte();
    in_msg_flg = in.readByte();

    if (trace.enabled(2))
      trace.write(
          title
              + ": received message "
              + IdMap.map(in_msg_id, MsgConst.msgMap)
              + " length "
              + in_msg_len
              + ((in_msg_flg & JDBC_MSG_EOD) == 0 ? "" : " EOD")
              + ((in_msg_flg & JDBC_MSG_EOG) == 0 ? "" : " EOG"));

    return (in_msg_id);
  } // receive
Пример #2
0
  protected DbConnIn(String host_id, byte info[], short info_len) throws EdbcEx {
    super(host_id, info, info_len);

    try {
      in = new InBuff(socket.getInputStream(), conn_id, 1 << JDBC_TL_PKT_MIN);
    } catch (Exception ex) {
      if (trace.enabled(1))
        trace.write(title + ": error creating input buffer: " + ex.getMessage());
      disconnect();
      throw EdbcEx.get(E_IO0001_CONNECT_ERR, ex);
    }

    String cs = null; // Character set connection parameter
    int size = JDBC_TL_PKT_MIN;
    short tl_id;

    try {
      if (trace.enabled(2)) trace.write(title + ": reading Connection Confirmation");

      if ((tl_id = in.receive()) != JDBC_TL_CC) {
        if (trace.enabled(1)) trace.write(title + ": invalid TL CC packet ID " + tl_id);
        throw EdbcEx.get(E_IO0001_CONNECT_ERR);
      }

      /*
       ** Read negotiated values from server (if available).
       */
      while (in.avail() >= 2) {
        byte param_id = in.readByte();
        short param_len = (short) (in.readByte() & 0xff);

        if (param_len == 255)
          if (in.avail() >= 2) param_len = in.readShort();
          else throw EdbcEx.get(E_IO0001_CONNECT_ERR);

        if (in.avail() < param_len) throw EdbcEx.get(E_IO0001_CONNECT_ERR);

        switch (param_id) {
          case JDBC_TL_CP_PROTO:
            if (param_len == 1) tl_proto = in.readByte();
            else throw EdbcEx.get(E_IO0001_CONNECT_ERR);
            break;

          case JDBC_TL_CP_SIZE:
            if (param_len == 1) size = in.readByte();
            else throw EdbcEx.get(E_IO0001_CONNECT_ERR);
            break;

          case JDBC_TL_CP_CHRSET:
            /*
             ** !!!!! FIX-ME !!!!!
             ** Bootstrapping character set will fail if DBMS
             ** character set and driver default encoding are
             ** not minimally compatible.
             */
            {
              byte ba[] = new byte[param_len];

              if (in.readBytes(ba, 0, param_len) == param_len) {
                try {
                  cs = new String(ba, "US-ASCII");
                } catch (Exception ex) {
                  if (trace.enabled(1)) trace.write(title + ": TL CP CHARSET: " + ex.toString());
                  throw EdbcEx.get(E_IO0001_CONNECT_ERR);
                }
              } else throw EdbcEx.get(E_IO0001_CONNECT_ERR);
            }
            break;

          case JDBC_TL_CP_MSG:
            if (info == null
                || param_len > info.length
                || in.readBytes(info, 0, param_len) != param_len)
              throw EdbcEx.get(E_IO0001_CONNECT_ERR);
            break;

          default:
            if (trace.enabled(1)) trace.write(title + ": TL CR param ID " + param_id);
            throw EdbcEx.get(E_IO0001_CONNECT_ERR);
        }
      }

      if (in.avail() > 0) throw EdbcEx.get(E_IO0001_CONNECT_ERR);

      /*
       ** Validate message parameters.
       */
      if (tl_proto < JDBC_TL_PROTO_1
          || tl_proto > JDBC_TL_DFLT_PROTO
          || size < JDBC_TL_PKT_MIN
          || size > JDBC_TL_PKT_MAX) {
        if (trace.enabled(1))
          trace.write(title + ": invalid TL parameter: protocol " + tl_proto + ", size " + size);
        throw EdbcEx.get(E_IO0001_CONNECT_ERR);
      }

      try {
        char_set = CharSet.getCharSet(cs);
      } catch (Exception ex) {
        if (trace.enabled(1)) trace.write(title + ": incompatible character set: " + cs);
        throw EdbcEx.get(E_IO0001_CONNECT_ERR);
      }
    } catch (EdbcEx ex) {
      if (trace.enabled(1)) trace.write(title + ": error negotiating parameters");
      disconnect();
      throw ex;
    }

    /*
     ** We initialized with minimum buffer size, then requested
     ** maximum size for the connection.  If the server accepts
     ** anything more than minimum, adjust the buffers accordingly.
     */
    if (size != JDBC_TL_PKT_MIN) setBuffSize(1 << size);

    if (trace.enabled(3)) {
      trace.write(title + ": TL connection parameters negotiated");
      trace.write("    TL protocol level : " + tl_proto);
      trace.write("    TL buffer size    : " + (1 << size));
      trace.write("    Character encoding: " + cs + " => " + char_set);
    }
  } // DbConnIn