public async Task BindAsync() { if (Threads.Count != _threadCount) { for (var index = 0; index < _threadCount; index++) { Threads.Add(new UvThread(Log)); } foreach (var thread in Threads) { await thread.StartAsync().ConfigureAwait(false); } } try { if (_threadCount == 1) { var listener = new UvListener(Threads[0], _endPointInformation, Log); listener.Dispatcher = _dispatcher; _listeners.Add(listener); await listener.StartAsync().ConfigureAwait(false); } else { var pipeName = (PlatformApis.IsWindows ? @"\\.\pipe\netgear_kestrel_" : "/tmp/netgear_kestrel_") + Guid.NewGuid().ToString("n"); var pipeMessage = Guid.NewGuid().ToByteArray(); var listenerPrimary = new UvListenerPrimary(Threads[0], _endPointInformation, Log); listenerPrimary.Dispatcher = _dispatcher; _listeners.Add(listenerPrimary); await listenerPrimary.StartAsync(pipeName, pipeMessage).ConfigureAwait(false); foreach (var thread in Threads.Skip(1)) { var listenerSecondary = new UvListenerSecondary(thread, Log); listenerSecondary.Dispatcher = _dispatcher; _listeners.Add(listenerSecondary); await listenerSecondary.StartAsync(pipeName, pipeMessage).ConfigureAwait(false); } } } catch (UvException ex) when(ex.StatusCode == UvConstants.EADDRINUSE) { await UnbindAsync().ConfigureAwait(false); throw new AddressInUseException(ex.Message, ex); } catch { await UnbindAsync().ConfigureAwait(false); throw; } }
public PipeReadContext(UvListenerPrimary listener) { _listener = listener; _bufHandle = GCHandle.Alloc(_buf, GCHandleType.Pinned); _bufPtr = _bufHandle.AddrOfPinnedObject(); }