Skip to content

Commit 5a097e8

Browse files
Added sockets limit.
1 parent 9999d1e commit 5a097e8

File tree

4 files changed

+29
-6
lines changed

4 files changed

+29
-6
lines changed

sockets/CFunctions.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ int CFunctions::sockOpen(lua_State* luaVM)
2626
// Make sure host is a string, and port is a number
2727
if ( lua_type ( luaVM, 1 ) == LUA_TSTRING && lua_type ( luaVM, 2 ) == LUA_TNUMBER )
2828
{
29+
// Socket limit exceeded ?
30+
if ( CSocketManager::SocketLimitExceeded ( ) )
31+
{
32+
lua_pushboolean(luaVM, false);
33+
return 1;
34+
}
35+
2936
// Put the host in a string, and the port in an unsigned short
3037
string strHost = lua_tostring ( luaVM, 1 );
3138
unsigned short usPort = static_cast < unsigned short > ( lua_tonumber ( luaVM, 2 ) );

sockets/CFunctions.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@ class CFunctions;
2222
#define __CFUNCTIONS_H
2323

2424
#include <stdio.h>
25-
2625
#include "ml_sockets.h"
27-
#include "include/ILuaModuleManager.h"
28-
extern ILuaModuleManager10 *pModuleManager;
2926

3027
class CFunctions
3128
{

sockets/CSocketManager.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,25 @@ void CSocketManager::DoPulse()
2828
}
2929
}
3030

31-
void CSocketManager::SocketAdd(CSocket*& pSocket, bool bListen )
31+
bool CSocketManager::SocketLimitExceeded ( void )
32+
{
33+
unsigned int uiSockets = vecSockets.size();
34+
if ( uiSockets > MAX_SOCKETS )
35+
{
36+
pModuleManager->ErrorPrintf ( "[Sockets] Sockets limit reached! (Max: %i)\n", MAX_SOCKETS );
37+
return true;
38+
}
39+
40+
return false;
41+
}
42+
43+
bool CSocketManager::SocketAdd(CSocket*& pSocket, bool bListen )
3244
{
3345
// Add the socket to the loop stuff
3446
sSocketsStorage* pSocketStorage = new sSocketsStorage ( pSocket, bListen );
3547
vecSockets.push_back ( pSocketStorage );
48+
49+
return true;
3650
}
3751

3852
bool CSocketManager::SocketRemove(CSocket*& pSocket)

sockets/CSocketManager.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@
1212
class CSocket;
1313

1414
#include <vector>
15-
#include "ml_sockets.h"
15+
#include "ml_sockets.h"
16+
#include "include/ILuaModuleManager.h"
17+
extern ILuaModuleManager10 *pModuleManager;
18+
19+
#define MAX_SOCKETS 255
1620

1721
struct sSocketsStorage
1822
{
@@ -31,7 +35,8 @@ class CSocketManager
3135
public:
3236
static void DoPulse ();
3337

34-
static void SocketAdd ( CSocket*& pSocket, bool bListen = false );
38+
static bool SocketLimitExceeded ( void );
39+
static bool SocketAdd ( CSocket*& pSocket, bool bListen = false );
3540
static bool SocketRemove( CSocket*& pSocket );
3641
static bool GetSocket ( CSocket*& pSocket, void* pUserdata );
3742

0 commit comments

Comments
 (0)