aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2018-08-09 22:19:28 +0800
committerchai <chaifix@163.com>2018-08-09 22:19:28 +0800
commit5fe41eca99adf4bf0fb5832033a96f98b530d4f1 (patch)
treef5144fce7ab194d122f84bf0de820b6b2c9525fa
parenta03a35b6cfe6399ceaff86a1cc035f1131427955 (diff)
*update
-rw-r--r--bin/jin.exebin817664 -> 824320 bytes
-rw-r--r--bin/main.lua15
-rw-r--r--build/vs2015/jin.vcxproj7
-rw-r--r--build/vs2015/jin.vcxproj.filters9
-rw-r--r--src/3rdparty/luax/luax.h13
-rw-r--r--src/lua/bit/luaopen_bit.cpp21
-rw-r--r--src/lua/embed/net.lua.h27
-rw-r--r--src/lua/math/luaopen_math.cpp10
-rw-r--r--src/lua/net/luaopen_Buffer.cpp191
9 files changed, 271 insertions, 22 deletions
diff --git a/bin/jin.exe b/bin/jin.exe
index 5605a29..0d065ea 100644
--- a/bin/jin.exe
+++ b/bin/jin.exe
Binary files differ
diff --git a/bin/main.lua b/bin/main.lua
index 4ef3d14..48bdd9a 100644
--- a/bin/main.lua
+++ b/bin/main.lua
@@ -33,6 +33,21 @@ jin.core.onLoad = function()
thread:send(2, "test thread data")
EventMsgCenter.unregisterAllMsgByEvent(Events.Player_Move)
end)
+
+----------------------------------------------------
+-- jin.bit测试
+local size = 0
+local buff = ""
+-- buff, size = jin.bit.write(buff, size, "test")
+-- buff, size = jin.bit.write(buff, size, "hello")
+buff, size = jin.bit.write(buff, size, "hello,world!")
+buff, size = jin.bit.write(buff, size, true)
+buff, size = jin.bit.write(buff, size, 250)
+local str, len = jin.bit.grabstring(buff, size)
+print(str)
+buff, size = jin.bit.shift(buff, size, len + 1)
+local a = jin.bit.grabboolean(buff, size)
+print(a)
end
jin.core.onEvent = function(e)
diff --git a/build/vs2015/jin.vcxproj b/build/vs2015/jin.vcxproj
index 5687630..5b141be 100644
--- a/build/vs2015/jin.vcxproj
+++ b/build/vs2015/jin.vcxproj
@@ -89,7 +89,7 @@
<Link>
<AdditionalDependencies>opengl32.lib;glu32.lib;SDL2main.lib;SDL2.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalLibraryDirectories>$(SolutionDir)libs\SDL2-2.0.5\lib\x86;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
- <SubSystem>Windows</SubSystem>
+ <SubSystem>Console</SubSystem>
<EntryPointSymbol>
</EntryPointSymbol>
</Link>
@@ -118,7 +118,7 @@
<OptimizeReferences>true</OptimizeReferences>
<AdditionalDependencies>opengl32.lib;glu32.lib;SDL2main.lib;SDL2.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalLibraryDirectories>$(SolutionDir)libs\SDL2-2.0.5\lib\x86;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
- <SubSystem>Windows</SubSystem>
+ <SubSystem>Console</SubSystem>
<EntryPointSymbol>
</EntryPointSymbol>
</Link>
@@ -202,6 +202,7 @@
<ClCompile Include="..\..\src\libjin\Utils\unittest.cpp" />
<ClCompile Include="..\..\src\lua\audio\luaopen_audio.cpp" />
<ClCompile Include="..\..\src\lua\audio\luaopen_Source.cpp" />
+ <ClCompile Include="..\..\src\lua\bit\luaopen_bit.cpp" />
<ClCompile Include="..\..\src\lua\core\luaopen_core.cpp" />
<ClCompile Include="..\..\src\lua\debug\luaopen_debug.cpp" />
<ClCompile Include="..\..\src\lua\event\luaopen_event.cpp" />
@@ -216,6 +217,7 @@
<ClCompile Include="..\..\src\lua\math\luaopen_math.cpp" />
<ClCompile Include="..\..\src\lua\mouse\luaopen_mouse.cpp" />
<ClCompile Include="..\..\src\lua\luaopen_jin.cpp" />
+ <ClCompile Include="..\..\src\lua\net\luaopen_Buffer.cpp" />
<ClCompile Include="..\..\src\lua\net\luaopen_net.cpp" />
<ClCompile Include="..\..\src\lua\thread\luaopen_thread.cpp" />
<ClCompile Include="..\..\src\lua\time\luaopen_time.cpp" />
@@ -306,7 +308,6 @@
<ClInclude Include="..\..\src\libjin\Utils\Log.h" />
<ClInclude Include="..\..\src\libjin\Utils\macros.h" />
<ClInclude Include="..\..\src\libjin\Utils\utils.h" />
- <ClInclude Include="..\..\src\lua\bit\luaopen_bit.cpp" />
<ClInclude Include="..\..\src\lua\embed\debug.lua.h" />
<ClInclude Include="..\..\src\lua\embed\embed.h" />
<ClInclude Include="..\..\src\lua\embed\graphics.lua.h" />
diff --git a/build/vs2015/jin.vcxproj.filters b/build/vs2015/jin.vcxproj.filters
index b668960..e9cd37e 100644
--- a/build/vs2015/jin.vcxproj.filters
+++ b/build/vs2015/jin.vcxproj.filters
@@ -399,6 +399,12 @@
<ClCompile Include="..\..\src\libjin\Net\Socket.cpp">
<Filter>src\libjin\Net</Filter>
</ClCompile>
+ <ClCompile Include="..\..\src\lua\bit\luaopen_bit.cpp">
+ <Filter>src\lua\bit</Filter>
+ </ClCompile>
+ <ClCompile Include="..\..\src\lua\net\luaopen_Buffer.cpp">
+ <Filter>src\lua\net</Filter>
+ </ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\src\3rdparty\lua51\lapi.h">
@@ -692,9 +698,6 @@
<ClInclude Include="..\..\src\lua\net\luaopen_Socket.cpp">
<Filter>src\lua\net</Filter>
</ClInclude>
- <ClInclude Include="..\..\src\lua\bit\luaopen_bit.cpp">
- <Filter>src\lua\bit</Filter>
- </ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="..\..\src\3rdparty\lua51\Makefile">
diff --git a/src/3rdparty/luax/luax.h b/src/3rdparty/luax/luax.h
index 94cc2e3..69b9669 100644
--- a/src/3rdparty/luax/luax.h
+++ b/src/3rdparty/luax/luax.h
@@ -61,6 +61,7 @@
#define luax_checknumber luaL_checknumber
#define luax_checkinteger luaL_checkinteger
#define luax_checkstring luaL_checkstring
+#define luax_checklstring luaL_checklstring
//#define luax_checkbool luaL_checkinteger
inline bool luax_checkbool(lua_State *L, int numArg)
{
@@ -248,6 +249,18 @@ inline int luax_istype(lua_State* L, int idx, const char* tname)
#define luax_istable(L, i) luax_is(table, L, i)
#define luax_isnil(L, i) luax_is(nil, L, i)
#define luax_isboolean(L, i) luax_is(boolean, L, i)
+inline int luax_isinteger(lua_State* L, int i)
+{
+ if (!luax_isnumber(L, i))
+ return 0;
+ return lua_tonumber(L, i) == lua_tointeger(L, i);
+}
+inline int luax_isfloat(lua_State* L, int i)
+{
+ if (!luax_isnumber(L, i))
+ return 0;
+ return lua_tonumber(L, i) != lua_tointeger(L, i);
+}
/**
* To userdata.
*/
diff --git a/src/lua/bit/luaopen_bit.cpp b/src/lua/bit/luaopen_bit.cpp
index fc3c188..e3a7bbb 100644
--- a/src/lua/bit/luaopen_bit.cpp
+++ b/src/lua/bit/luaopen_bit.cpp
@@ -1,5 +1,6 @@
#include "lua/luax.h"
#include "libjin/jin.h"
+#include <cstdlib>
namespace jin
{
@@ -61,12 +62,6 @@ namespace lua
return 1;
}
- // return buffer, size
- static int l_write(lua_State* L)
- {
-
- }
-
static const luaL_Reg f[] = {
{ "AND", l_and },
{ "OR" , l_or },
@@ -76,7 +71,18 @@ namespace lua
{ "RS", l_rshift },
{ "INC", l_include },
- { "write", l_write},
+ //{ "Buffer", l_newBuffer }, // ײ
+
+ { "buffer", l_buffer },
+ { "write", l_write },
+ { "shift", l_shift },
+
+ { "grabstring", l_grabstring },
+ { "grabinteger", l_grabinteger },
+ { "grabfloat", l_grabfloat },
+ { "grabboolean", l_grabboolean },
+
+ // , offset
{ 0, 0 }
};
@@ -84,6 +90,7 @@ namespace lua
int luaopen_bit(lua_State* L)
{
luax_newlib(L, f);
+
return 1;
}
diff --git a/src/lua/embed/net.lua.h b/src/lua/embed/net.lua.h
index 73b60c3..3db9264 100644
--- a/src/lua/embed/net.lua.h
+++ b/src/lua/embed/net.lua.h
@@ -4,10 +4,10 @@ jin.net = jin.net or {}
--[[
socketͨŵ
-* INT
-* FLOAT
-* BOOL
-* STRING
+* INT 32
+* FLOAT 32
+* BOOL 32
+* STRING --
STRINGжҽ磬0β
һЭ鶨ӣðЭͳһһluaļ
-- s2c_package.lua
@@ -29,10 +29,19 @@ local data, size = Socket:receive()
local message = jin.net.deserialize(Message.Skill, data, size)
]]
-jin.net.INT = 1
-jin.net.FLOAT = 2
-jin.net.BOOL = 3
-jin.net.STRING = 4
+jin.net.dataType = {
+ INT = 1,
+ FLOAT = 2,
+ BOOL = 3,
+ STRING = 4
+}
+
+jin.net.dataSize = {
+ INT = 4,
+ FLOAT = 4,
+ BOOL = 4,
+ STRING = -1,
+}
jin.net.deserialize = function(prototype, data, size)
local message = {}
@@ -55,7 +64,7 @@ end
-- Э
jin.net.decode = function()
-
+ local s = jin.bit.grabstring(buffer, size)
end
)"; \ No newline at end of file
diff --git a/src/lua/math/luaopen_math.cpp b/src/lua/math/luaopen_math.cpp
index e4f68df..462e8d1 100644
--- a/src/lua/math/luaopen_math.cpp
+++ b/src/lua/math/luaopen_math.cpp
@@ -6,7 +6,17 @@ namespace jin
namespace lua
{
+ static int l_mod(lua_State* L)
+ {
+ int n = luax_checkinteger(L, 1);
+ int m = luax_checkinteger(L, 2);
+ int mod = n % m;
+ luax_pushinteger(L, mod);
+ return 1;
+ }
+
static const luaL_Reg f[] = {
+ { "mod", l_mod },
{ 0, 0 }
};
diff --git a/src/lua/net/luaopen_Buffer.cpp b/src/lua/net/luaopen_Buffer.cpp
new file mode 100644
index 0000000..6e10f25
--- /dev/null
+++ b/src/lua/net/luaopen_Buffer.cpp
@@ -0,0 +1,191 @@
+#include "lua/luax.h"
+#include "../luaopen_types.h"
+#include "libjin/jin.h"
+
+namespace jin
+{
+namespace lua
+{
+namespace net
+{
+
+ class Buffer
+ {
+ public:
+ Buffer(size_t size);
+ Buffer(const char* data, size_t size);
+ ~Buffer()
+ {
+ }
+
+ void append(char* data, size_t size)
+ {
+
+ }
+
+ const char* grabString(int offset = 0)
+ {
+ int l = offset;
+ for (; l < size; ++l)
+ {
+ if (buffer[l] == 0)
+ {
+
+ }
+ }
+ return nullptr;
+ }
+
+ int grabInteger(int offset = 0)
+ {
+
+ }
+
+ float grabfloat(int offset = 0)
+ {
+
+ }
+
+ bool grabboolean(int offset = 0)
+ {
+
+ }
+
+ private:
+ char* buffer;
+ size_t size;
+
+ };
+
+ static int l_buffer(lua_State* L)
+ {
+ int size = luax_checkinteger(L, 1);
+ char* buffer = (char*)malloc(size);
+ memset(buffer, 0, size);
+ luax_pushlstring(L, buffer, size);
+ return 1;
+ }
+
+ // return buffer, size
+ static int l_write(lua_State* L)
+ {
+ const char* data = luax_checklstring(L, 1, NULL);
+ int len = luax_checkinteger(L, 2);
+ if (luax_isinteger(L, 3))
+ {
+ int n = luax_checkinteger(L, 3);
+ int size = len + sizeof(n);
+ char* buffer = (char*)malloc(size);
+ memcpy(buffer, data, len);
+ memcpy(buffer + len, &n, sizeof(n));
+ //free((void*)data);
+ luax_pushlstring(L, buffer, size);
+ luax_pushinteger(L, size);
+ return 2;
+ }
+ else if (luax_isfloat(L, 3))
+ {
+ float n = luax_checknumber(L, 3);
+ int size = len + sizeof(n);
+ char* buffer = (char*)malloc(size);
+ memcpy(buffer, data, len);
+ memcpy(buffer + len, &n, sizeof(n));
+ //free((void*)data);
+ luax_pushlstring(L, buffer, size);
+ luax_pushinteger(L, size);
+ return 2;
+ }
+ else if (luax_isboolean(L, 3))
+ {
+ bool b = luax_checkbool(L, 3);
+ int size = len + sizeof(b);
+ char* buffer = (char*)malloc(size);
+ memcpy(buffer, data, len);
+ memcpy(buffer + len, &b, sizeof(b));
+ //free((void*)data);
+ luax_pushlstring(L, buffer, size);
+ luax_pushinteger(L, size);
+ return 2;
+ }
+ else if (luax_isstring(L, 3))
+ {
+ const char* s = luax_checkstring(L, 3);
+ int l = strlen(s) + 1; // with \0
+ int size = len + l;
+ char* buffer = (char*)malloc(size);
+ memcpy(buffer, data, len);
+ memcpy(buffer + len, s, l);
+ luax_pushlstring(L, buffer, size);
+ luax_pushinteger(L, size);
+ return 2;
+ }
+ else
+ {
+ luax_typerror(L, 3, "number, bool or string");
+ return 0;
+ }
+ }
+
+ // jin.shift(buffer, shift)
+ static int l_shift(lua_State* L)
+ {
+ const char* buffer = luax_checklstring(L, 1, NULL);
+ int size = luax_checkinteger(L, 2);
+ int shift = luax_checkinteger(L, 3);
+ int ss = size - shift;
+ luax_pushlstring(L, buffer + shift, ss);
+ luax_pushinteger(L, ss);
+ return 2;
+ }
+
+ // jin.bit.grabstring(buffer, size)
+ static int l_grabstring(lua_State* L)
+ {
+ const char* buffer = luax_checklstring(L, 1, NULL);
+ int size = luax_checkinteger(L, 2);
+ int l = 0;
+ for (; l < size; ++l)
+ {
+ if (buffer[l] == 0)
+ {
+ int len = l + 1;
+ char* str = (char*)malloc(len);
+ memcpy(str, buffer, len);
+ luax_pushstring(L, str);
+ luax_pushinteger(L, l);
+ return 2;
+ }
+ }
+ return 0;
+ }
+
+ static int l_grabinteger(lua_State* L)
+ {
+ const char* buffer = luax_checklstring(L, 1, NULL);
+ int size = luax_checkinteger(L, 2);
+ int n = *((int*)buffer);
+ luax_pushinteger(L, n);
+ return 1;
+ }
+
+ static int l_grabfloat(lua_State* L)
+ {
+ const char* buffer = luax_checklstring(L, 1, NULL);
+ int size = luax_checkinteger(L, 2);
+ float n = *((float*)buffer);
+ luax_pushnumber(L, n);
+ return 1;
+ }
+
+ static int l_grabboolean(lua_State* L)
+ {
+ const char* buffer = luax_checklstring(L, 1, NULL);
+ int size = luax_checkinteger(L, 2);
+ bool n = *((bool*)buffer);
+ luax_pushboolean(L, n);
+ return 1;
+ }
+
+} // net
+} // lua
+} // jin \ No newline at end of file