MTSC  1.0.5
Build TCP servers out of modules with handlers for communication, exception and logging.
BroadcastHandler.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 
6 {
10  public class BroadcastHandler : IHandler
11  {
12  public BroadcastHandler()
13  {
14 
15  }
16 
17  void IHandler.ClientRemoved(Server server, ClientData client)
18  {
19 
20  }
21 
22  bool IHandler.HandleClient(Server server, ClientData client)
23  {
24  return false;
25  }
26 
27  bool IHandler.HandleReceivedMessage(Server server, ClientData client, Message message)
28  {
29  server.LogDebug("Broadcast: " + UnicodeEncoding.Unicode.GetString(message.MessageBytes));
30  server.LogDebug("From: " + client.TcpClient.Client.RemoteEndPoint.ToString());
31  foreach(ClientData clientStruct in server.Clients)
32  {
33  server.QueueMessage(clientStruct, message.MessageBytes);
34  }
35  return false;
36  }
37 
38  bool IHandler.HandleSendMessage(Server server, ClientData client, ref Message message)
39  {
40  return false;
41  }
42 
43  bool IHandler.PreHandleReceivedMessage(Server server, ClientData client, ref Message message)
44  {
45  return false;
46  }
47 
48  void IHandler.Tick(Server server)
49  {
50 
51  }
52  }
53 }
Interface for communication handlers.
Definition: IHandler.cs:11
bool PreHandleReceivedMessage(Server server, ClientData client, ref Message message)
Called before the message handling. Perform here any processing of the message.
Structure containing client information.
Definition: Server.cs:402
void QueueMessage(ClientData target, byte[] message)
Queues a message to be sent.
Definition: Server.cs:137
void ClientRemoved(Server server, ClientData client)
Handles the removal of a client from the server.
void Tick(Server server)
Method performs regular operations onto the server.
bool HandleReceivedMessage(Server server, ClientData client, Message message)
Handles the received message.
Basic server class to handle TCP connections.
Definition: Server.cs:20
List< ClientData > Clients
List of clients currently connected to the server.
Definition: Server.cs:47
bool HandleClient(Server server, ClientData client)
Handles a new client.
void LogDebug(string debugMessage)
Adds a debug message to be logged by the associated loggers.
Definition: Server.cs:156
bool HandleSendMessage(Server server, ClientData client, ref Message message)
Handles a message before sending.