using System;
using System.Collections.Generic;
using Uber.Core;
using Uber.Util;
using Uber.Messages;
namespace Uber.Net
{
///
/// Manages accepted IonTcpConnections and enables them to interact with the Ion environment.
///
public class TcpConnectionManager
{
#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 TcpConnectionListener 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 TcpConnectionManager(string sLocalIP, 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 TcpConnectionListener(sLocalIP, 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 TcpConnection GetConnection(uint clientID)
{
try { return mConnections[clientID]; }
catch { return null; }
}
///
/// Returns the IonTcpConnection listener instance.
///
public TcpConnectionListener 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(TcpConnection connection)
{
// TODO: check max simultaneous connections
// TODO: check max simultaneous connections per IP
// TODO: check project specific actions
// INFO: client ID = connection ID, client ID = session ID
// Add connection to collection
mConnections.Add(connection.ID, connection);
// Create session for new client
UberEnvironment.GetGame().GetClientManager().StartClient(connection.ID);
}
public void DropConnection(uint clientID)
{
TcpConnection connection = GetConnection(clientID);
if (connection != null)
{
//UberEnvironment.GetLogging().WriteInformation("Dropped IonTcpConnection [" + clientID + "] of " + connection.ipAddress);
connection.Stop();
mConnections.Remove(clientID);
}
}
public bool TestConnection(uint clientID)
{
TcpConnection connection = GetConnection(clientID);
if (connection != null)
return connection.TestConnection(); // Try to send data
return false; // Connection not here!
}
#endregion
}
}