/// <summary> /// Adds a callback to run each time the <see cref="IAdvancedBus"/> is connected to the RabbitMQ server. /// /// The callback is wrapped into a closure that makes sure that the callback is run only once. /// </summary> public RabbitMqServiceBuilder <T> OnConnectedOnce(OnConnectedCallback callback) { OnConnectedCallback CallbackOnceFactory() { var done = false; var @lock = new object(); return((bus, rabbitMqConfig, cancellationToken, logger) => { if (done) { return; } lock (@lock) { if (done) { return; } done = true; callback(bus, rabbitMqConfig, cancellationToken, logger); } }); } _configOnConnected.Add(CallbackOnceFactory()); return(this); }
private OnConnectedCallback HandleCallbackError(OnConnectedCallback callback) => (bus, rabbitMqConfig, cancellationToken, logger) => { try { callback(bus, rabbitMqConfig, cancellationToken, logger); } catch (Exception exception) { _logger?.LogCritical($"Failed to run callback on connection:\n{exception}", exception); } };
private void Connect(OnConnectedCallback callback) { onConnectCallback = callback; if (socket != null && socket.Connected) { return; } socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); connectArgs = new SocketAsyncEventArgs(); recvArgs = new SocketAsyncEventArgs(); sendArgs = new SocketAsyncEventArgs(); connectArgs.RemoteEndPoint = new DnsEndPoint(_ip, _port); connectArgs.Completed += new EventHandler <SocketAsyncEventArgs>(socketArgs_Completed); sendArgs.Completed += new EventHandler <SocketAsyncEventArgs>(sendArgs_Completed); recvArgs.Completed += new EventHandler <SocketAsyncEventArgs>(recvArgs_Completed); socket.ConnectAsync(connectArgs); }
public void Connect(string ip, int port, OnConnectedCallback connectedCallback = null, OnReceivedCallback receivedCallback = null) { mIPAddress = ip; m_Port = port; if (connectedCallback != null) { mOnConnectedCallback = connectedCallback; } if (receivedCallback != null) { mOnReceivedCallback = receivedCallback; } _Connect(); }
/// <summary> /// 异步连接 /// </summary> private async Task ConnectAsync() { while (true) { try { // 连接 await mqttClient.ConnectAsync(options, cancellationToken.Token); using (new MethodUtils.Unlocker(this)) { OnConnectedCallback?.Invoke(); } return; } catch (Exception e) { Tracker.LogNW(TAG, "connect fail"); using (new MethodUtils.Unlocker(this)) { OnExceptionCallback?.Invoke(e); } await Task.Delay(TimeSpan.FromMilliseconds(RETRY_DURATION)); } } }
private void OnConnected() { using (new MethodUtils.Unlocker(this)) { OnConnectedCallback?.Invoke(); } }
/// <summary> /// Adds a callback to run each time the <see cref="IAdvancedBus"/> is connected to the RabbitMQ server. /// </summary> public RabbitMqServiceBuilder <T> OnConnected(OnConnectedCallback callback) { _configOnConnected.Add(callback); return(this); }