private void CheckConnected() { using (Lock.Lock()) { if (!_isDisposed) { if (_connected) { if (_transport == null || !_transport.Connected) { _connected = false; Log.Debug("Client is now disconnected from server"); Lock.AfterUnlock(() => OnConnectedChanged(EventArgs.Empty)); } StopOutgoingHeartBeatTimer(); StopIncomingHeartBeatTimer(); StopConnectTimer(); } else { if (_transport != null && _transport.Connected && !_waitingForConnectedFrame) { var login = Login ?? string.Empty; var passcode = Passcode ?? string.Empty; // the client id provides some help with diagnostics by identifying the client process var processName = Path.GetFileNameWithoutExtension(Process.GetCurrentProcess().MainModule.FileName); var clientId = Environment.MachineName + "/" + processName + "/" + Process.GetCurrentProcess().Id; _sentHeartBeatValues = new HeartBeatValues(OutgoingHeartBeat, IncomingHeartBeat); // time to send a CONNECT message var frame = new StompFrame { Command = StompCommand.Connect, Headers = { {StompHeader.Login, login}, {StompHeader.Passcode, passcode}, {StompHeader.HeartBeat, _sentHeartBeatValues.ToString()}, {StompHeader.NonStandard.ClientId, clientId} } }; // a little enhancement, if we are re-connecting a previous session, // include the session id so that we can resume if (_sessionId != null) { frame.Headers[StompHeader.Session] = _sessionId; } _waitingForConnectedFrame = true; StartConnectTimer(); _transport.SendFrame(frame); Log.Debug("Sent " + frame.Command + " command to server"); } } } } }
public void To_string() { var hb = new HeartBeatValues("30,20"); Assert.AreEqual("30,20", hb.ToString()); }
// ReSharper restore UnusedParameter.Local // ReSharper restore MemberCanBeMadeStatic.Local private void ExpectingConnect(StompFrame frame) { if (frame.Command != StompCommand.Connect && frame.Command != StompCommand.Stomp) { string message = "Expecting " + StompCommand.Connected + " or " + StompCommand.Stomp + " command, received " + frame.Command; Log.Error(_logMessagePrefix + message); var errorFrame = StompFrameUtils.CreateErrorFrame(message); _transport.SendFrame(errorFrame); DisconnectWithoutLocking(); return; } var login = frame.Headers[StompHeader.Login]; var passcode = frame.Headers[StompHeader.Passcode]; if (!Authenticate(login, passcode)) { string message = "Received " + frame.Command + " frame, Access denied"; Log.Warn(_logMessagePrefix + message); var errorFrame = StompFrameUtils.CreateErrorFrame(message); _transport.SendFrame(errorFrame); DisconnectWithoutLocking(); return; } Log.Debug(_logMessagePrefix + "Received " + frame.Command + " frame, authenticated OK"); var sessionId = frame.Headers[StompHeader.Session]; ServerSideSession session = null; if (sessionId != null) { session = _serverData.FindSession(sessionId); if (session == null) { Log.Warn(_logMessagePrefix + "Received " + frame.Command + " frame for non-existent session: " + sessionId); var message = ErrorMessages.SessionDoesNotExistPrefix + sessionId; var errorFrame = StompFrameUtils.CreateErrorFrame(message); _transport.SendFrame(errorFrame); DisconnectWithoutLocking(); return; } if (!session.AddConnection(this)) { var message = frame.Command + " frame requested a session already in use: " + sessionId; Log.Warn(_logMessagePrefix + message); var errorFrame = StompFrameUtils.CreateErrorFrame(message); _transport.SendFrame(errorFrame); DisconnectWithoutLocking(); return; } Log.Debug(_logMessagePrefix + "Reconnected to session " + sessionId); } if (session == null) { session = _serverData.CreateSession(); session.AddConnection(this); Log.Debug(_logMessagePrefix + "Created new session " + session.SessionId); } // helps with debugging if we give the session a more friendly name session.ClientId = frame.Headers[StompHeader.NonStandard.ClientId]; _session = session; var connectedFrame = new StompFrame { Command = StompCommand.Connected, Headers = { {StompHeader.Session, session.SessionId} } }; if (frame.Headers[StompHeader.HeartBeat] == null) { // no heart-beat header received, so we default to 0,0 _negotiatedHeartBeats = new HeartBeatValues(0, 0); } else { var otherHeartBeatValues = new HeartBeatValues(frame.Headers[StompHeader.HeartBeat]); var myHeartBeatValues = new HeartBeatValues(30000, 30000); _negotiatedHeartBeats = myHeartBeatValues.CombineWith(otherHeartBeatValues); connectedFrame.Headers[StompHeader.HeartBeat] = _negotiatedHeartBeats.ToString(); } _transport.SendFrame(connectedFrame); StopConnectTimer(); StartIncomingHeartBeatTimer(); StartOutgoingHeartBeatTimer(); _stateAction = Connected; Log.Debug(_logMessagePrefix + "Session " + session + " connected"); }