Hie.Core.Endpoints.TcpReceiveEndpoint.ProcessIncomingStream C# (CSharp) Méthode

ProcessIncomingStream() private méthode

private ProcessIncomingStream ( int bytesRead, StateObject state ) : bool
bytesRead int
state StateObject
Résultat bool
		internal bool ProcessIncomingStream(int bytesRead, StateObject state)
		{
			bool endOfTransmission = false;

			for (int i = 0; (i < bytesRead && !endOfTransmission); i++)
			{
				state.Stream.WriteByte(state.Buffer[i]);

				switch (state.State)
				{
					case StateObject.FrameState.FindSoh:
					{
						if (_options.SohDelimiters.Length > 0)
						{
							if (state.Stream.Position < _options.SohDelimiters.Length)
							{
								continue;
							}

							bool foundDelimiters = CheckDelimiter(_options.SohDelimiters, state.Stream);

							if (foundDelimiters)
							{
								state.Stream.Position = 0;
								state.State = StateObject.FrameState.FindStx;
							}

							continue;
						}
						else
						{
							state.State = StateObject.FrameState.FindStx;
							goto case StateObject.FrameState.FindStx;
						}
					}
					case StateObject.FrameState.FindStx:
					{
						if (_options.EotDelimiters.Length == 0)
						{
							if (state.Stream.Position < _options.StxDelimiters.Length)
							{
								continue;
							}
						}
						else
						{
							if (state.Stream.Position < _options.StxDelimiters.Length && state.Stream.Position < _options.EotDelimiters.Length)
							{
								continue;
							}
						}

						bool foundEotDelimiters = CheckDelimiter(_options.EotDelimiters, state.Stream);
						bool foundStxDelimiters = CheckDelimiter(_options.StxDelimiters, state.Stream);

						if (foundEotDelimiters && foundStxDelimiters)
						{
							// Do nothing for now, but this is a really stupid case that "could" potentially happen
						}

						if (foundEotDelimiters && !foundStxDelimiters)
						{
							// Done, exit gracefuylly
							endOfTransmission = true;
							break; // consider return here
						}

						if (foundStxDelimiters && !foundEotDelimiters)
						{
							// Start search for ETX
							state.Stream.Position = 0;
							state.State = StateObject.FrameState.FindEtx;
						}

						continue;
					}
					case StateObject.FrameState.FindEtx:
					{
						if (state.Stream.Position < _options.EtxDelimiters.Length)
						{
							continue;
						}

						bool foundEtxDelimiters = CheckDelimiter(_options.EtxDelimiters, state.Stream);
						if (foundEtxDelimiters)
						{
							long pos = state.Stream.Position;
							byte[] data = new byte[(pos - _options.EtxDelimiters.Length)];
							state.Stream.Position = 0;
							state.Stream.Read(data, 0, (int) (pos - _options.EtxDelimiters.Length));

							SubmitPayloadToPipeline(data);

							// Done, change state
							state.Stream.Position = 0;
							state.State = StateObject.FrameState.FindStx;
						}

						continue;
					}
				}
			}

			return endOfTransmission;
		}

Usage Example

Exemple #1
0
        public void TcpReceiveEndpointMultiByteDelimitersTest()
        {
            var options = new TcpReceieveOptions();

            options.SohDelimiters = new byte[] { TcpReceieveOptions.SOH, 0x06 };
            options.StxDelimiters = new byte[] { TcpReceieveOptions.STX, 0x06 };
            options.EtxDelimiters = new byte[] { TcpReceieveOptions.ETX, 0x06 };
            options.EotDelimiters = new byte[] { TcpReceieveOptions.EOT, 0x07 };
            //BUG: If the end-delimiter of STX and EOT match it will not detect EOT
            //options.EOTDelimiters = new byte[] {TcpReceiveEndpoint.EOT, 0x06};

            TcpReceiveEndpoint endpoint = new TcpReceiveEndpoint(new IPEndPoint(IPAddress.Any, 6789), options);
            var host = new Mock <IApplicationHost>();

            endpoint.Initialize(host.Object, options);

            StateObject state = new StateObject(null);

            List <byte> data = new List <byte>();

            data.AddRange(options.SohDelimiters);
            data.AddRange(options.StxDelimiters);
            data.AddRange(new byte[] { 0x41, 0x41, 0x41, 0x41 });
            data.AddRange(options.EtxDelimiters);
            data.AddRange(options.EotDelimiters);
            data.CopyTo(state.Buffer, 0);

            bool isEot = endpoint.ProcessIncomingStream(state.Buffer.Length, state);

            Assert.IsTrue(isEot);
            host.Verify(app => app.ProcessInPipeline(It.IsAny <TcpReceiveEndpoint>(), It.IsNotNull <byte[]>()), Times.Once);
            host.Verify(app => app.ProcessInPipeline(It.IsAny <TcpReceiveEndpoint>(), It.Is <byte[]>(indata => indata.SequenceEqual(new byte[] { 0x41, 0x41, 0x41, 0x41 }))));
        }
All Usage Examples Of Hie.Core.Endpoints.TcpReceiveEndpoint::ProcessIncomingStream