private static void OnConnect(bool succ) { if (succ == true) { if (Communicate.IsConnectedGS() == true) { IGGDebug.Log("连接游戏服务器"); LoginDC.SendGSMsgCL2GS(); } else { IGGDebug.Log("连接登录服务器"); LoginDC.SendLoginMsgCL2LS(); } } else { IGGDebug.Log("连接服务器失败"); if (g_failfp != null) { g_failfp(); g_failfp = null; } } }
private static void OnConnectOverTime() { IGGDebug.Log("连接超时"); if (g_failfp != null) { g_failfp(); g_failfp = null; } }
private void Mqconn_ConnectFail(object sender, string e) { var conn = (IRabbitMQPersistentConnection)sender; ConnectFail?.Invoke(conn, e); if (ConnectFail == null) { throw new Exception("the rabbitmq connection named " + conn.Name + " failes"); } }
// 只被初始化一次 public static void Init(ConnectFail fp) { g_failfp = fp; if (g_HaveInit == false) { NetConnectState.eventDisconnect += OnDisConnect; NetConnectState.eventConnect += OnConnect; RegisterHooks(); g_HaveInit = true; } }
public bool TryConnect() { lock (sync_root) { if (IsConnected) { return(true); } var policy = Policy.Handle <SocketException>() .Or <BrokerUnreachableException>() .WaitAndRetry(_retryCount, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)), (ex, time) => { _logger.LogWarning("TryConnect Failed ," + ex.Message); } ); policy.Execute(() => { _connection = _connectionFactory.CreateConnection(); }); if (IsConnected) { _connection.ConnectionShutdown += OnConnectionShutdown; _connection.CallbackException += OnCallbackException; _connection.ConnectionBlocked += OnConnectionBlocked; _logger.LogInformation($"RabbitMQ persistent connection acquired a connection {_connection.LocalPort.ToString()} and is subscribed to failure events"); return(true); } else { _logger.LogWarning("FATAL ERROR: RabbitMQ connections could not be created and opened"); ConnectFail?.Invoke(this, "After several attempts RabbitMQ connections could not be created and opened"); return(false); } } }
/// <summary> /// BindQueue /// </summary> /// <param name="hostOption"></param> /// <param name="exchangeOption"></param> /// <param name="queueOption"></param> protected void BindQueue(HostOption hostOption, ExchangeOption exchangeOption, QueueOption queueOption) { mHostOption = hostOption; mExchangeOption = exchangeOption; mQueueOption = queueOption; mQueueName = queueOption.QueueName; mIsAutoAck = queueOption.AutoAck; mLastConsumerTag = queueOption.ConsumerTag; if (string.IsNullOrWhiteSpace(mLastConsumerTag)) { mLastConsumerTag = string.Empty; } try { var factory = new ConnectionFactory() { ClientProvidedName = hostOption.ClientName, HostName = hostOption.Host, Port = hostOption.Port, VirtualHost = hostOption.VirtualHost, UserName = hostOption.UserName, Password = hostOption.Password, RequestedConnectionTimeout = TimeSpan.FromSeconds(hostOption.ConnectionTimeout), RequestedHeartbeat = TimeSpan.FromSeconds(hostOption.HeartBeat) }; mConnection = factory.CreateConnection(); mChannel = mConnection.CreateModel(); mChannel.ExchangeDeclare(exchangeOption.ExchangeName, exchangeOption.ExchangeType); mChannel.QueueDeclare(queueOption.QueueName, queueOption.IsDurable, queueOption.IsExclusive, queueOption.IsAutoDeleted); mChannel.QueueBind(queueOption.QueueName, exchangeOption.ExchangeName, exchangeOption.RoutingKey); mChannel.BasicQos(0, queueOption.Qos, false); } catch (BrokerUnreachableException bex) { ConnectFail?.Invoke(this, new ConnectionFailEventArgs { Message = bex.Message }); } catch (ConnectFailureException cex) { ConnectFail?.Invoke(this, new ConnectionFailEventArgs { Message = cex.Message }); } catch (SocketException sex) { ConnectFail?.Invoke(this, new ConnectionFailEventArgs { Message = sex.Message }); } catch (Exception ex) { UnknowError?.Invoke(this, new UnknownExceptionEventArgs { Message = ex.Message }); } }
public static void InitFailNetLink(ConnectFail pfun) { g_failLiknfp = pfun; }