internal SocketConnection(Socket socket, PipeFactory factory) { socket.NoDelay = true; _socket = socket; if (factory == null) { _ownsFactory = true; factory = new PipeFactory(); } _factory = factory; _input = PipeFactory.Create(); _output = PipeFactory.Create(); _receiveTask = ReceiveFromSocketAndPushToWriterAsync(); _sendTask = ReadFromReaderAndWriteToSocketAsync(); }
static void MemoryPipeTest() { using (var pfac = new PipeFactory()) { // pipeline endpoint // if you want only to output Stream, call AsPipelineWriter(this Stream) extension method or CopyToAsync(Stream) var epwriter = pfac.CreateWriter(null, async(r, w) => { while (true) { try { var result = await r.ReadAsync(); if (result.IsCancelled || (result.IsCompleted && result.Buffer.IsEmpty)) { break; } foreach (var rbuf in result.Buffer) { for (int i = 0; i < rbuf.Length; i++) { Console.Write($"{rbuf.Span[i].ToString("x2")}"); if ((i & 0xf) == 0xf) { Console.WriteLine(); } else { Console.Write(":"); } } } r.Advance(result.Buffer.End); } catch (Exception e) { Console.WriteLine($"{e}"); } } }); var writer = pfac.Filter0xf0(epwriter); var reader = pfac.CreateFromEnumerable(Enumerable.Range(0, 10000)); reader.CopyToAsync(writer).Wait(); } }
public Pipe <TOut> Do <TFilter, TOut>(Action <TFilter> initAction) where TFilter : IFilter <T, TOut> { Pipe <TOut> result; if (Obj.Success) { var processor = PipeFactory.GetProcessor <TFilter>(); initAction(processor); var processingResult = processor.Process(Obj.Value); result = new Pipe <TOut>(PipeFactory).Use(processingResult); } else { result = new Pipe <TOut>(PipeFactory).Use(Result.Fail <TOut>(Obj.Error)); } return(result); }
public async Task MultipleCompleteReaderWriterCauseDisposeOnlyOnce() { var pool = new DisposeTrackingBufferPool(); using (var factory = new PipeFactory(pool)) { var readerWriter = factory.Create(); await readerWriter.Writer.WriteAsync(new byte[] { 1 }); readerWriter.Writer.Complete(); readerWriter.Reader.Complete(); Assert.Equal(1, pool.Disposed); readerWriter.Writer.Complete(); readerWriter.Reader.Complete(); Assert.Equal(1, pool.Disposed); } }
void Awake() { if (instance == null) { instance = this; } pipeState = PipeState.OFF; pipeObj.SetActive(false); pipePool = new ArrayList(); pipeList = new ArrayList(); for (int i = 0; i < poolSize; ++i) { PipeMsg pipMsg; pipMsg.pipe = Instantiate(pipeObj, pipeObj.transform.position, Quaternion.identity, transform); pipMsg.isPassed = false; pipePool.Add(pipMsg); } }
public async Task FlushCallbackRunsOnWriterScheduler() { using (var factory = new PipeFactory()) { using (var scheduler = new ThreadScheduler()) { var pipe = factory.Create(new PipeOptions { MaximumSizeLow = 32, MaximumSizeHigh = 64, WriterScheduler = scheduler }); var writableBuffer = pipe.Writer.Alloc(64); writableBuffer.Advance(64); var flushAsync = writableBuffer.FlushAsync(); Assert.False(flushAsync.IsCompleted); Func <Task> doWrite = async() => { var oid = Thread.CurrentThread.ManagedThreadId; await flushAsync; Assert.NotEqual(oid, Thread.CurrentThread.ManagedThreadId); pipe.Writer.Complete(); Assert.Equal(Thread.CurrentThread.ManagedThreadId, scheduler.Thread.ManagedThreadId); }; var writing = doWrite(); var result = await pipe.Reader.ReadAsync(); pipe.Reader.Advance(result.Buffer.End, result.Buffer.End); pipe.Reader.Complete(); await writing; } } }
public LibuvThread(LibuvTransport transport) { _transport = transport; _appLifetime = transport.AppLifetime; _log = transport.Log; _loop = new UvLoopHandle(_log); _post = new UvAsyncHandle(_log); _thread = new Thread(ThreadStart); _thread.Name = nameof(LibuvThread); #if !DEBUG // Mark the thread as being as unimportant to keeping the process alive. // Don't do this for debug builds, so we know if the thread isn't terminating. _thread.IsBackground = true; #endif QueueCloseHandle = PostCloseHandle; QueueCloseAsyncHandle = EnqueueCloseHandle; PipeFactory = new PipeFactory(); WriteReqPool = new WriteReqPool(this, _log); }
public async Task GetUInt64GivesExpectedValues() { using (var factory = new PipeFactory()) { var readerWriter = factory.Create(); var writeBuffer = readerWriter.Writer.Alloc(); writeBuffer.Ensure(50); writeBuffer.Advance(50); // not even going to pretend to write data here - we're going to cheat await writeBuffer.FlushAsync(); // by overwriting the buffer in-situ // now read it back var result = await readerWriter.Reader.ReadAsync(); var readBuffer = result.Buffer; ReadUInt64GivesExpectedValues(ref readBuffer); } }
public ITestState Start() { _threadPoolCounter = new ThreadPoolCounter(); PipeFactory <IResult> pipeFactory = new PipeFactory <IResult>(); _aggregator = new PipelineDataAggregator(_settings.Aggregators, pipeFactory); _aggregator.Start(); _globalCounters = GlobalCounters.CreateDefault(); _errorHandler = new ErrorHandler(); _limit = new LimitsHandler(_settings.Limits); _threading = _settings.Threading; _state = new TestState(_timer, _globalCounters, _threadPoolCounter); _speed = PriorityStrategyFactory.Create(_settings.Speeds, _timer); _speed.Setup(_state); IIterationContextFactory iterationContextFactory = CreateIterationContextFactory(); IScenarioHandlerFactory scenarioHandlerFactory = CreateScenarioHandlerFactory(); ISchedulerFactory schedulerFactory = CreateSchedulerFactory(); IDataCollectorFactory dataCollectorFactory = CreateDataCollectorFactory(pipeFactory, _threadPoolCounter); IScenarioThreadFactory scenarioThreadFactory = CreateScenarioThreadFactory(); IThreadFactory threadFactory = new ScenarioThreadFactory( iterationContextFactory, scenarioHandlerFactory, schedulerFactory, dataCollectorFactory, scenarioThreadFactory ); _pool = new ThreadPool(threadFactory, _threadPoolCounter); InitialThreadingSetup(); _timer.Start(); // This line also releases Worker-Threads from wait in IPrewait return(_state); }
internal SocketConnection(Socket socket, PipeFactory pipeFactory, ISocketsTrace trace) { Debug.Assert(socket != null); Debug.Assert(pipeFactory != null); Debug.Assert(trace != null); _socket = socket; PipeFactory = pipeFactory; _trace = trace; var localEndPoint = (IPEndPoint)_socket.LocalEndPoint; var remoteEndPoint = (IPEndPoint)_socket.RemoteEndPoint; LocalAddress = localEndPoint.Address; LocalPort = localEndPoint.Port; RemoteAddress = remoteEndPoint.Address; RemotePort = remoteEndPoint.Port; }
public void Setup() { PipeFactory = new PipeFactory(); Pipe = PipeFactory.Create(); var serviceContext = new ServiceContext { HttpParserFactory = f => new HttpParser <Http1ParsingHandler>(), ServerOptions = new KestrelServerOptions(), }; var http1ConnectionContext = new Http1ConnectionContext { ServiceContext = serviceContext, ConnectionFeatures = new FeatureCollection(), PipeFactory = PipeFactory, TimeoutControl = new MockTimeoutControl() }; Http1Connection = new Http1Connection <object>(application: null, context: http1ConnectionContext); }
public void TestStringPipe() { var config = TestUtility.GetConfig("Pipes", "TestPipe"); var context = new PluginContext(config, null, null); var source = new MockEventSource <string>(context); var sink = new MockEventSink(context); context.ContextData[PluginContext.SOURCE_TYPE] = source.GetType(); context.ContextData[PluginContext.SINK_TYPE] = sink.GetType(); var pipe = new PipeFactory().CreateInstance(PipeFactory.REGEX_FILTER_PIPE, context); source.Subscribe(pipe); pipe.Subscribe(sink); string record1 = "24,09/29/17,00:00:04,Database Cleanup Begin,,,,,0,6,,,,,,,,,0"; source.MockEvent(record1); source.MockEvent("25,09/29/17,00:00:04,0 leases expired and 0 leases deleted,,,,,0,6,,,,,,,,,0"); Assert.Single(sink.Records); Assert.Equal(record1, sink.Records[0]); }
public async Task TrimEndTrimsWhitespaceAtEnd(string input, string expected) { using (var factory = new PipeFactory()) { var readerWriter = factory.Create(); var writeBuffer = readerWriter.Writer.Alloc(); var bytes = Encoding.ASCII.GetBytes(input); writeBuffer.Write(bytes); await writeBuffer.FlushAsync(); var result = await readerWriter.Reader.ReadAsync(); var buffer = result.Buffer; var trimmed = buffer.TrimEnd(); var outputBytes = trimmed.ToArray(); Assert.Equal(expected, Encoding.ASCII.GetString(outputBytes)); } }
public TestInput() { _memoryPool = new MemoryPool(); _pipelineFactory = new PipeFactory(); Pipe = _pipelineFactory.Create(); FrameContext = new FrameContext { ServiceContext = new TestServiceContext(), Input = Pipe.Reader, ConnectionInformation = new MockConnectionInformation { PipeFactory = _pipelineFactory }, TimeoutControl = Mock.Of <ITimeoutControl>() }; Frame = new Frame <object>(null, FrameContext); Frame.FrameControl = Mock.Of <IFrameControl>(); }
public async Task ReadTWorksAgainstMultipleBuffers() { using (var factory = new PipeFactory()) { var readerWriter = factory.Create(); var output = readerWriter.Writer.Alloc(); // we're going to try to force 3 buffers for 8 bytes output.Write(new byte[] { 0, 1, 2 }); output.Ensure(4031); output.Write(new byte[] { 3, 4, 5 }); output.Ensure(4031); output.Write(new byte[] { 6, 7, 9 }); var readable = output.AsReadableBuffer(); Assert.Equal(9, readable.Length); int spanCount = 0; foreach (var _ in readable) { spanCount++; } Assert.Equal(3, spanCount); byte[] local = new byte[9]; readable.CopyTo(local); var span = new Span <byte>(local); Assert.Equal(span.Read <byte>(), readable.ReadLittleEndian <byte>()); Assert.Equal(span.Read <sbyte>(), readable.ReadLittleEndian <sbyte>()); Assert.Equal(span.Read <short>(), readable.ReadLittleEndian <short>()); Assert.Equal(span.Read <ushort>(), readable.ReadLittleEndian <ushort>()); Assert.Equal(span.Read <int>(), readable.ReadLittleEndian <int>()); Assert.Equal(span.Read <uint>(), readable.ReadLittleEndian <uint>()); Assert.Equal(span.Read <long>(), readable.ReadLittleEndian <long>()); Assert.Equal(span.Read <ulong>(), readable.ReadLittleEndian <ulong>()); Assert.Equal(span.Read <float>(), readable.ReadLittleEndian <float>()); Assert.Equal(span.Read <double>(), readable.ReadLittleEndian <double>()); await output.FlushAsync(); } }
public virtual ITestState Start() { _threadPoolCounter = new ThreadPoolCounter(); _globalCounters = GlobalCounters.CreateDefault(); ITestState testState = new TestState(_timer, _globalCounters, _threadPoolCounter); PipeFactory <IResult> pipeFactory = new PipeFactory <IResult>(); _aggregator = new PipelineDataAggregator(_settings.Aggregators, pipeFactory); _aggregator.Start(); _errorHandler = new ErrorHandler(); _dataReader = _settings.DataReader; _dataReader.Begin(testState); IIterationContextFactory iterationContextFactory = CreateIterationContextFactory(); IReplayScenarioHandlerFactory scenarioHandlerFactory = CreateScenarioHandlerFactory(); IReplaySchedulerFactory schedulerFactory = CreateSchedulerFactory(); IDataCollectorFactory dataCollectorFactory = CreateDataCollectorFactory(pipeFactory, _threadPoolCounter); IScenarioThreadFactory scenarioThreadFactory = CreateScenarioThreadFactory(); IThreadFactory threadFactory = new ReplayScenarioThreadFactory( iterationContextFactory, scenarioHandlerFactory, schedulerFactory, dataCollectorFactory, scenarioThreadFactory ); _pool = new ThreadPool(threadFactory, _threadPoolCounter); _pool.StartWorkersAsync(_settings.ThreadCount); while (_threadPoolCounter.CreatedThreadCount != _threadPoolCounter.InitializedThreadCount) { Thread.Sleep(100); _errorHandler.Assert(); } _timer.Start(); // This line also releases Worker-Threads from wait in IPrewait return(testState); }
public async Task EqualsDetectsDeltaForAllLocations() { using (var factory = new PipeFactory()) { var readerWriter = factory.Create(); // populate with dummy data const int DataSize = 10000; byte[] data = new byte[DataSize]; var rand = new Random(12345); rand.NextBytes(data); var writeBuffer = readerWriter.Writer.Alloc(); writeBuffer.Write(data); await writeBuffer.FlushAsync(); // now read it back var result = await readerWriter.Reader.ReadAsync(); var readBuffer = result.Buffer; Assert.False(readBuffer.IsSingleSpan); Assert.Equal(data.Length, readBuffer.Length); // check the entire buffer EqualsDetectsDeltaForAllLocations(readBuffer, data, 0, data.Length); // check the first 32 sub-lengths for (int i = 0; i <= 32; i++) { var slice = readBuffer.Slice(0, i); EqualsDetectsDeltaForAllLocations(slice, data, 0, i); } // check the last 32 sub-lengths for (int i = 0; i <= 32; i++) { var slice = readBuffer.Slice(data.Length - i, i); EqualsDetectsDeltaForAllLocations(slice, data, data.Length - i, i); } } }
public async Task WriteDuringReadIsNotReturned() { var pool = new DisposeTrackingBufferPool(); var writeSize = 512; using (var factory = new PipeFactory(pool)) { var pipe = factory.Create(); await pipe.Writer.WriteAsync(new byte[writeSize]); var buffer = pipe.Writer.Alloc(writeSize); var readResult = await pipe.Reader.ReadAsync(); pipe.Reader.Advance(readResult.Buffer.End); buffer.Write(new byte[writeSize]); buffer.Commit(); Assert.Equal(1, pool.CurrentlyRentedBlocks); } }
public async Task PeekWorkesWithEmptySegments() { using (var factory = new PipeFactory()) { var readerWriter = factory.Create(); var w = readerWriter.Writer.Alloc(); w.Append(ReadableBuffer.Create(new byte[] { 0 }, 0, 0)); w.Append(ReadableBuffer.Create(new byte[] { 1 }, 0, 1)); await w.FlushAsync(); var result = await readerWriter.Reader.ReadAsync(); var buffer = result.Buffer; var reader = new ReadableBufferReader(buffer); Assert.Equal(1, reader.Peek()); Assert.Equal(1, reader.Take()); Assert.Equal(-1, reader.Peek()); Assert.Equal(-1, reader.Take()); } }
public TestInput() { _memoryPool = new MemoryPool(); var pair = PipeFactory.CreateConnectionPair(_memoryPool); Transport = pair.Transport; Application = pair.Application; Http1ConnectionContext = new Http1ConnectionContext { ServiceContext = new TestServiceContext(), ConnectionFeatures = new FeatureCollection(), Application = Application, Transport = Transport, MemoryPool = _memoryPool, TimeoutControl = Mock.Of <ITimeoutControl>() }; Http1Connection = new Http1Connection(Http1ConnectionContext); Http1Connection.HttpResponseControl = Mock.Of <IHttpResponseControl>(); }
public async Task DefaultWriterSchedulerRunsInline() { using (var factory = new PipeFactory()) { var pipe = factory.Create(new PipeOptions { MaximumSizeLow = 32, MaximumSizeHigh = 64 }); var writableBuffer = pipe.Writer.Alloc(64); writableBuffer.Advance(64); var flushAsync = writableBuffer.FlushAsync(); Assert.False(flushAsync.IsCompleted); int id = 0; Func <Task> doWrite = async() => { await flushAsync; pipe.Writer.Complete(); Assert.Equal(Thread.CurrentThread.ManagedThreadId, id); }; var writing = doWrite(); var result = await pipe.Reader.ReadAsync(); id = Thread.CurrentThread.ManagedThreadId; pipe.Reader.Advance(result.Buffer.End, result.Buffer.End); pipe.Reader.Complete(); await writing; } }
public async Task CopyToAsyncNativeMemory() { using (var factory = new PipeFactory(NativeBufferPool.Shared)) { var readerWriter = factory.Create(); var output = readerWriter.Writer.Alloc(); output.Append("Hello World", TextEncoder.Utf8); await output.FlushAsync(); var ms = new MemoryStream(); var result = await readerWriter.Reader.ReadAsync(); var rb = result.Buffer; await rb.CopyToAsync(ms); ms.Position = 0; Assert.Equal(11, rb.Length); Assert.Equal(11, ms.Length); Assert.Equal(rb.ToArray(), ms.ToArray()); Assert.Equal("Hello World", Encoding.ASCII.GetString(ms.ToArray())); } }
public async Task TrySliceToSpan(string input, string sliceTo, string expected) { var sliceToBytes = Encoding.UTF8.GetBytes(sliceTo); using (var factory = new PipeFactory()) { var readerWriter = factory.Create(); var writeBuffer = readerWriter.Writer.Alloc(); var bytes = Encoding.UTF8.GetBytes(input); writeBuffer.Write(bytes); await writeBuffer.FlushAsync(); var result = await readerWriter.Reader.ReadAsync(); var buffer = result.Buffer; ReadableBuffer slice; ReadCursor cursor; Assert.True(buffer.TrySliceTo(sliceToBytes, out slice, out cursor)); Assert.Equal(expected, slice.GetUtf8String()); } }
public async Task DisposeAsync() { if (!_disposed) { _disposed = true; _stopping = true; _output.Reader.CancelPendingRead(); await Task.WhenAll(_sendTask, _receiveTask); _output.Writer.Complete(); _input.Reader.Complete(); _socket?.Dispose(); _socket = null; if (_ownsFactory) { _factory?.Dispose(); } _factory = null; } }
public async Task TakeTraversesSegments() { using (var factory = new PipeFactory()) { var readerWriter = factory.Create(); var w = readerWriter.Writer.Alloc(); w.Append(ReadableBuffer.Create(new byte[] { 1 }, 0, 1)); w.Append(ReadableBuffer.Create(new byte[] { 2 }, 0, 1)); w.Append(ReadableBuffer.Create(new byte[] { 3 }, 0, 1)); await w.FlushAsync(); var result = await readerWriter.Reader.ReadAsync(); var buffer = result.Buffer; var reader = new ReadableBufferReader(buffer); Assert.Equal(1, reader.Take()); Assert.Equal(2, reader.Take()); Assert.Equal(3, reader.Take()); Assert.Equal(-1, reader.Take()); } }
public void Setup() { PipeFactory = new PipeFactory(); Pipe = PipeFactory.Create(); var serviceContext = new ServiceContext { HttpParserFactory = f => new HttpParser <FrameAdapter>(), ServerOptions = new KestrelServerOptions(), }; var frameContext = new FrameContext { ServiceContext = serviceContext, ConnectionInformation = new MockConnectionInformation { PipeFactory = PipeFactory }, TimeoutControl = new MockTimeoutControl() }; Frame = new Frame <object>(application: null, frameContext: frameContext); }
public HttpConnectionTests() { _bufferPool = new MemoryPool(); var pair = PipeFactory.CreateConnectionPair(_bufferPool); _httpConnectionContext = new HttpConnectionContext { ConnectionId = "0123456789", ConnectionAdapters = new List <IConnectionAdapter>(), ConnectionFeatures = new FeatureCollection(), BufferPool = _bufferPool, HttpConnectionId = long.MinValue, Application = pair.Application, Transport = pair.Transport, ServiceContext = new TestServiceContext { SystemClock = new SystemClock() } }; _httpConnection = new HttpConnection(_httpConnectionContext); }
static void Main(string[] args) { Thread.Sleep(1000); // Wait for the service to start ISettingsContract settingsChannel = PipeFactory.NewSettingsChannel(); IMailContract mailChannel = PipeFactory.NewMailChannel(); settingsChannel.SetMailSettings(new SmtpMailServerSettings { Host = "smtp.gmail.com", Port = 587, RequiresSsl = true, RequiresAuthentication = true, Username = "******", Password = "******" }); // settingsChannel.SetQueueFolder("C:\\mail\queued"); // settingsChannel.SetFailedFolder("C:\\mail\failed"); /*for (int counter = 1; counter < 100; counter++) * { * MailMessage message = new MailMessage(); * message.To.Add(new MailAddress(@"*****@*****.**", "Test MailQueue.net account")); * message.From = new MailAddress(@"*****@*****.**", "Test MailQueue.net account"); * message.Subject = "Testing MailQueue.net, round " + counter; * message.Body = @"This is a test!"; * * if (counter % 10 == 0) * { * // A failure * mailChannel.QueueMessageWithSmtpSettings(new SerializableMailMessage(message), "localhost", 25, false, false, null, null); * } * else * { * // A supposed success * mailChannel.QueueMessage(new SerializableMailMessage(message)); * } * }*/ }
public Http1ConnectionTests() { _pipelineFactory = new MemoryPool(); var pair = PipeFactory.CreateConnectionPair(_pipelineFactory); _transport = pair.Transport; _application = pair.Application; _serviceContext = new TestServiceContext(); _timeoutControl = new Mock <ITimeoutControl>(); _http1ConnectionContext = new Http1ConnectionContext { ServiceContext = _serviceContext, ConnectionFeatures = new FeatureCollection(), MemoryPool = _pipelineFactory, TimeoutControl = _timeoutControl.Object, Application = pair.Application, Transport = pair.Transport }; _http1Connection = new TestHttp1Connection(_http1ConnectionContext); _http1Connection.Reset(); }
public FrameConnectionTests() { _pipeFactory = new PipeFactory(); _frameConnectionContext = new FrameConnectionContext { ConnectionId = "0123456789", ConnectionAdapters = new List <IConnectionAdapter>(), ConnectionInformation = new MockConnectionInformation { PipeFactory = _pipeFactory }, FrameConnectionId = long.MinValue, Input = _pipeFactory.Create(), Output = _pipeFactory.Create(), ServiceContext = new TestServiceContext { SystemClock = new SystemClock() } }; _frameConnection = new FrameConnection(_frameConnectionContext); }