Ejemplo n.º 1
0
		public HeartBeatValues CombineWith(HeartBeatValues other)
		{
			int outgoing;
			int incoming;

			if (Outgoing == 0 || other.Incoming == 0)
			{
				outgoing = 0;
			}
			else
			{
				outgoing = Math.Max(Outgoing, other.Incoming);
			}

			if (Incoming == 0 || other.Outgoing == 0)
			{
				incoming = 0;
			}
			else
			{
				incoming = Math.Max(Incoming, other.Outgoing);
			}

			return new HeartBeatValues(outgoing, incoming);
		}
		public void Combine_zero_incoming()
		{
			var hb1 = new HeartBeatValues("10,0");
			var hb2 = new HeartBeatValues("10,5");

			var hb3 = hb1.CombineWith(hb2);
			Assert.AreEqual(10, hb3.Outgoing);
			Assert.AreEqual(0, hb3.Incoming);
		}
		public void Parse_from_text()
		{
			var hb = new HeartBeatValues("30,20");
			Assert.AreEqual(30, hb.Outgoing);
			Assert.AreEqual(20, hb.Incoming);

			hb = new HeartBeatValues(null);
			Assert.AreEqual(0, hb.Outgoing);
			Assert.AreEqual(0, hb.Incoming);

			hb = new HeartBeatValues("   ");
			Assert.AreEqual(0, hb.Outgoing);
			Assert.AreEqual(0, hb.Incoming);

			hb = new HeartBeatValues("11");
			Assert.AreEqual(11, hb.Outgoing);
			Assert.AreEqual(0, hb.Incoming);

			hb = new HeartBeatValues(",11");
			Assert.AreEqual(0, hb.Outgoing);
			Assert.AreEqual(11, hb.Incoming);
		}
		public void To_string()
		{
			var hb = new HeartBeatValues("30,20");
			Assert.AreEqual("30,20", hb.ToString());
		}
Ejemplo n.º 5
0
		private void HandleConnected(StompFrame frame)
		{
			AssignSessionAndResubscribe(frame);
			StopConnectTimer();
			_connected = true;
			_waitingForConnectedFrame = false;
			Log.DebugFormat("Received {0} response, {1}={2}", frame.Command, StompHeader.Session, _sessionId);

			var serverHeartBeatValues = new HeartBeatValues(frame.Headers[StompHeader.HeartBeat]);
			_negotiatedHeartBeatValues = _sentHeartBeatValues.CombineWith(serverHeartBeatValues);

			StartIncomingHeartBeatTimer();
			StartOutgoingHeartBeatTimer();
			SendNextMessage();
		}
Ejemplo n.º 6
0
		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");
						}
					}
				}
			}
		}
		// 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");
		}