/// <summary>
        /// Creates a SslBrokerClient instance.
        /// </summary>
        /// <param name="hostInfo">Information about an agent.</param>
        /// <param name="collection">A collection of X.509 certificates. Useful when agents are not using a key that can be validated by a trusted X.509 certificate.</param>
        public SslBrokerClient(HostInfo hostInfo, X509CertificateCollection collection)
        {
            IList<HostInfo> hosts = new List<HostInfo>(1);
            hosts.Add(hostInfo);

            this.hosts = hosts;
            SslNetworkHandler sslNetHandler = new SslNetworkHandler(hosts, collection);
            protocolHandler = new BrokerProtocolHandler(messageSerializer, sslNetHandler);
        }
Beispiel #2
0
        /// <summary>
        /// Creates a BrokerClient instance and connects to an agent.
        /// </summary>
        /// <param name="hostInfo">Information about an agent.</param>
        public BrokerClient(HostInfo hostInfo)
        {
            IList<HostInfo> hosts = new List<HostInfo>(1);
            hosts.Add(hostInfo);

            this.hosts = hosts;
            NetworkHandler networkHandler = new NetworkHandler(hosts);
            protocolHandler = new BrokerProtocolHandler(messageSerializer, networkHandler);
            protocolHandler.OnCommunicationFailed += new CommunicationFailed(HandleOnCommunicationFailed);
        }
        /// <summary>
        /// Creates a BrokerClient instance and connects to an agent.
        /// </summary>
        /// <param name="hostInfo">Information about an agent.</param>
        public BrokerClient(HostInfo hostInfo)
        {
            IList <HostInfo> hosts = new List <HostInfo>(1);

            hosts.Add(hostInfo);

            this.hosts = hosts;
            NetworkHandler networkHandler = new NetworkHandler(hosts);

            protocolHandler = new BrokerProtocolHandler(messageSerializer, networkHandler);
            protocolHandler.OnCommunicationFailed += new CommunicationFailed(HandleOnCommunicationFailed);
        }
        private void StartReading(HostInfo hostInfo)
        {
            // Initialize comunication
            CreateAndConnect(hostInfo);

            // Start receiving
            ReadContext readContext = new ReadContext(4 * 1024, this.connectionVersion);

            ReadStream.BeginRead(readContext.Data, 0, readContext.Data.Length, new AsyncCallback(ReadCallback), readContext);
        }
        /// <summary>
        /// Method used by trying to reconnect.
        /// </summary>
        /// <param name="connectionVersion">A value indicating witch connection version is being used. Useful for ensuring that only one thread performs this action.</param>
        /// <returns>false if it failed to reconnect or false indicating that reconnection was sucessfull</returns>
        private bool OnIoFailure(long connectionVersion)
        {
            lock (this)
            {
                if (closed)
                    return false;

                // Check connection version
                if (connectionVersion < this.connectionVersion)
                {
                    // Another thread already sinalized the failure so return
                    return !closed; // If the reconnection was sucessful then it's not closed, otherwise it is.
                }
                log.ErrorFormat("Connection failed. {0}", hosts.Peek());

                // Fire event
                IoSyncStatus syncStatus = new IoSyncStatus(IoStatus.Failed, new NotifiableEvent<IoStatus>());
                IoFailureHandler handler = this.IoFailed;
                if (handler != null)
                    handler(syncStatus);

                // Close socket
                CloseCommunication();

                // Reconnect

                bool retry = false;
                int tryCount = 0;
                do
                {
                    try
                    {
                        ++this.connectionVersion;
                        StartReading(hostInfo);
                        retry = false;
                    }
                    catch (Exception)
                    {
                        retry = (++tryCount) != retriesCount;
                        if (!retry)
                        {
                            closed = true;
                            //Notify clients
                            syncStatus.Status = IoStatus.Closed;
                            syncStatus.OnChange.Fire(syncStatus.Status);

                            // Return
                            return false;
                        }
                        // wait for while, give the agent time to get back to life.
                        System.Threading.Thread.Sleep(1000);
                        hostInfo = hosts.Get();
                    }
                    if (retry)
                        log.ErrorFormat("Re-connection failed. {0}", hosts.Peek());
                } while (retry);

                syncStatus.Status = IoStatus.Ok;

                syncStatus.OnChange.Fire(syncStatus.Status);
            }
            return true;
        }
 protected void CreateAndConnect(HostInfo hostInfo)
 {
     this.socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
     IAsyncResult asyncResult = socket.BeginConnect(hostInfo.Hostname, hostInfo.Port, null, null);
     if (!asyncResult.CompletedSynchronously)
     {
         bool signaled = asyncResult.AsyncWaitHandle.WaitOne(15 * 1000, false);
         if (!signaled)
             throw new TimeoutException(String.Format("Connection timeout while connecting to agent {0}:{1}", hostInfo.Hostname, hostInfo.Port));
     }
     this.communicationStream = GetCommunicationStream();
 }
 public void Start()
 {
     try
     {
         hostInfo = hosts.Get();
         StartReading(hostInfo);
     }
     catch (Exception e)
     {
         // Connection failed
         if (!OnIoFailure(this.connectionVersion))
         {
             log.Error("Re-Connection failed", e);
             throw e;
         }
     }
 }
 /// <summary>
 /// Creates a SslBrokerClient instance.
 /// </summary>
 /// <param name="hostInfo">Information about an agent.</param>
 public SslBrokerClient(HostInfo hostInfo)
     : this(hostInfo, null)
 {
 }
 public static void SendMessageOverUdp(NetMessage message, HostInfo hostInfo, IMessageSerializer messageSerializer)
 {
     byte[] encodedData = messageSerializer.Marshall(message);
     UdpNetworkHandler.SendMessage(encodedData, hostInfo, messageSerializer);
 }
