File: socketmanager.cpp

package info (click to toggle)
dc-qt 0.2.0.alpha-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,936 kB
  • ctags: 5,361
  • sloc: cpp: 28,936; makefile: 19
file content (72 lines) | stat: -rw-r--r-- 1,152 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include "socketmanager.h"


using namespace std;

namespace rpc {

SocketManager* SocketManager::inst = 0;

SocketManager::SocketManager()
{
}

SocketManager::~SocketManager()
{
}

void SocketManager::addSocket( Socket *s )
{
	sockets.insert(s);
}

void SocketManager::removeSocket( Socket *s )
{
	sockets.erase(s);
}

void SocketManager::select( )
{
	set<Socket*> sl;
	fd_set fds;
	FD_ZERO(&fds);
	int maxfd = -1;
	set<Socket*>::iterator it = sockets.begin();
	while(it!=sockets.end()) {
		int fd = (*it)->getFd();
		FD_SET(fd,&fds);
		if(maxfd < fd) maxfd = fd;
		sl.insert(*it);		
		it++;
	}
	int nEvents = ::select( maxfd+1, &fds,NULL,NULL,NULL );
	if(nEvents>0) {
		
		for(it = sl.begin();it!=sl.end();it++) {
			int fd  = (*it)->getFd();
			if(FD_ISSET(fd,&fds)) (*it)->onRead();
		}

	} else if(nEvents==-1) {
		perror("select():");
	}
}

Socket* SocketManager::findSocket( int socketId )
{
	Socket* ret = NULL;
	
	std::set<Socket*>::const_iterator socketIt = sockets.begin();
	while( socketIt != sockets.end() ) {
		
		if( (*socketIt)->getId() == socketId ) {
			ret = *socketIt;
			break;
		}
		++socketIt;
	}
	return ret;
}

}	// namespace