-
Notifications
You must be signed in to change notification settings - Fork 0
/
eng_socket.cpp
210 lines (190 loc) · 5.36 KB
/
eng_socket.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include "assert.h"
#include "eng_socket.h"
#include "ConnectionManager.h"
#include "lua.hpp"
//------------------------------------------------------ Lua --------------------------------------------------
static int hasPendingMsg(lua_State *L)
{
return GetDefaultSocket()->HasPendingMsg(L);
}
static int connectToServer(lua_State *L)
{
return GetDefaultSocket()->ConnectToServer(L);
}
static int reconnectToServer(lua_State *L)
{
return GetDefaultSocket()->ReConnectToServer(L);
}
static int updateNetMsg(lua_State *L)
{
return GetDefaultSocket()->UpdateNetMsg(L);
}
static int closeConnection(lua_State *L)
{
return GetDefaultSocket()->CloseConnection(L);
}
static int initPike(lua_State *L)
{
return GetDefaultSocket()->InitPike(L);
}
static int keepAlived(lua_State *L)
{
return GetDefaultSocket()->KeepAlived(L);
}
static int generateSecret(lua_State *L)
{
unsigned int key1 = (unsigned int)luaL_checkinteger(L, 1);
unsigned int key2 = (unsigned int)luaL_checkinteger(L, 2);
unsigned int u1 = (unsigned int)(key2 >> 3) & 0xFF;
unsigned int u2 = (unsigned int)(key2 >> 23) & 0xFF;
unsigned int u3 = (unsigned int)(key1 >> 5) & 0xFF;
unsigned int u4 = (unsigned int)(key1 >> 15) & 0xFF;
unsigned int key = (u1 << 24) | (u3 << 16) | (u2 << 8) | u4;
lua_pushinteger(L,key);
return 1;
}
static const struct luaL_Reg emptyfuns[] = {
{NULL, NULL}
};
static const struct luaL_Reg funs[] = {
{"hasPendingMsg", hasPendingMsg},
{"updateNetMsg", updateNetMsg},
{"closeConnection", closeConnection},
{"connectToServer", connectToServer},
{"keepAlived",keepAlived},
{"initPike", initPike},
{"generateSecret", generateSecret },
{"reconnectToServer", reconnectToServer},
{NULL, NULL}
};
int ext_lua_socket(lua_State *L) {
int __recordStackIndex = lua_gettop((L));
luaL_newmetatable(L, SOCKET_NAMESPACE);
luaL_register(L, NULL, emptyfuns);
luaL_register(L, SOCKET_NAMESPACE, funs);
lua_pushvalue(L, -2);
lua_setmetatable(L, -2);
while (lua_gettop((L)) > (__recordStackIndex + (0))) lua_remove((L), __recordStackIndex + 1);
return 0;
}
SocketObject*GetDefaultSocket()
{
return GetSocketByName("defaultSocket");
}
SocketObject * GetSocketByName(const char * socketname)
{
for (std::set<SocketObject*>::iterator it = SocketObject::s_objList.begin(); it != SocketObject::s_objList.end(); ++it)
{
if ((*it)->getSocketName().compare(socketname) == 0)
{
return (*it);
}
}
return NULL;
}
SocketObject::SocketObject(const char* instClassName)
{
m_socketObjectName = instClassName;
s_objList.insert(this);
m_pConnectionMgr = NULL;
}
SocketObject::~SocketObject()
{
if(m_pConnectionMgr)
{
m_pConnectionMgr->Close();
m_pConnectionMgr->ClearCached();
}
s_objList.erase(this);
}
CConnectionManager*SocketObject::GetConnection()
{
if (m_pConnectionMgr == NULL)
{
m_pConnectionMgr = new CConnectionManager;
m_pConnectionMgr->SetName(getSocketName().c_str());
}
return m_pConnectionMgr;
}
void SocketObject::Restart()
{
if (m_pConnectionMgr != NULL)
{
delete m_pConnectionMgr;
}
m_pConnectionMgr = new CConnectionManager;
m_pConnectionMgr->SetName(getSocketName().c_str());
}
std::set<SocketObject*> SocketObject::s_objList;
int SocketObject::HasPendingMsg(lua_State *L)
{
NetMessage* pCurrentReadMsg = GetConnection()->getMsg();
if (pCurrentReadMsg && pCurrentReadMsg->isEnd())
{
lua_pushboolean(L, true);
}
else
{
lua_pushboolean(L, false);
}
return 1;
}
int SocketObject::CloseConnection(lua_State *L)
{
GetConnection()->Close();
GetConnection()->ClearCached();
return 1;
}
int SocketObject::ConnectToServer(lua_State *L)
{
const char* pAddr = luaL_checkstring(L, 1);
int port = luaL_checkinteger(L, 2);
bool needEncrypt = luaL_checkinteger(L, 3) == 1;
bool isSuccess = GetConnection()->Connect(pAddr, port);
lua_pushboolean(L, isSuccess);
return 1;
}
int SocketObject::KeepAlived(lua_State *L)
{
int keepalive = (int)luaL_checkinteger(L, 1);
int keepidle = (int)luaL_checkinteger(L, 2);
int keepinterval = (int)luaL_checkinteger(L, 3);
int keepcount = (int)luaL_checkinteger(L, 4);
int rst = GetConnection()->KeepAlived(keepalive, keepidle, keepinterval, keepcount);
lua_pushinteger(L, rst);
return 1;
}
int SocketObject::InitPike(lua_State *L)
{
int sendSeed = (int)luaL_checkinteger(L, 1);
int recvSeed = (int)luaL_checkinteger(L, 2);
GetConnection()->InitEncrypt(sendSeed, recvSeed);
return 1;
}
int SocketObject::ReConnectToServer(lua_State *L)
{
GetConnection()->ReConnect();
return 1;
}
int SocketObject::GetName(lua_State *L)
{
lua_pushstring(L,this->getSocketName().c_str());
return 1;
}
int SocketObject::UpdateNetMsg(lua_State*L)
{
int dt = luaL_checkinteger(L, 1);
GetConnection()->Update(dt);
return 1;
}
LUNPLUS_DEFINE_INTERFACE(SocketObject);
LUNPLUS_METHOD_BEGIN(SocketObject)
LUNPLUS_METHOD_DECLARE(SocketObject, GetName)
LUNPLUS_METHOD_DECLARE(SocketObject, HasPendingMsg)
LUNPLUS_METHOD_DECLARE(SocketObject, CloseConnection)
LUNPLUS_METHOD_DECLARE(SocketObject, ConnectToServer)
LUNPLUS_METHOD_DECLARE(SocketObject, KeepAlived)
LUNPLUS_METHOD_DECLARE(SocketObject, InitPike)
LUNPLUS_METHOD_DECLARE(SocketObject, ReConnectToServer)
LUNPLUS_METHOD_DECLARE(SocketObject, UpdateNetMsg)
LUNPLUS_METHOD_END