Skip to content

Commit 9999d1e

Browse files
- Fixed sockOpen problems (Also sockOpen return values too).
- Added "bool sockIsConnected ( userdata usSocket )". - Changed structure of CSocketManager.
1 parent d7b0892 commit 9999d1e

File tree

6 files changed

+82
-30
lines changed

6 files changed

+82
-30
lines changed

sockets/CFunctions.cpp

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,23 @@ int CFunctions::sockOpen(lua_State* luaVM)
3434
// Create the socket
3535
CSocket* pSocket = new CSocket(luaVM, strHost, usPort);
3636
void* pUserdata = pSocket->GetUserdata();
37+
int iError = pSocket->GetLastSocketError ( );
3738

3839
// The socket has got a userdata value if successfully created. It doesn't otherwise
39-
if ( pUserdata == NULL || pSocket->IsConnected () == false )
40+
if ( pUserdata == NULL /*|| pSocket->IsConnected () == false*/ )
4041
{
42+
SAFE_DELETE ( pSocket );
43+
44+
/*
45+
* (x86) TODO: Make static error codes, because error
46+
* codes between Win and Linux are different.
47+
* Put it in an array inside SocketErrors.h
48+
* --
49+
*/
4150
lua_pushboolean ( luaVM, false );
42-
lua_pushnumber ( luaVM, pSocket->GetLastSocketError ( ) );
51+
lua_pushnumber ( luaVM, iError );
4352

44-
// Delete the socket now.
45-
SAFE_DELETE ( pSocket );
46-
return 1;
53+
return 2;
4754
}
4855

4956
// Add the socket to the Pulse list
@@ -89,6 +96,35 @@ int CFunctions::sockWrite(lua_State *luaVM)
8996
return 1;
9097
}
9198

99+
// bool sockIsConnected ( userdata usSocket )
100+
int CFunctions::sockIsConnected ( lua_State *luaVM )
101+
{
102+
if ( luaVM )
103+
{
104+
// Make sure the socket is an userdata value
105+
if ( lua_type ( luaVM, 1 ) == LUA_TLIGHTUSERDATA )
106+
{
107+
// Prepare vars
108+
void* pUserdata = lua_touserdata ( luaVM, 1 );
109+
CSocket* pSocket = NULL;
110+
111+
// Get the socket
112+
if ( CSocketManager::GetSocket ( pSocket, pUserdata ) )
113+
{
114+
// Remove it
115+
if ( pSocket )
116+
{
117+
lua_pushboolean ( luaVM, pSocket->IsConnected ( ) );
118+
return 1;
119+
}
120+
}
121+
}
122+
}
123+
124+
lua_pushboolean ( luaVM, false );
125+
return 1;
126+
}
127+
92128
// bool sockClose ( userdata usSocket )
93129
int CFunctions::sockClose(lua_State *luaVM)
94130
{
@@ -105,10 +141,11 @@ int CFunctions::sockClose(lua_State *luaVM)
105141
if (CSocketManager::GetSocket(pSocket, pUserdata))
106142
{
107143
// Remove it
108-
CSocketManager::SocketRemove(pSocket);
109-
110-
lua_pushboolean(luaVM, true);
111-
return 1;
144+
if ( CSocketManager::SocketRemove(pSocket) )
145+
{
146+
lua_pushboolean(luaVM, true);
147+
return 1;
148+
}
112149
}
113150
}
114151
}

sockets/CFunctions.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@ extern ILuaModuleManager10 *pModuleManager;
3030
class CFunctions
3131
{
3232
public:
33-
static int sockOpen (lua_State* luaVM);
34-
static int sockWrite (lua_State* luaVM);
35-
static int sockClose (lua_State* luaVM);
33+
static int sockOpen (lua_State* luaVM);
34+
static int sockWrite (lua_State* luaVM);
35+
static int sockIsConnected (lua_State* luaVM);
36+
static int sockClose (lua_State* luaVM);
3637

3738
static void AddEvent (lua_State* luaVM, const string& strEventName);
3839
};

sockets/CSocket.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,16 +163,16 @@ bool CSocket::ProcessTargetLocation(const string& strHost, const unsigned short&
163163
else
164164
{
165165
Hostent = gethostbyname(strHost.c_str());
166-
if (Hostent == NULL)
167-
{
168-
return false;
169-
}
170-
else
166+
if (Hostent != NULL)
171167
{
172168
memcpy(&(m_sSockAddr.sin_addr), Hostent->h_addr_list[0], 4);
173169
return true;
174170
}
171+
172+
Hostent = NULL;
175173
}
174+
175+
return false;
176176
}
177177

178178
void CSocket::SetNonBlocking()

