Moving from govendor to dep, updated dependencies (#48)
* Moving from govendor to dep. * Making the pull request template more friendly. * Fixing akward space in PR template. * goimports run on whole project using ` goimports -w $(find . -type f -name '*.go' -not -path "./vendor/*" -not -path "./gen-go/*")` source of command: https://gist.github.com/bgentry/fd1ffef7dbde01857f66
This commit is contained in:
parent
9631aa3aab
commit
8d445c1c77
2186 changed files with 400410 additions and 352 deletions
47
vendor/git.apache.org/thrift.git/lib/lua/src/longnumberutils.c
generated
vendored
Normal file
47
vendor/git.apache.org/thrift.git/lib/lua/src/longnumberutils.c
generated
vendored
Normal file
|
@ -0,0 +1,47 @@
|
|||
//
|
||||
// Licensed to the Apache Software Foundation (ASF) under one
|
||||
// or more contributor license agreements. See the NOTICE file
|
||||
// distributed with this work for additional information
|
||||
// regarding copyright ownership. The ASF licenses this file
|
||||
// to you under the Apache License, Version 2.0 (the
|
||||
// "License"); you may not use this file except in compliance
|
||||
// with the License. You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing,
|
||||
// software distributed under the License is distributed on an
|
||||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the License for the
|
||||
// specific language governing permissions and limitations
|
||||
// under the License.
|
||||
//
|
||||
|
||||
#include <lua.h>
|
||||
#include <lauxlib.h>
|
||||
#include <stdlib.h>
|
||||
#include <inttypes.h>
|
||||
|
||||
const char * LONG_NUM_TYPE = "__thrift_longnumber";
|
||||
int64_t lualongnumber_checklong(lua_State *L, int index) {
|
||||
switch (lua_type(L, index)) {
|
||||
case LUA_TNUMBER:
|
||||
return (int64_t)lua_tonumber(L, index);
|
||||
case LUA_TSTRING:
|
||||
return atoll(lua_tostring(L, index));
|
||||
default:
|
||||
return *((int64_t *)luaL_checkudata(L, index, LONG_NUM_TYPE));
|
||||
}
|
||||
}
|
||||
|
||||
// Creates a new longnumber and pushes it onto the statck
|
||||
int64_t * lualongnumber_pushlong(lua_State *L, int64_t *val) {
|
||||
int64_t *data = (int64_t *)lua_newuserdata(L, sizeof(int64_t)); // longnum
|
||||
luaL_getmetatable(L, LONG_NUM_TYPE); // longnum, mt
|
||||
lua_setmetatable(L, -2); // longnum
|
||||
if (val) {
|
||||
*data = *val;
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
83
vendor/git.apache.org/thrift.git/lib/lua/src/luabitwise.c
generated
vendored
Normal file
83
vendor/git.apache.org/thrift.git/lib/lua/src/luabitwise.c
generated
vendored
Normal file
|
@ -0,0 +1,83 @@
|
|||
//
|
||||
// Licensed to the Apache Software Foundation (ASF) under one
|
||||
// or more contributor license agreements. See the NOTICE file
|
||||
// distributed with this work for additional information
|
||||
// regarding copyright ownership. The ASF licenses this file
|
||||
// to you under the Apache License, Version 2.0 (the
|
||||
// "License"); you may not use this file except in compliance
|
||||
// with the License. You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing,
|
||||
// software distributed under the License is distributed on an
|
||||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the License for the
|
||||
// specific language governing permissions and limitations
|
||||
// under the License.
|
||||
//
|
||||
|
||||
#include <lua.h>
|
||||
#include <lauxlib.h>
|
||||
|
||||
static int l_not(lua_State *L) {
|
||||
int a = luaL_checkinteger(L, 1);
|
||||
a = ~a;
|
||||
lua_pushnumber(L, a);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int l_xor(lua_State *L) {
|
||||
int a = luaL_checkinteger(L, 1);
|
||||
int b = luaL_checkinteger(L, 2);
|
||||
a ^= b;
|
||||
lua_pushnumber(L, a);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int l_and(lua_State *L) {
|
||||
int a = luaL_checkinteger(L, 1);
|
||||
int b = luaL_checkinteger(L, 2);
|
||||
a &= b;
|
||||
lua_pushnumber(L, a);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int l_or(lua_State *L) {
|
||||
int a = luaL_checkinteger(L, 1);
|
||||
int b = luaL_checkinteger(L, 2);
|
||||
a |= b;
|
||||
lua_pushnumber(L, a);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int l_shiftr(lua_State *L) {
|
||||
int a = luaL_checkinteger(L, 1);
|
||||
int b = luaL_checkinteger(L, 2);
|
||||
a = a >> b;
|
||||
lua_pushnumber(L, a);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int l_shiftl(lua_State *L) {
|
||||
int a = luaL_checkinteger(L, 1);
|
||||
int b = luaL_checkinteger(L, 2);
|
||||
a = a << b;
|
||||
lua_pushnumber(L, a);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static const struct luaL_Reg funcs[] = {
|
||||
{"band", l_and},
|
||||
{"bor", l_or},
|
||||
{"bxor", l_xor},
|
||||
{"bnot", l_not},
|
||||
{"shiftl", l_shiftl},
|
||||
{"shiftr", l_shiftr},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
int luaopen_libluabitwise(lua_State *L) {
|
||||
luaL_register(L, "libluabitwise", funcs);
|
||||
return 1;
|
||||
}
|
308
vendor/git.apache.org/thrift.git/lib/lua/src/luabpack.c
generated
vendored
Normal file
308
vendor/git.apache.org/thrift.git/lib/lua/src/luabpack.c
generated
vendored
Normal file
|
@ -0,0 +1,308 @@
|
|||
//
|
||||
// Licensed to the Apache Software Foundation (ASF) under one
|
||||
// or more contributor license agreements. See the NOTICE file
|
||||
// distributed with this work for additional information
|
||||
// regarding copyright ownership. The ASF licenses this file
|
||||
// to you under the Apache License, Version 2.0 (the
|
||||
// "License"); you may not use this file except in compliance
|
||||
// with the License. You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing,
|
||||
// software distributed under the License is distributed on an
|
||||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the License for the
|
||||
// specific language governing permissions and limitations
|
||||
// under the License.
|
||||
//
|
||||
|
||||
#include <lua.h>
|
||||
#include <lauxlib.h>
|
||||
#include <string.h>
|
||||
#include <inttypes.h>
|
||||
#include <netinet/in.h>
|
||||
|
||||
extern int64_t lualongnumber_checklong(lua_State *L, int index);
|
||||
extern int64_t lualongnumber_pushlong(lua_State *L, int64_t *val);
|
||||
|
||||
// host order to network order (64-bit)
|
||||
static int64_t T_htonll(uint64_t data) {
|
||||
uint32_t d1 = htonl((uint32_t)data);
|
||||
uint32_t d2 = htonl((uint32_t)(data >> 32));
|
||||
return ((uint64_t)d1 << 32) + (uint64_t)d2;
|
||||
}
|
||||
|
||||
// network order to host order (64-bit)
|
||||
static int64_t T_ntohll(uint64_t data) {
|
||||
uint32_t d1 = ntohl((uint32_t)data);
|
||||
uint32_t d2 = ntohl((uint32_t)(data >> 32));
|
||||
return ((uint64_t)d1 << 32) + (uint64_t)d2;
|
||||
}
|
||||
|
||||
/**
|
||||
* bpack(type, data)
|
||||
* c - Signed Byte
|
||||
* s - Signed Short
|
||||
* i - Signed Int
|
||||
* l - Signed Long
|
||||
* d - Double
|
||||
*/
|
||||
static int l_bpack(lua_State *L) {
|
||||
const char *code = luaL_checkstring(L, 1);
|
||||
luaL_argcheck(L, code[1] == '\0', 0, "Format code must be one character.");
|
||||
luaL_Buffer buf;
|
||||
luaL_buffinit(L, &buf);
|
||||
|
||||
switch (code[0]) {
|
||||
case 'c': {
|
||||
int8_t data = luaL_checknumber(L, 2);
|
||||
luaL_addlstring(&buf, (void*)&data, sizeof(data));
|
||||
break;
|
||||
}
|
||||
case 's': {
|
||||
int16_t data = luaL_checknumber(L, 2);
|
||||
data = (int16_t)htons(data);
|
||||
luaL_addlstring(&buf, (void*)&data, sizeof(data));
|
||||
break;
|
||||
}
|
||||
case 'i': {
|
||||
int32_t data = luaL_checkinteger(L, 2);
|
||||
data = (int32_t)htonl(data);
|
||||
luaL_addlstring(&buf, (void*)&data, sizeof(data));
|
||||
break;
|
||||
}
|
||||
case 'l': {
|
||||
int64_t data = lualongnumber_checklong(L, 2);
|
||||
data = (int64_t)T_htonll(data);
|
||||
luaL_addlstring(&buf, (void*)&data, sizeof(data));
|
||||
break;
|
||||
}
|
||||
case 'd': {
|
||||
double data = luaL_checknumber(L, 2);
|
||||
luaL_addlstring(&buf, (void*)&data, sizeof(data));
|
||||
break;
|
||||
}
|
||||
default:
|
||||
luaL_argcheck(L, 0, 0, "Invalid format code.");
|
||||
}
|
||||
|
||||
luaL_pushresult(&buf);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* bunpack(type, data)
|
||||
* c - Signed Byte
|
||||
* C - Unsigned Byte
|
||||
* s - Signed Short
|
||||
* i - Signed Int
|
||||
* l - Signed Long
|
||||
* d - Double
|
||||
*/
|
||||
static int l_bunpack(lua_State *L) {
|
||||
const char *code = luaL_checkstring(L, 1);
|
||||
luaL_argcheck(L, code[1] == '\0', 0, "Format code must be one character.");
|
||||
const char *data = luaL_checkstring(L, 2);
|
||||
#ifdef _LUA51_
|
||||
size_t len = lua_objlen(L, 2);
|
||||
#else
|
||||
size_t len = lua_rawlen(L, 2);
|
||||
#endif
|
||||
|
||||
switch (code[0]) {
|
||||
case 'c': {
|
||||
int8_t val;
|
||||
luaL_argcheck(L, len == sizeof(val), 1, "Invalid input string size.");
|
||||
memcpy(&val, data, sizeof(val));
|
||||
lua_pushnumber(L, val);
|
||||
break;
|
||||
}
|
||||
/**
|
||||
* unpack unsigned Byte.
|
||||
*/
|
||||
case 'C': {
|
||||
uint8_t val;
|
||||
luaL_argcheck(L, len == sizeof(val), 1, "Invalid input string size.");
|
||||
memcpy(&val, data, sizeof(val));
|
||||
lua_pushnumber(L, val);
|
||||
break;
|
||||
}
|
||||
case 's': {
|
||||
int16_t val;
|
||||
luaL_argcheck(L, len == sizeof(val), 1, "Invalid input string size.");
|
||||
memcpy(&val, data, sizeof(val));
|
||||
val = (int16_t)ntohs(val);
|
||||
lua_pushnumber(L, val);
|
||||
break;
|
||||
}
|
||||
case 'i': {
|
||||
int32_t val;
|
||||
luaL_argcheck(L, len == sizeof(val), 1, "Invalid input string size.");
|
||||
memcpy(&val, data, sizeof(val));
|
||||
val = (int32_t)ntohl(val);
|
||||
lua_pushnumber(L, val);
|
||||
break;
|
||||
}
|
||||
case 'l': {
|
||||
int64_t val;
|
||||
luaL_argcheck(L, len == sizeof(val), 1, "Invalid input string size.");
|
||||
memcpy(&val, data, sizeof(val));
|
||||
val = (int64_t)T_ntohll(val);
|
||||
lualongnumber_pushlong(L, &val);
|
||||
break;
|
||||
}
|
||||
case 'd': {
|
||||
double val;
|
||||
luaL_argcheck(L, len == sizeof(val), 1, "Invalid input string size.");
|
||||
memcpy(&val, data, sizeof(val));
|
||||
lua_pushnumber(L, val);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
luaL_argcheck(L, 0, 0, "Invalid format code.");
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert l into a zigzag long. This allows negative numbers to be
|
||||
* represented compactly as a varint.
|
||||
*/
|
||||
static int l_i64ToZigzag(lua_State *L) {
|
||||
int64_t n = lualongnumber_checklong(L, 1);
|
||||
int64_t result = (n << 1) ^ (n >> 63);
|
||||
lualongnumber_pushlong(L, &result);
|
||||
return 1;
|
||||
}
|
||||
/**
|
||||
* Convert n into a zigzag int. This allows negative numbers to be
|
||||
* represented compactly as a varint.
|
||||
*/
|
||||
static int l_i32ToZigzag(lua_State *L) {
|
||||
int32_t n = luaL_checkinteger(L, 1);
|
||||
uint32_t result = (uint32_t)(n << 1) ^ (n >> 31);
|
||||
lua_pushnumber(L, result);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert from zigzag int to int.
|
||||
*/
|
||||
static int l_zigzagToI32(lua_State *L) {
|
||||
uint32_t n = luaL_checkinteger(L, 1);
|
||||
int32_t result = (int32_t)(n >> 1) ^ (uint32_t)(-(int32_t)(n & 1));
|
||||
lua_pushnumber(L, result);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert from zigzag long to long.
|
||||
*/
|
||||
static int l_zigzagToI64(lua_State *L) {
|
||||
int64_t n = lualongnumber_checklong(L, 1);
|
||||
int64_t result = (int64_t)(n >> 1) ^ (uint64_t)(-(int64_t)(n & 1));
|
||||
lualongnumber_pushlong(L, &result);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert an i32 to a varint. Results in 1-5 bytes on the buffer.
|
||||
*/
|
||||
static int l_toVarint32(lua_State *L) {
|
||||
uint8_t buf[5];
|
||||
uint32_t n = luaL_checkinteger(L, 1);
|
||||
uint32_t wsize = 0;
|
||||
|
||||
while (1) {
|
||||
if ((n & ~0x7F) == 0) {
|
||||
buf[wsize++] = (int8_t)n;
|
||||
break;
|
||||
} else {
|
||||
buf[wsize++] = (int8_t)((n & 0x7F) | 0x80);
|
||||
n >>= 7;
|
||||
}
|
||||
}
|
||||
lua_pushlstring(L, buf, wsize);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert an i64 to a varint. Results in 1-10 bytes on the buffer.
|
||||
*/
|
||||
static int l_toVarint64(lua_State *L) {
|
||||
uint8_t data[10];
|
||||
uint64_t n = lualongnumber_checklong(L, 1);
|
||||
uint32_t wsize = 0;
|
||||
luaL_Buffer buf;
|
||||
luaL_buffinit(L, &buf);
|
||||
|
||||
while (1) {
|
||||
if ((n & ~0x7FL) == 0) {
|
||||
data[wsize++] = (int8_t)n;
|
||||
break;
|
||||
} else {
|
||||
data[wsize++] = (int8_t)((n & 0x7F) | 0x80);
|
||||
n >>= 7;
|
||||
}
|
||||
}
|
||||
|
||||
luaL_addlstring(&buf, (void*)&data, wsize);
|
||||
luaL_pushresult(&buf);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a varint to i64.
|
||||
*/
|
||||
static int l_fromVarint64(lua_State *L) {
|
||||
int64_t result;
|
||||
uint8_t byte = luaL_checknumber(L, 1);
|
||||
int32_t shift = luaL_checknumber(L, 2);
|
||||
uint64_t n = (uint64_t)lualongnumber_checklong(L, 3);
|
||||
n |= (uint64_t)(byte & 0x7f) << shift;
|
||||
|
||||
if (!(byte & 0x80)) {
|
||||
result = (int64_t)(n >> 1) ^ (uint64_t)(-(int64_t)(n & 1));
|
||||
lua_pushnumber(L, 0);
|
||||
} else {
|
||||
result = n;
|
||||
lua_pushnumber(L, 1);
|
||||
}
|
||||
lualongnumber_pushlong(L, &result);
|
||||
return 2;
|
||||
}
|
||||
|
||||
/**
|
||||
* To pack message type of compact protocol.
|
||||
*/
|
||||
static int l_packMesgType(lua_State *L) {
|
||||
int32_t version_n = luaL_checkinteger(L, 1);
|
||||
int32_t version_mask = luaL_checkinteger(L, 2);
|
||||
int32_t messagetype = luaL_checkinteger(L, 3);
|
||||
int32_t type_shift_amount = luaL_checkinteger(L, 4);
|
||||
int32_t type_mask = luaL_checkinteger(L, 5);
|
||||
int32_t to_mesg_type = (version_n & version_mask) |
|
||||
(((int32_t)messagetype << type_shift_amount) & type_mask);
|
||||
lua_pushnumber(L, to_mesg_type);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static const struct luaL_Reg lua_bpack[] = {
|
||||
{"bpack", l_bpack},
|
||||
{"bunpack", l_bunpack},
|
||||
{"i32ToZigzag", l_i32ToZigzag},
|
||||
{"i64ToZigzag", l_i64ToZigzag},
|
||||
{"zigzagToI32", l_zigzagToI32},
|
||||
{"zigzagToI64", l_zigzagToI64},
|
||||
{"toVarint32", l_toVarint32},
|
||||
{"toVarint64", l_toVarint64},
|
||||
{"fromVarint64", l_fromVarint64},
|
||||
{"packMesgType", l_packMesgType},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
int luaopen_libluabpack(lua_State *L) {
|
||||
luaL_register(L, "libluabpack", lua_bpack);
|
||||
return 1;
|
||||
}
|
228
vendor/git.apache.org/thrift.git/lib/lua/src/lualongnumber.c
generated
vendored
Normal file
228
vendor/git.apache.org/thrift.git/lib/lua/src/lualongnumber.c
generated
vendored
Normal file
|
@ -0,0 +1,228 @@
|
|||
//
|
||||
// Licensed to the Apache Software Foundation (ASF) under one
|
||||
// or more contributor license agreements. See the NOTICE file
|
||||
// distributed with this work for additional information
|
||||
// regarding copyright ownership. The ASF licenses this file
|
||||
// to you under the Apache License, Version 2.0 (the
|
||||
// "License"); you may not use this file except in compliance
|
||||
// with the License. You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing,
|
||||
// software distributed under the License is distributed on an
|
||||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the License for the
|
||||
// specific language governing permissions and limitations
|
||||
// under the License.
|
||||
//
|
||||
|
||||
#include <lua.h>
|
||||
#include <lauxlib.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
#include <inttypes.h>
|
||||
#include <string.h>
|
||||
|
||||
extern const char * LONG_NUM_TYPE;
|
||||
extern int64_t lualongnumber_checklong(lua_State *L, int index);
|
||||
extern int64_t lualongnumber_pushlong(lua_State *L, int64_t *val);
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
static void l_serialize(char *buf, int len, int64_t val) {
|
||||
snprintf(buf, len, "%"PRId64, val);
|
||||
}
|
||||
|
||||
static int64_t l_deserialize(const char *buf) {
|
||||
int64_t data;
|
||||
int rv;
|
||||
// Support hex prefixed with '0x'
|
||||
if (strstr(buf, "0x") == buf) {
|
||||
rv = sscanf(buf, "%"PRIx64, &data);
|
||||
} else {
|
||||
rv = sscanf(buf, "%"PRId64, &data);
|
||||
}
|
||||
if (rv == 1) {
|
||||
return data;
|
||||
}
|
||||
return 0; // Failed
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
static int l_new(lua_State *L) {
|
||||
int64_t val;
|
||||
const char *str = NULL;
|
||||
if (lua_type(L, 1) == LUA_TSTRING) {
|
||||
str = lua_tostring(L, 1);
|
||||
val = l_deserialize(str);
|
||||
} else if (lua_type(L, 1) == LUA_TNUMBER) {
|
||||
val = (int64_t)lua_tonumber(L, 1);
|
||||
str = (const char *)1;
|
||||
}
|
||||
lualongnumber_pushlong(L, (str ? &val : NULL));
|
||||
return 1;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// a + b
|
||||
static int l_add(lua_State *L) {
|
||||
int64_t a, b, c;
|
||||
a = lualongnumber_checklong(L, 1);
|
||||
b = lualongnumber_checklong(L, 2);
|
||||
c = a + b;
|
||||
lualongnumber_pushlong(L, &c);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// a / b
|
||||
static int l_div(lua_State *L) {
|
||||
int64_t a, b, c;
|
||||
a = lualongnumber_checklong(L, 1);
|
||||
b = lualongnumber_checklong(L, 2);
|
||||
c = a / b;
|
||||
lualongnumber_pushlong(L, &c);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// a == b (both a and b are lualongnumber's)
|
||||
static int l_eq(lua_State *L) {
|
||||
int64_t a, b;
|
||||
a = lualongnumber_checklong(L, 1);
|
||||
b = lualongnumber_checklong(L, 2);
|
||||
lua_pushboolean(L, (a == b ? 1 : 0));
|
||||
return 1;
|
||||
}
|
||||
|
||||
// garbage collection
|
||||
static int l_gc(lua_State *L) {
|
||||
lua_pushnil(L);
|
||||
lua_setmetatable(L, 1);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// a < b
|
||||
static int l_lt(lua_State *L) {
|
||||
int64_t a, b;
|
||||
a = lualongnumber_checklong(L, 1);
|
||||
b = lualongnumber_checklong(L, 2);
|
||||
lua_pushboolean(L, (a < b ? 1 : 0));
|
||||
return 1;
|
||||
}
|
||||
|
||||
// a <= b
|
||||
static int l_le(lua_State *L) {
|
||||
int64_t a, b;
|
||||
a = lualongnumber_checklong(L, 1);
|
||||
b = lualongnumber_checklong(L, 2);
|
||||
lua_pushboolean(L, (a <= b ? 1 : 0));
|
||||
return 1;
|
||||
}
|
||||
|
||||
// a % b
|
||||
static int l_mod(lua_State *L) {
|
||||
int64_t a, b, c;
|
||||
a = lualongnumber_checklong(L, 1);
|
||||
b = lualongnumber_checklong(L, 2);
|
||||
c = a % b;
|
||||
lualongnumber_pushlong(L, &c);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// a * b
|
||||
static int l_mul(lua_State *L) {
|
||||
int64_t a, b, c;
|
||||
a = lualongnumber_checklong(L, 1);
|
||||
b = lualongnumber_checklong(L, 2);
|
||||
c = a * b;
|
||||
lualongnumber_pushlong(L, &c);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// a ^ b
|
||||
static int l_pow(lua_State *L) {
|
||||
long double a, b;
|
||||
int64_t c;
|
||||
a = (long double)lualongnumber_checklong(L, 1);
|
||||
b = (long double)lualongnumber_checklong(L, 2);
|
||||
c = (int64_t)pow(a, b);
|
||||
lualongnumber_pushlong(L, &c);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// a - b
|
||||
static int l_sub(lua_State *L) {
|
||||
int64_t a, b, c;
|
||||
a = lualongnumber_checklong(L, 1);
|
||||
b = lualongnumber_checklong(L, 2);
|
||||
c = a - b;
|
||||
lualongnumber_pushlong(L, &c);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// tostring()
|
||||
static int l_tostring(lua_State *L) {
|
||||
int64_t a;
|
||||
char str[256];
|
||||
l_serialize(str, 256, lualongnumber_checklong(L, 1));
|
||||
lua_pushstring(L, str);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// -a
|
||||
static int l_unm(lua_State *L) {
|
||||
int64_t a, c;
|
||||
a = lualongnumber_checklong(L, 1);
|
||||
c = -a;
|
||||
lualongnumber_pushlong(L, &c);
|
||||
return 1;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
static const luaL_Reg methods[] = {
|
||||
{"__add", l_add},
|
||||
{"__div", l_div},
|
||||
{"__eq", l_eq},
|
||||
{"__gc", l_gc},
|
||||
{"__lt", l_lt},
|
||||
{"__le", l_le},
|
||||
{"__mod", l_mod},
|
||||
{"__mul", l_mul},
|
||||
{"__pow", l_pow},
|
||||
{"__sub", l_sub},
|
||||
{"__tostring", l_tostring},
|
||||
{"__unm", l_unm},
|
||||
{NULL, NULL},
|
||||
};
|
||||
|
||||
static const luaL_Reg funcs[] = {
|
||||
{"new", l_new},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
static void set_methods(lua_State *L,
|
||||
const char *metatablename,
|
||||
const struct luaL_Reg *methods) {
|
||||
luaL_getmetatable(L, metatablename); // mt
|
||||
// No need for a __index table since everything is __*
|
||||
for (; methods->name; methods++) {
|
||||
lua_pushstring(L, methods->name); // mt, "name"
|
||||
lua_pushcfunction(L, methods->func); // mt, "name", func
|
||||
lua_rawset(L, -3); // mt
|
||||
}
|
||||
lua_pop(L, 1);
|
||||
}
|
||||
|
||||
LUALIB_API int luaopen_liblualongnumber(lua_State *L) {
|
||||
luaL_newmetatable(L, LONG_NUM_TYPE);
|
||||
lua_pop(L, 1);
|
||||
set_methods(L, LONG_NUM_TYPE, methods);
|
||||
|
||||
luaL_register(L, "liblualongnumber", funcs);
|
||||
return 1;
|
||||
}
|
380
vendor/git.apache.org/thrift.git/lib/lua/src/luasocket.c
generated
vendored
Normal file
380
vendor/git.apache.org/thrift.git/lib/lua/src/luasocket.c
generated
vendored
Normal file
|
@ -0,0 +1,380 @@
|
|||
//
|
||||
// Licensed to the Apache Software Foundation (ASF) under one
|
||||
// or more contributor license agreements. See the NOTICE file
|
||||
// distributed with this work for additional information
|
||||
// regarding copyright ownership. The ASF licenses this file
|
||||
// to you under the Apache License, Version 2.0 (the
|
||||
// "License"); you may not use this file except in compliance
|
||||
// with the License. You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing,
|
||||
// software distributed under the License is distributed on an
|
||||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the License for the
|
||||
// specific language governing permissions and limitations
|
||||
// under the License.
|
||||
//
|
||||
|
||||
#include <lua.h>
|
||||
#include <lauxlib.h>
|
||||
|
||||
#include <unistd.h>
|
||||
#include "string.h"
|
||||
#include "socket.h"
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
static const char *SOCKET_ANY = "__thrift_socket_any";
|
||||
static const char *SOCKET_CONN = "__thrift_socket_connected";
|
||||
|
||||
static const char *SOCKET_GENERIC = "__thrift_socket_generic";
|
||||
static const char *SOCKET_CLIENT = "__thrift_socket_client";
|
||||
static const char *SOCKET_SERVER = "__thrift_socket_server";
|
||||
|
||||
static const char *DEFAULT_HOST = "localhost";
|
||||
|
||||
typedef struct __t_tcp {
|
||||
t_socket sock;
|
||||
int timeout; // Milliseconds
|
||||
} t_tcp;
|
||||
typedef t_tcp *p_tcp;
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Util
|
||||
|
||||
static void throw_argerror(lua_State *L, int index, const char *expected) {
|
||||
char msg[256];
|
||||
sprintf(msg, "%s expected, got %s", expected, luaL_typename(L, index));
|
||||
luaL_argerror(L, index, msg);
|
||||
}
|
||||
|
||||
static void *checkgroup(lua_State *L, int index, const char *groupname) {
|
||||
if (!lua_getmetatable(L, index)) {
|
||||
throw_argerror(L, index, groupname);
|
||||
}
|
||||
|
||||
lua_pushstring(L, groupname);
|
||||
lua_rawget(L, -2);
|
||||
if (lua_isnil(L, -1)) {
|
||||
lua_pop(L, 2);
|
||||
throw_argerror(L, index, groupname);
|
||||
} else {
|
||||
lua_pop(L, 2);
|
||||
return lua_touserdata(L, index);
|
||||
}
|
||||
return NULL; // Not reachable
|
||||
}
|
||||
|
||||
static void *checktype(lua_State *L, int index, const char *typename) {
|
||||
if (strcmp(typename, SOCKET_ANY) == 0 ||
|
||||
strcmp(typename, SOCKET_CONN) == 0) {
|
||||
return checkgroup(L, index, typename);
|
||||
} else {
|
||||
return luaL_checkudata(L, index, typename);
|
||||
}
|
||||
}
|
||||
|
||||
static void settype(lua_State *L, int index, const char *typename) {
|
||||
luaL_getmetatable(L, typename);
|
||||
lua_setmetatable(L, index);
|
||||
}
|
||||
|
||||
#define LUA_SUCCESS_RETURN(L) \
|
||||
lua_pushnumber(L, 1); \
|
||||
return 1
|
||||
|
||||
#define LUA_CHECK_RETURN(L, err) \
|
||||
if (err) { \
|
||||
lua_pushnil(L); \
|
||||
lua_pushstring(L, err); \
|
||||
return 2; \
|
||||
} \
|
||||
LUA_SUCCESS_RETURN(L)
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
static int l_socket_create(lua_State *L);
|
||||
static int l_socket_destroy(lua_State *L);
|
||||
static int l_socket_settimeout(lua_State *L);
|
||||
static int l_socket_getsockinfo(lua_State *L);
|
||||
|
||||
static int l_socket_accept(lua_State *L);
|
||||
static int l_socket_listen(lua_State *L);
|
||||
|
||||
static int l_socket_create_and_connect(lua_State *L);
|
||||
static int l_socket_connect(lua_State *L);
|
||||
static int l_socket_send(lua_State *L);
|
||||
static int l_socket_receive(lua_State *L);
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
static const struct luaL_Reg methods_generic[] = {
|
||||
{"destroy", l_socket_destroy},
|
||||
{"settimeout", l_socket_settimeout},
|
||||
{"getsockinfo", l_socket_getsockinfo},
|
||||
{"listen", l_socket_listen},
|
||||
{"connect", l_socket_connect},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
static const struct luaL_Reg methods_server[] = {
|
||||
{"destroy", l_socket_destroy},
|
||||
{"getsockinfo", l_socket_getsockinfo},
|
||||
{"accept", l_socket_accept},
|
||||
{"send", l_socket_send},
|
||||
{"receive", l_socket_receive},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
static const struct luaL_Reg methods_client[] = {
|
||||
{"destroy", l_socket_destroy},
|
||||
{"settimeout", l_socket_settimeout},
|
||||
{"getsockinfo", l_socket_getsockinfo},
|
||||
{"send", l_socket_send},
|
||||
{"receive", l_socket_receive},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
static const struct luaL_Reg funcs_luasocket[] = {
|
||||
{"create", l_socket_create},
|
||||
{"create_and_connect", l_socket_create_and_connect},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// Check/enforce inheritance
|
||||
static void add_to_group(lua_State *L,
|
||||
const char *metatablename,
|
||||
const char *groupname) {
|
||||
luaL_getmetatable(L, metatablename); // mt
|
||||
lua_pushstring(L, groupname); // mt, "name"
|
||||
lua_pushboolean(L, 1); // mt, "name", true
|
||||
lua_rawset(L, -3); // mt
|
||||
lua_pop(L, 1);
|
||||
}
|
||||
|
||||
static void set_methods(lua_State *L,
|
||||
const char *metatablename,
|
||||
const struct luaL_Reg *methods) {
|
||||
luaL_getmetatable(L, metatablename); // mt
|
||||
// Create the __index table
|
||||
lua_pushstring(L, "__index"); // mt, "__index"
|
||||
lua_newtable(L); // mt, "__index", t
|
||||
for (; methods->name; methods++) {
|
||||
lua_pushstring(L, methods->name); // mt, "__index", t, "name"
|
||||
lua_pushcfunction(L, methods->func); // mt, "__index", t, "name", func
|
||||
lua_rawset(L, -3); // mt, "__index", t
|
||||
}
|
||||
lua_rawset(L, -3); // mt
|
||||
lua_pop(L, 1);
|
||||
}
|
||||
|
||||
int luaopen_libluasocket(lua_State *L) {
|
||||
luaL_newmetatable(L, SOCKET_GENERIC);
|
||||
luaL_newmetatable(L, SOCKET_CLIENT);
|
||||
luaL_newmetatable(L, SOCKET_SERVER);
|
||||
lua_pop(L, 3);
|
||||
add_to_group(L, SOCKET_GENERIC, SOCKET_ANY);
|
||||
add_to_group(L, SOCKET_CLIENT, SOCKET_ANY);
|
||||
add_to_group(L, SOCKET_SERVER, SOCKET_ANY);
|
||||
add_to_group(L, SOCKET_CLIENT, SOCKET_CONN);
|
||||
add_to_group(L, SOCKET_SERVER, SOCKET_CONN);
|
||||
set_methods(L, SOCKET_GENERIC, methods_generic);
|
||||
set_methods(L, SOCKET_CLIENT, methods_client);
|
||||
set_methods(L, SOCKET_SERVER, methods_server);
|
||||
|
||||
luaL_register(L, "luasocket", funcs_luasocket);
|
||||
return 1;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// General
|
||||
|
||||
// sock,err create(bind_host, bind_port)
|
||||
// sock,err create(bind_host) -> any port
|
||||
// sock,err create() -> any port on localhost
|
||||
static int l_socket_create(lua_State *L) {
|
||||
const char *err;
|
||||
t_socket sock;
|
||||
const char *addr = lua_tostring(L, 1);
|
||||
if (!addr) {
|
||||
addr = DEFAULT_HOST;
|
||||
}
|
||||
unsigned short port = lua_tonumber(L, 2);
|
||||
err = tcp_create(&sock);
|
||||
if (!err) {
|
||||
err = tcp_bind(&sock, addr, port); // bind on create
|
||||
if (err) {
|
||||
tcp_destroy(&sock);
|
||||
} else {
|
||||
p_tcp tcp = (p_tcp) lua_newuserdata(L, sizeof(t_tcp));
|
||||
settype(L, -2, SOCKET_GENERIC);
|
||||
socket_setnonblocking(&sock);
|
||||
tcp->sock = sock;
|
||||
tcp->timeout = 0;
|
||||
return 1; // Return userdata
|
||||
}
|
||||
}
|
||||
LUA_CHECK_RETURN(L, err);
|
||||
}
|
||||
|
||||
// destroy()
|
||||
static int l_socket_destroy(lua_State *L) {
|
||||
p_tcp tcp = (p_tcp) checktype(L, 1, SOCKET_ANY);
|
||||
const char *err = tcp_destroy(&tcp->sock);
|
||||
LUA_CHECK_RETURN(L, err);
|
||||
}
|
||||
|
||||
// send(socket, data)
|
||||
static int l_socket_send(lua_State *L) {
|
||||
p_tcp self = (p_tcp) checktype(L, 1, SOCKET_CONN);
|
||||
p_tcp tcp = (p_tcp) checktype(L, 2, SOCKET_CONN);
|
||||
size_t len;
|
||||
const char *data = luaL_checklstring(L, 3, &len);
|
||||
const char *err =
|
||||
tcp_send(&tcp->sock, data, len, tcp->timeout);
|
||||
LUA_CHECK_RETURN(L, err);
|
||||
}
|
||||
|
||||
#define LUA_READ_STEP 8192
|
||||
static int l_socket_receive(lua_State *L) {
|
||||
p_tcp self = (p_tcp) checktype(L, 1, SOCKET_CONN);
|
||||
p_tcp handle = (p_tcp) checktype(L, 2, SOCKET_CONN);
|
||||
size_t len = luaL_checknumber(L, 3);
|
||||
char buf[LUA_READ_STEP];
|
||||
const char *err = NULL;
|
||||
int received;
|
||||
size_t got = 0, step = 0;
|
||||
luaL_Buffer b;
|
||||
|
||||
luaL_buffinit(L, &b);
|
||||
do {
|
||||
step = (LUA_READ_STEP < len - got ? LUA_READ_STEP : len - got);
|
||||
err = tcp_raw_receive(&handle->sock, buf, step, self->timeout, &received);
|
||||
if (err == NULL) {
|
||||
luaL_addlstring(&b, buf, received);
|
||||
got += received;
|
||||
}
|
||||
} while (err == NULL && got < len);
|
||||
|
||||
if (err) {
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, err);
|
||||
return 2;
|
||||
}
|
||||
luaL_pushresult(&b);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// settimeout(timeout)
|
||||
static int l_socket_settimeout(lua_State *L) {
|
||||
p_tcp self = (p_tcp) checktype(L, 1, SOCKET_ANY);
|
||||
int timeout = luaL_checknumber(L, 2);
|
||||
self->timeout = timeout;
|
||||
LUA_SUCCESS_RETURN(L);
|
||||
}
|
||||
|
||||
// table getsockinfo()
|
||||
static int l_socket_getsockinfo(lua_State *L) {
|
||||
char buf[256];
|
||||
short port = 0;
|
||||
p_tcp tcp = (p_tcp) checktype(L, 1, SOCKET_ANY);
|
||||
if (socket_get_info(&tcp->sock, &port, buf, 256) == SUCCESS) {
|
||||
lua_newtable(L); // t
|
||||
lua_pushstring(L, "host"); // t, "host"
|
||||
lua_pushstring(L, buf); // t, "host", buf
|
||||
lua_rawset(L, -3); // t
|
||||
lua_pushstring(L, "port"); // t, "port"
|
||||
lua_pushnumber(L, port); // t, "port", port
|
||||
lua_rawset(L, -3); // t
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Server
|
||||
|
||||
// accept()
|
||||
static int l_socket_accept(lua_State *L) {
|
||||
const char *err;
|
||||
p_tcp self = (p_tcp) checktype(L, 1, SOCKET_SERVER);
|
||||
t_socket sock;
|
||||
err = tcp_accept(&self->sock, &sock, self->timeout);
|
||||
if (!err) { // Success
|
||||
// Create a reference to the client
|
||||
p_tcp client = (p_tcp) lua_newuserdata(L, sizeof(t_tcp));
|
||||
settype(L, 2, SOCKET_CLIENT);
|
||||
socket_setnonblocking(&sock);
|
||||
client->sock = sock;
|
||||
client->timeout = self->timeout;
|
||||
return 1;
|
||||
}
|
||||
LUA_CHECK_RETURN(L, err);
|
||||
}
|
||||
|
||||
static int l_socket_listen(lua_State *L) {
|
||||
const char* err;
|
||||
p_tcp tcp = (p_tcp) checktype(L, 1, SOCKET_GENERIC);
|
||||
int backlog = 10;
|
||||
err = tcp_listen(&tcp->sock, backlog);
|
||||
if (!err) {
|
||||
// Set the current as a server
|
||||
settype(L, 1, SOCKET_SERVER); // Now a server
|
||||
}
|
||||
LUA_CHECK_RETURN(L, err);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Client
|
||||
|
||||
// create_and_connect(host, port, timeout)
|
||||
extern double __gettime();
|
||||
static int l_socket_create_and_connect(lua_State *L) {
|
||||
const char* err = NULL;
|
||||
double end;
|
||||
t_socket sock;
|
||||
const char *host = luaL_checkstring(L, 1);
|
||||
unsigned short port = luaL_checknumber(L, 2);
|
||||
int timeout = luaL_checknumber(L, 3);
|
||||
|
||||
// Create and connect loop for timeout milliseconds
|
||||
end = __gettime() + timeout/1000;
|
||||
do {
|
||||
// Create the socket
|
||||
err = tcp_create(&sock);
|
||||
if (!err) {
|
||||
// Connect
|
||||
err = tcp_connect(&sock, host, port, timeout);
|
||||
if (err) {
|
||||
tcp_destroy(&sock);
|
||||
usleep(100000); // sleep for 100ms
|
||||
} else {
|
||||
p_tcp tcp = (p_tcp) lua_newuserdata(L, sizeof(t_tcp));
|
||||
settype(L, -2, SOCKET_CLIENT);
|
||||
socket_setnonblocking(&sock);
|
||||
tcp->sock = sock;
|
||||
tcp->timeout = timeout;
|
||||
return 1; // Return userdata
|
||||
}
|
||||
}
|
||||
} while (err && __gettime() < end);
|
||||
|
||||
LUA_CHECK_RETURN(L, err);
|
||||
}
|
||||
|
||||
// connect(host, port)
|
||||
static int l_socket_connect(lua_State *L) {
|
||||
const char *err;
|
||||
p_tcp tcp = (p_tcp) checktype(L, 1, SOCKET_GENERIC);
|
||||
const char *host = luaL_checkstring(L, 2);
|
||||
unsigned short port = luaL_checknumber(L, 3);
|
||||
err = tcp_connect(&tcp->sock, host, port, tcp->timeout);
|
||||
if (!err) {
|
||||
settype(L, 1, SOCKET_CLIENT); // Now a client
|
||||
}
|
||||
LUA_CHECK_RETURN(L, err);
|
||||
}
|
78
vendor/git.apache.org/thrift.git/lib/lua/src/socket.h
generated
vendored
Normal file
78
vendor/git.apache.org/thrift.git/lib/lua/src/socket.h
generated
vendored
Normal file
|
@ -0,0 +1,78 @@
|
|||
//
|
||||
// Licensed to the Apache Software Foundation (ASF) under one
|
||||
// or more contributor license agreements. See the NOTICE file
|
||||
// distributed with this work for additional information
|
||||
// regarding copyright ownership. The ASF licenses this file
|
||||
// to you under the Apache License, Version 2.0 (the
|
||||
// "License"); you may not use this file except in compliance
|
||||
// with the License. You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing,
|
||||
// software distributed under the License is distributed on an
|
||||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the License for the
|
||||
// specific language governing permissions and limitations
|
||||
// under the License.
|
||||
//
|
||||
|
||||
#ifndef LUA_THRIFT_SOCKET_H
|
||||
#define LUA_THRIFT_SOCKET_H
|
||||
|
||||
#include <sys/socket.h>
|
||||
|
||||
#ifdef _WIN32
|
||||
// SOL
|
||||
#else
|
||||
typedef int t_socket;
|
||||
typedef t_socket* p_socket;
|
||||
#endif
|
||||
|
||||
// Error Codes
|
||||
enum {
|
||||
SUCCESS = 0,
|
||||
TIMEOUT = -1,
|
||||
CLOSED = -2,
|
||||
};
|
||||
typedef int T_ERRCODE;
|
||||
|
||||
static const char * TIMEOUT_MSG = "Timeout";
|
||||
static const char * CLOSED_MSG = "Connection Closed";
|
||||
|
||||
typedef struct sockaddr t_sa;
|
||||
typedef t_sa * p_sa;
|
||||
|
||||
T_ERRCODE socket_create(p_socket sock, int domain, int type, int protocol);
|
||||
T_ERRCODE socket_destroy(p_socket sock);
|
||||
T_ERRCODE socket_bind(p_socket sock, p_sa addr, int addr_len);
|
||||
T_ERRCODE socket_get_info(p_socket sock, short *port, char *buf, size_t len);
|
||||
T_ERRCODE socket_send(p_socket sock, const char *data, size_t len, int timeout);
|
||||
T_ERRCODE socket_recv(p_socket sock, char *data, size_t len, int timeout,
|
||||
int *received);
|
||||
|
||||
void socket_setblocking(p_socket sock);
|
||||
void socket_setnonblocking(p_socket sock);
|
||||
|
||||
T_ERRCODE socket_accept(p_socket sock, p_socket sibling,
|
||||
p_sa addr, socklen_t *addr_len, int timeout);
|
||||
T_ERRCODE socket_listen(p_socket sock, int backlog);
|
||||
|
||||
T_ERRCODE socket_connect(p_socket sock, p_sa addr, int addr_len, int timeout);
|
||||
|
||||
const char * tcp_create(p_socket sock);
|
||||
const char * tcp_destroy(p_socket sock);
|
||||
const char * tcp_bind(p_socket sock, const char *host, unsigned short port);
|
||||
const char * tcp_send(p_socket sock, const char *data, size_t w_len,
|
||||
int timeout);
|
||||
const char * tcp_receive(p_socket sock, char *data, size_t r_len, int timeout);
|
||||
const char * tcp_raw_receive(p_socket sock, char * data, size_t r_len,
|
||||
int timeout, int *received);
|
||||
|
||||
const char * tcp_listen(p_socket sock, int backlog);
|
||||
const char * tcp_accept(p_socket sock, p_socket client, int timeout);
|
||||
|
||||
const char * tcp_connect(p_socket sock, const char *host, unsigned short port,
|
||||
int timeout);
|
||||
|
||||
#endif
|
370
vendor/git.apache.org/thrift.git/lib/lua/src/usocket.c
generated
vendored
Normal file
370
vendor/git.apache.org/thrift.git/lib/lua/src/usocket.c
generated
vendored
Normal file
|
@ -0,0 +1,370 @@
|
|||
//
|
||||
// Licensed to the Apache Software Foundation (ASF) under one
|
||||
// or more contributor license agreements. See the NOTICE file
|
||||
// distributed with this work for additional information
|
||||
// regarding copyright ownership. The ASF licenses this file
|
||||
// to you under the Apache License, Version 2.0 (the
|
||||
// "License"); you may not use this file except in compliance
|
||||
// with the License. You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing,
|
||||
// software distributed under the License is distributed on an
|
||||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the License for the
|
||||
// specific language governing permissions and limitations
|
||||
// under the License.
|
||||
//
|
||||
|
||||
#include <sys/time.h>
|
||||
#include <sys/types.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <netdb.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <stdio.h> // TODO REMOVE
|
||||
|
||||
#include "socket.h"
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Private
|
||||
|
||||
// Num seconds since Jan 1 1970 (UTC)
|
||||
#ifdef _WIN32
|
||||
// SOL
|
||||
#else
|
||||
double __gettime() {
|
||||
struct timeval v;
|
||||
gettimeofday(&v, (struct timezone*) NULL);
|
||||
return v.tv_sec + v.tv_usec/1.0e6;
|
||||
}
|
||||
#endif
|
||||
|
||||
#define WAIT_MODE_R 1
|
||||
#define WAIT_MODE_W 2
|
||||
#define WAIT_MODE_C (WAIT_MODE_R|WAIT_MODE_W)
|
||||
T_ERRCODE socket_wait(p_socket sock, int mode, int timeout) {
|
||||
int ret = 0;
|
||||
fd_set rfds, wfds;
|
||||
struct timeval tv;
|
||||
double end, t;
|
||||
if (!timeout) {
|
||||
return TIMEOUT;
|
||||
}
|
||||
|
||||
end = __gettime() + timeout/1000;
|
||||
do {
|
||||
FD_ZERO(&rfds);
|
||||
FD_ZERO(&wfds);
|
||||
|
||||
// Specify what I/O operations we care about
|
||||
if (mode & WAIT_MODE_R) {
|
||||
FD_SET(*sock, &rfds);
|
||||
}
|
||||
if (mode & WAIT_MODE_W) {
|
||||
FD_SET(*sock, &wfds);
|
||||
}
|
||||
|
||||
// Check for timeout
|
||||
t = end - __gettime();
|
||||
if (t < 0.0) {
|
||||
break;
|
||||
}
|
||||
|
||||
// Wait
|
||||
tv.tv_sec = (int)t;
|
||||
tv.tv_usec = (int)((t - tv.tv_sec) * 1.0e6);
|
||||
ret = select(*sock+1, &rfds, &wfds, NULL, &tv);
|
||||
} while (ret == -1 && errno == EINTR);
|
||||
if (ret == -1) {
|
||||
return errno;
|
||||
}
|
||||
|
||||
// Check for timeout
|
||||
if (ret == 0) {
|
||||
return TIMEOUT;
|
||||
}
|
||||
|
||||
// Verify that we can actually read from the remote host
|
||||
if (mode & WAIT_MODE_C && FD_ISSET(*sock, &rfds) &&
|
||||
recv(*sock, (char*) &rfds, 0, 0) != 0) {
|
||||
return errno;
|
||||
}
|
||||
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// General
|
||||
|
||||
T_ERRCODE socket_create(p_socket sock, int domain, int type, int protocol) {
|
||||
*sock = socket(domain, type, protocol);
|
||||
if (*sock > 0) {
|
||||
return SUCCESS;
|
||||
} else {
|
||||
return errno;
|
||||
}
|
||||
}
|
||||
|
||||
T_ERRCODE socket_destroy(p_socket sock) {
|
||||
// TODO Figure out if I should be free-ing this
|
||||
if (*sock > 0) {
|
||||
socket_setblocking(sock);
|
||||
close(*sock);
|
||||
*sock = -1;
|
||||
}
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
T_ERRCODE socket_bind(p_socket sock, p_sa addr, int addr_len) {
|
||||
int ret = SUCCESS;
|
||||
socket_setblocking(sock);
|
||||
if (bind(*sock, addr, addr_len)) {
|
||||
ret = errno;
|
||||
}
|
||||
socket_setnonblocking(sock);
|
||||
return ret;
|
||||
}
|
||||
|
||||
T_ERRCODE socket_get_info(p_socket sock, short *port, char *buf, size_t len) {
|
||||
struct sockaddr_storage sa;
|
||||
memset(&sa, 0, sizeof(sa));
|
||||
socklen_t addrlen = sizeof(sa);
|
||||
int rc = getsockname(*sock, (struct sockaddr*)&sa, &addrlen);
|
||||
if (!rc) {
|
||||
if (sa.ss_family == AF_INET6) {
|
||||
struct sockaddr_in6* sin = (struct sockaddr_in6*)(&sa);
|
||||
if (!inet_ntop(AF_INET6, &sin->sin6_addr, buf, len)) {
|
||||
return errno;
|
||||
}
|
||||
*port = ntohs(sin->sin6_port);
|
||||
} else {
|
||||
struct sockaddr_in* sin = (struct sockaddr_in*)(&sa);
|
||||
if (!inet_ntop(AF_INET, &sin->sin_addr, buf, len)) {
|
||||
return errno;
|
||||
}
|
||||
*port = ntohs(sin->sin_port);
|
||||
}
|
||||
return SUCCESS;
|
||||
}
|
||||
return errno;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Server
|
||||
|
||||
T_ERRCODE socket_accept(p_socket sock, p_socket client,
|
||||
p_sa addr, socklen_t *addrlen, int timeout) {
|
||||
int err;
|
||||
if (*sock < 0) {
|
||||
return CLOSED;
|
||||
}
|
||||
do {
|
||||
*client = accept(*sock, addr, addrlen);
|
||||
if (*client > 0) {
|
||||
return SUCCESS;
|
||||
}
|
||||
err = errno;
|
||||
} while (err != EINTR);
|
||||
if (err == EAGAIN || err == ECONNABORTED) {
|
||||
return socket_wait(sock, WAIT_MODE_R, timeout);
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
T_ERRCODE socket_listen(p_socket sock, int backlog) {
|
||||
int ret = SUCCESS;
|
||||
socket_setblocking(sock);
|
||||
if (listen(*sock, backlog)) {
|
||||
ret = errno;
|
||||
}
|
||||
socket_setnonblocking(sock);
|
||||
return ret;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Client
|
||||
|
||||
T_ERRCODE socket_connect(p_socket sock, p_sa addr, int addr_len, int timeout) {
|
||||
int err;
|
||||
if (*sock < 0) {
|
||||
return CLOSED;
|
||||
}
|
||||
|
||||
do {
|
||||
if (connect(*sock, addr, addr_len) == 0) {
|
||||
return SUCCESS;
|
||||
}
|
||||
} while ((err = errno) == EINTR);
|
||||
if (err != EINPROGRESS && err != EAGAIN) {
|
||||
return err;
|
||||
}
|
||||
return socket_wait(sock, WAIT_MODE_C, timeout);
|
||||
}
|
||||
|
||||
T_ERRCODE socket_send(
|
||||
p_socket sock, const char *data, size_t len, int timeout) {
|
||||
int err, put = 0;
|
||||
if (*sock < 0) {
|
||||
return CLOSED;
|
||||
}
|
||||
do {
|
||||
put = send(*sock, data, len, 0);
|
||||
if (put > 0) {
|
||||
return SUCCESS;
|
||||
}
|
||||
err = errno;
|
||||
} while (err != EINTR);
|
||||
|
||||
if (err == EAGAIN) {
|
||||
return socket_wait(sock, WAIT_MODE_W, timeout);
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
T_ERRCODE socket_recv(
|
||||
p_socket sock, char *data, size_t len, int timeout, int *received) {
|
||||
int err, got = 0;
|
||||
if (*sock < 0) {
|
||||
return CLOSED;
|
||||
}
|
||||
|
||||
int flags = fcntl(*sock, F_GETFL, 0);
|
||||
do {
|
||||
got = recv(*sock, data, len, 0);
|
||||
if (got > 0) {
|
||||
*received = got;
|
||||
return SUCCESS;
|
||||
}
|
||||
err = errno;
|
||||
|
||||
// Connection has been closed by peer
|
||||
if (got == 0) {
|
||||
return CLOSED;
|
||||
}
|
||||
} while (err != EINTR);
|
||||
|
||||
if (err == EAGAIN) {
|
||||
return socket_wait(sock, WAIT_MODE_R, timeout);
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Util
|
||||
|
||||
void socket_setnonblocking(p_socket sock) {
|
||||
int flags = fcntl(*sock, F_GETFL, 0);
|
||||
flags |= O_NONBLOCK;
|
||||
fcntl(*sock, F_SETFL, flags);
|
||||
}
|
||||
|
||||
void socket_setblocking(p_socket sock) {
|
||||
int flags = fcntl(*sock, F_GETFL, 0);
|
||||
flags &= (~(O_NONBLOCK));
|
||||
fcntl(*sock, F_SETFL, flags);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// TCP
|
||||
|
||||
#define ERRORSTR_RETURN(err) \
|
||||
if (err == SUCCESS) { \
|
||||
return NULL; \
|
||||
} else if (err == TIMEOUT) { \
|
||||
return TIMEOUT_MSG; \
|
||||
} else if (err == CLOSED) { \
|
||||
return CLOSED_MSG; \
|
||||
} \
|
||||
return strerror(err)
|
||||
|
||||
const char * tcp_create(p_socket sock) {
|
||||
int err = socket_create(sock, AF_INET, SOCK_STREAM, 0);
|
||||
ERRORSTR_RETURN(err);
|
||||
}
|
||||
|
||||
const char * tcp_destroy(p_socket sock) {
|
||||
int err = socket_destroy(sock);
|
||||
ERRORSTR_RETURN(err);
|
||||
}
|
||||
|
||||
const char * tcp_bind(p_socket sock, const char *host, unsigned short port) {
|
||||
int err;
|
||||
struct hostent *h;
|
||||
struct sockaddr_in local;
|
||||
memset(&local, 0, sizeof(local));
|
||||
local.sin_family = AF_INET;
|
||||
local.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||
local.sin_port = htons(port);
|
||||
if (strcmp(host, "*") && !inet_aton(host, &local.sin_addr)) {
|
||||
h = gethostbyname(host);
|
||||
if (!h) {
|
||||
return hstrerror(h_errno);
|
||||
}
|
||||
memcpy(&local.sin_addr,
|
||||
(struct in_addr *)h->h_addr_list[0],
|
||||
sizeof(struct in_addr));
|
||||
}
|
||||
err = socket_bind(sock, (p_sa) &local, sizeof(local));
|
||||
ERRORSTR_RETURN(err);
|
||||
}
|
||||
|
||||
const char * tcp_listen(p_socket sock, int backlog) {
|
||||
int err = socket_listen(sock, backlog);
|
||||
ERRORSTR_RETURN(err);
|
||||
}
|
||||
|
||||
const char * tcp_accept(p_socket sock, p_socket client, int timeout) {
|
||||
int err = socket_accept(sock, client, NULL, NULL, timeout);
|
||||
ERRORSTR_RETURN(err);
|
||||
}
|
||||
|
||||
const char * tcp_connect(p_socket sock,
|
||||
const char *host,
|
||||
unsigned short port,
|
||||
int timeout) {
|
||||
int err;
|
||||
struct hostent *h;
|
||||
struct sockaddr_in remote;
|
||||
memset(&remote, 0, sizeof(remote));
|
||||
remote.sin_family = AF_INET;
|
||||
remote.sin_port = htons(port);
|
||||
if (strcmp(host, "*") && !inet_aton(host, &remote.sin_addr)) {
|
||||
h = gethostbyname(host);
|
||||
if (!h) {
|
||||
return hstrerror(h_errno);
|
||||
}
|
||||
memcpy(&remote.sin_addr,
|
||||
(struct in_addr *)h->h_addr_list[0],
|
||||
sizeof(struct in_addr));
|
||||
}
|
||||
err = socket_connect(sock, (p_sa) &remote, sizeof(remote), timeout);
|
||||
ERRORSTR_RETURN(err);
|
||||
}
|
||||
|
||||
#define WRITE_STEP 8192
|
||||
const char * tcp_send(
|
||||
p_socket sock, const char * data, size_t w_len, int timeout) {
|
||||
int err;
|
||||
size_t put = 0, step;
|
||||
if (!w_len) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
do {
|
||||
step = (WRITE_STEP < w_len - put ? WRITE_STEP : w_len - put);
|
||||
err = socket_send(sock, data + put, step, timeout);
|
||||
put += step;
|
||||
} while (err == SUCCESS && put < w_len);
|
||||
ERRORSTR_RETURN(err);
|
||||
}
|
||||
|
||||
const char * tcp_raw_receive(
|
||||
p_socket sock, char * data, size_t r_len, int timeout, int *received) {
|
||||
int err = socket_recv(sock, data, r_len, timeout, received);
|
||||
ERRORSTR_RETURN(err);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue