Exemplo n.º 1
0
		public void TestMultipleSendAndReceive(MemoryStream[] messages)
		{
			Dictionary<int, MemoryStream> streams = new Dictionary<int, MemoryStream> ();
			foreach (var m in messages) {
				if (streams.ContainsKey ((int)m.Length))
					throw new Exception ("Incorrect test argumenrs. Streams must have an unique size");
				streams.Add ((int)m.Length, m);
			}

			var sender = new QuantumSender ();
			var receiver = new QuantumReceiver ();

			List<MemoryStream> received = new List<MemoryStream> ();
			receiver.OnLightMessage += (QuantumReceiver arg1, QuantumHead arg2, MemoryStream arg3) => {
				received.Add(arg3);
			};

			receiver.OnCollectingError += (QuantumReceiver arg1, QuantumHead arg2, byte[] arg3) => {
				throw new Exception("Error during collecting");
			};


			int MaxQuantumSize = 500;
			byte[] Quantum;
			int msgId;

			MemoryStream TransportStream = new MemoryStream ();

			Stopwatch tmr = new Stopwatch ();
			tmr.Start ();

			foreach (var m in messages) {
				m.Position = 0;
				sender.Set (m);
				if(sender.TryNext (MaxQuantumSize, out Quantum, out msgId)) {
					if (Quantum == null || Quantum.Length == 0)
						throw new Exception ("Wrong quant size");
					TransportStream.Write(Quantum,0,Quantum.Length);
				}
			}

			while (sender.TryNext (MaxQuantumSize, out Quantum, out msgId)) {
				if (Quantum == null || Quantum.Length == 0)
					throw new Exception ("Wrong quant size");
				TransportStream.Write(Quantum,0,Quantum.Length);
			}
			//Immitate Stream-Style Delivery;
			TransportStream.Position = 0;
			while (TransportStream.Position < TransportStream.Length) {
				var size = Tools.rnd.Next () % (MaxQuantumSize * 5);
				size = (int)Math.Min (TransportStream.Length - TransportStream.Position, size);
				byte[] msg = new byte[size];
				TransportStream.Read (msg, 0, size);
				receiver.Set (msg);
			}
			tmr.Stop ();
			//Check Received:
			if (received == null)
				throw new Exception ("no receiving");

			if (received.Count != streams.Count)
				throw new Exception ("Not all messages Received");

			int totalBytesCount=0;
			foreach (var r in received) {
				totalBytesCount += (int)r.Length;

				if (!streams.ContainsKey ((int)r.Length))
					throw new Exception ("incorrect length received");
				var snd = streams [(int)r.Length];
				if (!Tools.CompareStreams (snd, r))
					throw new Exception ("Sended and received messages are not equal");
				streams.Remove ((int)r.Length);
			}
			Console.WriteLine ("Send receive operation for " + totalBytesCount + " was done in " + tmr.ElapsedMilliseconds);
		}
Exemplo n.º 2
0
		public void TestSingleSendAndReceive(MemoryStream lightMessage)
		{
			var sender = new QuantumSender ();
			var receiver = new QuantumReceiver ();

			MemoryStream received = null;
			receiver.OnLightMessage += (QuantumReceiver arg1, QuantumHead arg2, MemoryStream arg3) => {
				received = arg3;
			};
			receiver.OnCollectingError += (QuantumReceiver arg1, QuantumHead arg2, byte[] arg3) => {
				throw new Exception("Error during collecting");
			};

			Stopwatch tmr = new Stopwatch ();
			tmr.Start ();

			sender.Set (lightMessage);

			int MaxQuantumSize = 500;
			byte[] Quantum;
			int msgId;

			while (sender.TryNext (MaxQuantumSize, out Quantum, out msgId)) {
				if (Quantum == null || Quantum.Length == 0)
					throw new Exception ("Wrong quant size");
				receiver.Set (Quantum);
			}
			if (received == null)
				throw new Exception ("no receiving");

			received.Position = 0;
			lightMessage.Position = 0;

			tmr.Stop ();
			while (lightMessage.Position < lightMessage.Length) {
				var Bsended = lightMessage.ReadByte ();
				var Breceived = received.ReadByte ();
				if (Bsended != Breceived)
					throw new Exception ("received and sended values are not equal");
			}
			Console.WriteLine ("Full Mono SR["+lightMessage.Length +"] done in " + tmr.ElapsedMilliseconds);
		}