Skip to content

Commit

Permalink
websocket 压缩格式
Browse files Browse the repository at this point in the history
  • Loading branch information
sucer committed Apr 17, 2019
1 parent f96c87f commit d3d1e0b
Show file tree
Hide file tree
Showing 8 changed files with 609 additions and 16 deletions.
5 changes: 4 additions & 1 deletion game_room/luaclib-src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 10,7 @@ MYSQL_INC = /usr/include/mysql/

.PHONY : all clean

all : depend protobuf.so pbcc.so skynetHelper.so sui.so memory.so mysqlutil.so websocketnetpack.so
all : depend protobuf.so pbcc.so skynetHelper.so sui.so memory.so mysqlutil.so websocketnetpack.so gzip.so

depend :
cd lua-arc4random && make
Expand Down Expand Up @@ -40,6 40,9 @@ mysqlutil.so : lua-mysqlutil.c
websocketnetpack.so : lua-websocketnetpack.c
gcc -g -O2 -Wall -I$(LUA_INC) -fPIC --shared -I$(SKYNET_INC) $^ -o $@

gzip.so : lua-gzip.c
gcc -g -O2 -Wall -I$(LUA_INC) -fPIC --shared $^ -o $@ -lzlib

clean :
cd lua-cjson && make clean
cd lua-arc4random && make clean
Expand Down
134 changes: 134 additions & 0 deletions game_room/luaclib-src/lua-gzip.c
Original file line number Diff line number Diff line change
@@ -0,0 1,134 @@
#include <stdlib.h>
#include <stdio.h>
#include <memory.h>
#include <errno.h>
#include <lua.h>
#include <lauxlib.h>
#include <zlib.h>

#define GZIP_WBITS 15 16
#define GZIP_HEADER_BOUND 16
#define DEF_MEM_LEVEL 8
#define GROWTH_MULTIPLIER 2

static int gzip_compress(lua_State *L) {
size_t dataLen;
unsigned char *const data = (unsigned char *) luaL_checklstring(L, 1, &dataLen);
const int level = luaL_optinteger(L, 2, Z_DEFAULT_COMPRESSION);

const unsigned int outputLen = compressBound(dataLen) GZIP_HEADER_BOUND;
unsigned char *const output = malloc(outputLen);
if (!output) {
return luaL_error(L, "malloc failed: %d", errno);
}

z_stream stream;
memset(&stream, 0, sizeof(stream));
stream.next_in = data;
stream.avail_in = dataLen;
stream.next_out = output;
stream.avail_out = outputLen;
const int err1 = deflateInit2(&stream, level, Z_DEFLATED, GZIP_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY);
if (err1 != Z_OK) {
free(output);
return luaL_error(L, "deflateInit2 failed: %d", err1);
}

gz_header header;
memset(&header, 0, sizeof(header));
const int err2 = deflateSetHeader(&stream, &header);
if (err2 != Z_OK) {
free(output);
return luaL_error(L, "deflateSetHeader failed: %d", err2);
}

const int err3 = deflate(&stream, Z_FINISH);
const int err4 = deflateEnd(&stream);
if (err3 == Z_STREAM_END && err4 == Z_OK)
{
const char * ptroutput = (const char *)output;
lua_pushlstring(L, ptroutput, stream.total_out);
free(output);
return 1;
}
free(output);

if (err3 == Z_OK) {
return luaL_error(L, "deflate failed: insufficient buffer.");
} else if (err3 != Z_STREAM_END) {
return luaL_error(L, "deflate failed: %d", err3);
} else {
return luaL_error(L, "deflateEnd failed: %d", err4);
}
}

static int gzip_decompress(lua_State *L) {
size_t dataLen;
unsigned char *const data = (unsigned char*) luaL_checklstring(L, 1, &dataLen);

size_t outputLen = dataLen*GROWTH_MULTIPLIER;
unsigned char *output = malloc(outputLen);
if (!output) {
return luaL_error(L, "malloc failed: %d", errno);
}

z_stream stream;
memset(&stream, 0, sizeof(stream));
stream.next_in = data;
stream.avail_in = dataLen;
stream.next_out = output;
stream.avail_out = outputLen;
const int err1 = inflateInit2(&stream, GZIP_WBITS);
if (err1 != Z_OK) {
free(output);
return luaL_error(L, "inflateInit2 failed: %d", err1);
}

for (;;) {
const int err2 = inflate(&stream, Z_FINISH);
if (err2 == Z_BUF_ERROR && stream.avail_in > 0) {
outputLen *= GROWTH_MULTIPLIER;
unsigned char *const output2 = realloc(output, outputLen);
if (!output2) {
free(output);
return luaL_error(L, "realloc failed: %d", errno);
}
output = output2;
stream.next_out = output stream.total_out;
stream.avail_out = outputLen - stream.total_out;
continue;
}

const int err3 = inflateEnd(&stream);
if (err2 == Z_STREAM_END && err3 == Z_OK) {
const char * ptroutput = (const char *)output;
lua_pushlstring(L, ptroutput, stream.total_out);
free(output);
return 1;
}
free(output);

if (err2 != Z_STREAM_END) {
return luaL_error(L, "inflate failed: %d", err2);
} else {
return luaL_error(L, "inflateEnd failed: %d", err3);
}
}
}

/*
gzip.compress(data) -> (data)
gzip.decompress(data) -> (data)
*/

int luaopen_gzip(lua_State *L)
{
luaL_checkversion(L);
luaL_Reg l[] = {
{ "compress", gzip_compress },
{ "decompress", gzip_decompress },
{ NULL, NULL },
};
luaL_newlib(L, l);
return 1;
}
1 change: 0 additions & 1 deletion game_room/lualib/utility/gateserver_ws.lua
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 185,6 @@ function gateserver.start(handler)
gateserver.closeclient(fd)
return
end

handler.message(fd, msg, sz)
else
skynet.error(string.format("Drop message from fd (%d) : %s", fd, netpack.tostring(msg,sz)))
Expand Down
Loading

0 comments on commit d3d1e0b

Please sign in to comment.