Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace SIS.WebServer
- {
- using System;
- using System.Net;
- using System.Net.Sockets;
- using System.Threading.Tasks;
- using Routing;
- public class Server
- {
- private const string LocalHostIpAddress = "127.0.0.1";
- private readonly int port;
- private readonly TcpListener listener;
- private readonly ServerRoutingTable serverRoutingTable;
- private bool IsRunning;
- public Server(int port,ServerRoutingTable serverRoutingTable)
- {
- this.port = port;
- this.serverRoutingTable = serverRoutingTable;
- this.listener=new TcpListener(IPAddress.Parse(LocalHostIpAddress),port);
- }
- public void Run()
- {
- this.listener.Start();
- this.IsRunning = true;
- Console.WriteLine($"Server started at http://{LocalHostIpAddress}:{port}");
- var task =Task.Run(this.ListenLoop);
- task.Wait();
- }
- public async Task ListenLoop()
- {
- while (this.IsRunning)
- {
- var client = await this.listener.AcceptSocketAsync();
- var connectionHandler = new ConnectionHandler(client, this.serverRoutingTable);
- var responseTask = connectionHandler.ProcessRequestAsync();
- responseTask.Wait();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement