Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Net;
- using System.Net.Sockets;
- using ChannelSockets;
- using System.Collections.Concurrent;
- namespace ExampleServer
- {
- public class Client
- {
- public IPEndPoint EndPoint { get; set; }
- public string Username { get; set; }
- public ushort ID { get; set; }
- }
- class Program
- {
- public struct Channels
- {
- public const byte UnreliableUpdate = 0;
- public const byte ReliableUpdate = 1;
- public const byte Chat = 2;
- public const byte Downloads = 3;
- }
- public static ChannelSocket socket = new ChannelSocket(30303);
- static Dictionary<IPEndPoint, Client> clients = new Dictionary<IPEndPoint,Client>();
- static Queue<ushort> clientIDs = new Queue<ushort>(Enumerable.Range(0, 1000).Select(i => (ushort)i));
- static ConcurrentQueue<Client> pendingClients = new ConcurrentQueue<Client>();
- static ConcurrentQueue<IPEndPoint> disconnectedEndPoints = new ConcurrentQueue<IPEndPoint>();
- static void Main(string[] args)
- {
- socket.Add(Channels.UnreliableUpdate, ChannelType.Unreliable);
- socket.Add(Channels.ReliableUpdate, ChannelType.Reliable);
- socket.Add(Channels.Chat, ChannelType.Reliable);
- socket.Add(Channels.Downloads, ChannelType.Reliable);
- socket.Connection = (c) =>
- {
- // Ban connections like this
- if (!c.EndPoint.Equals(IPAddress.Parse("111.111.111.111")))
- {
- c.Allow = false;
- return;
- }
- // Authenticate users like this
- string username;
- string password;
- if (c.Packet.ReadString(out username) && c.Packet.ReadString(out password) &&
- username == "foo" && password == "bar")
- {
- pendingClients.Enqueue(new Client() { EndPoint = c.EndPoint, Username = username });
- }
- else
- {
- c.Allow = false;
- }
- };
- // Callback if an endpoint times out or disconnects
- socket.Disconnection = (c) =>
- {
- // Destination initiated the disconnection or server did or the connection timed out
- disconnectedEndPoints.Enqueue(c.EndPoint);
- };
- // Server command line interface
- while (true)
- {
- Console.WriteLine("Enter command");
- Console.WriteLine("1. List clients");
- Console.WriteLine("2. Read chat");
- Console.WriteLine("3. Send 'hello world' to clients");
- Console.WriteLine("4. Disconnect clients");
- var key = Console.ReadKey().Key;
- Console.Clear();
- // Process clients that just disconnected or connected. This would be done in a game loop normally
- IPEndPoint disconnectedEndPoint;
- while (disconnectedEndPoints.TryDequeue(out disconnectedEndPoint))
- {
- Client client;
- if (clients.TryGetValue(disconnectedEndPoint, out client))
- {
- // Recycle the client ID
- clientIDs.Enqueue(client.ID);
- clients.Remove(disconnectedEndPoint);
- }
- }
- Client connectedClient;
- while (pendingClients.TryDequeue(out connectedClient))
- {
- // Assign a unique client ID to the client
- connectedClient.ID = clientIDs.Dequeue();
- clients.Add(connectedClient.EndPoint, connectedClient);
- }
- switch (key)
- {
- case ConsoleKey.D1:
- Console.WriteLine("Clients:");
- foreach (Client client in clients.Values)
- {
- Console.WriteLine(client.Username);
- }
- break;
- case ConsoleKey.D2:
- Console.WriteLine("Chat:");
- EndPointPacket endPointPacket;
- while (socket[Channels.Chat].Packets.TryDequeue(out endPointPacket))
- {
- ushort clientID;
- string message;
- if (endPointPacket.Packet.ReadUInt16(out clientID) && endPointPacket.Packet.ReadString(out message))
- {
- Client client;
- if (clientID == ushort.MaxValue && clients.TryGetValue(endPointPacket.EndPoint, out client))
- {
- // TODO: Remove non-visible characters
- Console.WriteLine(client.Username + ": " + message.Trim());
- }
- }
- }
- break;
- case ConsoleKey.D3:
- foreach (IPEndPoint endPoint in clients.Keys)
- {
- var packet = socket[Channels.Chat].CreatePacket();
- packet.WriteUInt16(ushort.MaxValue); // Client ID, ushort.MaxValue is the Server
- packet.WriteString("hello world");
- socket[Channels.Chat].Send(packet, endPoint);
- }
- Console.WriteLine("Messages sent");
- break;
- case ConsoleKey.D4:
- foreach (IPEndPoint endPoint in clients.Keys)
- {
- socket.Disconnect(endPoint);
- }
- Console.WriteLine("Disconnected clients");
- break;
- }
- Console.WriteLine();
- Console.WriteLine("Press any key...");
- Console.ReadKey(true);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement