using System;
using Aleeda.HabboHotel.Client;
using System.Collections.Generic;
using Aleeda.HabboHotel.Client.Utilities;
using Aleeda.HabboHotel.Messenger;
using Aleeda.Net.Messages;
using Aleeda.Storage;
namespace Aleeda.Net.Connections
{
///
/// Manages accepted IonTcpConnections and enables them to interact with the Ion environment.
///
public class IonTcpConnectionManager
{
#region Fields
///
/// A 32 bit integer that holds the maximum amount of connections in the connection manager.
///
private readonly int mMaxSimultaneousConnections;
///
/// A System.Collections.Generic.Dictionary with client IDs as keys and IonTcpConnections as values. This collection holds active IonTcpConnections.
///
private Dictionary mConnections;
private IonTcpConnectionListener mListener;
#endregion
#region Constructors
///
/// Constructs an instance of IonTcpConnectionManager, constructs an IonTcpConnectionListener, binds it to a given local IP and TCP port and sets the maximum amount of connections.
///
/// The local IP address to bind the listener to.
/// The TCP port number to bind the listener to.
/// The maximum amount of connections in the connection manager.
public IonTcpConnectionManager(int Port, int maxSimultaneousConnections)
{
int initialCapacity = maxSimultaneousConnections;
if (maxSimultaneousConnections > 4)
initialCapacity /= 4; // Set 1/4 of max connections as initial capacity to avoid too much resizing
mConnections = new Dictionary(initialCapacity);
mMaxSimultaneousConnections = maxSimultaneousConnections;
mListener = new IonTcpConnectionListener(Port, this);
}
#endregion
#region Methods
///
/// Destroys all resources in the connection manager.
///
public void DestroyManager()
{
mConnections.Clear();
mConnections = null;
mListener = null;
}
///
/// Returns true if the connection collection currently contains a connection with a given client ID.
///
/// The client ID to check.
public bool ContainsConnection(uint clientID)
{
return mConnections.ContainsKey(clientID);
}
///
/// Tries to return the IonTcpConnection instance of a given client ID. Null is returned if the connection is not in the manager.
///
/// The ID of the client to get connection of as an unsigned 32 bit integer.
public IonTcpConnection GetConnection(uint clientID)
{
try { return mConnections[clientID]; }
catch { return null; }
}
///
/// Returns the IonTcpConnection listener instance.
///
public IonTcpConnectionListener GetListener()
{
return mListener;
}
///
/// Handles a newly created IonTcpConnection and performs some checks, before adding it to the connection collection and starting the client session.
///
/// The IonTcpConnection instance representing the new connection to handle.
public void HandleNewConnection(IonTcpConnection connection)
{
// INFO: client ID = connection ID, client ID = session ID
// Add connection to collection
mConnections.Add(connection.ID, connection);
// Create session for new client
AleedaEnvironment.GetHabboHotel().GetClients().StartClient(connection.ID);
}
public void DropConnection(uint clientID)
{
IonTcpConnection connection = GetConnection(clientID);
if (connection != null)
{
//AleedaEnvironment.GetLog().WriteInformation("Dropped IonTcpConnection [" + clientID + "] of " + connection.ipAddress);
connection.Stop();
mConnections.Remove(clientID);
}
}
public bool TestConnection(uint clientID)
{
IonTcpConnection connection = GetConnection(clientID);
if (connection != null)
return connection.TestConnection(); // Try to send data
return false; // Connection not here!
}
#endregion
}
}