Advertisement
Aliendreamer

webserver

Oct 13th, 2018
360
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.46 KB | None | 0 0
  1. namespace SIS.WebServer
  2. {
  3.     using System;
  4.     using System.Net;
  5.     using System.Net.Sockets;
  6.     using System.Threading.Tasks;
  7.     using Routing;
  8.  
  9.     public class Server
  10.     {
  11.         private const string LocalHostIpAddress = "127.0.0.1";
  12.  
  13.         private readonly int port;
  14.  
  15.         private readonly TcpListener listener;
  16.  
  17.         private readonly ServerRoutingTable serverRoutingTable;
  18.  
  19.         private bool IsRunning;
  20.  
  21.         public Server(int port,ServerRoutingTable serverRoutingTable)
  22.         {
  23.  
  24.             this.port = port;
  25.             this.serverRoutingTable = serverRoutingTable;
  26.             this.listener=new TcpListener(IPAddress.Parse(LocalHostIpAddress),port);
  27.         }
  28.  
  29.         public void Run()
  30.         {
  31.            
  32.             this.listener.Start();
  33.             this.IsRunning = true;
  34.             Console.WriteLine($"Server started at http://{LocalHostIpAddress}:{port}");
  35.  
  36.             var task =Task.Run(this.ListenLoop);
  37.             task.Wait();
  38.         }
  39.  
  40.         public async Task ListenLoop()
  41.         {
  42.  
  43.             while (this.IsRunning)
  44.             {
  45.  
  46.                 var client = await this.listener.AcceptSocketAsync();
  47.                 var connectionHandler = new ConnectionHandler(client, this.serverRoutingTable);              
  48.                 var responseTask = connectionHandler.ProcessRequestAsync();
  49.                 responseTask.Wait();
  50.            
  51.             }                    
  52.         }
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement