Skip to content

Instantly share code, notes, and snippets.

@Jinz0
Created August 15, 2012 22:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jinz0/3364284 to your computer and use it in GitHub Desktop.
Save Jinz0/3364284 to your computer and use it in GitHub Desktop.
Vortex Listener Factory
#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
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment