Example #1
0
 public void WriteBytes(byte[] value, int offset, int length)
 {
     InitializeWritingMode();
     try
     {
         dataOut.Write(value, offset, length);
     }
     catch (Exception e)
     {
         throw NMSExceptionSupport.Create(e);
     }
 }
Example #2
0
 public void WriteSingle(float value)
 {
     InitializeWritingMode();
     try
     {
         dataOut.Write(value);
     }
     catch (Exception e)
     {
         throw NMSExceptionSupport.Create(e);
     }
 }
 public void WriteBytes(byte[] value)
 {
     InitializeWriting();
     try
     {
         dataOut.Write(value, 0, value.Length);
     }
     catch (Exception e)
     {
         throw NMSExceptionSupport.Create(e);
     }
 }
 public void WriteInt64(long value)
 {
     InitializeWriting();
     try
     {
         dataOut.Write(value);
     }
     catch (Exception e)
     {
         throw NMSExceptionSupport.Create(e);
     }
 }
Example #5
0
        public IConnection CreateConnection(string userName, string password)
        {
            Connection connection = null;

            try
            {
                Tracer.InfoFormat("Connecting to: {0}", brokerUri.ToString());

                ITransport transport = TransportFactory.CreateTransport(brokerUri);

                connection = new Connection(brokerUri, transport, this.ClientIdGenerator);

                connection.UserName = userName;
                connection.Password = password;

                ConfigureConnection(connection);

                if (this.clientId != null)
                {
                    // Set the connection factory version as the default, the user can
                    // still override this via a call to Connection.ClientId = XXX
                    connection.DefaultClientId = this.clientId;
                }

                connection.ITransport.Start();

                return(connection);
            }
            catch (NMSException e)
            {
                try
                {
                    connection.Close();
                }
                catch
                {
                }

                throw e;
            }
            catch (Exception e)
            {
                try
                {
                    connection.Close();
                }
                catch
                {
                }

                throw NMSExceptionSupport.Create("Could not connect to broker URL: " + this.brokerUri + ". Reason: " + e.Message, e);
            }
        }
 public void WriteBoolean(bool value)
 {
     InitializeWriting();
     try
     {
         dataOut.Write(value);
     }
     catch (Exception e)
     {
         throw NMSExceptionSupport.Create(e);
     }
 }
Example #7
0
        /// <summary>
        /// Used to get an enqueued message from the unconsumedMessages list. The
        /// amount of time this method blocks is based on the timeout value.  if
        /// timeout == Timeout.Infinite then it blocks until a message is received.
        /// if timeout == 0 then it it tries to not block at all, it returns a
        /// message if it is available if timeout > 0 then it blocks up to timeout
        /// amount of time.  Expired messages will consumed by this method.
        /// </summary>
        /// <param name="timeout">
        /// A <see cref="System.TimeSpan"/>
        /// </param>
        /// <returns>
        /// A <see cref="MessageDispatch"/>
        /// </returns>
        private MessageDispatch Dequeue(TimeSpan timeout)
        {
            DateTime deadline = DateTime.Now;

            if (timeout > TimeSpan.Zero)
            {
                deadline += timeout;
            }

            while (true)
            {
                MessageDispatch dispatch = this.unconsumedMessages.Dequeue(timeout);

                // Grab a single date/time for calculations to avoid timing errors.
                DateTime dispatchTime = DateTime.Now;

                if (dispatch == null)
                {
                    if (timeout > TimeSpan.Zero && !this.unconsumedMessages.Closed)
                    {
                        if (dispatchTime > deadline)
                        {
                            // Out of time.
                            timeout = TimeSpan.Zero;
                        }
                        else
                        {
                            // Adjust the timeout to the remaining time.
                            timeout = deadline - dispatchTime;
                        }
                    }
                    else
                    {
                        // Informs the caller of an error in the event that an async exception
                        // took down the parent connection.
                        if (this.failureError != null)
                        {
                            throw NMSExceptionSupport.Create(this.failureError);
                        }

                        return(null);
                    }
                }
                else if (dispatch.Message == null)
                {
                    return(null);
                }
                else
                {
                    return(dispatch);
                }
            }
        }
