diff options
-rw-r--r-- | bin/client/main.lua | 28 | ||||
-rw-r--r-- | bin/jin.exe | bin | 811520 -> 811520 bytes | |||
-rw-r--r-- | src/libjin/Thread/Thread.cpp | 14 | ||||
-rw-r--r-- | src/libjin/Thread/Thread.h | 8 | ||||
-rw-r--r-- | src/libjin/Thread/thread.cpp | 14 | ||||
-rw-r--r-- | src/libjin/Thread/thread.h | 8 | ||||
-rw-r--r-- | src/libjin/thread/thread.cpp | 14 | ||||
-rw-r--r-- | src/libjin/thread/thread.h | 8 | ||||
-rw-r--r-- | src/lua/graphics/luaopen_Font.cpp | 2 | ||||
-rw-r--r-- | src/lua/luaopen_types.h | 1 | ||||
-rw-r--r-- | src/lua/net/Socket.h | 2 | ||||
-rw-r--r-- | src/lua/net/luaopen_Buffer.cpp | 2 | ||||
-rw-r--r-- | src/lua/net/luaopen_Socket.cpp | 2 | ||||
-rw-r--r-- | src/lua/net/luaopen_net.cpp | 48 | ||||
-rw-r--r-- | src/lua/thread/Thread.h | 14 | ||||
-rw-r--r-- | src/lua/thread/luaopen_Thread.cpp | 10 | ||||
-rw-r--r-- | src/lua/thread/luaopen_thread.cpp | 10 |
17 files changed, 80 insertions, 105 deletions
diff --git a/bin/client/main.lua b/bin/client/main.lua index 5d10e6a..9c176f8 100644 --- a/bin/client/main.lua +++ b/bin/client/main.lua @@ -11,14 +11,13 @@ while true do thread:send(2, buf) end ]] - jin.core.onLoad = function() - -- jin.net.init() - -- socket = jin.net.Socket("TCP", "127.0.0.1", 8809) + jin.net.init() + socket = jin.net.Socket("TCP", "127.0.0.1", 8803) - -- thread = jin.thread.Thread("asynReceive", asynReceive) - -- thread:start() - -- thread:send(1, socket) + thread = jin.thread.Thread("asynReceive", asynReceive) + thread:start() + thread:send(1, socket) end jin.core.onEvent = function(e) @@ -32,18 +31,13 @@ end local x = 0 local y = 0 jin.core.onUpdate = function(dt) - -- if thread:receive(2) then - -- local buf = thread:fetch(2) - -- x, xl = buf:grabFloat(0) - -- y, yl = buf:grabFloat(xl) - -- end - - -- local buf = jin.net.Buffer(0) - -- buf:append(x) - -- buf:append(y) - -- thread:send(3, buf) + if thread:receive(2) then + local buf = thread:fetch(2) + x, xl = buf:grabFloat(0) + y, yl = buf:grabFloat(xl) + end end jin.core.onDraw = function() jin.graphics.circle("fill", x, y, 12) -end
\ No newline at end of file +end diff --git a/bin/jin.exe b/bin/jin.exe Binary files differindex 1845588..dfcdc16 100644 --- a/bin/jin.exe +++ b/bin/jin.exe diff --git a/src/libjin/Thread/Thread.cpp b/src/libjin/Thread/Thread.cpp index 2be44a1..13e691a 100644 --- a/src/libjin/Thread/Thread.cpp +++ b/src/libjin/Thread/Thread.cpp @@ -171,14 +171,6 @@ namespace thread ////////////////////////////////////////////////////////////////////// - int Thread::ThreadFunciton(void* p) - { - Thread* thread = (Thread*)p; - if (thread->threadRunner != nullptr) - thread->threadRunner(thread); - return 0; - } - Thread::Thread(const std::string tname, ThreadRunner runner) : name(tname) , running(false) @@ -207,7 +199,7 @@ namespace thread return running; }; - bool Thread::start() + bool Thread::start(void* p) { Lock l(mutex); if (running) @@ -219,8 +211,8 @@ namespace thread #endif } #if JIN_THREAD_SDL - handle = SDL_CreateThread(ThreadFunciton, name.c_str(), this); - #elif JIN_THREAD_CPP + handle = SDL_CreateThread(threadRunner, name.c_str(), p); + #elif JIN_THREAD_CPP handle = new std::thread(); #endif return (running = (handle != nullptr)); diff --git a/src/libjin/Thread/Thread.h b/src/libjin/Thread/Thread.h index 5b74f23..046c8f6 100644 --- a/src/libjin/Thread/Thread.h +++ b/src/libjin/Thread/Thread.h @@ -97,10 +97,10 @@ namespace thread }; public: - typedef void(ThreadRunner)(Thread* thread); + typedef int(*ThreadRunner)(void* obj); Thread(const std::string name, ThreadRunner threadfuncs); ~Thread(); - bool start(); + bool start(void* p); void wait(); void send(int slot, const Variant& value); bool receive(int slot); @@ -120,7 +120,7 @@ namespace thread #endif Mutex* mutex; // mutex variable Conditional* condition; // condition variable - ThreadRunner* threadRunner; // thread function + ThreadRunner threadRunner; // thread function ThreadData* common; // threads common data const std::string name; // thread name, for debugging purposes /** @@ -154,8 +154,6 @@ namespace thread */ bool running; // running - static int ThreadFunciton(void* p); - }; } // thread diff --git a/src/libjin/Thread/thread.cpp b/src/libjin/Thread/thread.cpp index 2be44a1..13e691a 100644 --- a/src/libjin/Thread/thread.cpp +++ b/src/libjin/Thread/thread.cpp @@ -171,14 +171,6 @@ namespace thread ////////////////////////////////////////////////////////////////////// - int Thread::ThreadFunciton(void* p) - { - Thread* thread = (Thread*)p; - if (thread->threadRunner != nullptr) - thread->threadRunner(thread); - return 0; - } - Thread::Thread(const std::string tname, ThreadRunner runner) : name(tname) , running(false) @@ -207,7 +199,7 @@ namespace thread return running; }; - bool Thread::start() + bool Thread::start(void* p) { Lock l(mutex); if (running) @@ -219,8 +211,8 @@ namespace thread #endif } #if JIN_THREAD_SDL - handle = SDL_CreateThread(ThreadFunciton, name.c_str(), this); - #elif JIN_THREAD_CPP + handle = SDL_CreateThread(threadRunner, name.c_str(), p); + #elif JIN_THREAD_CPP handle = new std::thread(); #endif return (running = (handle != nullptr)); diff --git a/src/libjin/Thread/thread.h b/src/libjin/Thread/thread.h index 5b74f23..046c8f6 100644 --- a/src/libjin/Thread/thread.h +++ b/src/libjin/Thread/thread.h @@ -97,10 +97,10 @@ namespace thread }; public: - typedef void(ThreadRunner)(Thread* thread); + typedef int(*ThreadRunner)(void* obj); Thread(const std::string name, ThreadRunner threadfuncs); ~Thread(); - bool start(); + bool start(void* p); void wait(); void send(int slot, const Variant& value); bool receive(int slot); @@ -120,7 +120,7 @@ namespace thread #endif Mutex* mutex; // mutex variable Conditional* condition; // condition variable - ThreadRunner* threadRunner; // thread function + ThreadRunner threadRunner; // thread function ThreadData* common; // threads common data const std::string name; // thread name, for debugging purposes /** @@ -154,8 +154,6 @@ namespace thread */ bool running; // running - static int ThreadFunciton(void* p); - }; } // thread diff --git a/src/libjin/thread/thread.cpp b/src/libjin/thread/thread.cpp index 2be44a1..13e691a 100644 --- a/src/libjin/thread/thread.cpp +++ b/src/libjin/thread/thread.cpp @@ -171,14 +171,6 @@ namespace thread ////////////////////////////////////////////////////////////////////// - int Thread::ThreadFunciton(void* p) - { - Thread* thread = (Thread*)p; - if (thread->threadRunner != nullptr) - thread->threadRunner(thread); - return 0; - } - Thread::Thread(const std::string tname, ThreadRunner runner) : name(tname) , running(false) @@ -207,7 +199,7 @@ namespace thread return running; }; - bool Thread::start() + bool Thread::start(void* p) { Lock l(mutex); if (running) @@ -219,8 +211,8 @@ namespace thread #endif } #if JIN_THREAD_SDL - handle = SDL_CreateThread(ThreadFunciton, name.c_str(), this); - #elif JIN_THREAD_CPP + handle = SDL_CreateThread(threadRunner, name.c_str(), p); + #elif JIN_THREAD_CPP handle = new std::thread(); #endif return (running = (handle != nullptr)); diff --git a/src/libjin/thread/thread.h b/src/libjin/thread/thread.h index 5b74f23..046c8f6 100644 --- a/src/libjin/thread/thread.h +++ b/src/libjin/thread/thread.h @@ -97,10 +97,10 @@ namespace thread }; public: - typedef void(ThreadRunner)(Thread* thread); + typedef int(*ThreadRunner)(void* obj); Thread(const std::string name, ThreadRunner threadfuncs); ~Thread(); - bool start(); + bool start(void* p); void wait(); void send(int slot, const Variant& value); bool receive(int slot); @@ -120,7 +120,7 @@ namespace thread #endif Mutex* mutex; // mutex variable Conditional* condition; // condition variable - ThreadRunner* threadRunner; // thread function + ThreadRunner threadRunner; // thread function ThreadData* common; // threads common data const std::string name; // thread name, for debugging purposes /** @@ -154,8 +154,6 @@ namespace thread */ bool running; // running - static int ThreadFunciton(void* p); - }; } // thread diff --git a/src/lua/graphics/luaopen_Font.cpp b/src/lua/graphics/luaopen_Font.cpp index 3643412..8585d46 100644 --- a/src/lua/graphics/luaopen_Font.cpp +++ b/src/lua/graphics/luaopen_Font.cpp @@ -11,7 +11,7 @@ namespace lua static int l_gc(lua_State* L) { - Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_FONT, sizeof(Proxy)); + Proxy* proxy = (Proxy*)luax_checktype(L, 1, JIN_GRAPHICS_FONT); proxy->release(); return 0; } diff --git a/src/lua/luaopen_types.h b/src/lua/luaopen_types.h index 74c658f..b32d849 100644 --- a/src/lua/luaopen_types.h +++ b/src/lua/luaopen_types.h @@ -61,7 +61,6 @@ namespace lua { if (obj == nullptr) return; - obj->retain(); object = obj; type = t; } diff --git a/src/lua/net/Socket.h b/src/lua/net/Socket.h index 5834092..e3ef9c2 100644 --- a/src/lua/net/Socket.h +++ b/src/lua/net/Socket.h @@ -16,7 +16,6 @@ namespace net class Socket : public Object { public: - Socket() {} Socket(SocketInformation info) { @@ -63,6 +62,7 @@ namespace net private: jin::net::Socket* socket; + Socket() {} ~Socket() { delete socket; diff --git a/src/lua/net/luaopen_Buffer.cpp b/src/lua/net/luaopen_Buffer.cpp index 0ab47cb..6d42c86 100644 --- a/src/lua/net/luaopen_Buffer.cpp +++ b/src/lua/net/luaopen_Buffer.cpp @@ -116,7 +116,7 @@ namespace net } static const luaL_Reg netbuffer_function[] = { - { "__gc", l_gc }, + { "__gc", l_gc }, { "append", l_append }, { "grabString", l_grabString }, { "grabInteger", l_grabInteger }, diff --git a/src/lua/net/luaopen_Socket.cpp b/src/lua/net/luaopen_Socket.cpp index abcd8f2..5ccdaa0 100644 --- a/src/lua/net/luaopen_Socket.cpp +++ b/src/lua/net/luaopen_Socket.cpp @@ -45,8 +45,8 @@ namespace lua Socket* socket = checkSocket(L); char buffer[BUFFER_SIZE] = {0}; int size = socket->receive(buffer, BUFFER_SIZE); - net::Buffer* netBuffer = new net::Buffer(buffer, size); Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_NETWORK_BUFFER, sizeof(Proxy)); + net::Buffer* netBuffer = new net::Buffer(buffer, size); proxy->bind(netBuffer, JIN_NETWORK_BUFFER); return 1; } diff --git a/src/lua/net/luaopen_net.cpp b/src/lua/net/luaopen_net.cpp index 13d24dd..7ec9cc7 100644 --- a/src/lua/net/luaopen_net.cpp +++ b/src/lua/net/luaopen_net.cpp @@ -19,33 +19,35 @@ namespace lua // jin.net.Socket() static int l_Socket(lua_State* L) { - const char* socketType = luax_checkstring(L, 1); SocketInformation info = { 0 }; - if (strcmp(socketType, "TCP") == 0) - info.type = SocketType::TCP; - else if (strcmp(socketType, "UDP") == 0) - info.type = SocketType::UDP; - else { - luax_error(L, "jin.net.Socket() first paramter wrong, must be TCP or UDP"); - return 0; + const char* socketType = luax_checkstring(L, 1); + if (strcmp(socketType, "TCP") == 0) + info.type = SocketType::TCP; + else if (strcmp(socketType, "UDP") == 0) + info.type = SocketType::UDP; + else + { + luax_error(L, "jin.net.Socket() first paramter wrong, must be TCP or UDP"); + return 0; + } + // type, port + if (luax_gettop(L) == 2) + { + info.port = luax_checkinteger(L, 2); + } + // type, address, port + else if (luax_gettop(L) == 3) + { + if (luax_isstringstrict(L, 2)) + info.address = tk_strtohl(luax_checkstring(L, 2)); + else if (luax_isintegerstrict(L, 2)) + info.address = luax_checkinteger(L, 2); + info.port = luax_checkinteger(L, 3); + } } - // type, port - if (luax_gettop(L) == 2) - { - info.port = luax_checkinteger(L, 2); - } - // type, address, port - else if (luax_gettop(L) == 3) - { - if (luax_isstringstrict(L, 2)) - info.address = tk_strtohl(luax_checkstring(L, 2)); - else if(luax_isintegerstrict(L, 2)) - info.address = luax_checkinteger(L, 2); - info.port = luax_checkinteger(L, 3); - } - Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_NETWORK_SOCKET, sizeof(Proxy)); Socket* socket = new Socket(info); + Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_NETWORK_SOCKET, sizeof(Proxy)); proxy->bind(socket, JIN_NETWORK_SOCKET); return 1; } diff --git a/src/lua/thread/Thread.h b/src/lua/thread/Thread.h index 63f7524..6969fd8 100644 --- a/src/lua/thread/Thread.h +++ b/src/lua/thread/Thread.h @@ -12,17 +12,18 @@ namespace thread { public: typedef jin::thread::Thread::Variant Variant; + typedef jin::thread::Thread::ThreadRunner ThreadRunner; - Thread(std::string _name, std::string _code, jin::thread::Thread::ThreadRunner runner) + Thread(std::string _name, std::string _code, ThreadRunner runner) : name(_name) , code(_code) { thread = new jin::thread::Thread(_name, runner); } - bool start() + bool start(void* p) { - return thread->start(); + return thread->start(p); } void wait() @@ -75,7 +76,10 @@ namespace thread thread->unlock(); } - static void threadRunner(jin::thread::Thread* t); + static void threadRunner(Thread* t); + + const std::string name; + const std::string code; private: ~Thread() @@ -85,8 +89,6 @@ namespace thread jin::thread::Thread* thread; - const std::string name; - const std::string code; }; } // thread diff --git a/src/lua/thread/luaopen_Thread.cpp b/src/lua/thread/luaopen_Thread.cpp index 70a30ee..b6fb33b 100644 --- a/src/lua/thread/luaopen_Thread.cpp +++ b/src/lua/thread/luaopen_Thread.cpp @@ -21,7 +21,7 @@ namespace lua return nullptr; } - void Thread::threadRunner(jin::thread::Thread* t) + static int threadRunner(void* t) { Thread* thread = (Thread*)t; lua_State* L = lua_open(); @@ -29,10 +29,12 @@ namespace lua luaopen_jin(L); luax_getglobal(L, MODULE_NAME); Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_THREAD_THREAD, sizeof(Proxy)); + thread->retain(); proxy->bind(thread, JIN_THREAD_THREAD); luax_setfield(L, -2, "_curThread"); luax_dostring(L, thread->code.c_str()); luax_close(L); + return 0; } static int l_thread_gc(lua_State* L) @@ -45,7 +47,7 @@ namespace lua static int l_start(lua_State* L) { Thread* t = checkThread(L); - bool result = t->start(); + bool result = t->start(t); luax_pushboolean(L, result); return 1; } @@ -125,6 +127,7 @@ namespace lua case thread::Thread::Variant::POINTER: Proxy* p = (Proxy*)v.pointer; Proxy* proxy = (Proxy*)luax_newinstance(L, p->type, sizeof(Proxy)); + p->object->retain(); proxy->bind(p->object, p->type); break; @@ -158,6 +161,7 @@ namespace lua case thread::Thread::Variant::POINTER: Proxy* p = (Proxy*)v.pointer; Proxy* proxy = (Proxy*)luax_newinstance(L, p->type, sizeof(Proxy)); + p->object->retain(); proxy->bind(p->object, p->type); break; @@ -216,7 +220,7 @@ namespace lua const char* name = luax_checkstring(L, 1); const char* code = luax_checkstring(L, 2); Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_THREAD_THREAD, sizeof(Proxy)); - Thread* thread = new Thread(name, code, Thread::threadRunner); + Thread* thread = new Thread(name, code, threadRunner); proxy->bind(thread, JIN_THREAD_THREAD); return 1; } diff --git a/src/lua/thread/luaopen_thread.cpp b/src/lua/thread/luaopen_thread.cpp index 70a30ee..b6fb33b 100644 --- a/src/lua/thread/luaopen_thread.cpp +++ b/src/lua/thread/luaopen_thread.cpp @@ -21,7 +21,7 @@ namespace lua return nullptr; } - void Thread::threadRunner(jin::thread::Thread* t) + static int threadRunner(void* t) { Thread* thread = (Thread*)t; lua_State* L = lua_open(); @@ -29,10 +29,12 @@ namespace lua luaopen_jin(L); luax_getglobal(L, MODULE_NAME); Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_THREAD_THREAD, sizeof(Proxy)); + thread->retain(); proxy->bind(thread, JIN_THREAD_THREAD); luax_setfield(L, -2, "_curThread"); luax_dostring(L, thread->code.c_str()); luax_close(L); + return 0; } static int l_thread_gc(lua_State* L) @@ -45,7 +47,7 @@ namespace lua static int l_start(lua_State* L) { Thread* t = checkThread(L); - bool result = t->start(); + bool result = t->start(t); luax_pushboolean(L, result); return 1; } @@ -125,6 +127,7 @@ namespace lua case thread::Thread::Variant::POINTER: Proxy* p = (Proxy*)v.pointer; Proxy* proxy = (Proxy*)luax_newinstance(L, p->type, sizeof(Proxy)); + p->object->retain(); proxy->bind(p->object, p->type); break; @@ -158,6 +161,7 @@ namespace lua case thread::Thread::Variant::POINTER: Proxy* p = (Proxy*)v.pointer; Proxy* proxy = (Proxy*)luax_newinstance(L, p->type, sizeof(Proxy)); + p->object->retain(); proxy->bind(p->object, p->type); break; @@ -216,7 +220,7 @@ namespace lua const char* name = luax_checkstring(L, 1); const char* code = luax_checkstring(L, 2); Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_THREAD_THREAD, sizeof(Proxy)); - Thread* thread = new Thread(name, code, Thread::threadRunner); + Thread* thread = new Thread(name, code, threadRunner); proxy->bind(thread, JIN_THREAD_THREAD); return 1; } |