sockets/CSocketManager.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99
#include "CSocketManager.h"
1010

1111
// Vector for holding all sockets
12-
vector <CSocket*> vecSockets;
12+
vector <sSocketsStorage*> vecSockets;
1313

1414
void CSocketManager::DoPulse()
1515
{
1616
// Loop through all sockets
1717
for (unsigned int i = 0; i < vecSockets.size(); ++i)
1818
{
1919
// Get the socket by the ID
20-
CSocket* pSocket = vecSockets[i];
20+
CSocket* pSocket = (CSocket *)vecSockets[i]->m_pSocket;
2121

2222
// Do a pulse at the socket
2323
if (!pSocket->DoPulse())
@@ -28,23 +28,24 @@ void CSocketManager::DoPulse()
2828
}
2929
}
3030

31-
void CSocketManager::SocketAdd(CSocket*& pSocket)
31+
void CSocketManager::SocketAdd(CSocket*& pSocket, bool bListen )
3232
{
3333
// Add the socket to the loop stuff
34-
vecSockets.push_back(pSocket);
34+
sSocketsStorage* pSocketStorage = new sSocketsStorage ( pSocket, bListen );
35+
vecSockets.push_back ( pSocketStorage );
3536
}
3637

3738
bool CSocketManager::SocketRemove(CSocket*& pSocket)
3839
{
3940
// Check if an socket was actually specified
40-
if (pSocket == NULL)
41+
if ( pSocket == NULL )
4142
return false;
4243

4344
// Loop through all sockets
4445
for (unsigned int i = 0; i < vecSockets.size(); ++i)
4546
{
4647
// If the current is the one we're looking for...
47-
if (vecSockets[i] == pSocket)
48+
if ((CSocket *)vecSockets[i]->m_pSocket == pSocket)
4849
{
4950
// Remove it from the vector and delete it, then return true
5051
vecSockets.erase(vecSockets.begin() + i);
@@ -66,10 +67,10 @@ bool CSocketManager::GetSocket(CSocket*& pSocket, void* pUserdata)
6667
for (unsigned int i = 0; i < vecSockets.size(); ++i)
6768
{
6869
// Compare the current socket's userdata with the one we're looking for
69-
if (vecSockets[i]->GetUserdata() == pUserdata)
70+
if ( (CSocket *)vecSockets[i]->m_pSocket->GetUserdata() == pUserdata )
7071
{
7172
// If it's the one we want, assign pSocket to it and return true
72-
pSocket = vecSockets[i];
73+
pSocket = (CSocket *)vecSockets[i]->m_pSocket;
7374
return true;
7475
}
7576
}
@@ -81,5 +82,5 @@ void CSocketManager::HandleStop()
8182
{
8283
// Triggered at module stop. Simply destroys all sockets
8384
for (unsigned int i = 0; i < vecSockets.size(); ++i)
84-
SAFE_DELETE(vecSockets[i]);
85+
SAFE_DELETE((CSocket *)vecSockets[i]->m_pSocket);
8586
}

sockets/CSocketManager.h

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,26 @@ class CSocket;
1414
#include <vector>
1515
#include "ml_sockets.h"
1616

17+
struct sSocketsStorage
18+
{
19+
CSocket* m_pSocket;
20+
bool m_bListen;
21+
22+
sSocketsStorage ( CSocket* pSocket, bool bListen = false )
23+
{
24+
m_pSocket = pSocket;
25+
m_bListen = bListen;
26+
}
27+
};
28+
1729
class CSocketManager
1830
{
1931
public:
20-
static void DoPulse();
32+
static void DoPulse ();
2133

22-
static void SocketAdd (CSocket*& pSocket);
23-
static bool SocketRemove(CSocket*& pSocket);
24-
static bool GetSocket (CSocket*& pSocket, void* pUserdata);
34+
static void SocketAdd ( CSocket*& pSocket, bool bListen = false );
35+
static bool SocketRemove( CSocket*& pSocket );
36+
static bool GetSocket ( CSocket*& pSocket, void* pUserdata );
2537

2638
static void HandleStop ();
2739
};

sockets/ml_sockets.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ MTAEXPORT void RegisterFunctions ( lua_State * luaVM )
5858
// Register functions
5959
pModuleManager->RegisterFunction ( luaVM, "sockOpen", CFunctions::sockOpen );
6060
pModuleManager->RegisterFunction ( luaVM, "sockWrite", CFunctions::sockWrite );
61+
pModuleManager->RegisterFunction ( luaVM, "sockIsConnected", CFunctions::sockIsConnected );
6162
pModuleManager->RegisterFunction ( luaVM, "sockClose", CFunctions::sockClose );
6263

6364
// Add events

0 commit comments

Comments
 (0)