/// <summary> /// Starts the <see cref="ZeroMQServer"/> synchronously and begins accepting client connections asynchronously. /// </summary> /// <exception cref="InvalidOperationException">Attempt is made to <see cref="Start()"/> the <see cref="ZeroMQServer"/> when it is running.</exception> public override void Start() { if (CurrentState == ServerState.NotRunning) { int maxClientConnections, maxQueueSize; // Initialize if needed if (!Initialized) { Initialize(); } // Overwrite config file if max client connections exists in connection string. if (m_configData.ContainsKey("maxClientConnections") && int.TryParse(m_configData["maxClientConnections"], out maxClientConnections)) { MaxClientConnections = maxClientConnections; } // Overwrite config file if max send queue size exists in connection string. if (m_configData.ContainsKey("maxSendQueueSize") && int.TryParse(m_configData["maxSendQueueSize"], out maxQueueSize)) { m_maxSendQueueSize = maxQueueSize; } // Overwrite config file if max receive queue size exists in connection string. if (m_configData.ContainsKey("maxReceiveQueueSize") && int.TryParse(m_configData["maxReceiveQueueSize"], out maxQueueSize)) { m_maxReceiveQueueSize = maxQueueSize; } // Create ZeroMQ Router socket - closest match to IServer implementation m_zeroMQServer = new ZSocket(ZContext.Create(), ZSocketType.ROUTER); m_zeroMQServer.Identity = ServerID.ToByteArray(); m_zeroMQServer.SendHighWatermark = m_maxSendQueueSize; m_zeroMQServer.ReceiveHighWatermark = m_maxReceiveQueueSize; m_zeroMQServer.Immediate = true; m_zeroMQServer.SetOption(ZSocketOption.LINGER, 0); m_zeroMQServer.SetOption(ZSocketOption.SNDTIMEO, 1000); m_zeroMQServer.SetOption(ZSocketOption.RCVTIMEO, -1); m_zeroMQServer.SetOption(ZSocketOption.RECONNECT_IVL, -1); m_zeroMQServer.IPv6 = (Transport.GetDefaultIPStack() == IPStack.IPv6); m_zeroMQServer.Bind(m_configData["server"]); // Notify that the server has been started successfully. OnServerStarted(); m_receiveDataThread = new Thread(ReceiveDataHandler); m_receiveDataThread.IsBackground = true; m_receiveDataThread.Start(); } }
private void OpenSocket(object state) { int connectionAttempts = 0; while (MaxConnectionAttempts == -1 || connectionAttempts < MaxConnectionAttempts) { if ((object)m_zeroMQClient.Provider != null) { // Disconnect any existing ZeroMQ socket try { m_zeroMQClient.Provider.Disconnect(ServerUri); } catch (Exception ex) { OnDisconnectException(ex); } m_zeroMQClient.Reset(); } try { OnConnectionAttempt(); // Create ZeroMQ Dealer socket - closest match to IClient implementation m_zeroMQClient.Provider = new ZSocket(ZContext.Create(), ZSocketType.DEALER); m_zeroMQClient.Provider.Identity = m_zeroMQClient.ID.ToByteArray(); m_zeroMQClient.Provider.SendHighWatermark = m_maxSendQueueSize; m_zeroMQClient.Provider.ReceiveHighWatermark = m_maxReceiveQueueSize; m_zeroMQClient.Provider.Immediate = true; m_zeroMQClient.Provider.SetOption(ZSocketOption.LINGER, 0); m_zeroMQClient.Provider.SetOption(ZSocketOption.SNDTIMEO, 1000); m_zeroMQClient.Provider.SetOption(ZSocketOption.RCVTIMEO, -1); m_zeroMQClient.Provider.SetOption(ZSocketOption.RECONNECT_IVL, -1); m_zeroMQClient.Provider.IPv6 = (Transport.GetDefaultIPStack() == IPStack.IPv6); m_zeroMQClient.Provider.Connect(ServerUri); m_connectionHandle.Set(); connectionAttempts = 0; OnConnectionEstablished(); } catch (Exception ex) { m_zeroMQClient.Provider = null; // Log exception during connection attempt OnConnectionException(ex); // Keep retrying connection Thread.Sleep(1000); connectionAttempts++; continue; } try { // Start data reception loop ReceiveDataHandler(); } catch (Exception ex) { // Notify of the exception. OnReceiveDataException(ex); } // If client is no longer connected, exit loop, else sleep for a moment before attempting reconnect if (Enabled) { Thread.Sleep(1000); } else { break; } } }