use sproto global slot

This commit is contained in:
Cloud Wu
2015-02-28 13:33:56 +08:00
parent 318ee300e3
commit 688cc843ce
8 changed files with 86 additions and 40 deletions

View File

@@ -9,24 +9,6 @@
#include <string.h>
#include <stdbool.h>
#if defined(_WIN32) || defined(_WIN64)
#include <winsock2.h>
static void
init_winsock() {
WSADATA wsaData;
WSAStartup(MAKEWORD(2,2), &wsaData);
}
#else
static void
init_winsock() {
}
#endif
#define DEFAULT_CAP 64
#define MAX_NUMBER 1024
@@ -1100,7 +1082,10 @@ static uint32_t oid_counter;
static void
init_oid_header() {
init_winsock();
if (oid_counter) {
// already init
return;
}
pid_t pid = getpid();
uint32_t h = 0;
char hostname[256];
@@ -1116,8 +1101,12 @@ init_oid_header() {
oid_header[2] = (h>>16) & 0xff;
oid_header[3] = pid & 0xff;
oid_header[4] = (pid >> 8) & 0xff;
oid_counter = h ^ time(NULL) ^ (uintptr_t)&h;
uint32_t c = h ^ time(NULL) ^ (uintptr_t)&h;
if (c == 0) {
c = 1;
}
oid_counter = c;
}
static inline int

View File

@@ -6,6 +6,7 @@
#include "lauxlib.h"
#include "sproto.h"
#define MAX_GLOBALSPROTO 16
#define ENCODE_BUFFERSIZE 2050
//#define ENCODE_BUFFERSIZE 2050
@@ -458,27 +459,46 @@ lprotocol(lua_State *L) {
}
/* global sproto pointer for multi states */
static void * G_sproto = NULL;
static size_t G_sproto_sz = 0;
struct sproto_bin {
void *ptr;
size_t sz;
};
static struct sproto_bin G_sproto[MAX_GLOBALSPROTO];
static int
lsaveproto(lua_State *L) {
size_t sz;
void * buffer = (void *)luaL_checklstring(L,1,&sz);
void * tmp = malloc(sz);
memcpy(tmp, buffer, sz);
if (G_sproto) {
free(G_sproto);
int index = luaL_optinteger(L, 2, 0);
void * tmp;
struct sproto_bin * sbin = &G_sproto[index];
if (index < 0 || index >= MAX_GLOBALSPROTO) {
return luaL_error(L, "Invalid global slot index %d", index);
}
G_sproto = tmp;
G_sproto_sz = sz;
tmp = malloc(sz);
memcpy(tmp, buffer, sz);
if (sbin->ptr) {
free(sbin->ptr);
}
sbin->ptr = tmp;
sbin->sz = sz;
return 0;
}
static int
lloadproto(lua_State *L) {
lua_pushlightuserdata(L, G_sproto);
lua_pushinteger(L, G_sproto_sz);
int index = luaL_optinteger(L, 1, 0);
struct sproto_bin * sbin = &G_sproto[index];
if (index < 0 || index >= MAX_GLOBALSPROTO) {
return luaL_error(L, "Invalid global slot index %d", index);
}
if (sbin->ptr == NULL) {
return luaL_error(L, "nil sproto at index %d", index);
}
lua_pushlightuserdata(L, sbin->ptr);
lua_pushinteger(L, sbin->sz);
return 2;
}