Sign in to follow this  

How do I make sure that client requesting data has been logged in?

This topic is 2774 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm using raknet (3.73). I have a server and client log into the server. I want only clients that have been logged in to be able to request data from the server. However, I do not know of a simple way of doing this other than checking each incoming packet and comparing them against my 'authenticated user's list'. Is there a simpler way of making sure that clients requesting data are actually logged in?

Share this post


Link to post
Share on other sites
Basically what you can do is create a client context class and associate it with the client GUID. You create a new object for each connection that is accepted. Whenever you receive a packet, you obtain the client context object for that GUID. From there, you can check against its internal state. When the connection is lost, you remove the client context object.

Here's some short snippets I threw together that should get you pointed in the right direction:

...

struct ClientContext
{
RakNetGUID guid;
SystemAddress clientID;
bool isLoggedIn;

ClientContext(const RakNetGUID & guid_, const SystemAddress & clientID_)
: guid(guid_), isLoggedIn(false), clientID(clientID_)
{
}
};

...

std::map<uint64_t, ClientContext *> clients;

// If you are going to be running a multi-threaded setup where 'clients' might
// be accessed from different threads, then you need to create a
// synchronization object to serialize access!

...

// Inside the event handling loop for RakNet (I'm using the chat example as base code)
case ID_NEW_INCOMING_CONNECTION:
{
ClientContext * client = new ClientContext(p->guid, p->systemAddress);
clients.insert(std::make_pair(p->guid.g, client));
} break;

...

case ID_DISCONNECTION_NOTIFICATION:
// You will want to handle ID_CONNECTION_LOST and any others that result in disconnection as well.
{
std::map<uint64_t, ClientContext *>::iterator itr = clients.find(p->guid.g);
if(itr == clients.end())
break;
ClientContext * client = itr->second;
clients.erase(itr);
// Execute any logic before the client is deleted as needed
delete client;
} break;

...

// For any of your defined packets
case ID_USER_PACKET_ENUM:
{
std::map<uint64_t, ClientContext *>::iterator itr = clients.find(p->guid.g);
if(itr == clients.end())
break; // Ignore packet, sanity check
ClientContext * client = itr->second;

// Now use 'client' as you need, for example:

// This would go before any commands that require login. You do need to
// check the state for *each* one! You must always validate state when
// it comes to networking if you want to reduce the chance of your
// system being exploited.
if(client -> isLoggedIn == false)
{
server->CloseConnection(client -> clientID, true);
break;
}
} break;

...




Hope that helps!

Share this post


Link to post
Share on other sites

This topic is 2774 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this