public static void Main() { //Preparing the Threads TcpHandler tcpHandler = null; var tasks = new List <Task>(); try { //Using standard Port 8000 and allowing 5 clients in the queue tcpHandler = new TcpHandler(); while (true) { //Starting the Threads ConcurrentConnections.Wait(); tasks.Add(Task.Run(() => ReceiveClient(tcpHandler))); } } //Hopefully no Exceptions here catch (Exception e) { Console.WriteLine(e); throw; } finally { //When finished stop the Server and set all threads on hold tcpHandler?.Stop(); Task.WaitAll(tasks.ToArray()); } }
public void SendResponse(IResponse response) { using StreamWriter writer = new StreamWriter(TcpHandler.GetStream()) { AutoFlush = true }; writer.WriteLine(response.Content); }
public IResponse ExecuteRequest() { StreamReader reader = new StreamReader(TcpHandler.GetStream()); Request = new RequestContext(reader); if (Request.CheckRequest()) { RequestManager = new MessageManager(Request); var text = RequestManager.ProcessRequest(); TextResponse response = new TextResponse(StatusCodesEnum.Ok, text); return(response); } return(new TextResponse(StatusCodesEnum.BadRequest, Request.GetBadRequest("Bad Request!"))); }
public void CloseClient() { TcpHandler.CloseClient(); }