Skip to content

Commit 3ccf218

Browse files
Reverted: r144, r145, r147, r148, r149, r150
1 parent 25128b5 commit 3ccf218

File tree

11 files changed

+93
-211
lines changed

11 files changed

+93
-211
lines changed

sockets/CFunctions.cpp

Lines changed: 11 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -18,52 +18,31 @@
1818

1919
#include "CFunctions.h"
2020

21-
// bool sockOpen ( string strHost, int usPort [, bool bListen = false ] )
2221
int CFunctions::sockOpen(lua_State* luaVM)
2322
{
2423
if ( luaVM )
2524
{
2625
// Make sure host is a string, and port is a number
27-
if ( lua_type ( luaVM, 1 ) == LUA_TSTRING && lua_type ( luaVM, 2 ) == LUA_TNUMBER )
26+
if (lua_type(luaVM, 1) == LUA_TSTRING && lua_type(luaVM, 2) == LUA_TNUMBER)
2827
{
29-
// Socket limit exceeded ?
30-
if ( CSocketManager::SocketLimitExceeded ( ) )
31-
{
32-
lua_pushboolean(luaVM, false);
33-
return 1;
34-
}
35-
3628
// Put the host in a string, and the port in an unsigned short
37-
string strHost = lua_tostring ( luaVM, 1 );
29+
string strHost = lua_tostring(luaVM, 1);
3830
unsigned short usPort = static_cast < unsigned short > ( lua_tonumber ( luaVM, 2 ) );
39-
//bool bListen = ( ( lua_type ( luaVM, 3 ) == LUA_TBOOLEAN ) ? lua_toboolean ( luaVM, 3 ) == 1 : false );
4031

4132
// Create the socket
4233
CSocket* pSocket = new CSocket(luaVM, strHost, usPort);
4334
void* pUserdata = pSocket->GetUserdata();
44-
int iError = pSocket->GetLastSocketError ( );
45-
46-
//printf ( "Socket returned (error) code: %d\n", iError );
4735

4836
// The socket has got a userdata value if successfully created. It doesn't otherwise
49-
if ( pUserdata == NULL /*|| pSocket->IsConnected () == false*/ )
37+
if (pUserdata == NULL)
5038
{
51-
SAFE_DELETE ( pSocket );
52-
53-
/*
54-
* (x86) TODO: Make static error codes, because error
55-
* codes between Win and Linux are different.
56-
* Put it in an array inside SocketErrors.h
57-
* --
58-
*/
59-
lua_pushboolean ( luaVM, false );
60-
lua_pushnumber ( luaVM, iError );
61-
62-
return 2;
39+
SAFE_DELETE(pSocket);
40+
lua_pushboolean(luaVM, false);
41+
return 1;
6342
}
6443

6544
// Add the socket to the Pulse list
66-
CSocketManager::SocketAdd ( pSocket/*, bListen*/ );
45+
CSocketManager::SocketAdd(pSocket);
6746

6847
// Return the userdata
6948
lua_pushlightuserdata(luaVM, pUserdata);
@@ -75,7 +54,6 @@ int CFunctions::sockOpen(lua_State* luaVM)
7554
return 1;
7655
}
7756

78-
// bool sockWrite ( userdata usSocket, string strData )
7957
int CFunctions::sockWrite(lua_State *luaVM)
8058
{
8159
if ( luaVM )
@@ -105,36 +83,6 @@ int CFunctions::sockWrite(lua_State *luaVM)
10583
return 1;
10684
}
10785

108-
// bool sockIsConnected ( userdata usSocket )
109-
int CFunctions::sockIsConnected ( lua_State *luaVM )
110-
{
111-
if ( luaVM )
112-
{
113-
// Make sure the socket is an userdata value
114-
if ( lua_type ( luaVM, 1 ) == LUA_TLIGHTUSERDATA )
115-
{
116-
// Prepare vars
117-
void* pUserdata = lua_touserdata ( luaVM, 1 );
118-
CSocket* pSocket = NULL;
119-
120-
// Get the socket
121-
if ( CSocketManager::GetSocket ( pSocket, pUserdata ) )
122-
{
123-
// Remove it
124-
if ( pSocket )
125-
{
126-
lua_pushboolean ( luaVM, pSocket->IsConnected ( ) );
127-
return 1;
128-
}
129-
}
130-
}
131-
}
132-
133-
lua_pushboolean ( luaVM, false );
134-
return 1;
135-
}
136-
137-
// bool sockClose ( userdata usSocket )
13886
int CFunctions::sockClose(lua_State *luaVM)
13987
{
14088
if ( luaVM )
@@ -150,11 +98,10 @@ int CFunctions::sockClose(lua_State *luaVM)
15098
if (CSocketManager::GetSocket(pSocket, pUserdata))
15199
{
152100
// Remove it
153-
if ( CSocketManager::SocketRemove(pSocket) )
154-
{
155-
lua_pushboolean(luaVM, true);
156-
return 1;
157-
}
101+
CSocketManager::SocketRemove(pSocket);
102+
103+
lua_pushboolean(luaVM, true);
104+
return 1;
158105
}
159106
}
160107
}

sockets/CFunctions.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,17 @@ class CFunctions;
2222
#define __CFUNCTIONS_H
2323

2424
#include <stdio.h>
25+
2526
#include "ml_sockets.h"
27+
#include "include/ILuaModuleManager.h"
28+
extern ILuaModuleManager10 *pModuleManager;
2629

2730
class CFunctions
2831
{
2932
public:
30-
static int sockOpen (lua_State* luaVM);
31-
static int sockWrite (lua_State* luaVM);
32-
static int sockIsConnected (lua_State* luaVM);
33-
static int sockClose (lua_State* luaVM);
33+
static int sockOpen (lua_State* luaVM);
34+
static int sockWrite (lua_State* luaVM);
35+
static int sockClose (lua_State* luaVM);
3436

3537
static void AddEvent (lua_State* luaVM, const string& strEventName);
3638
};

sockets/CSocket.cpp

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,6 @@ bool CSocket::DoPulse()
8484
FD_SET(m_pSocket, &wfds);
8585
// See if socket it writable
8686
int ret = select(m_pSocket+1, NULL, &wfds, NULL, &tv);
87-
//printf ( "select(...) returned: %d\n", ret );
88-
8987
if (ret == 0)
9088
return true; // Not writable yet
9189
if (ret == -1)
@@ -98,7 +96,6 @@ bool CSocket::DoPulse()
9896

9997
// Receive the data
10098
int iLength = recv(m_pSocket, chBuffer, SOCK_RECV_LIMIT, 0);
101-
// printf ( "Bytes received: %d\n", iLenght );
10299

103100
// Check if there were any errors
104101
int iError = GetLastSocketError();
@@ -126,8 +123,12 @@ bool CSocket::DoPulse()
126123
}
127124
}
128125
}
126+
else
127+
// If the socket doesn't exist, well, error?
128+
return false;
129129

