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 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 override Array DecodeSlice(byte[] slice) { DbDataType dbType = DbDataType.Array; Array sliceData = null; Array tempData = null; Type systemType = GetSystemType(); int[] lengths = new int[Descriptor.Dimensions]; int[] lowerBounds = new int[Descriptor.Dimensions]; int type = 0; int index = 0; for (int i = 0; i < Descriptor.Dimensions; i++) { lowerBounds[i] = Descriptor.Bounds[i].LowerBound; lengths[i] = Descriptor.Bounds[i].UpperBound; if (lowerBounds[i] == 0) { lengths[i]++; } } sliceData = Array.CreateInstance(systemType, lengths, lowerBounds); tempData = Array.CreateInstance(systemType, sliceData.Length); type = TypeHelper.GetSqlTypeFromBlrType(Descriptor.DataType); dbType = TypeHelper.GetDbDataTypeFromBlrType(Descriptor.DataType, 0, Descriptor.Scale); using (XdrStream xdr = new XdrStream(slice, _database.Charset)) { while (xdr.Position < xdr.Length) { switch (dbType) { case DbDataType.Char: tempData.SetValue(xdr.ReadString(Descriptor.Length), index); break; case DbDataType.VarChar: tempData.SetValue(xdr.ReadString(), index); break; case DbDataType.SmallInt: tempData.SetValue(xdr.ReadInt16(), index); break; case DbDataType.Integer: tempData.SetValue(xdr.ReadInt32(), index); break; case DbDataType.BigInt: tempData.SetValue(xdr.ReadInt64(), index); break; case DbDataType.Numeric: case DbDataType.Decimal: tempData.SetValue(xdr.ReadDecimal(type, Descriptor.Scale), index); break; case DbDataType.Float: tempData.SetValue(xdr.ReadSingle(), index); break; case DbDataType.Double: tempData.SetValue(xdr.ReadDouble(), index); break; case DbDataType.Date: tempData.SetValue(xdr.ReadDate(), index); break; case DbDataType.Time: tempData.SetValue(xdr.ReadTime(), index); break; case DbDataType.TimeStamp: tempData.SetValue(xdr.ReadDateTime(), index); break; } index++; } #if NET40 if (systemType.IsPrimitive) #else if (systemType.GetTypeInfo().IsPrimitive) #endif { // For primitive types we can use System.Buffer to copy generated data to destination array Buffer.BlockCopy(tempData, 0, sliceData, 0, Buffer.ByteLength(tempData)); } else { sliceData = tempData; } } return(sliceData); }
protected override System.Array DecodeSlice(byte[] slice) { DbDataType dbType = DbDataType.Array; Array sliceData = null; Array tempData = null; Type systemType = GetSystemType(); int[] lengths = new int[Descriptor.Dimensions]; int[] lowerBounds = new int[Descriptor.Dimensions]; int type = 0; int index = 0; // Get upper and lower bounds of each dimension for (int i = 0; i < Descriptor.Dimensions; i++) { lowerBounds[i] = Descriptor.Bounds[i].LowerBound; lengths[i] = Descriptor.Bounds[i].UpperBound; if (lowerBounds[i] == 0) { lengths[i]++; } } // Create arrays sliceData = Array.CreateInstance(systemType, lengths, lowerBounds); tempData = Array.CreateInstance(systemType, sliceData.Length); // Infer Firebird and Db datatypes type = TypeHelper.GetFbType(Descriptor.DataType); dbType = TypeHelper.GetDbDataType(Descriptor.DataType, 0, Descriptor.Scale); // Decode slice data XdrStream xdr = new XdrStream(slice, _database.Charset); while (xdr.Position < xdr.Length) { switch (dbType) { case DbDataType.Char: tempData.SetValue(xdr.ReadString(Descriptor.Length), index); break; case DbDataType.VarChar: tempData.SetValue(xdr.ReadString(), index); break; case DbDataType.SmallInt: tempData.SetValue(xdr.ReadInt16(), index); break; case DbDataType.Integer: tempData.SetValue(xdr.ReadInt32(), index); break; case DbDataType.BigInt: tempData.SetValue(xdr.ReadInt64(), index); break; case DbDataType.Numeric: case DbDataType.Decimal: tempData.SetValue(xdr.ReadDecimal(type, Descriptor.Scale), index); break; case DbDataType.Float: tempData.SetValue(xdr.ReadSingle(), index); break; case DbDataType.Double: tempData.SetValue(xdr.ReadDouble(), index); break; case DbDataType.Date: tempData.SetValue(xdr.ReadDate(), index); break; case DbDataType.Time: tempData.SetValue(xdr.ReadTime(), index); break; case DbDataType.TimeStamp: tempData.SetValue(xdr.ReadDateTime(), index); break; } index++; } if (systemType.IsPrimitive) { // For primitive types we can use System.Buffer to copy generated data to destination array Buffer.BlockCopy(tempData, 0, sliceData, 0, Buffer.ByteLength(tempData)); } else { sliceData = tempData; } // Close XDR stream xdr.Close(); return sliceData; }
protected override System.Array DecodeSlice(byte[] slice) { DbDataType dbType = DbDataType.Array; Array sliceData = null; Array tempData = null; Type systemType = this.GetSystemType(); int[] lengths = new int[this.Descriptor.Dimensions]; int[] lowerBounds = new int[this.Descriptor.Dimensions]; int type = 0; int index = 0; // Get upper and lower bounds of each dimension for (int i = 0; i < this.Descriptor.Dimensions; i++) { lowerBounds[i] = this.Descriptor.Bounds[i].LowerBound; lengths[i] = this.Descriptor.Bounds[i].UpperBound; if (lowerBounds[i] == 0) { lengths[i]++; } } // Create arrays #if (NET_CF) sliceData = Array.CreateInstance(systemType, lengths); #else sliceData = Array.CreateInstance(systemType, lengths, lowerBounds); #endif tempData = Array.CreateInstance(systemType, sliceData.Length); // Infer Firebird and Db datatypes type = TypeHelper.GetFbType(this.Descriptor.DataType); dbType = TypeHelper.GetDbDataType(this.Descriptor.DataType, 0, this.Descriptor.Scale); // Decode slice data XdrStream xdr = new XdrStream(slice, this.database.Charset); while (xdr.Position < xdr.Length) { switch (dbType) { case DbDataType.Char: tempData.SetValue(xdr.ReadString(this.Descriptor.Length), index); break; case DbDataType.VarChar: tempData.SetValue(xdr.ReadString(), index); break; case DbDataType.SmallInt: tempData.SetValue(xdr.ReadInt16(), index); break; case DbDataType.Integer: tempData.SetValue(xdr.ReadInt32(), index); break; case DbDataType.BigInt: tempData.SetValue(xdr.ReadInt64(), index); break; case DbDataType.Numeric: case DbDataType.Decimal: tempData.SetValue(xdr.ReadDecimal(type, this.Descriptor.Scale), index); break; case DbDataType.Float: tempData.SetValue(xdr.ReadSingle(), index); break; case DbDataType.Double: tempData.SetValue(xdr.ReadDouble(), index); break; case DbDataType.Date: tempData.SetValue(xdr.ReadDate(), index); break; case DbDataType.Time: tempData.SetValue(xdr.ReadTime(), index); break; case DbDataType.TimeStamp: tempData.SetValue(xdr.ReadDateTime(), index); break; } index++; } if (systemType.IsPrimitive) { // For primitive types we can use System.Buffer to copy generated data to destination array Buffer.BlockCopy(tempData, 0, sliceData, 0, Buffer.ByteLength(tempData)); } else { sliceData = tempData; } // Close XDR stream xdr.Close(); return(sliceData); }
public int ReadInt32() { return(_inputStream.ReadInt32()); }
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 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); } }