开发者

Storing Socket Information Random Access Speed

Current situation: I have a linux server (for chatting with strangers) set up and I have a question on efficiency.

Currently I'm using a map in a simple manager class to pair up two strangers:

int sockManager::set_pair(int me, int them) {
 if (them != -1) {
    pairs[me] = them;
    pairs[them] = me;
    return 1;
 }
 return -1;
}
int sockManager::get_pair(int me) {
 return pairs[me];
}
void sockManager::add_single(int me) {
 pairs[me] = -1;
}
void sockManager::remove_single(int me) {
 if (pairs[me] != -1)
    pairs[pairs[me]] = -1;
 pairs.erase(me);
}
int sockManager::find_unconnected(int me) {
 if (pairs[me] != -1)
 return pairs[me];
 for (iter = pairs.begin();iter!=pairs.end();iter++){
  if (iter->second == -1 && iter->first != me)
  return iter->first;
 }
 return -1;
}
int sockManager::get_size(){
 return pairs.size();
}

The reason I'm using this manager class is for expandability. For instance at this moment I want to change the way I find two clients开发者_开发知识库 to connect. I'd like to have a "needs to connect" flag, and a simple thread to constantly run through and find clients to connect, when a client can't be connected, if the time has been more than x seconds, send them a funny saying or something (keep them entertained) I also need to make sure that a client isn't connecting to someone with the same IP address as theirs...

Here's the question: I'm using a map to store each socket and it's corresponding partner. Is there another, faster, way to store each socket and partner combination? (for random access, like the speed to lookup an int[socket] vs the map method.) {Whether using an array of structs for pairs was my initial question, then I realized that would be way dumb... The file descriptors don't go up by 1 each time so it would be completely useless to think an array would speed things up}

Subjective(ish) question: I would like to store flags for each connection, do you think the best and most easily expandable method would be to make the value (as opposed to key) of the map pair into a struct holding the necessary info (partner socket, flag to connect, flag to ...) or should I make another map with specifically the flag members as the value? Memory won't be an issue.

Third More Solid [&& slightly unrelated] question: I am using an FD_Set with the select() function, and I've read it's quite slow with large numbers of FD's (I know it can handle a max of 1024, but the same applies to poll()) How would I go about implementing an event-based system to listen for incoming data from users? (OS: ubuntu linux) and How much faster will it be? I've stress tested my server as it is and I can handle several hundred clients across the network without a noticeable slowdown on the user's side. I'd like to support upwards of 3k at a time.

I figured out more by asking the question and realizing how many dumb questions I was about to ask than reading up on stuff for hours...


How would I go about implementing an event-based system to listen for incoming data from users? (OS: ubuntu linux) and How much faster will it be? I've stress tested my server as it is and I can handle several hundred clients across the network without a noticeable slowdown on the user's side. I'd like to support upwards of 3k at a time.

Typically, people trying to solve the c10k problem will use an event-handling loop as provided by libevent or MTasker. Threading will typically get you in the low-hundreds without too much hassle, but at some point the memory usage gets very restrictive. Event-based programming typically lets servers scale into the thousands, but it depends on many factors.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