130-
return false;
130+
// If the call makes it up till here, it has been a huge success! Cake and true as a reward!
131+
return true;
131132
}
132133

133134
bool CSocket::IsConnected()
@@ -162,16 +163,16 @@ bool CSocket::ProcessTargetLocation(const string& strHost, const unsigned short&
162163
else
163164
{
164165
Hostent = gethostbyname(strHost.c_str());
165-
if (Hostent != NULL)
166+
if (Hostent == NULL)
167+
{
168+
return false;
169+
}
170+
else
166171
{
167172
memcpy(&(m_sSockAddr.sin_addr), Hostent->h_addr_list[0], 4);
168173
return true;
169174
}
170-
171-
Hostent = NULL;
172175
}
173-
174-
return false;
175176
}
176177

177178
void CSocket::SetNonBlocking()
@@ -188,8 +189,13 @@ void CSocket::SetNonBlocking()
188189

189190
int CSocket::GetLastSocketError()
190191
{
191-
// Multi-platform function for getting the last socket error (See CSocket.h)
192+
// Multi-platform function for getting the last socket error
193+
194+
#ifdef WIN32
195+
return WSAGetLastError();
196+
#else
192197
return errno;
198+
#endif
193199
}
194200

195201
void CSocket::CloseSocket()
@@ -204,8 +210,7 @@ void CSocket::CloseSocket()
204210
#endif
205211

206212
// Unset the socket variable, so there's no mistaking there
207-
m_pSocket = NULL;
208-
m_bConnected = false;
213+
m_pSocket = NULL;
209214
}
210215

211216
int CSocket::HandleConnection(const int& iError)
@@ -220,11 +225,11 @@ int CSocket::HandleConnection(const int& iError)
220225
{
221226
return 0;
222227
}
223-
224-
#ifdef _DEBUG
225-
printf ( "Could not connect due to error: %i\n", iError ); // TEMP DEBUG
226-
#endif
227-
return ERR_CONNECT_FAILURE;
228+
else
229+
{
230+
printf("Could not connect due to error: %i",iError); // TEMP DEBUG
231+
return -1;
232+
}
228233
}
229234