Example #8
0
 public void WriteString(string value)
 {
     InitializeWritingMode();
     try
     {
         // note if dataOut is an EndianBinaryWriter, strings are written with a 16bit short length.
         dataOut.Write(value);
     }
     catch (Exception e)
     {
         throw NMSExceptionSupport.Create(e);
     }
 }
Example #9
0
        public void Oneway(Command command)
        {
            CheckConnected();

            try
            {
                transport.Oneway(command);
            }
            catch (Exception ex)
            {
                throw NMSExceptionSupport.Create(ex);
            }
        }
Example #10
0
 public void WriteChar(char value)
 {
     InitializeWriting();
     try
     {
         this.dataOut.Write(PrimitiveMap.CHAR_TYPE);
         this.dataOut.Write(value);
     }
     catch (IOException e)
     {
         NMSExceptionSupport.Create(e);
     }
 }
Example #11
0
 public void WriteInt16(short value)
 {
     InitializeWriting();
     try
     {
         this.dataOut.Write(PrimitiveMap.SHORT_TYPE);
         this.dataOut.Write(value);
     }
     catch (IOException e)
     {
         NMSExceptionSupport.Create(e);
     }
 }
Example #12
0
 public void WriteBoolean(bool value)
 {
     InitializeWriting();
     try
     {
         this.dataOut.Write(PrimitiveMap.BOOLEAN_TYPE);
         this.dataOut.Write(value);
     }
     catch (IOException e)
     {
         NMSExceptionSupport.Create(e);
     }
 }
 public IConnection CreateConnection(string userName, string password)
 {
     try
     {
         ConnectionInfo connectionInfo = ConfigureConnectionInfo(userName, password);
         IProvider      provider       = ProviderFactory.Create(BrokerUri);
         return(new NmsConnection(connectionInfo, provider));
     }
     catch (Exception e)
     {
         throw NMSExceptionSupport.Create(e);
     }
 }
Example #14
0
 public void WriteSingle(float value)
 {
     InitializeWriting();
     try
     {
         this.dataOut.Write(PrimitiveMap.FLOAT_TYPE);
         this.dataOut.Write(value);
     }
     catch (IOException e)
     {
         NMSExceptionSupport.Create(e);
     }
 }
Example #15
0
 public void WriteDouble(double value)
 {
     InitializeWriting();
     try
     {
         this.dataOut.Write(PrimitiveMap.DOUBLE_TYPE);
         this.dataOut.Write(value);
     }
     catch (IOException e)
     {
         NMSExceptionSupport.Create(e);
     }
 }
Example #16
0
 public void WriteString(string value)
 {
     InitializeWriting();
     try
     {
         // JMS, CMS and NMS all encode the String using a 16 bit size header.
         dataOut.WriteString16(value);
     }
     catch (Exception e)
     {
         throw NMSExceptionSupport.Create(e);
     }
 }
Example #17
0
 public void WriteInt32(int value)
 {
     InitializeWriting();
     try
     {
         this.dataOut.Write(PrimitiveMap.INTEGER_TYPE);
         this.dataOut.Write(value);
     }
     catch (IOException e)
     {
         NMSExceptionSupport.Create(e);
     }
 }
Example #18
0
 public void WriteInt64(long value)
 {
     InitializeWriting();
     try
     {
         this.dataOut.Write(PrimitiveMap.LONG_TYPE);
         this.dataOut.Write(value);
     }
     catch (IOException e)
     {
         NMSExceptionSupport.Create(e);
     }
 }
