private void ConnectCB(IAsyncResult rez) { var _tcp = rez.AsyncState as TcpClient; try { _tcp.EndConnect(rez); _stream = new MqStreamer(_tcp, Received, SendIdle); var id = string.Format("{0}_{1:X4}", Environment.MachineName, System.Diagnostics.Process.GetCurrentProcess().Id); var ConnInfo = new MqConnect(); ConnInfo.keepAlive = (ushort)(KEEP_ALIVE / 900); ConnInfo.cleanSession = true; ConnInfo.clientId = id; if (_uName != null) { ConnInfo.userName = _uName; if (_uPass != null) { ConnInfo.userPassword = _uPass; } } this.Send(ConnInfo); _tOut.Change(3000, KEEP_ALIVE); // better often than never } catch (Exception ex) { var se = ex as SocketException; if (se != null && (se.SocketErrorCode == SocketError.ConnectionRefused || se.SocketErrorCode == SocketError.TryAgain || se.SocketErrorCode == SocketError.TimedOut)) { status = Status.Disconnected; _tOut.Change((new Random()).Next(KEEP_ALIVE * 3, KEEP_ALIVE * 6), Timeout.Infinite); } else { status = Status.NotAccepted; _tOut.Change(Timeout.Infinite, Timeout.Infinite); } Log.Warning("{0} Connection FAILED - {1}", this.Signature, ex.Message); } }
public void Stop() { if(_stream!=null) { if(_connected) { _connected=false; _owner.Unsubscribe("/#", OwnerChanged); _owner.Remove(); _tOut.Change(Timeout.Infinite, Timeout.Infinite); if(StatusChg!=null) { StatusChg(_connected); } _stream.Close(); _stream=null; Log.Info("{0} Disconnected", BrokerName); } } }
private void ConnectCB(IAsyncResult rez) { var _tcp=rez.AsyncState as TcpClient; try { _tcp.EndConnect(rez); _stream=new MqStreamer(_tcp, Received, SendIdle); _stream.isSndPaused=true; var re=((IPEndPoint)_stream.Socket.Client.RemoteEndPoint); try { BrokerName=Dns.GetHostEntry(re.Address).HostName; } catch(SocketException) { BrokerName=re.Address.ToString(); } _owner=_mq.Get<MqClient>(BrokerName); _owner.value=this; _connected=false; string id=Topic.root.Get<string>("/local/cfg/id").value; if(string.IsNullOrEmpty(id)) { id=string.Format("{0}@{1}_{2:X4}", Environment.UserName, Environment.MachineName, System.Diagnostics.Process.GetCurrentProcess().Id); } ConnInfo.clientId=id; ConnInfo.userName=_settings.Get<string>("_username"); _settings.Get<string>("_username").saved=true; ConnInfo.userPassword=_settings.Get<string>("_password"); _settings.Get<string>("_password").saved=true; if(string.IsNullOrEmpty(ConnInfo.userName) && addr=="localhost") { ConnInfo.userName="******"; ConnInfo.userPassword=string.Empty; } this.Send(ConnInfo); _owner.Subscribe("/#", OwnerChanged); _tOut.Change(3000, _keepAliveMS); // more often than not } catch(Exception ex) { Log.Error("Connect to {0}:{1} failed, {2}", addr, port, ex.Message); if(StatusChg!=null) { StatusChg(false); } _tOut.Change(_keepAliveMS*5, Timeout.Infinite); } }
public bool Reconnect(bool slow=false) { if(_stream!=null) { if(_connected) { _connected=false; if(StatusChg!=null) { StatusChg(_connected); } _tOut.Change(_keepAliveMS*2, Timeout.Infinite); } else { _tOut.Change(_keepAliveMS*(slow?10:5), Timeout.Infinite); } _stream.Close(); _stream=null; return false; } if(slow) { _tOut.Change(_keepAliveMS*5, Timeout.Infinite); return false; } string connectionstring=_settings.Get<string>("_URL").value; _settings.Get<string>("_URL").saved=true; _settings.Get<string>("_username").saved=true; _settings.Get<string>("_password").saved=true; if(string.IsNullOrEmpty(connectionstring)) { return false; } if(connectionstring=="#local") { connectionstring="localhost"; } if(connectionstring.IndexOf(':')>0) { addr=connectionstring.Substring(0, connectionstring.IndexOf(':')); port=int.Parse(connectionstring.Substring(connectionstring.IndexOf(':')+1)); } else { addr=connectionstring; port=1883; } TcpClient _tcp=new TcpClient(); _tcp.SendTimeout=900; _tcp.ReceiveTimeout=0; _tcp.BeginConnect(addr, port, new AsyncCallback(ConnectCB), _tcp); return true; }