Exemple #1
0
        public async Task WaitForEvents(RemoteEvent remoteEvent, AsyncWrappingCommonArgs async)
        {
            while (true)
            {
                try
                {
                    var operation = await _database.ReadOperation(async).ConfigureAwait(false);

                    switch (operation)
                    {
                    case IscCodes.op_event:
                        var dbHandle = await _database.Xdr.ReadInt32(async).ConfigureAwait(false);

                        var buffer = await _database.Xdr.ReadBuffer(async).ConfigureAwait(false);

                        var ast = new byte[8];
                        await _database.Xdr.ReadBytes(ast, 8, async).ConfigureAwait(false);

                        var eventId = await _database.Xdr.ReadInt32(async).ConfigureAwait(false);

                        await remoteEvent.EventCounts(buffer, async).ConfigureAwait(false);

                        break;

                    default:
                        Debug.Assert(false);
                        break;
                    }
                }
                catch (Exception) when(Volatile.Read(ref _closing))
                {
                    return;
                }
                catch (Exception ex)
                {
                    remoteEvent.EventError(ex);
                    break;
                }
            }
        }
Exemple #2
0
        private byte[] ReceiveSliceResponse(ArrayDesc desc)
        {
            try
            {
                int operation = _database.ReadOperation();

                if (operation == IscCodes.op_slice)
                {
                    bool isVariying = false;
                    int  elements   = 0;
                    int  length     = _database.XdrStream.ReadInt32();

                    length = _database.XdrStream.ReadInt32();

                    switch (desc.DataType)
                    {
                    case IscCodes.blr_text:
                    case IscCodes.blr_text2:
                    case IscCodes.blr_cstring:
                    case IscCodes.blr_cstring2:
                        elements = length / desc.Length;
                        length  += elements * ((4 - desc.Length) & 3);
                        break;

                    case IscCodes.blr_varying:
                    case IscCodes.blr_varying2:
                        elements   = length / desc.Length;
                        isVariying = true;
                        break;

                    case IscCodes.blr_short:
                        length = length * desc.Length;
                        break;
                    }

                    if (isVariying)
                    {
                        using (XdrStream xdr = new XdrStream())
                        {
                            for (int i = 0; i < elements; i++)
                            {
                                byte[] buffer = _database.XdrStream.ReadOpaque(_database.XdrStream.ReadInt32());

                                xdr.WriteBuffer(buffer, buffer.Length);
                            }

                            return(xdr.ToArray());
                        }
                    }
                    else
                    {
                        return(_database.XdrStream.ReadOpaque(length));
                    }
                }
                else
                {
                    _database.SetOperation(operation);
                    _database.ReadResponse();

                    return(null);
                }
            }
            catch (IOException ex)
            {
                throw IscException.ForErrorCode(IscCodes.isc_net_read_err, ex);
            }
        }
Exemple #3
0
        public override DbValue[] Fetch()
        {
            if (State == StatementState.Deallocated)
            {
                throw new InvalidOperationException("Statement is not correctly created.");
            }
            if (StatementType == DbStatementType.StoredProcedure && !_allRowsFetched)
            {
                _allRowsFetched = true;
                return(GetOutputParameters());
            }
            else if (StatementType == DbStatementType.Insert && _allRowsFetched)
            {
                return(null);
            }
            else if (StatementType != DbStatementType.Select && StatementType != DbStatementType.SelectForUpdate)
            {
                return(null);
            }

            if (!_allRowsFetched && _rows.Count == 0)
            {
                try
                {
                    _database.Xdr.Write(IscCodes.op_fetch);
                    _database.Xdr.Write(_handle);
                    _database.Xdr.WriteBuffer(_fields.ToBlrArray());
                    _database.Xdr.Write(0);                     // p_sqldata_message_number
                    _database.Xdr.Write(_fetchSize);            // p_sqldata_messages
                    _database.Xdr.Flush();

                    var operation = _database.ReadOperation();
                    if (operation == IscCodes.op_fetch_response)
                    {
                        var hasOperation = true;
                        while (!_allRowsFetched)
                        {
#warning Possible refactoring to have smoother code for ReadResponse
                            var response = hasOperation ? _database.ReadResponse(operation) : _database.ReadResponse();
                            hasOperation = false;
                            if (response is FetchResponse fetchResponse)
                            {
                                if (fetchResponse.Count > 0 && fetchResponse.Status == 0)
                                {
                                    _rows.Enqueue(ReadRow());
                                }
                                else if (fetchResponse.Status == 100)
                                {
                                    _allRowsFetched = true;
                                }
                                else
                                {
                                    break;
                                }
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                    else
                    {
                        _database.ReadResponse(operation);
                    }
                }
                catch (IOException ex)
                {
                    throw IscException.ForErrorCode(IscCodes.isc_network_error, ex);
                }
            }

            if (_rows != null && _rows.Count > 0)
            {
                return(_rows.Dequeue());
            }
            else
            {
                _rows.Clear();
                return(null);
            }
        }
        private async Task <byte[]> ReceiveSliceResponse(ArrayDesc desc, AsyncWrappingCommonArgs async)
        {
            try
            {
                var operation = await _database.ReadOperation(async).ConfigureAwait(false);

                if (operation == IscCodes.op_slice)
                {
                    var isVariying = false;
                    var elements   = 0;
                    var length     = await _database.Xdr.ReadInt32(async).ConfigureAwait(false);

                    length = await _database.Xdr.ReadInt32(async).ConfigureAwait(false);

                    switch (desc.DataType)
                    {
                    case IscCodes.blr_text:
                    case IscCodes.blr_text2:
                    case IscCodes.blr_cstring:
                    case IscCodes.blr_cstring2:
                        elements = length / desc.Length;
                        length  += elements * ((4 - desc.Length) & 3);
                        break;

                    case IscCodes.blr_varying:
                    case IscCodes.blr_varying2:
                        elements   = length / desc.Length;
                        isVariying = true;
                        break;

                    case IscCodes.blr_short:
                        length = length * desc.Length;
                        break;
                    }

                    if (isVariying)
                    {
                        using (var ms = new MemoryStream())
                        {
                            var xdr = new XdrReaderWriter(new DataProviderStreamWrapper(ms));
                            for (var i = 0; i < elements; i++)
                            {
                                var buffer = await _database.Xdr.ReadOpaque(await _database.Xdr.ReadInt32(async).ConfigureAwait(false), async).ConfigureAwait(false);

                                await xdr.WriteBuffer(buffer, buffer.Length, async).ConfigureAwait(false);
                            }
                            await xdr.Flush(async).ConfigureAwait(false);

                            return(ms.ToArray());
                        }
                    }
                    else
                    {
                        return(await _database.Xdr.ReadOpaque(length, async).ConfigureAwait(false));
                    }
                }
                else
                {
                    await _database.ReadResponse(operation, async).ConfigureAwait(false);

                    return(null);
                }
            }
            catch (IOException ex)
            {
                throw IscException.ForIOException(ex);
            }
        }
Exemple #5
0
        private byte[] ReceiveSliceResponse(ArrayDesc desc)
        {
            try
            {
                var operation = _database.ReadOperation();
                if (operation == IscCodes.op_slice)
                {
                    var isVariying = false;
                    var elements   = 0;
                    var length     = _database.Xdr.ReadInt32();

                    length = _database.Xdr.ReadInt32();

                    switch (desc.DataType)
                    {
                    case IscCodes.blr_text:
                    case IscCodes.blr_text2:
                    case IscCodes.blr_cstring:
                    case IscCodes.blr_cstring2:
                        elements = length / desc.Length;
                        length  += elements * ((4 - desc.Length) & 3);
                        break;

                    case IscCodes.blr_varying:
                    case IscCodes.blr_varying2:
                        elements   = length / desc.Length;
                        isVariying = true;
                        break;

                    case IscCodes.blr_short:
                        length = length * desc.Length;
                        break;
                    }

                    if (isVariying)
                    {
                        using (var ms = new MemoryStream())
                        {
                            var xdr = new XdrReaderWriter(ms);
                            for (var i = 0; i < elements; i++)
                            {
                                var buffer = _database.Xdr.ReadOpaque(_database.Xdr.ReadInt32());
                                xdr.WriteBuffer(buffer, buffer.Length);
                            }
                            xdr.Flush();
                            return(ms.ToArray());
                        }
                    }
                    else
                    {
                        return(_database.Xdr.ReadOpaque(length));
                    }
                }
                else
                {
                    _database.ReadResponse(operation);
                    return(null);
                }
            }
            catch (IOException ex)
            {
                throw IscException.ForErrorCode(IscCodes.isc_network_error, ex);
            }
        }
        public override async Task <DbValue[]> Fetch(AsyncWrappingCommonArgs async)
        {
            EnsureNotDeallocated();

            if (StatementType == DbStatementType.StoredProcedure && !_allRowsFetched)
            {
                _allRowsFetched = true;
                return(GetOutputParameters());
            }
            else if (StatementType == DbStatementType.Insert && _allRowsFetched)
            {
                return(null);
            }
            else if (StatementType != DbStatementType.Select && StatementType != DbStatementType.SelectForUpdate)
            {
                return(null);
            }

            if (!_allRowsFetched && _rows.Count == 0)
            {
                try
                {
                    await _database.Xdr.Write(IscCodes.op_fetch, async).ConfigureAwait(false);

                    await _database.Xdr.Write(_handle, async).ConfigureAwait(false);

                    await _database.Xdr.WriteBuffer(_fields.ToBlrArray(), async).ConfigureAwait(false);

                    await _database.Xdr.Write(0, async).ConfigureAwait(false);                     // p_sqldata_message_number

                    await _database.Xdr.Write(_fetchSize, async).ConfigureAwait(false);            // p_sqldata_messages

                    await _database.Xdr.Flush(async).ConfigureAwait(false);

                    var operation = await _database.ReadOperation(async).ConfigureAwait(false);

                    if (operation == IscCodes.op_fetch_response)
                    {
                        var hasOperation = true;
                        while (!_allRowsFetched)
                        {
                            var response = hasOperation
                                                                ? await _database.ReadResponse(operation, async).ConfigureAwait(false)
                                                                : await _database.ReadResponse(async).ConfigureAwait(false);

                            hasOperation = false;
                            if (response is FetchResponse fetchResponse)
                            {
                                if (fetchResponse.Count > 0 && fetchResponse.Status == 0)
                                {
                                    _rows.Enqueue(await ReadRow(async).ConfigureAwait(false));
                                }
                                else if (fetchResponse.Status == 100)
                                {
                                    _allRowsFetched = true;
                                }
                                else
                                {
                                    break;
                                }
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                    else
                    {
                        await _database.ReadResponse(operation, async).ConfigureAwait(false);
                    }
                }
                catch (IOException ex)
                {
                    throw IscException.ForIOException(ex);
                }
            }

            if (_rows != null && _rows.Count > 0)
            {
                return(_rows.Dequeue());
            }
            else
            {
                _rows.Clear();
                return(null);
            }
        }