forked from jeske/SimpleHttpServer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpServer.cs
More file actions
70 lines (55 loc) · 1.64 KB
/
HttpServer.cs
File metadata and controls
70 lines (55 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Copyright (C) 2016 by David Jeske, Barend Erasmus and donated to the public domain
using log4net;
using SimpleHttpServer;
using SimpleHttpServer.Models;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace SimpleHttpServer
{
public class HttpServer
{
#region Fields
private readonly int _port;
private TcpListener _listener;
private readonly HttpProcessor _processor;
private bool _isActive = true;
#endregion
private static readonly ILog _log = LogManager.GetLogger(typeof(HttpServer));
#region Public Methods
public HttpServer(int port, List<Route> routes, Func<HttpRequest,HttpResponse> notFoundOverrideCallable = null)
{
_port = port;
_processor = new HttpProcessor
{
NotFoundOverrideCallable = notFoundOverrideCallable
};
foreach (var route in routes)
{
_processor.AddRoute(route);
}
}
public void StopListen()
{
_isActive = false;
_listener.Stop();
}
public void Listen()
{
_listener = new TcpListener(IPAddress.Any, _port);
_listener.Start();
while (_isActive)
{
var s = _listener.AcceptTcpClient();
var thread = new Thread(() => _processor.HandleClient(s));
thread.Start();
Thread.Sleep(1);
}
}
#endregion
}
}