Skip to content

Commit a076bec

Browse files
committed
Replaced select() with poll() in EHS
1 parent bd4e0a8 commit a076bec

File tree

9 files changed

+75
-75
lines changed

9 files changed

+75
-75
lines changed

Server/dbconmy/StdInc.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#pragma message("Compiling precompiled header.\n")
33

44
#include <windows.h>
5-
#include <winsock.h>
65
#include <mmsystem.h>
76
#endif
87

Server/mods/deathmatch/StdInc.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#ifdef WIN32
22
#pragma message("Compiling precompiled header.\n")
33

4+
#include <winsock2.h>
45
#include <windows.h>
5-
#include <winsock.h>
66
#include <mmsystem.h>
77
#endif
88

@@ -305,4 +305,4 @@ struct SAclRequest;
305305
#include "../../version.h"
306306

307307
extern CNetServer* g_pRealNetServer;
308-
extern CGame* g_pGame;
308+
extern CGame* g_pGame;

Server/mods/deathmatch/logic/ASE.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ class ASE;
2020

2121
#ifdef WIN32
2222
#include <conio.h>
23-
#include <winsock.h>
2423
#define sockclose closesocket
2524
#else
2625
#include <sys/socket.h>

Server/mods/deathmatch/logic/CLanBroadcast.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ class CLanBroadcastDLL;
2222

2323
#ifdef WIN32
2424
#include <conio.h>
25-
#include <winsock.h>
2625
#else
2726
#include <sys/socket.h>
2827
#include <sys/stat.h>

vendor/ehs/CPollFdSet.h

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
//
2+
// CPollFdSet.h
3+
//
4+
// Array of file descriptors for poll()
5+
//
6+
7+
class CPollFdSet
8+
{
9+
public:
10+
// FD_ZERO
11+
void Reset( void )
12+
{
13+
m_uiFdCount = 0;
14+
}
15+
16+
// FD_SET
17+
// Assumes the fd has not been added already
18+
void Add( int fd, short events )
19+
{
20+
unsigned int i = m_uiFdCount;
21+
if ( i < FD_ARRAY_LENGTH )
22+
{
23+
m_FdArray[i].fd = fd;
24+
m_FdArray[i].events = events;
25+
m_FdArray[i].revents = 0;
26+
m_uiFdCount = i + 1;
27+
}
28+
}
29+
30+
// FD_ISSET
31+
// Check if event has been triggered for the fd
32+
bool IsSet( int fd, short events ) const
33+
{
34+
for( unsigned int i = 0 ; i < m_uiFdCount ; i++ )
35+
{
36+
if ( m_FdArray[i].fd == fd &&
37+
(m_FdArray[i].events & events) != 0 )
38+
{
39+
return true;
40+
}
41+
}
42+
return false;
43+
}
44+
45+
unsigned int GetFdCount( void ) const
46+
{
47+
return m_uiFdCount;
48+
}
49+
50+
pollfd* GetFdArray( void )
51+
{
52+
return m_FdArray;
53+
}
54+
55+
protected:
56+
const static int FD_ARRAY_LENGTH = 2048;
57+
unsigned int m_uiFdCount;
58+
pollfd m_FdArray[FD_ARRAY_LENGTH];
59+
};

