summaryrefslogtreecommitdiff
path: root/modules/socketengines/m_socketengine_select.cpp
blob: 13791d9266c8b86e347346bf5ffc2bc1ac5823c0 (plain)
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include "module.h"

class SocketEngineSelect : public SocketEngineBase
{
 private:
 	/* Max Read FD */
	int MaxFD;
	/* Read FDs */
	fd_set ReadFDs;
	/* Write FDs */
	fd_set WriteFDs;

 public:
	SocketEngineSelect()
	{
		MaxFD = 0;
		FD_ZERO(&ReadFDs);
		FD_ZERO(&WriteFDs);
	}

	~SocketEngineSelect()
	{
		FD_ZERO(&ReadFDs);
		FD_ZERO(&WriteFDs);
	}

	void AddSocket(Socket *s)
	{
		if (s->GetSock() > MaxFD)
			MaxFD = s->GetSock();
		FD_SET(s->GetSock(), &ReadFDs);
		Sockets.insert(std::make_pair(s->GetSock(), s));
	}

	void DelSocket(Socket *s)
	{
		if (s->GetSock() == MaxFD)
			--MaxFD;
		FD_CLR(s->GetSock(), &ReadFDs);
		FD_CLR(s->GetSock(), &WriteFDs);
		Sockets.erase(s->GetSock());
	}

	void MarkWritable(Socket *s)
	{
		if (s->HasFlag(SF_WRITABLE))
			return;
		FD_SET(s->GetSock(), &WriteFDs);
		s->SetFlag(SF_WRITABLE);
	}

	void ClearWritable(Socket *s)
	{
		if (!s->HasFlag(SF_WRITABLE))
			return;
		FD_CLR(s->GetSock(), &WriteFDs);
		s->UnsetFlag(SF_WRITABLE);
	}

	void Process()
	{
		fd_set rfdset = ReadFDs, wfdset = WriteFDs, efdset = ReadFDs;
		timeval tval;
		tval.tv_sec = Config->ReadTimeout;
		tval.tv_usec = 0;

		int sresult = select(MaxFD + 1, &rfdset, &wfdset, &efdset, &tval);
		Anope::CurTime = time(NULL);

		if (sresult == -1)
		{
#ifdef WIN32
			errno = WSAGetLastError();
#endif
			Log() << "SockEngine::Process(): error: " << strerror(errno);
		}
		else if (sresult)
		{
			for (std::map<int, Socket *>::const_iterator it = Sockets.begin(), it_end = Sockets.end(); it != it_end; ++it)
			{
				Socket *s = it->second;

				if (s->HasFlag(SF_DEAD))
					continue;
				if (FD_ISSET(s->GetSock(), &efdset))
				{
					s->ProcessError();
					s->SetFlag(SF_DEAD);
					continue;
				}
				if (FD_ISSET(s->GetSock(), &rfdset) && !s->ProcessRead())
					s->SetFlag(SF_DEAD);
				if (FD_ISSET(s->GetSock(), &wfdset) && !s->ProcessWrite())
					s->SetFlag(SF_DEAD);
			}

			for (std::map<int, Socket *>::iterator it = Sockets.begin(), it_end = Sockets.end(); it != it_end; )
			{
				Socket *s = it->second;
				++it;

				if (s->HasFlag(SF_DEAD))
					delete s;
			}
		}
	}
};

class ModuleSocketEngineSelect : public Module
{
	SocketEngineSelect engine;

 public:
	ModuleSocketEngineSelect(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator)
	{
		this->SetAuthor("Anope");
		this->SetPermanent(true);
		this->SetType(SOCKETENGINE);

		SocketEngine = &engine;
	}

	~ModuleSocketEngineSelect()
	{
		SocketEngine = NULL;
	}
};

MODULE_INIT(ModuleSocketEngineSelect)