compiled-in LUA
This commit is contained in:
87
main.cpp
87
main.cpp
@@ -1,64 +1,27 @@
|
||||
#include "pip.h"
|
||||
|
||||
|
||||
int main(int argc, char * argv[]) {
|
||||
PICLI cli(argc, argv);
|
||||
PITimer tm;
|
||||
cli.addArgument("connect", true);
|
||||
cli.addArgument("name", true);
|
||||
PICloudClient c("127.0.0.1:10101");
|
||||
// c.setReopenEnabled(true);
|
||||
PICloudServer s("127.0.0.1:10101");
|
||||
PIVector<PICloudServer::Client *> clients;
|
||||
CONNECTL(&tm, tickEvent, ([&](void *, int){
|
||||
if (c.isConnected()) {
|
||||
PIString str = "ping";
|
||||
piCout << "[Client] send:" << str;
|
||||
c.write(str.toByteArray());
|
||||
}
|
||||
if (s.isRunning()) {
|
||||
for (auto cl : clients) {
|
||||
if (cl->isOpened()) {
|
||||
PIString str = "ping_S";
|
||||
piCout << "[Server] send to" << cl << ":" << str;
|
||||
cl->write(str.toByteArray());
|
||||
}
|
||||
}
|
||||
}
|
||||
}));
|
||||
CONNECTL(&c, threadedReadEvent, ([&](uchar * readed, int size){
|
||||
PIByteArray ba(readed, size);
|
||||
PIString str = PIString(ba);
|
||||
piCout << "[Client] data:" << str;
|
||||
if (str == "ping_S") c.write(PIString("pong_S").toByteArray());
|
||||
}));
|
||||
CONNECTL(&s, newConnection, ([&](PICloudServer::Client * cl){
|
||||
piCout << "[Server] new client:" << cl;
|
||||
clients << cl;
|
||||
CONNECTL(cl, threadedReadEvent, ([&c, &s, cl](uchar * readed, int size){
|
||||
PIByteArray ba(readed, size);
|
||||
PIString str = PIString(ba);
|
||||
piCout << "[Server] data from" << cl << ":" << str;
|
||||
if (str == "ping") cl->write(PIString("pong").toByteArray());
|
||||
}));
|
||||
CONNECTL(cl, closed, ([&clients, cl](){
|
||||
cl->stop();
|
||||
clients.removeAll(cl);
|
||||
cl->deleteLater();
|
||||
}));
|
||||
cl->startThreadedRead();
|
||||
}));
|
||||
if (cli.hasArgument("name")) s.setServerName(cli.argumentValue("name"));
|
||||
if (cli.hasArgument("connect")) {
|
||||
c.setServerName(cli.argumentValue("connect"));
|
||||
c.startThreadedRead();
|
||||
} else {
|
||||
s.startThreadedRead();
|
||||
}
|
||||
tm.start(1000);
|
||||
PIKbdListener ls;
|
||||
ls.enableExitCapture(PIKbdListener::F10);
|
||||
ls.start();
|
||||
WAIT_FOR_EXIT
|
||||
return 0;
|
||||
extern "C" {
|
||||
# include "lua.h"
|
||||
# include "lauxlib.h"
|
||||
# include "lualib.h"
|
||||
}
|
||||
#include <LuaBridge/LuaBridge.h>
|
||||
#include "pistring_std.h"
|
||||
|
||||
static const char * script
|
||||
= "-- script.lua \n"
|
||||
"testString = \"LuaBridge works!\" \n"
|
||||
"number = 42 \n";
|
||||
|
||||
using namespace luabridge;
|
||||
int main() {
|
||||
lua_State* L = luaL_newstate();
|
||||
luaL_dostring(L, script);
|
||||
luaL_openlibs(L);
|
||||
lua_pcall(L, 0, 0, 0);
|
||||
LuaRef s = getGlobal(L, "testString");
|
||||
LuaRef n = getGlobal(L, "number");
|
||||
std::string luaString = s.cast<std::string>();
|
||||
int answer = n.cast<int>();
|
||||
piCout << StdString2PIString(luaString);
|
||||
piCout << "And here's our number:" << answer;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user