230235
void CSocket::TriggerEvent(const string eventName, const string arg1)

sockets/CSocket.h

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@
1717

1818
#ifdef WIN32
1919
#include <winsock2.h>
20-
21-
#undef errno
22-
#define errno WSAGetLastError()
2320
#else
2421
#include <unistd.h>
2522
#include <sys/types.h>
@@ -38,19 +35,19 @@
3835
class CSocket
3936
{
4037
public:
41-
CSocket (lua_State *luaVM, const string& strHost, const unsigned short& usPort);
42-
~CSocket ();
38+
CSocket (lua_State *luaVM, const string& strHost, const unsigned short& usPort);
39+
~CSocket ();
4340

44-
bool Send (const string& data);
45-
bool DoPulse ();
46-
bool IsConnected ();
47-
int GetLastSocketError ();
41+
bool Send (const string& data);
42+
bool DoPulse ();
43+
bool IsConnected ();
4844

49-
void* GetUserdata ();
45+
void* GetUserdata ();
5046

5147
private:
5248
bool ProcessTargetLocation (const string& strHost, const unsigned short& usPort);
5349
void SetNonBlocking ();
50+
int GetLastSocketError ();
5451
void CloseSocket ();
5552
int HandleConnection (const int& iError);
5653
void TriggerEvent (const string eventName, const string arg1 = "");

sockets/CSocketManager.cpp

Lines changed: 12 additions & 27 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 <sSocketsStorage*> vecSockets;
12+
vector <CSocket*> 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 = (CSocket *)vecSockets[i]->m_pSocket;
20+
CSocket* pSocket = vecSockets[i];
2121

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

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 )
31+
void CSocketManager::SocketAdd(CSocket*& pSocket)
4432
{
4533
// Add the socket to the loop stuff
46-
sSocketsStorage* pSocketStorage = new sSocketsStorage ( pSocket, bListen );
47-
vecSockets.push_back ( pSocketStorage );
48-
49-
return true;
34+
vecSockets.push_back(pSocket);
5035
}
5136

52-
bool CSocketManager::SocketRemove ( CSocket*& pSocket )
37+
bool CSocketManager::SocketRemove(CSocket*& pSocket)
5338
{
5439
// Check if an socket was actually specified
55-
if ( pSocket == NULL )
40+
if (pSocket == NULL)
5641
return false;
5742

5843
// Loop through all sockets
5944
for (unsigned int i = 0; i < vecSockets.size(); ++i)
6045
{
6146
// If the current is the one we're looking for...
62-
if ((CSocket *)vecSockets[i]->m_pSocket == pSocket)
47+
if (vecSockets[i] == pSocket)
6348
{
6449
// Remove it from the vector and delete it, then return true
6550
vecSockets.erase(vecSockets.begin() + i);
@@ -71,7 +56,7 @@ bool CSocketManager::SocketRemove ( CSocket*& pSocket )
7156
return false;
7257
}
7358

74-
bool CSocketManager::GetSocket ( CSocket*& pSocket, void* pUserdata )
59+
bool CSocketManager::GetSocket(CSocket*& pSocket, void* pUserdata)
7560
{
7661
// Make sure a value has been passed. Don't bother if there hasn't been
7762
if (pUserdata == NULL)
@@ -81,20 +66,20 @@ bool CSocketManager::GetSocket ( CSocket*& pSocket, void* pUserdata )
8166
for (unsigned int i = 0; i < vecSockets.size(); ++i)
8267
{
8368
// Compare the current socket's userdata with the one we're looking for
84-
if ( (CSocket *)vecSockets[i]->m_pSocket->GetUserdata() == pUserdata )
69+
if (vecSockets[i]->GetUserdata() == pUserdata)
8570
{
8671
// If it's the one we want, assign pSocket to it and return true
87-
pSocket = (CSocket *)vecSockets[i]->m_pSocket;
72+
pSocket = vecSockets[i];
8873
return true;
8974
}
9075
}
9176

9277
return false;
9378
}
9479

95-
void CSocketManager::HandleStop ( void )
80+
void CSocketManager::HandleStop()
9681
{
9782
// Triggered at module stop. Simply destroys all sockets
9883
for (unsigned int i = 0; i < vecSockets.size(); ++i)
99-
SAFE_DELETE((CSocket *)vecSockets[i]->m_pSocket);
84+
SAFE_DELETE(vecSockets[i]);
10085
}

0 commit comments

Comments
 (0)