Ejemplo n.º 1
0
struct nl_client *
nl_register_client(void)
{
    struct nl_client *cl;

    cl = calloc(sizeof(*cl), 1);
    if (!cl)
        return cl;

    cl->cl_buf = calloc(NL_MSG_DEFAULT_SIZE, 1);
    if (!cl->cl_buf)
        goto exit_register;
    cl->cl_buf_len = NL_MSG_DEFAULT_SIZE;
    cl->cl_resp_buf = malloc(NL_RESP_DEFAULT_SIZE);
    if (!cl->cl_resp_buf)
        goto exit_register;
    cl->cl_resp_buf_len = NL_RESP_DEFAULT_SIZE;

    cl->cl_id = 0;
    cl->cl_sock = -1;

    return cl;

exit_register:
    if (cl)
        nl_free(cl);

    return NULL;
}
Ejemplo n.º 2
0
void uv_udp_handle_t::write(request_udp_write_t& request)
{
	int32_t err = UV_UNKNOWN;
	if (request.m_shared_write) {
		uv_udp_handle_t* handle = (uv_udp_handle_t*)singleton_ref(network_t).get_shared_write_socket(request.m_socket_fd);
		if (handle != NULL) {
			uint32_t length = request.m_length > 0 ? request.m_length : (uint32_t)buffer_data_length(request.m_buffer);
			if (uv_is_closing((uv_handle_t*)handle)) {
				err = NL_EUDPSCLOSED;
			} else {
				err = handle->write_handle(request);
			}
		} else {
			err = NL_EUDPNOWSHARED;
		}
	} else {
		err = request.m_socket_handle->write_handle(request);
	}
	if (err != UV_OK) { /* write error had been occurred */
		if (request.m_length > 0) {
			nl_free((void*)request.m_string);
		} else {
			buffer_release(request.m_buffer);
		}
		if (request.m_session != LUA_REFNIL) {
			singleton_ref(node_lua_t).context_send(request.m_source, 0, request.m_session, RESPONSE_UDP_WRITE, (nl_err_code)err);
		}
	}
}
Ejemplo n.º 3
0
struct nl_client *
nl_register_client(void)
{
    struct nl_client *cl;

    cl = calloc(sizeof(*cl), 1);
    if (!cl)
        return cl;

    cl->cl_buf = calloc(NL_MSG_DEFAULT_SIZE, 1);
    if (!cl->cl_buf)
        goto exit_register;
    cl->cl_buf_len = NL_MSG_DEFAULT_SIZE;
    cl->cl_resp_buf = malloc(NL_RESP_DEFAULT_SIZE);
    if (!cl->cl_resp_buf)
        goto exit_register;
    cl->cl_resp_buf_len = NL_RESP_DEFAULT_SIZE;

    /* this really is OK... */
    cl->cl_id = __sync_fetch_and_add(&nl_client_ids, 1);

    cl->cl_sock = -1;

    return cl;

exit_register:
    if (cl)
        nl_free(cl);

    return NULL;
}
Ejemplo n.º 4
0
void
nl_free_client(struct nl_client *cl)
{
    nl_free(cl);
    free(cl);

    return;
}
Ejemplo n.º 5
0
void context_lua_t::lua_free_env(lua_State *L, char* env[])
{
	char** envp = env;
	while (*envp) {
		nl_free((void*)*envp);
		*envp++ = NULL;
	}
}
Ejemplo n.º 6
0
void uv_udp_handle_t::on_write(uv_udp_send_t* req, int status)
{
	write_uv_request_t *uv_request = (write_uv_request_t*)req->data;
	uv_udp_handle_t *socket_handle = (uv_udp_handle_t*)(req->handle->data);
	if (uv_request->m_length > 0) {
		nl_free((void*)uv_request->m_string);
	} else {
		buffer_release(uv_request->m_buffer);
	}
	if (uv_request->m_session != LUA_REFNIL) {
		singleton_ref(node_lua_t).context_send(uv_request->m_source, 0, uv_request->m_session, RESPONSE_UDP_WRITE, status == 0 ? UV_OK : singleton_ref(network_t).last_error());
	}
	socket_handle->put_write_cached_request(uv_request);
}
Ejemplo n.º 7
0
bool context_lua_t::init(int32_t argc, char* argv[], char* env[])
{
	m_lstate = luaL_newstateex(this);
	if (argc <= 0) {
		printf("[error] context:0x%08x init failed: lua file is needed to initialize lua context\n", m_handle);			/* to be implemented : put reason to another context and output it */
		return false;
	}
	int32_t envc = 2;
	if (argc > MAX_CTX_ARGC || !lua_checkstack(m_lstate, argc + envc)) {
		printf("[error] context:0x%08x init failed: too many arguments to initialize lua context %s\n", m_handle, argv[0]); /* to be implemented : put reason to another context and output it */
		return false;
	}
	int32_t total_length = 0;
	int32_t lengths[MAX_CTX_ARGC];
	for (int32_t i = 0; i < argc; ++i) {
		lengths[i] = strlen(argv[i]);
		lua_pushlstring(m_lstate, argv[i], lengths[i]);
		total_length += lengths[i] + 1;
	}
	char** envp = env;
	char* lua_path = NULL;
	char* lua_cpath = NULL;
	while (*envp) {
		if (strncmp(*envp, "LUA_PATH=", 9) == 0) {
			lua_path = *envp + 9;
		} else if (strncmp(*envp, "LUA_CPATH=", 10) == 0) {
			lua_cpath = *envp + 10;
		}
		++envp;
	}
	lua_pushstring(m_lstate, lua_path);
	lua_pushstring(m_lstate, lua_cpath);
	char* args = (char*)nl_calloc(total_length, 1);
	char* argp = args;
	for (int32_t i = 0; i < argc; ++i) {
		memcpy(argp, argv[i], lengths[i]);
		argp += lengths[i];
		*argp++ = (i != argc - 1) ? ' ' : '\0';
	}
	printf("[alert] context:0x%08x init: %s\n", m_handle, args); /* to be implemented : put reason to another context and output it */
	nl_free(args);
	return singleton_ref(node_lua_t).context_send(this, m_handle, 0, LUA_CTX_INIT, (int64_t)argc);
}