private void DatabaseInfo(byte[] items, byte[] buffer, int bufferLength) { try { XdrStream.Write(IscCodes.op_info_database); XdrStream.Write(_handle); XdrStream.Write(Incarnation); XdrStream.WriteBuffer(items, items.Length); XdrStream.Write(bufferLength); XdrStream.Flush(); GenericResponse response = (GenericResponse)ReadResponse(); int responseLength = bufferLength; if (response.Data.Length < bufferLength) { responseLength = response.Data.Length; } Buffer.BlockCopy(response.Data, 0, buffer, 0, responseLength); } catch (IOException ex) { throw IscException.ForErrorCode(IscCodes.isc_network_error, ex); } }
public virtual void DropDatabase() { lock (SyncObject) { try { XdrStream.Write(IscCodes.op_drop_database); XdrStream.Write(_handle); XdrStream.Flush(); ReadResponse(); _handle = 0; } catch (IOException ex) { throw IscException.ForErrorCode(IscCodes.isc_network_error, ex); } finally { try { CloseConnection(); } catch { } } } }
public void Dispose() { if (!_disposed) { _disposed = true; if (TransactionCount > 0) { throw IscException.ForErrorCodeIntParam(IscCodes.isc_open_trans, TransactionCount); } try { CloseEventManager(); var detach = _handle != -1; if (detach) { XdrStream.Write(IscCodes.op_detach); XdrStream.Write(_handle); } XdrStream.Write(IscCodes.op_disconnect); XdrStream.Flush(); if (detach) { ReadResponse(); } CloseConnection(); #warning Here _xdrStream?.Dispose(); } catch (IOException ex) { try { CloseConnection(); } catch (IOException ex2) { throw IscException.ForErrorCode(IscCodes.isc_network_error, ex2); } throw IscException.ForErrorCode(IscCodes.isc_network_error, ex); } finally { _xdrStream = null; _connection = null; _charset = null; _eventManager = null; _serverVersion = null; _dialect = 0; _handle = -1; _packetSize = 0; _warningMessage = null; _transactionCount = 0; } } }
protected virtual byte[] WriteParameters() { if (_parameters == null) { return(null); } using (var xdr = new XdrStream(_database.Charset)) { for (var i = 0; i < _parameters.Count; i++) { var field = _parameters[i]; try { WriteRawParameter(xdr, field); xdr.Write(field.NullFlag); } catch (IOException ex) { throw IscException.ForErrorCode(IscCodes.isc_net_write_err, ex); } } return(xdr.ToArray()); } }
/// <summary> /// isc_database_info /// </summary> private void DatabaseInfo(byte[] items, byte[] buffer, int bufferLength) { lock (SyncObject) { try { // see src/remote/protocol.h for packet definition (p_info struct) XdrStream.Write(IscCodes.op_info_database); // operation XdrStream.Write(_handle); // db_handle XdrStream.Write(0); // incarnation XdrStream.WriteBuffer(items, items.Length); // items XdrStream.Write(bufferLength); // result buffer length XdrStream.Flush(); GenericResponse response = (GenericResponse)ReadResponse(); int responseLength = bufferLength; if (response.Data.Length < bufferLength) { responseLength = response.Data.Length; } Buffer.BlockCopy(response.Data, 0, buffer, 0, responseLength); } catch (IOException ex) { throw IscException.ForErrorCode(IscCodes.isc_network_error, ex); } } }
public void QueueEvents(RemoteEvent remoteEvent) { try { if (_eventManager == null) { ConnectionRequest(out var auxHandle, out var ipAddress, out var portNumber); _eventManager = new GdsEventManager(auxHandle, ipAddress, portNumber); var dummy = _eventManager.WaitForEventsAsync(remoteEvent); } remoteEvent.LocalId++; EventParameterBuffer epb = remoteEvent.BuildEpb(); byte[] epbData = epb.ToArray(); XdrStream.Write(IscCodes.op_que_events); XdrStream.Write(_handle); XdrStream.WriteBuffer(epbData); XdrStream.Write(AddressOfAstRoutine); XdrStream.Write(ArgumentToAstRoutine); XdrStream.Write(remoteEvent.LocalId); XdrStream.Flush(); GenericResponse response = (GenericResponse)ReadResponse(); remoteEvent.RemoteId = response.ObjectHandle; } catch (IOException ex) { throw IscException.ForErrorCode(IscCodes.isc_net_read_err, ex); } }
public virtual void ConnectionRequest(out int auxHandle, out string ipAddress, out int portNumber) { lock (SyncObject) { try { XdrStream.Write(IscCodes.op_connect_request); XdrStream.Write(IscCodes.P_REQ_async); // Connection type XdrStream.Write(_handle); // Related object XdrStream.Write(0); // Partner identification XdrStream.Flush(); ReadOperation(); auxHandle = XdrStream.ReadInt32(); // garbage XdrStream.ReadBytes(8); int respLen = XdrStream.ReadInt32(); respLen += respLen % 4; // sin_family XdrStream.ReadBytes(2); respLen -= 2; // sin_port byte[] buffer = XdrStream.ReadBytes(2); portNumber = (ushort)IPAddress.NetworkToHostOrder(BitConverter.ToInt16(buffer, 0)); respLen -= 2; // * The address returned by the server may be incorrect if it is behind a NAT box // * so we must use the address that was used to connect the main socket, not the // * address reported by the server. // sin_addr buffer = XdrStream.ReadBytes(4); //ipAddress = string.Format( // CultureInfo.InvariantCulture, // "{0}.{1}.{2}.{3}", // buffer[0], buffer[1], buffer[2], buffer[3]); ipAddress = _connection.IPAddress.ToString(); respLen -= 4; // garbage XdrStream.ReadBytes(respLen); // Read Status Vector XdrStream.ReadStatusVector(); } catch (IOException ex) { throw IscException.ForErrorCode(IscCodes.isc_net_read_err, ex); } } }
public virtual void Detach() { lock (SyncObject) { if (TransactionCount > 0) { throw IscException.ForErrorCodeIntParam(IscCodes.isc_open_trans, TransactionCount); } try { if (_handle != 0) { XdrStream.Write(IscCodes.op_detach); XdrStream.Write(_handle); } XdrStream.Write(IscCodes.op_disconnect); XdrStream.Flush(); // Close the Event Manager CloseEventManager(); // Disconnect CloseConnection(); #warning Here // Close Input and Output streams _xdrStream?.Close(); // Clear members _transactionCount = 0; _handle = 0; _dialect = 0; _packetSize = 0; _xdrStream = null; _charset = null; _connection = null; _serverVersion = null; } catch (IOException ex) { try { CloseConnection(); } catch (IOException ex2) { throw IscException.ForErrorCode(IscCodes.isc_network_error, ex2); } throw IscException.ForErrorCode(IscCodes.isc_network_error, ex); } } }
protected virtual void SendCreateToBuffer(DatabaseParameterBuffer dpb, string database) { XdrStream.Write(IscCodes.op_create); XdrStream.Write(DatabaseObjectId); if (!string.IsNullOrEmpty(Password)) { dpb.Append(IscCodes.isc_dpb_password, Password); } XdrStream.WriteBuffer(Encoding2.Default.GetBytes(database)); XdrStream.WriteBuffer(dpb.ToArraySegment()); }
protected virtual void SendAttachToBuffer(DatabaseParameterBuffer dpb, string database) { XdrStream.Write(IscCodes.op_attach); XdrStream.Write(0); if (!string.IsNullOrEmpty(Password)) { dpb.Append(IscCodes.isc_dpb_password, Password); } XdrStream.WriteBuffer(Encoding.Default.GetBytes(database)); XdrStream.WriteBuffer(dpb.ToArray()); }
protected virtual void SendCreateToBuffer(DatabaseParameterBuffer dpb, string database) { XdrStream.Write(IscCodes.op_create); #warning Some constant for default database object ID XdrStream.Write(0); if (!string.IsNullOrEmpty(Password)) { dpb.Append(IscCodes.isc_dpb_password, Password); } XdrStream.WriteBuffer(Encoding.Default.GetBytes(database)); XdrStream.WriteBuffer(dpb.ToArray()); }
public virtual void ConnectionRequest(out int auxHandle, out string ipAddress, out int portNumber) { try { XdrStream.Write(IscCodes.op_connect_request); XdrStream.Write(IscCodes.P_REQ_async); XdrStream.Write(_handle); XdrStream.Write(PartnerIdentification); XdrStream.Flush(); ReadOperation(); auxHandle = XdrStream.ReadInt32(); var garbage1 = new byte[8]; XdrStream.ReadBytes(garbage1, 8); var respLen = XdrStream.ReadInt32(); respLen += respLen % 4; var sin_family = new byte[2]; XdrStream.ReadBytes(sin_family, 2); respLen -= 2; var sin_port = new byte[2]; XdrStream.ReadBytes(sin_port, 2); portNumber = (ushort)IPAddress.NetworkToHostOrder(BitConverter.ToInt16(sin_port, 0)); respLen -= 2; // * The address returned by the server may be incorrect if it is behind a NAT box // * so we must use the address that was used to connect the main socket, not the // * address reported by the server. var sin_addr = new byte[4]; XdrStream.ReadBytes(sin_addr, 4); //ipAddress = string.Format( // CultureInfo.InvariantCulture, // "{0}.{1}.{2}.{3}", // buffer[0], buffer[1], buffer[2], buffer[3]); ipAddress = _connection.IPAddress.ToString(); respLen -= 4; var garbage2 = new byte[respLen]; XdrStream.ReadBytes(garbage2, respLen); XdrStream.ReadStatusVector(); } catch (IOException ex) { throw IscException.ForErrorCode(IscCodes.isc_net_read_err, ex); } }
protected void SendExecuteToBuffer() { // this may throw error, so it needs to be before any writing byte[] descriptor = null; if (this.parameters != null) { using (XdrStream xdr = new XdrStream(this.database.Charset)) { xdr.Write(this.parameters); descriptor = xdr.ToArray(); } } // Write the message if (this.statementType == DbStatementType.StoredProcedure) { this.database.Write(IscCodes.op_execute2); } else { this.database.Write(IscCodes.op_execute); } this.database.Write(this.handle); this.database.Write(this.transaction.Handle); if (this.parameters != null) { this.database.WriteBuffer(this.parameters.ToBlrArray()); this.database.Write(0); // Message number this.database.Write(1); // Number of messages this.database.Write(descriptor, 0, descriptor.Length); } else { this.database.WriteBuffer(null); this.database.Write(0); this.database.Write(0); } if (this.statementType == DbStatementType.StoredProcedure) { this.database.WriteBuffer((this.fields == null) ? null : this.fields.ToBlrArray()); this.database.Write(0); // Output message number } }
public virtual void DropDatabase() { try { XdrStream.Write(IscCodes.op_drop_database); XdrStream.Write(_handle); XdrStream.Flush(); ReadResponse(); _handle = -1; } catch (IOException ex) { throw IscException.ForErrorCode(IscCodes.isc_network_error, ex); } }
public void CancelEvents(RemoteEvent events) { try { XdrStream.Write(IscCodes.op_cancel_events); XdrStream.Write(_handle); XdrStream.Write(events.LocalId); XdrStream.Flush(); ReadResponse(); } catch (IOException ex) { throw IscException.ForErrorCode(IscCodes.isc_net_read_err, ex); } }
public void QueueEvents(RemoteEvent events) { if (_eventManager == null) { string ipAddress = string.Empty; int portNumber = 0; int auxHandle = 0; ConnectionRequest(out auxHandle, out ipAddress, out portNumber); _eventManager = new GdsEventManager(auxHandle, ipAddress, portNumber); } lock (SyncObject) { try { events.LocalId = Interlocked.Increment(ref _eventsId); // Enqueue events in the event manager _eventManager.QueueEvents(events); EventParameterBuffer epb = events.ToEpb(); XdrStream.Write(IscCodes.op_que_events); XdrStream.Write(_handle); // Database object id XdrStream.WriteBuffer(epb.ToArray()); // Event description block XdrStream.Write(0); // Address of ast routine XdrStream.Write(0); // Argument to ast routine XdrStream.Write(events.LocalId); // Client side id of remote event XdrStream.Flush(); GenericResponse response = (GenericResponse)ReadResponse(); // Update event Remote event ID events.RemoteId = response.ObjectHandle; } catch (IOException ex) { throw IscException.ForErrorCode(IscCodes.isc_net_read_err, ex); } } }
public void CancelEvents(RemoteEvent events) { lock (SyncObject) { try { XdrStream.Write(IscCodes.op_cancel_events); XdrStream.Write(_handle); // Database object id XdrStream.Write(events.LocalId); // Event ID XdrStream.Flush(); ReadResponse(); _eventManager.CancelEvents(events); } catch (IOException ex) { throw IscException.ForErrorCode(IscCodes.isc_net_read_err, ex); } } }
protected void DoReleaseObjectPacket(int op, int id) { XdrStream.Write(op); XdrStream.Write(id); }
public virtual void Identify(string database) { // handles this.networkStream XdrStream inputStream = this.CreateXdrStream(); XdrStream outputStream = this.CreateXdrStream(); try { outputStream.Write(IscCodes.op_connect); outputStream.Write(IscCodes.op_attach); outputStream.Write(IscCodes.CONNECT_VERSION2); // CONNECT_VERSION2 outputStream.Write(1); // Architecture of client - Generic outputStream.Write(database); // Database path outputStream.Write(3); // Protocol versions understood outputStream.WriteBuffer(UserIdentificationStuff()); // User identification Stuff outputStream.Write(IscCodes.PROTOCOL_VERSION10); // Protocol version outputStream.Write(1); // Architecture of client - Generic outputStream.Write(2); // Minimum type (ptype_rpc) outputStream.Write(3); // Maximum type (ptype_batch_send) outputStream.Write(0); // Preference weight outputStream.Write(IscCodes.PROTOCOL_VERSION11); // Protocol version outputStream.Write(1); // Architecture of client - Generic outputStream.Write(2); // Minumum type (ptype_rpc) outputStream.Write(5); // Maximum type (ptype_lazy_send) outputStream.Write(1); // Preference weight outputStream.Write(IscCodes.PROTOCOL_VERSION12); // Protocol version outputStream.Write(1); // Architecture of client - Generic outputStream.Write(2); // Minumum type (ptype_rpc) outputStream.Write(5); // Maximum type (ptype_lazy_send) outputStream.Write(2); // Preference weight outputStream.Flush(); if (inputStream.ReadOperation() == IscCodes.op_accept) { this.protocolVersion = inputStream.ReadInt32(); // Protocol version this.protocolArchitecture = inputStream.ReadInt32(); // Architecture for protocol this.protocolMinimunType = inputStream.ReadInt32(); // Minimum type if (this.protocolVersion < 0) { this.protocolVersion = (ushort)(this.protocolVersion & IscCodes.FB_PROTOCOL_MASK) | IscCodes.FB_PROTOCOL_FLAG; } } else { try { this.Disconnect(); } catch { } finally { throw new IscException(IscCodes.isc_connect_reject); } } } catch (IOException) { throw new IscException(IscCodes.isc_network_error); } }
public void Write(string value) { _outputStream.Write(value); }
protected void WriteRawParameter(XdrStream xdr, DbField field) { if (field.DbDataType != DbDataType.Null) { field.FixNull(); switch (field.DbDataType) { case DbDataType.Char: if (field.Charset.IsOctetsCharset) { xdr.WriteOpaque(field.DbValue.GetBinary(), field.Length); } else { var svalue = field.DbValue.GetString(); if ((field.Length % field.Charset.BytesPerCharacter) == 0 && svalue.Length > field.CharCount) { throw IscException.ForErrorCodes(new[] { IscCodes.isc_arith_except, IscCodes.isc_string_truncation }); } xdr.WriteOpaque(field.Charset.GetBytes(svalue), field.Length); } break; case DbDataType.VarChar: if (field.Charset.IsOctetsCharset) { xdr.WriteBuffer(field.DbValue.GetBinary()); } else { var svalue = field.DbValue.GetString(); if ((field.Length % field.Charset.BytesPerCharacter) == 0 && svalue.Length > field.CharCount) { throw IscException.ForErrorCodes(new[] { IscCodes.isc_arith_except, IscCodes.isc_string_truncation }); } xdr.WriteBuffer(field.Charset.GetBytes(svalue)); } break; case DbDataType.SmallInt: xdr.Write(field.DbValue.GetInt16()); break; case DbDataType.Integer: xdr.Write(field.DbValue.GetInt32()); break; case DbDataType.BigInt: case DbDataType.Array: case DbDataType.Binary: case DbDataType.Text: xdr.Write(field.DbValue.GetInt64()); break; case DbDataType.Decimal: case DbDataType.Numeric: xdr.Write(field.DbValue.GetDecimal(), field.DataType, field.NumericScale); break; case DbDataType.Float: xdr.Write(field.DbValue.GetFloat()); break; case DbDataType.Guid: xdr.Write(field.DbValue.GetGuid()); break; case DbDataType.Double: xdr.Write(field.DbValue.GetDouble()); break; case DbDataType.Date: xdr.Write(field.DbValue.GetDate()); break; case DbDataType.Time: xdr.Write(field.DbValue.GetTime()); break; case DbDataType.TimeStamp: xdr.Write(field.DbValue.GetDate()); xdr.Write(field.DbValue.GetTime()); break; case DbDataType.Boolean: xdr.Write(field.DbValue.GetBoolean()); break; default: throw IscException.ForStrParam($"Unknown SQL data type: {field.DataType}."); } } }
protected void SendExecuteToBuffer() { // this may throw error, so it needs to be before any writing byte[] descriptor = null; if (this.parameters != null) { using (XdrStream xdr = new XdrStream(this.database.Charset)) { xdr.Write(this.parameters); descriptor = xdr.ToArray(); } } // Write the message if (this.statementType == DbStatementType.StoredProcedure) { this.database.Write(IscCodes.op_execute2); } else { this.database.Write(IscCodes.op_execute); } this.database.Write(this.handle); this.database.Write(this.transaction.Handle); if (this.parameters != null) { this.database.WriteBuffer(this.parameters.ToBlrArray()); this.database.Write(0); // Message number this.database.Write(1); // Number of messages this.database.Write(descriptor, 0, descriptor.Length); } else { this.database.WriteBuffer(null); this.database.Write(0); this.database.Write(0); } if (this.statementType == DbStatementType.StoredProcedure) { this.database.WriteBuffer((this.fields == null) ? null : this.fields.ToBlrArray()); this.database.Write(0); // Output message number } }
private byte[] EncodeSliceArray(Array sourceArray) { DbDataType dbType = DbDataType.Array; Charset charset = _database.Charset; XdrStream xdr = new XdrStream(_database.Charset); int subType = (Descriptor.Scale < 0) ? 2 : 0; int type = 0; type = TypeHelper.GetFbType(Descriptor.DataType); dbType = TypeHelper.GetDbDataType(Descriptor.DataType, subType, Descriptor.Scale); foreach (object source in sourceArray) { switch (dbType) { case DbDataType.Char: byte[] buffer = charset.GetBytes(source.ToString()); xdr.WriteOpaque(buffer, Descriptor.Length); break; case DbDataType.VarChar: xdr.Write((string)source); break; case DbDataType.SmallInt: xdr.Write((short)source); break; case DbDataType.Integer: xdr.Write((int)source); break; case DbDataType.BigInt: xdr.Write((long)source); break; case DbDataType.Decimal: case DbDataType.Numeric: xdr.Write((decimal)source, type, Descriptor.Scale); break; case DbDataType.Float: xdr.Write((float)source); break; case DbDataType.Double: xdr.Write((double)source); break; case DbDataType.Date: xdr.WriteDate(Convert.ToDateTime(source, CultureInfo.CurrentCulture.DateTimeFormat)); break; case DbDataType.Time: xdr.WriteTime((TimeSpan)source); break; case DbDataType.TimeStamp: xdr.Write(Convert.ToDateTime(source, CultureInfo.CurrentCulture.DateTimeFormat)); break; default: throw new NotSupportedException("Unknown data type"); } } return xdr.ToArray(); }
private byte[] EncodeSliceArray(Array sourceArray) { DbDataType dbType = DbDataType.Array; Charset charset = _database.Charset; int subType = (Descriptor.Scale < 0) ? 2 : 0; int type = 0; using (XdrStream xdr = new XdrStream(_database.Charset)) { type = TypeHelper.GetSqlTypeFromBlrType(Descriptor.DataType); dbType = TypeHelper.GetDbDataTypeFromBlrType(Descriptor.DataType, subType, Descriptor.Scale); foreach (object source in sourceArray) { switch (dbType) { case DbDataType.Char: byte[] buffer = charset.GetBytes(source.ToString()); xdr.WriteOpaque(buffer, Descriptor.Length); break; case DbDataType.VarChar: xdr.Write((string)source); break; case DbDataType.SmallInt: xdr.Write((short)source); break; case DbDataType.Integer: xdr.Write((int)source); break; case DbDataType.BigInt: xdr.Write((long)source); break; case DbDataType.Decimal: case DbDataType.Numeric: xdr.Write((decimal)source, type, Descriptor.Scale); break; case DbDataType.Float: xdr.Write((float)source); break; case DbDataType.Double: xdr.Write((double)source); break; case DbDataType.Date: xdr.WriteDate(Convert.ToDateTime(source, CultureInfo.CurrentCulture.DateTimeFormat)); break; case DbDataType.Time: xdr.WriteTime((TimeSpan)source); break; case DbDataType.TimeStamp: xdr.Write(Convert.ToDateTime(source, CultureInfo.CurrentCulture.DateTimeFormat)); break; default: throw TypeHelper.InvalidDataType((int)dbType); } } return(xdr.ToArray()); } }
public virtual void Identify(string database) { // handles this.networkStream XdrStream inputStream = this.CreateXdrStream(); XdrStream outputStream = this.CreateXdrStream(); try { // Here we identify the user to the engine. // This may or may not be used as login info to a database. #if (!NET_CF) byte[] user = Encoding.Default.GetBytes(System.Environment.UserName); byte[] host = Encoding.Default.GetBytes(System.Net.Dns.GetHostName()); #else byte[] user = Encoding.Default.GetBytes("fbnetcf"); byte[] host = Encoding.Default.GetBytes(System.Net.Dns.GetHostName()); #endif using (MemoryStream user_id = new MemoryStream()) { // User Name user_id.WriteByte(1); user_id.WriteByte((byte)user.Length); user_id.Write(user, 0, user.Length); // Host name user_id.WriteByte(4); user_id.WriteByte((byte)host.Length); user_id.Write(host, 0, host.Length); // Attach/create using this connection will use user verification user_id.WriteByte(6); user_id.WriteByte(0); outputStream.Write(IscCodes.op_connect); outputStream.Write(IscCodes.op_attach); outputStream.Write(IscCodes.CONNECT_VERSION2); // CONNECT_VERSION2 outputStream.Write(1); // Architecture of client - Generic outputStream.Write(database); // Database path outputStream.Write(3); // Protocol versions understood outputStream.WriteBuffer(user_id.ToArray()); // User identification Stuff outputStream.Write(IscCodes.PROTOCOL_VERSION10); // Protocol version outputStream.Write(1); // Architecture of client - Generic outputStream.Write(2); // Minimum type (ptype_rpc) outputStream.Write(3); // Maximum type (ptype_batch_send) outputStream.Write(0); // Preference weight outputStream.Write(IscCodes.PROTOCOL_VERSION11); // Protocol version outputStream.Write(1); // Architecture of client - Generic outputStream.Write(2); // Minumum type (ptype_rpc) outputStream.Write(5); // Maximum type (ptype_lazy_send) outputStream.Write(1); // Preference weight outputStream.Write(IscCodes.PROTOCOL_VERSION12); // Protocol version outputStream.Write(1); // Architecture of client - Generic outputStream.Write(2); // Minumum type (ptype_rpc) outputStream.Write(5); // Maximum type (ptype_lazy_send) outputStream.Write(2); // Preference weight } outputStream.Flush(); if (inputStream.ReadOperation() == IscCodes.op_accept) { this.protocolVersion = inputStream.ReadInt32(); // Protocol version this.protocolArchitecture = inputStream.ReadInt32(); // Architecture for protocol this.protocolMinimunType = inputStream.ReadInt32(); // Minimum type if (this.protocolVersion < 0) { this.protocolVersion = (ushort)(this.protocolVersion & IscCodes.FB_PROTOCOL_MASK) | IscCodes.FB_PROTOCOL_FLAG; } } else { try { this.Disconnect(); } catch (Exception) { } finally { throw new IscException(IscCodes.isc_connect_reject); } } } catch (IOException) { throw new IscException(IscCodes.isc_network_error); } }