/// <summary> /// Create an HTTP server. Usually only one HTTP server will be created /// </summary> /// <param name="port"> /// A <see cref="System.Int32"/> /// TCP port, for example 80 /// </param> /// <param name="maxConnections"> /// A <see cref="System.Int32"/> /// Maximum number of requests served simultaneously, for example 5 /// </param> /// <param name="rootPath"> /// A <see cref="System.String"/> /// Full path to the files to be served by the server /// </param> public Http(int port, int maxConnections, string rootPath) { instance = this; this.Port = port; this.MaxConnections = maxConnections; this.RootPath = rootPath; // i need a thread waiting for incoming connection requests // and a pool of threads which serve the requests threadPool = new JQuant.ThreadPool("HTTPjob", MaxConnections); // i need a TCP socket where I wait for incoming connection requests tcpListener = default(System.Net.Sockets.TcpListener); try { tcpListener = new System.Net.Sockets.TcpListener(Port); } catch (Exception e) { System.Console.WriteLine("Failed to open port " + Port); System.Console.WriteLine(e.ToString()); } if (tcpListener != default(System.Net.Sockets.TcpListener)) { } mainThread = new MainThread(RootPath, tcpListener, threadPool); }
protected void debugThreadPoolTestCallback(IWrite iWrite, string cmdName, object[] cmdArguments) { int maxJobs = 5; JQuant.ThreadPool threadPool = new JQuant.ThreadPool("test", 1, maxJobs, ThreadPriority.Lowest); threadpoolTestTicks = new long[maxJobs]; long tick = DateTime.Now.Ticks; for (int i = 0; i < maxJobs; i++) { threadpoolTestTicks[i] = tick; } for (int i = 0; i < maxJobs; i++) { threadPool.PlaceJob(ThreadPoolJobEnter, ThreadPoolJobDone, i); } Thread.Sleep(500); debugThreadPoolShowCallback(iWrite, cmdName, cmdArguments); threadPool.Dispose(); for (int i = 0; i < threadpoolTestTicks.Length; i++) { iWrite.WriteLine("ThreadPoolJob done idx =" + i + ", time = " + (double)threadpoolTestTicks[i] / (double)(10 * 1) + " micros"); } }
public MainThread(string rootPath, System.Net.Sockets.TcpListener tcpListener, JQuant.ThreadPool threadPool) : base("HTTP", 10, MBX_TIMEOUT_IDLE) { this.threadPool = threadPool; this.tcpListener = tcpListener; this.rootPath = rootPath; string patternGet = "^GET /(.*) HTTP/.+"; this.regexPatternGet = new System.Text.RegularExpressions.Regex(patternGet); }