-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLuaUDPDbgLib.cpp
executable file
·155 lines (128 loc) · 4.13 KB
/
LuaUDPDbgLib.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
//
//---------------------------------------------------------------------------
#include "stdinc.h"
//---------------------------------------------------------------------------
#include "LuaInc.h"
//---------------------------------------------------------------------------
#include "LuaUDPDbgLib.h"
//---------------------------------------------------------------------------
#include "LuaScriptManager.h"
#include "UdpDebug.h"
#include "utility.h"
//---------------------------------------------------------------------------
#ifdef _WIN32
#pragma hdrstop
#endif
//---------------------------------------------------------------------------
#include "LuaScript.h"
//---------------------------------------------------------------------------
static int Reg(lua_State * L) {
if(lua_gettop(L) != 3) {
luaL_error(L, "bad argument count to 'Reg' (3 expected, got %d)", lua_gettop(L));
lua_settop(L, 0);
lua_pushnil(L);
return 1;
}
if(lua_type(L, 1) != LUA_TSTRING || lua_type(L, 2) != LUA_TNUMBER || lua_type(L, 3) != LUA_TBOOLEAN) {
luaL_checktype(L, 1, LUA_TSTRING);
luaL_checktype(L, 2, LUA_TNUMBER);
luaL_checktype(L, 3, LUA_TBOOLEAN);
lua_settop(L, 0);
lua_pushnil(L);
return 1;
}
Script * cur = clsScriptManager::mPtr->FindScript(L);
if(cur == NULL || cur->bRegUDP == true) {
lua_settop(L, 0);
lua_pushnil(L);
return 1;
}
size_t szLen;
char * sIP = (char *)lua_tolstring(L, 1, &szLen);
if(szLen < 7 || szLen > 15 || isIP(sIP) == false) {
lua_settop(L, 0);
lua_pushnil(L);
return 1;
}
#if LUA_VERSION_NUM < 503
uint16_t usPort = (uint16_t)lua_tonumber(L, 2);
#else
uint16_t usPort = (uint16_t)lua_tounsigned(L, 2);
#endif
bool bAllData = lua_toboolean(L, 3) == 0 ? false : true;
if(clsUdpDebug::mPtr->New(sIP, usPort, bAllData, cur->sName) == false) {
lua_settop(L, 0);
lua_pushnil(L);
return 1;
}
cur->bRegUDP = true;
lua_settop(L, 0);
lua_pushboolean(L, 1);
return 1;
}
//------------------------------------------------------------------------------
static int Unreg(lua_State * L) {
if(lua_gettop(L) != 0) {
luaL_error(L, "bad argument count to 'Unreg' (0 expected, got %d)", lua_gettop(L));
lua_settop(L, 0);
return 0;
}
Script * cur = clsScriptManager::mPtr->FindScript(L);
if(cur == NULL || cur->bRegUDP == false) {
lua_settop(L, 0);
return 0;
}
clsUdpDebug::mPtr->Remove(cur->sName);
cur->bRegUDP = false;
return 0;
}
//------------------------------------------------------------------------------
static int Send(lua_State * L) {
if(lua_gettop(L) != 1) {
luaL_error(L, "bad argument count to 'Send' (1 expected, got %d)", lua_gettop(L));
lua_settop(L, 0);
lua_pushnil(L);
return 1;
}
if(lua_type(L, 1) != LUA_TSTRING) {
luaL_checktype(L, 1, LUA_TSTRING);
lua_settop(L, 0);
lua_pushnil(L);
return 1;
}
size_t szLen;
char * sMsg = (char *)lua_tolstring(L, 1, &szLen);
if(szLen == 0) {
lua_settop(L, 0);
lua_pushnil(L);
return 1;
}
Script * cur = clsScriptManager::mPtr->FindScript(L);
if(cur == NULL || cur->bRegUDP == false) {
clsUdpDebug::mPtr->Broadcast(sMsg, szLen);
lua_settop(L, 0);
lua_pushboolean(L, 1);
return 1;
}
clsUdpDebug::mPtr->Send(cur->sName, sMsg, szLen);
lua_pushboolean(L, 1);
return 1;
}
//------------------------------------------------------------------------------
static const luaL_Reg udpdbg[] = {
{ "Reg", Reg },
{ "Unreg", Unreg },
{ "Send", Send },
{ NULL, NULL }
};
//---------------------------------------------------------------------------
#if LUA_VERSION_NUM > 501
int RegUDPDbg(lua_State * L) {
luaL_newlib(L, udpdbg);
return 1;
#else
void RegUDPDbg(lua_State * L) {
luaL_register(L, "UDPDbg", udpdbg);
#endif
}
//---------------------------------------------------------------------------