forked from jwcpp/jwEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLua_KcpServer.cpp
More file actions
95 lines (77 loc) · 2.23 KB
/
Copy pathLua_KcpServer.cpp
File metadata and controls
95 lines (77 loc) · 2.23 KB
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
#include <string>
#include "sol/sol.hpp"
#include "KcpEvent.h"
#include "KcpClient.h"
#include "KcpServer.h"
#include "UdpPacket.h"
#include "KcpSession.h"
#include "EventLoop.h"
class Lua_KcpServer : public KcpServer, public KcpEvent
{
public:
Lua_KcpServer() :
KcpServer(EventLoop::Instance(), this)
{
}
protected:
virtual void onAccept(KcpSession * conn) {
on_accept(conn);
};
virtual void onClose(KcpSession * conn) {
on_close(conn);
};
virtual void onMsg(KcpSession * conn, int msgtype, UdpPacket * pack) {
on_msg(conn, msgtype, pack);
};
public:
std::function<void(KcpSession *)> on_accept;
std::function<void(KcpSession *)> on_close;
std::function<void(KcpSession *,int, UdpPacket *)> on_msg;
};
class Lua_KcpClient : public KcpClient, public KcpEvent
{
public:
Lua_KcpClient() :
KcpClient(EventLoop::Instance(), this)
{
}
protected:
virtual void onConnect(KcpSession * conn) {
on_connect(conn);
};
virtual void onClose(KcpSession * conn) {
on_close(conn);
};
virtual void onMsg(KcpSession * conn, int msgtype, UdpPacket * pack) {
on_msg(conn, msgtype, pack);
};
public:
std::function<void(KcpSession *)> on_connect;
std::function<void(KcpSession *)> on_close;
std::function<void(KcpSession *, int, UdpPacket *)> on_msg;
};
void luabind_kcpserver(sol::state & lua)
{
lua.new_usertype<KcpSession>("KcpSession",
"sendPacket", &KcpSession::sendPacket,
"sendMsg", &KcpSession::sendData,
"flushKcp", &KcpSession::flushKcp,
"getSessionId", &KcpSession::getSessionId);
lua.new_usertype<Lua_KcpClient>("KcpClient",
//sol::constructors<Lua_NetClient(EventLoop *)>(),
"createSession", &Lua_KcpClient::createSession,
"closeSession", &Lua_KcpClient::closeSession,
"on_connect", &Lua_KcpClient::on_connect,
"on_close", &Lua_KcpClient::on_close,
"on_msg", &Lua_KcpClient::on_msg);
lua.new_usertype<Lua_KcpServer>("KcpServer",
//sol::constructors<Lua_NetServer(EventLoop *)>(),
"start", &Lua_KcpServer::start,
"closeSession", &Lua_KcpServer::closeSession,
"shutdown", &Lua_KcpServer::shutdown,
"on_accept", &Lua_KcpServer::on_accept,
"on_close", &Lua_KcpServer::on_close,
"on_msg", &Lua_KcpServer::on_msg);
lua.new_usertype<UdpPacket>("KcpPacket",
sol::base_classes, sol::bases<BasePacket>());
}