How to disable abridging in an ArrayList object?

Hi!

Let's say I have this object:

private ArrayList aryClients; // list of Client Connections

Now, on every client connection that my server gets I add a Client object to that list.

It's all good, but think of this scariano:

4 clients connected. Now aryClients has 4 items from index 0 to 3.

The 3rd client who's Client object is in aryClients has disconnected, so item 2 was removed.

Now this is where we're getting into my problem; I want that item 2 will stay null and item 3 (of the 4th client) will stay at index 3. Why you're asking Because I need to have quick access to clients' info in my server (it's gonna be a game server), and making a search (thought about the Windows handle of the client's sock - uniqe) everytime is simply foolish.

Therefore, if i'd save the index value that aryClients.Add() gives me, my life would be much more happier .

But this is the problem (again) - the index changes

I thought about using a simple array, like:

public Client[] aryC;

....................

aryC = new Client[100000];

But I'm sure C# has a solution for me... Shame there's no property like "AutoAbridge" in the ArrayList class.




Answer this question

How to disable abridging in an ArrayList object?

  • Rozee

    Yes it's an idea, BUT what happens after 2 weeks/months the server is running Manyyy clients have dis/connected so the index value might bring an exception at some point.

    Between opening this thread and writing this msg, I managed to "solve" this problem by using a simple array:

    public Client[] Clients;

    .........

    Clients = new Client[100000];

    I made my own function to add a new client and the FULL code (I put it inside a class) looks like:

    internal class ClientsList

    {

    public Client[] Clients;

    private int Clients_CurrentId = 0;

    private int iCount = 0;

    public int Count

    {

    get { return iCount; }

    }

    public ClientsList(int Limit)

    {

    Clients = new Client[Limit];

    }

    public int Add(Client c)

    { // returns index id

    lock (Clients)

    {

    if (Clients_CurrentId == Clients.Length)

    Clients_CurrentId = 0;

    if (Clients[Clients_CurrentId] != null) // should we search for free slot

    { // yes we need :(

    int iTmp = lClients_SearchForFreeSlot();

    if (iTmp != -1)

    Clients_CurrentId = iTmp;

    else

    return -1; // no free slot found so return -1

    }

    Clients[Clients_CurrentId] = c;

    Clients_CurrentId++;

    }

    iCount++;

    return (Clients_CurrentId - 1); // minus 1 coz we just raised it by 1 !

    }

    private int lClients_SearchForFreeSlot()

    {

    bool bFoundFreeId = false;

    int i = 0;

    for (; i < Clients.Length; i++)

    {

    if (ClientsIdea == null)

    {

    bFoundFreeId = true;

    break;

    }

    }

    if (!bFoundFreeId)

    return -1; // no free slot was found :(

    return i;

    }

    public void Remove(int index)

    {

    Clients[index] = null;

    iCount--;

    }

    }

    Well, I hope some1 here give me a ".NET pre-made solution" to implement but for now I'll use this code.. Hope it will be helpful for others.



  • hingos

    By the way, in general, if I'm already talking here, is this a proper way to create a gaming server

    Ok so it's a noobish question but hey if MS decided to allow me to post questions then why not do it all the way

    I never expirienced in building complicated server such as this one. Maybe I'm over my head hehe



  • Angry Coder

    Index changes because you remove ArrayList item. It you what to keep items with the same indexes, don't remove them. When client disconnected, set it's reference in the list to null instead of removing it.
  • Erkan Yilmaz

    It seems to me that using a Hashtable would be more appropriate. You would use the unique value as the key for the user. This would not take up the extra unneeded space and would grow as needed.

  • IWY

    Alex, just to make it simple for you to see what I mean and maybe even understand the code I just posted;

    This makes a 100k users server. On a new client event it saves the Client object in this ClientsList object. If the whole 100k items in the array are not null it means the server is full.

    This solution makes my loops inside the server bigger because now it needs to for through 100k items, BUT, it solved the issue.



  • LeeroyB

    I never used Hashtables.. Do you know any online doc for me to read about it Thanks tho!

  • How to disable abridging in an ArrayList object?