Skip to content

Commit a4155c6

Browse files
Some other fixes, changed return structure of sockOpen when it fails connecting.
bConnected, iErrorCode sockOpen (...)
1 parent 3d4b61a commit a4155c6

File tree

4 files changed

+34
-25
lines changed

4 files changed

+34
-25
lines changed

sockets/CFunctions.cpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,36 @@
1818

1919
#include "CFunctions.h"
2020

21+
// bool sockOpen ( string strHost, int usPort [, bool bListen = false ] )
2122
int CFunctions::sockOpen(lua_State* luaVM)
2223
{
2324
if ( luaVM )
2425
{
2526
// Make sure host is a string, and port is a number
26-
if (lua_type(luaVM, 1) == LUA_TSTRING && lua_type(luaVM, 2) == LUA_TNUMBER)
27+
if ( lua_type ( luaVM, 1 ) == LUA_TSTRING && lua_type ( luaVM, 2 ) == LUA_TNUMBER )
2728
{
2829
// Put the host in a string, and the port in an unsigned short
29-
string strHost = lua_tostring(luaVM, 1);
30+
string strHost = lua_tostring ( luaVM, 1 );
3031
unsigned short usPort = static_cast < unsigned short > ( lua_tonumber ( luaVM, 2 ) );
32+
//bool bListen = ( ( lua_type ( luaVM, 3 ) == LUA_TBOOLEAN ) ? lua_toboolean ( luaVM, 3 ) : false );
3133

3234
// Create the socket
3335
CSocket* pSocket = new CSocket(luaVM, strHost, usPort);
3436
void* pUserdata = pSocket->GetUserdata();
3537

3638
// The socket has got a userdata value if successfully created. It doesn't otherwise
37-
if (pUserdata == NULL)
39+
if ( pUserdata == NULL || pSocket->IsConnected () == false )
3840
{
39-
SAFE_DELETE(pSocket);
40-
lua_pushboolean(luaVM, false);
41+
lua_pushboolean ( luaVM, false );
42+
lua_pushnumber ( luaVM, pSocket->GetLastSocketError ( ) );
43+
44+
// Delete the socket now.
45+
SAFE_DELETE ( pSocket );
4146
return 1;
4247
}
4348

4449
// Add the socket to the Pulse list
45-
CSocketManager::SocketAdd(pSocket);
50+
CSocketManager::SocketAdd ( pSocket/*, bListen*/ );
4651

4752
// Return the userdata
4853
lua_pushlightuserdata(luaVM, pUserdata);
@@ -54,6 +59,7 @@ int CFunctions::sockOpen(lua_State* luaVM)
5459
return 1;
5560
}
5661

62+
// bool sockWrite ( userdata usSocket, string strData )
5763
int CFunctions::sockWrite(lua_State *luaVM)
5864
{
5965
if ( luaVM )
@@ -83,6 +89,7 @@ int CFunctions::sockWrite(lua_State *luaVM)
8389
return 1;
8490
}
8591

92+
// bool sockClose ( userdata usSocket )
8693
int CFunctions::sockClose(lua_State *luaVM)
8794
{
8895
if ( luaVM )

sockets/CSocket.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -189,13 +189,8 @@ void CSocket::SetNonBlocking()
189189

190190
int CSocket::GetLastSocketError()
191191
{
192-
// Multi-platform function for getting the last socket error
193-
194-
#ifdef WIN32
195-
return WSAGetLastError();
196-
#else
192+
// Multi-platform function for getting the last socket error (See CSocket.h)
197193
return errno;
198-
#endif
199194
}
200195

201196
void CSocket::CloseSocket()
@@ -210,7 +205,8 @@ void CSocket::CloseSocket()
210205
#endif
211206

212207
// Unset the socket variable, so there's no mistaking there
213-
m_pSocket = NULL;
208+
m_pSocket = NULL;
209+
m_bConnected = false;
214210
}
215211

216212
int CSocket::HandleConnection(const int& iError)
@@ -227,8 +223,10 @@ int CSocket::HandleConnection(const int& iError)
227223
}
228224
else
229225
{
230-
printf("Could not connect due to error: %i",iError); // TEMP DEBUG
231-
return -1;
226+
#ifdef _DEBUG
227+
printf ( "Could not connect due to error: %i\n", iError ); // TEMP DEBUG
228+
#endif
229+
return ERR_CONNECT_FAILURE;
232230
}
233231
}
234232

sockets/CSocket.h

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

1818
#ifdef WIN32
1919
#include <winsock2.h>
20+
21+
#undef errno
22+
#define errno WSAGetLastError()
2023
#else
2124
#include <unistd.h>
2225
#include <sys/types.h>
@@ -35,19 +38,19 @@
3538
class CSocket
3639
{
3740
public:
38-
CSocket (lua_State *luaVM, const string& strHost, const unsigned short& usPort);
39-
~CSocket ();
41+
CSocket (lua_State *luaVM, const string& strHost, const unsigned short& usPort);
42+
~CSocket ();
4043

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

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

4751
private:
4852
bool ProcessTargetLocation (const string& strHost, const unsigned short& usPort);
4953
void SetNonBlocking ();
50-
int GetLastSocketError ();
5154
void CloseSocket ();
5255
int HandleConnection (const int& iError);
5356
void TriggerEvent (const string eventName, const string arg1 = "");

sockets/ml_sockets.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,18 @@
2121

2222
// Disable Visual Studio warnings
2323
#ifdef WIN32
24-
#pragma warning (disable : 4267) // DISABLE: conversion from 'size_t' to 'int', possible loss of data
24+
#pragma warning (disable : 4267) // DISABLE: conversion from 'size_t' to 'int', possible loss of data.
2525
#pragma warning (disable : 4996) // DISABLE: 'strcpy': This function or variable may be unsafe.
26-
#pragma warning (disable : 4244) // DISABLE: conversion from 'SOCKET' to 'int', possible loss of data
26+
#pragma warning (disable : 4244) // DISABLE: conversion from 'SOCKET' to 'int', possible loss of data.
27+
#pragma warning (disable : 4800) // DISABLE: 'int' : forcing value to bool 'true' or 'false' (performance warning).
2728
#endif
2829

2930
#include <assert.h>
3031

3132
/** MODULE SPECIFIC INFORMATION **/
3233
#define MODULE_NAME "Sockets Module"
3334
#define MODULE_AUTHOR "Gamesnert, MCvarial & x86"
34-
#define MODULE_VERSION 1.2f
35+
#define MODULE_VERSION 1.3f
3536

3637
// Include default MTA module SDK includes
3738
#include "Common.h"

0 commit comments

Comments
 (0)