MTSC  1.0.5
Build TCP servers out of modules with handlers for communication, exception and logging.
HttpHandler.cs
1 using MTSC.Common.Http;
3 using System;
4 using System.Collections.Generic;
5 using System.Net.Sockets;
6 using System.Text;
7 
8 namespace MTSC.Client.Handlers
9 {
13  public class HttpHandler : IHandler
14  {
15  #region Fields
16  List<IHttpModule> httpModules = new List<IHttpModule>();
17  #endregion
18  #region Constructors
19  public HttpHandler()
20  {
21 
22  }
23  #endregion
24  #region Public Methods
25  public void SendRequest(Client client, HttpMessage request)
30  {
31  client.QueueMessage(request.BuildRequest());
32  }
38  public HttpHandler AddModule(IHttpModule httpModule)
39  {
40  httpModules.Add(httpModule);
41  return this;
42  }
43  #endregion
44  #region Interface Implementation
45  void IHandler.Disconnected(Client client)
50  {
51 
52  }
59  bool IHandler.HandleReceivedMessage(Client client, Message message)
60  {
61  HttpMessage httpMessage = new HttpMessage();
62  httpMessage.ParseResponse(message.MessageBytes);
63  foreach(IHttpModule httpModule in httpModules)
64  {
65  if (httpModule.HandleResponse(this, httpMessage))
66  {
67  break;
68  }
69  }
70  return false;
71  }
78  bool IHandler.HandleSendMessage(Client client, ref Message message)
79  {
80  return false;
81  }
87  bool IHandler.InitializeConnection(Client client)
88  {
89  return true;
90  }
97  bool IHandler.PreHandleReceivedMessage(Client client, ref Message message)
98  {
99  return false;
100  }
105  void IHandler.Tick(Client tcpClient)
106  {
107 
108  }
109  #endregion
110  }
111 }
bool HandleResponse(IHandler handler, HttpMessage response)
Handle a response received from the server.
HttpHandler AddModule(IHttpModule httpModule)
Add a http module.
Definition: HttpHandler.cs:38
byte [] BuildRequest()
Build the request bytes based on the message contents.
Definition: HttpMessage.cs:232
void SendRequest(Client client, HttpMessage request)
Send a request to the server.
Definition: HttpHandler.cs:29
Handler for handling client http communication.
Definition: HttpHandler.cs:13
void Disconnected(Client client)
Called when the client is disconnected.
void QueueMessage(byte[] message)
Add a message to the message queue.
Definition: Client.cs:59
Handler interface for client communication.
Definition: IHandler.cs:11
Definition: Client.cs:14
void ParseResponse(byte[] responseBytes)
Parse the received bytes and populate the message contents.
Definition: HttpMessage.cs:406
Base class for TCP Client.
Definition: Client.cs:19