Beispiel #10
0
        /// <summary>
        /// Publish a message over UDP.
        /// </summary>
        /// <param name="message">Message content.</param>
        /// <param name="destination">Message destination.</param>
        /// <param name="hostInfo">Agent information.</param>
        /// <param name="messageSerializer">Serialization type.</param>
        public static void PublishMessageOverUdp(NetBrokerMessage message, string destination, HostInfo hostInfo, IMessageSerializer messageSerializer)
        {
            NetPublish publish = new NetPublish(destination, NetAction.DestinationType.TOPIC, message);

            NetAction action = new NetAction(NetAction.ActionType.PUBLISH);
            action.PublishMessage = publish;

            NetMessage netMessage = new NetMessage(action, message.Headers);

            BrokerProtocolHandler.SendMessageOverUdp(netMessage, hostInfo, messageSerializer);
        }
 /// <summary>
 /// Creates a SslBrokerClient instance.
 /// </summary>
 /// <param name="hostInfo">Information about an agent.</param>
 public SslBrokerClient(HostInfo hostInfo) : this(hostInfo, null)
 {
 }
        /// <summary>
        /// Enqueue a message over UDP.
        /// </summary>
        /// <param name="message">Message content.</param>
        /// <param name="destination">Message destination.</param>
        /// <param name="hostInfo">Agent information.</param>
        /// <param name="messageSerializer">Serialization type.</param>
        public static void EnqueueMessageOverUdp(NetBrokerMessage message, string destination, HostInfo hostInfo, IMessageSerializer messageSerializer)
        {
            NetPublish publish = new NetPublish(destination, NetAction.DestinationType.QUEUE, message);

            NetAction action = new NetAction(NetAction.ActionType.PUBLISH);

            action.PublishMessage = publish;

            NetMessage netMessage = new NetMessage(action, message.Headers);

            BrokerProtocolHandler.SendMessageOverUdp(netMessage, hostInfo, messageSerializer);
        }