vendor/ehs/ehs.cpp

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,11 @@ static bool MUTEX_TRY_LOCK( MUTEX_TYPE& x )
4040
int EHSServer::CreateFdSet ( )
4141
{
4242
MUTEX_LOCK ( m_oMutex );
43-
// don't lock mutex, as it is only called from within a locked section
4443

45-
FD_ZERO( &m_oReadFds );
44+
m_oReadFds.Reset();
4645

4746
// add the accepting FD
48-
FD_SET( m_poNetworkAbstraction->GetFd ( ), &m_oReadFds );
47+
m_oReadFds.Add( m_poNetworkAbstraction->GetFd(), POLLIN );
4948

5049
int nHighestFd = m_poNetworkAbstraction->GetFd ( );
5150

@@ -61,7 +60,7 @@ int EHSServer::CreateFdSet ( )
6160

6261
EHS_TRACE ( "Adding %d to FD SET\n", nCurrentFd );
6362

64-
FD_SET ( nCurrentFd, &m_oReadFds );
63+
m_oReadFds.Add( nCurrentFd, POLLIN );
6564

6665
// store the highest FD in the set to return it
6766
if ( nCurrentFd > nHighestFd ) {
@@ -889,22 +888,9 @@ void EHSServer::HandleData ( int inTimeoutMilliseconds, ///< milliseconds for ti
889888

890889
MUTEX_UNLOCK ( m_oMutex );
891890

892-
// set up the timeout and normalize
893-
timeval tv = { 0, inTimeoutMilliseconds * 1000 };
894-
tv.tv_sec = tv.tv_usec / 1000000;
895-
tv.tv_usec %= 1000000;
896-
897-
// create the FD set for select
898-
int nHighestFd = CreateFdSet ( );
899-
900-
// call select
901-
//fprintf ( stderr, "##### [%d] Calling select\n", inThreadId );
902-
int nSocketCount = select ( nHighestFd + 1,
903-
&m_oReadFds,
904-
NULL,
905-
NULL,
906-
&tv );
907-
//fprintf ( stderr, "##### [%d] Done calling select\n", inThreadId );
891+
// create the FD set for poll
892+
CreateFdSet();
893+
int nSocketCount = poll( m_oReadFds.GetFdArray(), m_oReadFds.GetFdCount(), inTimeoutMilliseconds );
908894

909895
// handle select error
910896
#ifdef _WIN32
@@ -971,7 +957,7 @@ void EHSServer::CheckAcceptSocket ( )
971957
{
972958

973959
// see if we got data on this socket
974-
if ( FD_ISSET ( m_poNetworkAbstraction->GetFd ( ), &m_oReadFds ) ) {
960+
if ( m_oReadFds.IsSet( m_poNetworkAbstraction->GetFd(), POLLIN ) ) {
975961

976962

977963
//printf ( "Accept new connection\n");
@@ -1031,7 +1017,7 @@ void EHSServer::CheckClientSockets ( )
10311017
i != m_oEHSConnectionList.end ( );
10321018
i++ ) {
10331019

1034-
if ( FD_ISSET ( (*i)->GetNetworkAbstraction ( )->GetFd ( ), &m_oReadFds ) ) {
1020+
if ( m_oReadFds.IsSet( (*i)->GetNetworkAbstraction()->GetFd(), POLLIN ) ) {
10351021

10361022
if ( MUTEX_TRY_LOCK ( (*i)->m_oConnectionMutex ) == false )
10371023
continue;

vendor/ehs/ehs.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
#pragma warning(disable : 4786)
5050

5151
// to use winsock2.h instead of winsock.h
52+
#include <winsock2.h>
5253
#include <windows.h>
5354

5455
#include <time.h>
@@ -473,8 +474,7 @@ class EHSServer {
473474
int CreateFdSet ( );
474475

475476
/// this is the read set for sending to select(2)
476-
fd_set m_oReadFds;
477-
int m_TestPadding[2000];
477+
CPollFdSet m_oReadFds;
478478

479479
/// List of all connections currently attached to the server
480480
EHSConnectionList m_oEHSConnectionList;

vendor/ehs/socket.cpp

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,6 @@ Zac Hansen ( xaxxon@slackworks.com )
4242
#endif
4343
#endif // sun
4444

45-
#ifdef _WIN32
46-
#include <Winsock2.h>
47-
#endif
48-
#if !defined(_WIN32) || _WIN32_WINNT >= 0x600
49-
#define USE_POLL
50-
#pragma message ("EHS Socket using poll()")
51-
#ifdef _WIN32
52-
#define poll WSAPoll
53-
#else
54-
#include <sys/poll.h>
55-
#endif
56-
#endif
5745

5846
#include "socket.h"
5947

@@ -337,21 +325,10 @@ void Socket::SetReuseAddress( bool bOn )
337325

338326
bool Socket::IsReadable( int inTimeoutMilliseconds )
339327
{
340-
#ifndef USE_POLL
341-
timeval tv = { 0, inTimeoutMilliseconds * 1000 };
342-
tv.tv_sec = tv.tv_usec / 1000000;
343-
tv.tv_usec %= 1000000;
344-
fd_set rfds;
345-
FD_ZERO(&rfds);
346-
FD_SET(nAcceptSocket, &rfds);
347-
// See if socket it writable
348-
int ret = select(nAcceptSocket+1, &rfds, NULL, NULL, &tv);
349-
#else
350328
pollfd fds[1] = { 0 };
351329
fds[0].fd = nAcceptSocket;
352330
fds[0].events = POLLIN;
353331
int ret = poll(fds, 1, inTimeoutMilliseconds);
354-
#endif
355332
if (ret == 0)
356333
return false; // Not readable yet
357334
if (ret == -1)
@@ -362,21 +339,10 @@ bool Socket::IsReadable( int inTimeoutMilliseconds )
362339

363340
bool Socket::IsWritable( int inTimeoutMilliseconds )
364341
{
365-
#ifndef USE_POLL
366-
timeval tv = { 0, inTimeoutMilliseconds * 1000 };
367-
tv.tv_sec = tv.tv_usec / 1000000;
368-
tv.tv_usec %= 1000000;
369-
fd_set wfds;
370-
FD_ZERO(&wfds);
371-
FD_SET(nAcceptSocket, &wfds);
372-
// See if socket it writable
373-
int ret = select(nAcceptSocket+1, NULL, &wfds, NULL, &tv);
374-
#else
375342
pollfd fds[1] = { 0 };
376343
fds[0].fd = nAcceptSocket;
377344
fds[0].events = POLLOUT;
378345
int ret = poll(fds, 1, inTimeoutMilliseconds);
379-
#endif
380346
if (ret == 0)
381347
return false; // Not writable yet
382348
if (ret == -1)
@@ -387,21 +353,10 @@ bool Socket::IsWritable( int inTimeoutMilliseconds )
387353

388354
bool Socket::IsAtError( int inTimeoutMilliseconds )
389355
{
390-
#ifndef USE_POLL
391-
timeval tv = { 0, inTimeoutMilliseconds * 1000 };
392-
tv.tv_sec = tv.tv_usec / 1000000;
393-
tv.tv_usec %= 1000000;
394-
fd_set efds;
395-
FD_ZERO(&efds);
396-
FD_SET(nAcceptSocket, &efds);
397-
// See if socket it writable
398-
int ret = select(nAcceptSocket+1, NULL, NULL, &efds, &tv);
399-
#else
400356
pollfd fds[1] = { 0 };
401357
fds[0].fd = nAcceptSocket;
402358
fds[0].events = POLLERR;
403359
int ret = poll(fds, 1, inTimeoutMilliseconds);
404-
#endif
405360
if (ret == 0)
406361
return false; // Not error
407362
if (ret == -1)

vendor/ehs/socket.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ Zac Hansen ( xaxxon@slackworks.com )
3838
#pragma warning(disable : 4786)
3939

4040
// to use winsock2.h instead of winsock.h
41-
#include <winsock.h>
41+
#include <winsock2.h>
4242
#include <windows.h>
4343
#include <time.h>
4444

@@ -58,16 +58,19 @@ Zac Hansen ( xaxxon@slackworks.com )
5858
#include <sys/ioctl.h>
5959
#include <unistd.h>
6060
#include <string.h>
61+
#include <sys/poll.h>
6162

6263
///////////////////////////////////
6364
#endif // end platform headers //
6465
///////////////////////////////////
6566

67+
#include "CPollFdSet.h"
6668
#include "networkabstraction.h"
6769

6870
#ifdef _WIN32
6971
#define E_WOULDBLOCK WSAEWOULDBLOCK
7072
#define GetLastSocketError() WSAGetLastError()
73+
#define poll(fds,nfds,timeout) WSAPoll(fds,nfds,timeout)
7174
#else
7275
#define E_WOULDBLOCK EWOULDBLOCK
7376
#define GetLastSocketError() errno

0 commit comments

Comments
 (0)