Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Text;
- using System.Net;
- using System.Net.Sockets;
- using System.Threading;
- namespace CF.NetOp
- {
- /// <summary>
- /// A Server.
- /// </summary>
- public class Server
- {
- public class RecEventArgs : EventArgs
- {
- public string RecS;
- public byte[] RecB;
- public int ListenerID;
- public RecEventArgs(string received, byte[] receivedb, int LID)
- {
- this.RecS = received;
- this.RecB = receivedb;
- this.ListenerID = LID;
- }
- }
- private class Listener
- {
- private TcpClient tcpc;
- private int ListenerID = 0;
- private byte[] rbuf = new byte[512];
- private bool Alive = false;
- private Thread ListenT;
- /// <summary>
- /// This event is being raised if we received something.
- /// </summary>
- public event EventHandler<RecEventArgs> Rec;
- /// <summary>
- /// Creates the listener.
- /// </summary>
- /// <param name="AcceptedClient">The TcpListener.AcceptTcpClient() client.</param>
- public Listener(ref TcpClient AcceptedClient)
- {
- this.tcpc = AcceptedClient;
- this.Alive = true;
- StartListening();
- }
- /// <summary>
- /// Creates the listener.
- /// </summary>
- /// <param name="AcceptedClient">The TcpListener.AcceptTcpClient() client.</param>
- /// <param name="ListenerID">An ID to identify the listener's slot.</param>
- public Listener(ref TcpClient AcceptedClient, int ListenerID)
- {
- this.ListenerID = ListenerID;
- this.tcpc = AcceptedClient;
- this.Alive = true;
- StartListening();
- }
- private void StartListening()
- {
- this.ListenT = new Thread(new ThreadStart(Listen));
- this.ListenT.Start();
- }
- private void Listen()
- {
- this.tcpc.GetStream().BeginRead(this.rbuf, 0, 512, Received, null);
- }
- private void Received(IAsyncResult ar)
- {
- int BytesRead = this.tcpc.GetStream().EndRead(ar);
- if (BytesRead < 1)
- {
- this.Alive = false;
- return;
- }
- byte[] data = new byte[BytesRead];
- for (int i = 0; i < BytesRead; i++)
- data[i] = this.rbuf[i];
- this.tcpc.GetStream().Flush();
- if (this.Alive)
- Listen();
- Rec(this, new RecEventArgs(Encoding.ASCII.GetString(data), data, this.ListenerID));
- }
- /// <summary>
- /// Sends a preset buffer to the client.
- /// </summary>
- /// <param name="Data">The buffer to send.</param>
- public void Send(byte[] Data)
- {
- this.tcpc.GetStream().Write(Data, 0, Data.Length);
- // this.tcpc.GetStream().Flush();
- }
- /// <summary>
- /// Determines whether this client is alive or not.
- /// </summary>
- /// <returns>Returns true in case of aliveness otherwise false.</returns>
- public bool IsAlive()
- {
- return this.Alive;
- }
- /// <summary>
- /// Shuts this client down.
- /// </summary>
- public void Close()
- {
- this.Alive = false;
- this.tcpc.Close();
- }
- }
- private int Port = 0;
- private TcpListener tcpl;
- private int ClientsCount = 0;
- private Thread CAT;
- private Listener[] Listeners;
- public event EventHandler<RecEventArgs> Rec;
- /// <summary>
- /// Sets up a new listening port.
- /// </summary>
- /// <param name="Port">Port to listen on.</param>
- /// <param name="MaxClients">Maximum number of clients to allow</param>
- public Server(int Port, int MaxClients)
- {
- this.Port = Port;
- this.Listeners = new Listener[MaxClients];
- this.tcpl = new TcpListener(IPAddress.Any, Port);
- this.tcpl.Start();
- }
- /// <summary>
- /// Sets up a new listening port and lets you choose
- /// whether to launch the ClientAcceptor emmediately.
- /// </summary>
- /// <param name="Port">Port to listen on.</param>
- /// <param name="MaxClients">Maximum number of clients to allow</param>
- /// <param name="LaunchAcceptClients">Emmediately launch ClientAcceptor or not.</param>
- public Server(int Port, int MaxClients, bool LaunchAcceptClients)
- {
- this.Port = Port;
- this.Listeners = new Listener[MaxClients];
- this.tcpl = new TcpListener(IPAddress.Any, Port);
- this.tcpl.Start();
- if (LaunchAcceptClients)
- AcceptClients();
- }
- private bool AcceptNewClients = false;
- /// <summary>
- /// Launches the ClientAcceptor.
- /// </summary>
- public void AcceptClients()
- {
- this.AcceptNewClients = true;
- this.CAT = new Thread(new ThreadStart(_ClientAcceptor));
- this.CAT.Start();
- }
- private void _ClientAcceptor()
- {
- while (this.AcceptNewClients)
- {
- TcpClient newclient = this.tcpl.AcceptTcpClient();
- this.Listeners[this.ClientsCount] = new Listener(ref newclient, this.ClientsCount);
- this.Listeners[this.ClientsCount].Rec += new EventHandler<RecEventArgs>(LRec);
- this.Listeners[this.ClientsCount].Send(Encoding.ASCII.GetBytes("Hello Client!"));
- ClientsCount++;
- }
- }
- void LRec(object sender, Server.RecEventArgs e)
- {
- Rec(this, e);
- }
- /// <summary>
- /// Sends a preset buffer to the client.
- /// </summary>
- /// <param name="Data">The buffer to send.</param>
- /// <param name="ClientID">The Client to send to.</param>
- public void SendToClient(byte[] Data, int ClientID)
- {
- this.Listeners[ClientID].Send(Data);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement