2

I'm in a situation where I have a session which gets an ID assigned by it with a simple counter. Well, after a lot of connections the counter will be at 500, even though there might be only 2 people connected. What I want to do is have a counter which flags numbers as free so they are re-usable! I don't have any idea how to tackle this though. I hope one of you might be able to help me out!

public void createSession(Socket gameClient)
{
    uint sessionID = mSessionCounter++;
    Session Session = new Session(sessionID, gameClient);
    mSessions.Add(sessionID, Session);
    CommandLine.WriteLine("Created session " + sessionID + " for " + Session.ipAddress, CommandLine.logType.sessionConnectionEvent);
    Session.Start();
}
Fabian Pas
  • 444
  • 5
  • 18
  • Is this for a web application? What technology? Why do you need to reuse the IDs? When do you consider an ID no longer in use? – Anders Arpi Nov 22 '12 at 15:15
  • why care if the number goes over 500. Why do you need to keep this low? – musefan Nov 22 '12 at 15:16
  • It's a server, I just think it would be more approriate to have the second user coming online have ID 2, if not 3, instead of 321. – Fabian Pas Nov 22 '12 at 15:16
  • How do you differentiate users from each other? By IP? By login? How will you know if someone has disconnected? If you want "active user count" you should not base it off an assigned ID. – Anders Arpi Nov 22 '12 at 15:16
  • 1
    Quite strange desire, but feasible, if you would be kind enough to post the code that assigns IDs. – J0HN Nov 22 '12 at 15:18
  • I run a alive check every 30 seconds also there is an IP. – Fabian Pas Nov 22 '12 at 15:18
  • I have edited the question, also why is it such strange thing? Is it really that weird!? – Fabian Pas Nov 22 '12 at 15:19
  • 1
    It's weird because it serves no obvious point whatsoever. Whether the number is 1 or 45201230 doesn't really matter to the computer, only to you. – Anders Arpi Nov 22 '12 at 15:19
  • Maybe try using a GUID instead, it will be unique AND it will remove your paranoia because there wont be any "missing" sequence values for you to worry about ;-) – musefan Nov 22 '12 at 15:20
  • Hmm, that is true. Every session gets a user assigned, I guess it's stupid after all! Paranoia, that's a good one. How would I close a question? – Fabian Pas Nov 22 '12 at 15:21
  • so... say 300 users connect. At one point most of the users have disconnected, but the ones with SessionID 295 and 300 are still connected. Would you like to change their SessionIDs to 1 and 2 while they are still connected? Or would you only like to reassign SessionID 1 to the next user who connects? – Paolo Falabella Nov 22 '12 at 15:25
  • Why not just use the IP address for the ID. Is is possible for more than one active game to come from a single IP address? If the counter blows thru the max value for the ID then it does matter to the computer so it is not just paranoia. GUID is statistically unique. – paparazzo Nov 22 '12 at 15:28
  • well, depending on how long-lived your server process is and how long a typical session is, you may in fact run out of sessionIds after 2,147,483,647 sessions.... So if you're in a situation where you think you may have many many short-lived sessions, some form of reuse of sessionIds may not be paranoia – Paolo Falabella Nov 22 '12 at 15:30
  • @Blam, I would also do a check to ensure the GUID hadn't already been used anyway... just to be super-safe – musefan Nov 22 '12 at 15:39
  • @Paolo, you could roll over to 0 in that case – Jan Nov 22 '12 at 15:39
  • Well, Paola, I indeed would like to reassign SessionID 1 to the next user who connects. Hmm, this food for thought! – Fabian Pas Nov 22 '12 at 15:54

1 Answers1

2

Just a quick idea of who you could do this. Although re-using may not be required in your case, it might still be something worth knowing how to do (plus having an answer to this question would be good).

If you need to check which session ID's are in use at any time, then you need to be able to track them. For this I would suggest a simple List would do fine, so lets start with something like:

static List<int> CurrentSessionIDs = new List<int>();

this should be globally accessible. To ensure this list is kept clean, in your function that checks for "stay alive" you can remove an item like so:

CurrentSessionIDs.Remove(sessionID);

Next, you want to work out the first available session number using your list as an exceptions list. You can do this with Linq as follows:

//this would start at 0 for the first session number
int newSessionNumber = Enumerable.Range(0, int.MaxValue).Except(CurrentSessionIDs).First();

Note: this bit of lovely Linq syntax for finding the first available number was inspired by this post (credit where it's due, etc.)

Community
  • 1
  • 1
musefan
  • 47,875
  • 21
  • 135
  • 185