Example #19
0
        protected virtual Connection CreateActiveMQConnection(string userName, string password)
        {
            Connection connection = null;

            try
            {
                Tracer.InfoFormat("Connecting to: {0}", brokerUri.ToString());

                ITransport transport = TransportFactory.CreateTransport(brokerUri);

                connection = CreateActiveMQConnection(transport);

                ConfigureConnection(connection);

                connection.UserName = userName;
                connection.Password = password;

                if (this.clientId != null)
                {
                    connection.DefaultClientId = this.clientId;
                }

                connection.ITransport.Start();

                return(connection);
            }
            catch (NMSException e)
            {
                try
                {
                    connection.Close();
                }
                catch
                {
                }

                throw e;
            }
            catch (Exception e)
            {
                try
                {
                    connection.Close();
                }
                catch
                {
                }

                throw NMSExceptionSupport.Create("Could not connect to broker URL: " + this.brokerUri + ". Reason: " + e.Message, e);
            }
        }
Example #20
0
 public void WriteBytes(byte[] value, int offset, int length)
 {
     InitializeWriting();
     try
     {
         this.dataOut.Write(PrimitiveMap.BYTE_ARRAY_TYPE);
         this.dataOut.Write((int)length);
         this.dataOut.Write(value, offset, length);
     }
     catch (IOException e)
     {
         NMSExceptionSupport.Create(e);
     }
 }
        public void Put(object value)
        {
            object entry = value;

            if (entry != null && entry is byte[] bytes)
            {
                byte[] bin = new byte[bytes.Length];
                bytes.CopyTo(bin, 0);
                entry = bin;
            }
            if (list.Add(entry) < 0)
            {
                throw NMSExceptionSupport.Create($"Failed to add {entry.ToString()} to stream.", null);
            }
        }
        private void ReportReconnectFailure(Exception lastFailure)
        {
            Tracer.Error($"Failed to connect after: {reconnectControl.ReconnectAttempts} attempt(s)");
            if (failed.CompareAndSet(false, true))
            {
                if (lastFailure == null)
                {
                    lastFailure = new IOException($"Failed to connect after: {reconnectControl.ReconnectAttempts} attempt(s)");
                }

                var exception = NMSExceptionSupport.Create(lastFailure);
                listener.OnConnectionFailure(exception);
                throw exception;
            }
        }
Example #23
0
        public void Put(object value)
        {
            object entry = value;

            if (entry != null && entry is byte[])
            {
                byte[] bin = new byte[(entry as byte[]).Length];
                (entry as byte[]).CopyTo(bin, 0);
                entry = bin;
            }
            if (list.Add(entry) < 0)
            {
                throw NMSExceptionSupport.Create(string.Format("Failed to add {0} to stream.", entry.ToString()), null);
            }
            position++;
        }
        private async Task DoRollback(bool startNewTransaction)
        {
            Tracer.Debug($"Rollback: {this.transactionInfo.Id}");

            var oldTransactionId = this.transactionInfo.Id;
            var nextTx           = startNewTransaction ? GetNextTransactionInfo() : null;

            try
            {
                await this.connection.Rollback(this.transactionInfo, nextTx);

                OnTransactionRolledBack();
                Reset();
                this.transactionInfo = nextTx;
            }
            catch (Exception e)
            {
                Tracer.Info($"Rollback failed for transaction: {oldTransactionId}");
                throw NMSExceptionSupport.Create(e);
            }
            finally
            {
                Reset();

                try
                {
                    // If the provider failed to start a new transaction there will not be
                    // a current provider transaction id present, so we attempt to create
                    // one to recover our state.
                    if (startNewTransaction && nextTx.ProviderTxId == null)
                    {
                        await Begin();
                    }
                }
                catch (Exception e)
                {
                    // TODO
                    // At this point the transacted session is now unrecoverable, we should
                    // probably close it.
                    Tracer.Info($"Failed to start new Transaction after failed rollback of: {this.transactionInfo} {e}");
                }
            }
        }
