I am trying to build a high performance socket server and this is what I have come up with so far. Can anyone please review my code and see if I can make any improvements on it?
// State object for reading client data asynchronously
public class StateObject
{
// Client socket.
public Socket workSocket = null;
// Size of receive buffer.
public const int BufferSize = 1024;
// Receive buffer.
public byte[] buffer = new byte[BufferSize];
// Received data string.
public StringBuilder sb = new StringBuilder();
// GUID of client for identification
public string guid = string.Empty;
}
public class CAsnycSocketServer : IDisposable
{
public AsyncCallback workerCallBack;
private Socket mainSocket = null;
private static ArrayList ClientSockets;
private Socket workerSocket = null;
private bool shutdownServer = false;
public static ManualResetEvent allDone = new ManualResetEvent(false);
public int MaxConnections { get; set; } //Not implemented
/// <summary>
/// Default constructor
/// </summary>
public CAsnycSocketServer()
{
}
/// <summary>
/// Constructor to start listening
/// </summary>
/// <param name="port"></param>
public CAsnycSocketServer(int port)
{
StartListening(port);
}
public void ShutdownServer()
{
shutdownServer = true;
CloseSockets();
}
public void Dispose()
{
shutdownServer = true;
CloseSockets();
mainSocket.Dispose();
ClientSockets.Clear();
}
/// <summary>
/// Gets the current connected client count
/// </summary>
/// <returns></returns>
public int getConnectedClientCount()
{
lock (ClientSockets.SyncRoot)
{
return ClientSockets.Count;
}
}
/// <summary>
/// Closes the main listening socket and all the clients
/// </summary>
public void CloseSockets()
{
if (mainSocket != null)
{
mainSocket.Close();
}
lock (ClientSockets.SyncRoot)
{
foreach (StateObject so in ClientSockets)
{
if (so.workSocket != null)
{
so.workSocket.Close();
so.workSocket.Dispose();
}
}
}
ClientSockets.Clear();
}
/// <summary>
/// Starts listening for client connections
/// </summary>
/// <param name="port"></param>
public void StartListening(int port)
{
try
{
mainSocket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.Tcp);
IPEndPoint ipLocal = new IPEndPoint(IPAddress.Any, port);
// Bind to local IP Address...
mainSocket.Bind(ipLocal);
// Start listening...
mainSocket.Listen(100);
ClientSockets = new ArrayList();
while (!shutdownServer)
{
// Set the event to nonsignaled state.
allDone.Reset();
// Start an asynchronous socket to listen for connections.
Console.WriteLine("Server listening for connections");
mainSocket.BeginAccept(new AsyncCallback(ClientConnectCallback), mainSocket);
// Wait until a connection is made before continuing.
allDone.WaitOne();
}
}
catch (Exception ex)
{
}
}
/// <summary>
/// Callback which occurs when a client connects to the server
/// </summary>
/// <param name="ar"></param>
public void ClientConnectCallback(IAsyncResult ar)
{
// Signal the main thread to continue.
allDone.Set();
try
{
// Get the socket that handles the client request.
Socket listener = (Socket)ar.AsyncState;
Socket handler = listener.EndAccept(ar);
// Create the state object.
StateObject state = new StateObject();
state.guid = Guid.NewGuid().ToString();
state.workSocket = handler;
ClientSockets.Add(state);
Console.WriteLine("Client connected : " + state.guid + " Count : " + ClientSockets.Count);
handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ClientDataCallback), state);
}
catch (ObjectDisposedException)
{
}
catch (SocketException se)
{
}
}
/// <summary>
/// Callback for when a client sends data to the server
/// </summary>
/// <param name="ar"></param>
public void ClientDataCallback(IAsyncResult ar)
{
try
{
String content = String.Empty;
// Retrieve the state object and the handler socket
// from the asynchronous state object.
StateObject state = (StateObject)ar.AsyncState;
Socket handler = state.workSocket;
// Read data from the client socket.
int bytesRead = handler.EndReceive(ar);
if (bytesRead == 0)
{
//Client has disconnected
Console.WriteLine("Client disconnected : {0}", state.guid);
CleanAndRemoveClient(state);
}
if (bytesRead > 0)
{
// There might be more data, so store the data received so far.
state.sb.Append(Encoding.ASCII.GetString(
state.buffer, 0, bytesRead));
// Check for newline, If it is not there, read
// more data.
content = state.sb.ToString();
if (content.EndsWith(Environment.NewLine) == true)
{
// All the data has been read from the
// client. Display it on the console.
Console.WriteLine("Read {0} bytes from socket. \n Data : {1} GUID : {2}",
content.Length, content, state.guid);
// Echo the data back to the client.
//Send(handler, content);
state.sb = new StringBuilder();
}
else
{
// Not all data received. Get more.
handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ClientDataCallback), state);
}
}
}
catch (Exception ex)
{
CleanAndRemove();
}
}
/// <summary>
/// Not implemented
/// </summary>
/// <param name="handler"></param>
/// <param name="data"></param>
//private static void Send(Socket handler, String data)
//{
// // Convert the string data to byte data using ASCII encoding.
// byte[] byteData = Encoding.ASCII.GetBytes(data);
// // Begin sending the data to the remote device.
// handler.BeginSend(byteData, 0, byteData.Length, 0,
// new AsyncCallback(SendCallback), handler);
//}
/// <summary>
/// Sends data to a client based on the passed GUID to identify the client
/// </summary>
/// <param name="guid"></param>
/// <param name="data"></param>
private void SendToClient(string guid, string data)
{
// Convert the string data to byte data using ASCII encoding.
byte[] byteData = Encoding.ASCII.GetBytes(data);
foreach (StateObject so in ClientSockets)
{
if (so.guid == guid)
{
if (so.workSocket.Connected)
{
// Begin sending the data to the remote device.
so.workSocket.BeginSend(byteData, 0, byteData.Length, 0,
new AsyncCallback(SendCallback), so.workSocket);
}
}
}
}
/// <summary>
/// Overload - send byte data to a client
/// </summary>
/// <param name="guid"></param>
/// <param name="data"></param>
private void SendToClient(string guid, byte[] data)
{
foreach (StateObject so in ClientSockets)
{
if (so.guid == guid)
{
if (so.workSocket.Connected)
{
// Begin sending the data to the remote device.
so.workSocket.BeginSend(data, 0, data.Length, 0,
new AsyncCallback(SendCallback), so.workSocket);
}
}
}
}
/// <summary>
/// Callback when a send completes to a client
/// </summary>
/// <param name="ar"></param>
private void SendCallback(IAsyncResult ar)
{
try
{
// Retrieve the socket from the state object.
Socket handler = (Socket)ar.AsyncState;
// Complete sending the data to the remote device.
int bytesSent = handler.EndSend(ar);
Console.WriteLine("Sent {0} bytes to client.", bytesSent);
//handler.Shutdown(SocketShutdown.Both);
//handler.Close();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
/// <summary>
/// Closes, disposes and removes a disconnected client from the ClientSockets arrayList
/// </summary>
/// <param name="state"></param>
private void CleanAndRemoveClient(StateObject state)
{
lock (ClientSockets.SyncRoot)
{
for (int i = ClientSockets.Count - 1; i >= 0; i--)
{
StateObject so = (StateObject)ClientSockets[i];
if (so == state)
{
so.workSocket.Close();
so.workSocket.Dispose();
ClientSockets.RemoveAt(i);
}
}
}
}
/// <summary>
/// Closes, disposes and removes a any disconnected clients from the ClientSockets arrayList
/// </summary>
/// <param name="state"></param>
private void CleanAndRemove()
{
lock (ClientSockets.SyncRoot)
{
for (int i = ClientSockets.Count - 1; i >= 0; i--)
{
StateObject so = (StateObject)ClientSockets[i];
if (so.workSocket.Connected == false)
{
so.workSocket.Close();
so.workSocket.Dispose();
ClientSockets.RemoveAt(i);
}
}
}
}
}