/// <inheritdoc /> public IntPtr AllocateMemory <T>(int count = 1) where T : struct { int totalSize = count * Marshal.SizeOf(typeof(T)); using (_mutex.Lock()) { if (_memoryBlocks.Count > 1) { _highWaterMark += totalSize; } if (_position + totalSize > _currentSize) { if (_memoryBlocks.Count == 1) { _highWaterMark += (_position + totalSize - _highWaterMark); } _currentSize = System.Math.Max(2 * _currentSize, totalSize); _memoryBlocks.Add(Marshal.AllocHGlobal(_currentSize)); _position = 0; } IntPtr ret = (_memoryBlocks.Last() + _position); _position += totalSize; return(ret); } }
/// <summary> /// Stops the Server from running. /// </summary> public virtual void Stop() { Running.Value = false; try { _tcpListener.Stop(); } catch { } // Give the `_tcpListenerThread` Thread time to finish. Thread.Sleep(1); if (_tcpListenerThread.ThreadState != ThreadState.Stopped) { try { Master.ForceQuitThread(_tcpListenerThread); } catch { } } try { _tcpReceiveTask.Dispose(); } catch { } using (_networkStreamsMutex.Lock()) { foreach (KeyValuePair <int, Stream> stream in _streams) { try { stream.Value.Close(); } catch { } } } foreach (var thread in _clientHandlerThreads) { if (thread.ThreadState != ThreadState.Stopped) { try { Master.ForceQuitThread(thread); } catch { } } } WorkerThreads.Instance.Stop(ServerShutdownClientHandlerForceQuitTimeout); }
/// <summary> /// Starts a new Webserver listening to all previously added Responses. /// </summary> /// <param name="port">the port to listen to</param> /// <param name="certificate">The ssl certificate for https (if null: connection will be http; if set will only be https) /// <para /> If the webserver does not respond after including your certificate, it might not be loaded or set up correctly. /// Consider looking at the LamestWebserver Logger output.</param> /// <param name="enabledSslProtocols">The available ssl protocols if the connection is https.</param> private WebServer(int port, X509Certificate2 certificate = null, SslProtocols enabledSslProtocols = SslProtocols.Tls12) : base(port) { EnabledSslProtocols = enabledSslProtocols; Certificate = certificate; RequestHandler = RequestHandler.CurrentRequestHandler; Start(); using (RunningServerMutex.Lock()) RunningServers.Add(this); }
/// <summary> /// Constructs a new FlushableMemoryPool. /// </summary> public FlushableMemoryPool(int DefaultAllocationSize = 1024) { using (_threadCountMutex.Lock()) { ThreadID = _threadIndex++; _concurrentThreads++; if (MaximumThreads > 0 && _concurrentThreads >= MaximumThreads) { throw new IndexOutOfRangeException($"The maximum specified count of concurrent Flushable Memory Pools has been exceeded. You can remove or increase this limit by changing {nameof(FlushableMemoryPool)}.{nameof(MaximumThreads)} either to <= 0 to disable it, or to a greater value to increase the limit."); } } _highWaterMark = DefaultAllocationSize; _memoryBlocks.Add(Marshal.AllocHGlobal(_highWaterMark)); }