Example #25
0
        public Response SyncRequest(Command command, TimeSpan requestTimeout)
        {
            CheckConnected();

            try
            {
                Response response = transport.Request(command, requestTimeout);
                if (response is ExceptionResponse)
                {
                    ExceptionResponse exceptionResponse = (ExceptionResponse)response;
                    BrokerError       brokerError       = exceptionResponse.Exception;
                    throw new BrokerException(brokerError);
                }
                return(response);
            }
            catch (Exception ex)
            {
                throw NMSExceptionSupport.Create(ex);
            }
        }
Example #26
0
        private void InitializeObjectSerializer(AMQPObjectEncodingType type)
        {
            switch (type)
            {
            case AMQPObjectEncodingType.AMQP_TYPE:
                objectSerializer = new AMQPTypeSerializer(this);
                break;

            case AMQPObjectEncodingType.DOTNET_SERIALIZABLE:
                objectSerializer = new DotnetObjectSerializer(this);
                break;

            case AMQPObjectEncodingType.JAVA_SERIALIZABLE:
                objectSerializer = new JavaObjectSerializer(this);
                break;

            default:
                throw NMSExceptionSupport.Create(new ArgumentException("Unsupported object encoding."));
            }
        }
        public NmsConnection(ConnectionInfo connectionInfo, IProvider provider)
        {
            if (provider == null)
            {
                throw new ArgumentNullException($"{nameof(provider)}", "Remove provider instance not set.");
            }

            ConnectionInfo = connectionInfo;
            this.provider  = provider;
            provider.SetProviderListener(this);

            try
            {
                provider.Start();
            }
            catch (Exception e)
            {
                throw NMSExceptionSupport.Create(e);
            }
        }
Example #28
0
        public object ReadObject()
        {
            FailIfWriteOnlyMsgBody();
            FailIfBytesInBuffer();
            object result = null;
            object value  = null;

            try
            {
                value = cloak.Peek();
                if (value == null)
                {
                    result = null;
                }
                else if (value is byte[])
                {
                    byte[] buffer = value as byte[];
                    result = new byte[buffer.Length];
                    Array.Copy(buffer, 0, result as byte[], 0, buffer.Length);
                }
                else if (ConversionSupport.IsNMSType(value))
                {
                    result = value;
                }
            }
            catch (EndOfStreamException eos)
            {
                throw NMSExceptionSupport.CreateMessageEOFException(eos);
            }
            catch (IOException ioe)
            {
                throw NMSExceptionSupport.CreateMessageFormatException(ioe);
            }
            catch (Exception e)
            {
                Tracer.InfoFormat("Unexpected exception caught reading Object stream. Exception = {0}", e);
                throw NMSExceptionSupport.Create("Unexpected exception caught reading Object stream.", e);
            }
            cloak.Pop();
            return(result);
        }
Example #29
0
 public void WriteString(string value)
 {
     InitializeWriting();
     try
     {
         if (value.Length > 8192)
         {
             this.dataOut.Write(PrimitiveMap.BIG_STRING_TYPE);
             this.dataOut.WriteString32(value);
         }
         else
         {
             this.dataOut.Write(PrimitiveMap.STRING_TYPE);
             this.dataOut.WriteString16(value);
         }
     }
     catch (IOException e)
     {
         NMSExceptionSupport.Create(e);
     }
 }
Example #30
0
        internal void OnAsyncException(Exception error)
        {
            if (!this.closed.Value && !this.closing.Value)
            {
                if (this.ExceptionListener != null)
                {
                    if (!(error is NMSException))
                    {
                        error = NMSExceptionSupport.Create(error);
                    }
                    NMSException e = (NMSException)error;

                    // Called in another thread so that processing can continue
                    // here, ensures no lock contention.
                    executor.QueueUserWorkItem(AsyncCallExceptionListener, e);
                }
                else
                {
                    Tracer.Debug("Async exception with no exception listener: " + error);
                }
            }
        }