public
Created

Vortex Listener Factory

  • Download Gist
ListenerFactory.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
#region Usings
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
 
#endregion
 
 
namespace Vortex.Net
{
 
/// <summary>
/// The main server listener for Vortex.Net
/// </summary>
 
public class ListenerFactory
{
#region Fields & Properties
/// <summary>
/// The main listener instance, held in a socket.
/// </summary>
private Socket ListenerSocket;
 
/// <summary>
/// The callback handler for a new connection
/// </summary>
private AsyncCallback OnConnectionCallback;
 
/// <summary>
/// Holds the IP address for the socket.
/// </summary>
private IPAddress IpAddress;
 
/// <summary>
/// Port for listening.
/// </summary>
private int Port { get; set; }
 
/// <summary>
/// Boolean to determine whether the factory is listening.
/// </summary>
private bool IsListening;
 
 
/// <summary>
/// The connection queue as an integer.
/// </summary>
private int QueuedConnections;
#endregion
 
 
/// <summary>
/// Construct a new listener factory object
/// </summary>
/// <param name="port">The port for the server to listen on</param>
/// <param name="ipAddress">The ip address</param>
/// <param name="QueuedConnections">The backlog for the listening instance</param>
 
#region Constructor
public ListenerFactory(int port, IPAddress ipAddress, int QueuedConnections)
{
this.QueuedConnections = QueuedConnections;
this.Port = port;
this.IpAddress = ipAddress;
this.ListenerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
 
this.BeginListening(QueuedConnections);
}
 
#endregion
 
#region Private Methods
 
private void BeginListening(int QueuedConnections)
{
IPEndPoint endpoint = new IPEndPoint(IPAddress.Any,90);
this.ListenerSocket.Bind(endpoint);
this.ListenerSocket.Listen(QueuedConnections);
this.IsListening = true;
this.Listen();
StartAccepting();
Console.WriteLine("Initialised Listener Factory [port:{0}, ip:{1}]", this.Port, this.IpAddress);
this.OnConnectionCallback = new AsyncCallback(OnConnectionReceived);
}
 
private void OnConnectionReceived(IAsyncResult IAr)
{
lock (this.ListenerSocket)
{
int i = 0;
Socket UserSocket = this.ListenerSocket.EndAccept(IAr);
Connection Client = new Connection(UserSocket, i++);
Console.WriteLine("Accepted client {0} with IP of {1}", i, UserSocket.RemoteEndPoint);
}
}
 
 
private void Listen()
{
if (IsListening)
{
Console.WriteLine("Socket is already listening!");
this.EndSocketAccept();
}
else
{
this.ListenerSocket.Listen(this.QueuedConnections);
}
}
 
private void StartAccepting()
{
if (IsListening)
{
this.ListenerSocket.BeginAccept(OnConnectionCallback, ListenerSocket);
}
else
{
Console.WriteLine("Cannot begin accepting clients on a null socket object!");
}
}
 
private void EndSocketAccept()
{
Console.WriteLine("Shutting down socket");
if (IsListening)
{
//this.ListenerSocket.Shutdown(SocketShutdown.Both);
//added safety?
this.ListenerSocket = null;
Environment.Exit(1);
}
else
{
Console.WriteLine("You cannot terminate a socket that has not been initialized!");
}
}
#endregion
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.