Initial commit
This commit is contained in:
@@ -0,0 +1,654 @@
|
||||
/*
|
||||
hm_base - hearthmod base library
|
||||
Copyright (C) 2016 Filip Pancik
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include <sys/types.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netdb.h>
|
||||
#include <sys/wait.h>
|
||||
#include <netinet/in.h>
|
||||
#include <netinet/tcp.h>
|
||||
#include <net/if.h>
|
||||
#include <arpa/inet.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/un.h>
|
||||
#include <sys/fcntl.h> // fcntl
|
||||
#include <sys/ioctl.h>
|
||||
#include <unistd.h> // close
|
||||
#include <ev.h>
|
||||
#include <errno.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
#include <hmbase.h>
|
||||
|
||||
#define ASYNC_HANG_TIMEOUT 14
|
||||
|
||||
struct ht_s **async_clients;
|
||||
|
||||
static unsigned long long client_index = 0;
|
||||
|
||||
static int async_client_accept(struct conn_client_s *client);
|
||||
static void *connector_addclient(struct conn_server_s *cs, struct conn_client_s *cc);
|
||||
|
||||
void shutdown_server(struct conn_server_s *cs)
|
||||
{
|
||||
struct conn_client_holder_s *c, *dc;
|
||||
struct hm_pool_s *p;
|
||||
|
||||
hm_log(LOG_INFO, cs->log, "{Connector}: shutting down server, clients: %d", cs->clients);
|
||||
|
||||
for(c = cs->clients_head; c != NULL; ) {
|
||||
if(c->signal_shutdown == 0 && c->client) {
|
||||
c->client->error_callback(c->client, CL_SERVERSHUTDOWN_ERR);
|
||||
}
|
||||
|
||||
dc = c;
|
||||
c = c->next;
|
||||
hm_pfree(cs->pool, dc);
|
||||
}
|
||||
|
||||
p = cs->pool;
|
||||
ev_io_stop(cs->loop, &cs->listener);
|
||||
connector_fd_close(cs->fd);
|
||||
hm_pfree(p, cs);
|
||||
}
|
||||
|
||||
static void client_error(struct conn_client_s *c, enum clerr_e err)
|
||||
{
|
||||
async_shutdown_client(c);
|
||||
}
|
||||
|
||||
int setnonblock(int fd)
|
||||
{
|
||||
int nb;
|
||||
|
||||
nb = 1;
|
||||
return ioctl(fd, FIONBIO, &nb);
|
||||
}
|
||||
|
||||
#ifdef HM_LOBBYSERVER
|
||||
static char read_byte(char **dst, const char *end)
|
||||
{
|
||||
char out;
|
||||
|
||||
if(*dst + sizeof(char) > end) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
out = *((char *)(*dst));
|
||||
|
||||
(*dst)++;
|
||||
return out;
|
||||
}
|
||||
|
||||
static int read_uint(char **dst, const char *end)
|
||||
{
|
||||
int num = 0, num2;
|
||||
int i;
|
||||
|
||||
for(i = 0; (i < 5 && *dst < end); i++) {
|
||||
num2 = read_byte(dst, end);
|
||||
|
||||
if(i == 4 && (num2 & 240) != 0) {
|
||||
abort();
|
||||
}
|
||||
|
||||
if((num2 & 128) == 0) {
|
||||
return num | (unsigned int)((unsigned int)num2 << 7 * i);
|
||||
}
|
||||
|
||||
num |= (unsigned int)((unsigned int)(num2 & 127) << 7 * i);
|
||||
}
|
||||
|
||||
return num;
|
||||
}
|
||||
|
||||
static void recv_append_lobby(struct conn_client_s *c, char *src, int nsrc)
|
||||
{
|
||||
unsigned short h;
|
||||
int i, total;
|
||||
struct conn_server_s *cs = c->parent;
|
||||
|
||||
if(nsrc < 2) {
|
||||
return;
|
||||
}
|
||||
|
||||
hm_log(LOG_DEBUG, c->log, "{Connector}: Received %d bytes", nsrc);
|
||||
|
||||
/** hbs lobby client */
|
||||
if(nsrc > 12 && *(int *)src == -1) {
|
||||
hm_log(LOG_DEBUG, c->log, "{Connector}: HBS lobby client packet");
|
||||
cs->recv(c, src, nsrc);
|
||||
return;
|
||||
}
|
||||
|
||||
/** hbs game client */
|
||||
hm_log(LOG_DEBUG, c->log, "{Connector}: HBS game client packet");
|
||||
h = (unsigned char )src[0];
|
||||
h <<= 8;
|
||||
h |= (unsigned char )src[1];
|
||||
|
||||
if(h == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
h += 2;
|
||||
|
||||
for(total = 0, i = 2; i < h; i++) {
|
||||
if(src[i] == 0x28 && (i + 1) < h) {
|
||||
char *ptr = &src[i + 1];
|
||||
total = read_uint(&ptr, ptr + nsrc - h);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
total += h;
|
||||
|
||||
cs->recv(c, src, total);
|
||||
|
||||
if(total < nsrc) {
|
||||
hm_log(LOG_DEBUG, c->log, "{Connector}: Next packet of %d bytes prepared", nsrc - total);
|
||||
recv_append_lobby(c, src + total, nsrc - total);
|
||||
}
|
||||
}
|
||||
#endif // HM_LOBBYSERVER
|
||||
|
||||
static void recv_append(struct conn_client_s *c)
|
||||
{
|
||||
#ifdef HM_GAMESERVER
|
||||
int sz;
|
||||
char *next;
|
||||
struct conn_server_s *cs = c->parent;
|
||||
|
||||
next = rb_recv_read(&c->rb, &sz);
|
||||
rb_recv_pop(&c->rb);
|
||||
cs->recv(c, next, sz);
|
||||
#elif defined HM_LOBBYSERVER
|
||||
int sz;
|
||||
char *next;
|
||||
|
||||
next = rb_recv_read(&c->rb, &sz);
|
||||
c->net_nbuf = sz;
|
||||
memcpy(c->net_buf, next, sz);
|
||||
rb_recv_pop(&c->rb);
|
||||
recv_append_lobby(c, c->net_buf, c->net_nbuf);
|
||||
#endif
|
||||
}
|
||||
|
||||
static void connector_client(struct ev_loop *loop, ev_io *w, int revents)
|
||||
{
|
||||
(void) revents;
|
||||
struct sockaddr_storage addr;
|
||||
socklen_t sl = sizeof(addr);
|
||||
int client;
|
||||
struct conn_client_s *cc;
|
||||
struct conn_server_s *cs = w->data;
|
||||
|
||||
assert(cs);
|
||||
|
||||
client = accept(w->fd, (struct sockaddr *) &addr, &sl);
|
||||
if(client == -1) {
|
||||
switch (errno) {
|
||||
case EMFILE:
|
||||
hm_log(LOG_ERR, cs->log, "{Connector}: accept() failed; too many open files for this process");
|
||||
break;
|
||||
|
||||
case ENFILE:
|
||||
hm_log(LOG_ERR, cs->log, "{client} accept() failed; too many open files for this system");
|
||||
break;
|
||||
|
||||
default:
|
||||
assert(errno == EINTR || errno == EWOULDBLOCK || errno == EAGAIN);
|
||||
break;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
int flag = 1;
|
||||
int ret = setsockopt(client, IPPROTO_TCP, TCP_NODELAY, (char *)&flag, sizeof(flag));
|
||||
if(ret == -1) {
|
||||
hm_log(LOG_ERR, cs->log, "{Connector}: Couldn't setsockopt on client (TCP_NODELAY)");
|
||||
}
|
||||
#ifdef TCP_CWND
|
||||
int cwnd = 10;
|
||||
ret = setsockopt(client, IPPROTO_TCP, TCP_CWND, &cwnd, sizeof(cwnd));
|
||||
if(ret == -1) {
|
||||
hm_log(LOG_ERR, cs->log, "Couldn't setsockopt on client (TCP_CWND)");
|
||||
}
|
||||
#endif
|
||||
|
||||
setnonblock(client);
|
||||
setkeepalive(client);
|
||||
|
||||
#define PEER_NAME
|
||||
#ifdef PEER_NAME
|
||||
socklen_t len;
|
||||
struct sockaddr_storage paddr;
|
||||
char ipstr[INET6_ADDRSTRLEN];
|
||||
int pport, peer;
|
||||
|
||||
len = sizeof(paddr);
|
||||
peer = getpeername(client, (struct sockaddr*)&paddr, &len);
|
||||
|
||||
// deal with both IPv4 and IPv6:
|
||||
if(paddr.ss_family == AF_INET) {
|
||||
struct sockaddr_in *s = (struct sockaddr_in *)&paddr;
|
||||
pport = ntohs(s->sin_port);
|
||||
inet_ntop(AF_INET, &s->sin_addr, ipstr, sizeof(ipstr));
|
||||
} else { // AF_INET6
|
||||
struct sockaddr_in6 *s = (struct sockaddr_in6 *)&paddr;
|
||||
pport = ntohs(s->sin6_port);
|
||||
inet_ntop(AF_INET6, &s->sin6_addr, ipstr, sizeof(ipstr));
|
||||
}
|
||||
|
||||
if(peer == -1) {
|
||||
hm_log(LOG_WARNING, cs->log, "{Connector}: Couldn't retrieve peer name");
|
||||
}
|
||||
#endif
|
||||
|
||||
cc = hm_palloc(cs->pool, sizeof(struct conn_client_s));
|
||||
if(cc == NULL) {
|
||||
hm_log(LOG_WARNING, cs->log, "{Connector}: Memory allocation failed");
|
||||
return;
|
||||
}
|
||||
|
||||
memset(cc, 0, sizeof(struct conn_client_s));
|
||||
|
||||
cc->loop = loop;
|
||||
cc->fd = client;
|
||||
cc->pool = cs->pool;
|
||||
cc->log = cs->log;
|
||||
cc->read.data = cc;
|
||||
cc->write.data = cc;
|
||||
cc->parent = cs;
|
||||
cc->error_callback = client_error;
|
||||
cc->client_dc = cs->client_dc;
|
||||
#ifdef PEER_NAME
|
||||
cc->nip = strlen(ipstr);
|
||||
memcpy(cc->ip, ipstr, cc->nip);
|
||||
cc->port = pport;
|
||||
#endif
|
||||
|
||||
if(connector_addclient(cs, cc) == NULL) {
|
||||
hm_pfree(cs->pool, cc);
|
||||
return;
|
||||
}
|
||||
|
||||
cc->recv = recv_append;
|
||||
async_client_accept(cc);
|
||||
}
|
||||
|
||||
int connector_server(struct conn_server_s *cs)
|
||||
{
|
||||
int t = 1;
|
||||
int timeout = 1;
|
||||
struct addrinfo *ai, hints;
|
||||
memset(&hints, 0, sizeof hints);
|
||||
hints.ai_family = AF_UNSPEC;
|
||||
hints.ai_socktype = SOCK_STREAM;
|
||||
hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
|
||||
const int gai_err = getaddrinfo(cs->host, cs->port, &hints, &ai);
|
||||
|
||||
if(gai_err != 0) {
|
||||
hm_log(LOG_CRIT, cs->log, "{Connector}: [%s]", gai_strerror(gai_err));
|
||||
return -1;
|
||||
}
|
||||
|
||||
cs->fd = socket(ai->ai_family, SOCK_STREAM | SOCK_NONBLOCK, IPPROTO_TCP);
|
||||
if(cs->fd == -1) {
|
||||
hm_log(LOG_CRIT, cs->log, "{Connector}: socket() failed");
|
||||
return -2;
|
||||
}
|
||||
|
||||
hm_log(LOG_INFO, cs->log, "{Connector}: opening server on %s:%s fd: %d", cs->host, cs->port, cs->fd);
|
||||
#ifdef SO_REUSEADDR
|
||||
setsockopt(cs->fd, SOL_SOCKET, SO_REUSEADDR, &t, sizeof(int));
|
||||
#endif
|
||||
#ifdef SO_REUSEPORT
|
||||
setsockopt(cs->fd, SOL_SOCKET, SO_REUSEPORT, &t, sizeof(int));
|
||||
#endif
|
||||
|
||||
if(bind(cs->fd, ai->ai_addr, ai->ai_addrlen)) {
|
||||
hm_log(LOG_CRIT, cs->log, "{Connector}: bind() failed");
|
||||
return -3;
|
||||
}
|
||||
|
||||
#if TCP_DEFER_ACCEPT
|
||||
setsockopt(cs->fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &timeout, sizeof(int));
|
||||
#endif
|
||||
|
||||
freeaddrinfo(ai);
|
||||
listen(cs->fd, 128);
|
||||
|
||||
ev_io_init(&cs->listener, connector_client, cs->fd, EV_READ);
|
||||
cs->listener.data = cs;
|
||||
ev_io_start(cs->loop, &cs->listener);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static void *connector_addclient(struct conn_server_s *cs, struct conn_client_s *cc)
|
||||
{
|
||||
struct conn_client_holder_s *cch;
|
||||
|
||||
cch = hm_palloc(cs->pool, sizeof(*cch));
|
||||
|
||||
if(cch == NULL) {
|
||||
hm_log(LOG_WARNING, cs->log, "{Connector}: Memory allocation failed");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memset(cch, 0, sizeof(*cch));
|
||||
|
||||
cch->client = cc;
|
||||
cch->next = cs->clients_head;
|
||||
|
||||
cc->shutdown_signal_holder = &cch->signal_shutdown;
|
||||
|
||||
cs->clients_head = cch;
|
||||
cs->clients++;
|
||||
|
||||
cc->holder = cch;
|
||||
|
||||
hm_log(LOG_NOTICE, cs->log, "{Connector}: adding client %.*s:%d, total: %d", cc->nip, cc->ip, cc->port, cs->clients);
|
||||
|
||||
return cch;
|
||||
}
|
||||
|
||||
int async_shutdown_client(struct conn_client_s *c)
|
||||
{
|
||||
if(c == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
ev_io_stop(c->loop, &c->read);
|
||||
ev_io_stop(c->loop, &c->write);
|
||||
ev_timer_stop(c->loop, &c->hang_timer);
|
||||
|
||||
/** pop in case it wasn't sent */
|
||||
rb_send_pop(&c->rb, c->pool);
|
||||
|
||||
/** let holder know we're about to shutdown */
|
||||
if(c->shutdown_signal_holder) {
|
||||
*c->shutdown_signal_holder = 1;
|
||||
}
|
||||
|
||||
if(c->monitor == 1 && c->parent != NULL) {
|
||||
c->parent->clients--;
|
||||
}
|
||||
|
||||
hm_log(LOG_DEBUG, c->log, "{Connector}: async removing client %.*s:%d fd: %d type: %d alive since: %s", c->nip, c->ip, c->port, c->fd, c->type, c->date);
|
||||
|
||||
connector_fd_close(c->fd);
|
||||
|
||||
if(c->client_dc) {
|
||||
c->client_dc(c->data, c->foreign_client_index, c->hbs_id);
|
||||
}
|
||||
|
||||
#ifdef HM_GAMESERVER
|
||||
HT_REM(async_clients, c->client_index, strlen(c->client_index), c->pool);
|
||||
#endif
|
||||
|
||||
hm_pfree(c->pool, c);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void async_handle_socket_errno(struct conn_client_s *c)
|
||||
{
|
||||
if(errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) {
|
||||
return;
|
||||
}
|
||||
|
||||
if(c == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
if(errno == ECONNRESET) {
|
||||
hm_log(LOG_ERR, c->log, "{Connector} Connection reset by peer");
|
||||
} else if(errno == ETIMEDOUT) {
|
||||
hm_log(LOG_ERR, c->log, "{Connector} Connection to backend timed out");
|
||||
} else if(errno == EPIPE) {
|
||||
hm_log(LOG_ERR, c->log, "{Connector} Broken pipe to backend (EPIPE)");
|
||||
} else {
|
||||
hm_log(LOG_ERR, c->log, "{Connector} errno: %d", errno);
|
||||
}
|
||||
}
|
||||
|
||||
static void async_hang_timeout(struct ev_loop *loop, struct ev_timer *timer, int revents)
|
||||
{
|
||||
struct conn_client_s *c = (struct conn_client_s *)timer->data;
|
||||
|
||||
assert(c);
|
||||
|
||||
hm_log(LOG_DEBUG, c->log, "{Connector}: read timeout");
|
||||
|
||||
if(c->error_callback) {
|
||||
c->error_callback(c, CL_HANGTIMEOUT_ERR);
|
||||
}
|
||||
}
|
||||
|
||||
#define NETDUMP(type, _buf, qq)\
|
||||
int i;\
|
||||
if(1 == 1) {\
|
||||
if(type == 0) {\
|
||||
hm_log(LOG_DEBUG, c->log, "sent: [%d] bytes %d", qq, fd);\
|
||||
} else {\
|
||||
hm_log(LOG_DEBUG, c->log, "received: [%d] bytes to fd %d", qq, fd);\
|
||||
}\
|
||||
printf("Ascii:\n--------\n");\
|
||||
for(i = 0; i < qq; i++) {\
|
||||
printf("%c", _buf[i] );\
|
||||
}\
|
||||
printf("Binary: ");\
|
||||
for( i = 0; i < qq; i++ ) {\
|
||||
printf("%.2x", (unsigned char)_buf[i] );\
|
||||
}\
|
||||
printf("\n");\
|
||||
}
|
||||
|
||||
static void async_read(struct ev_loop *loop, ev_io *w, int revents)
|
||||
{
|
||||
(void) revents;
|
||||
int t = 0;
|
||||
struct conn_client_s *c = (struct conn_client_s *)w->data;
|
||||
int fd = w->fd, used;
|
||||
|
||||
char *buf = rb_recv_ptr(&c->rb, &used);
|
||||
|
||||
if(c->want_shutdown) {
|
||||
if(c->error_callback) {
|
||||
c->error_callback(c, CL_WANTSHUTDOWN_ERR);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
hm_log(LOG_DEBUG, c->log, "Received from fd %d", fd);
|
||||
|
||||
t = recv(fd, buf, RB_SLOT_SIZE - used, 0);
|
||||
|
||||
if(t > 0) {
|
||||
//NETDUMP(1, buf, t)
|
||||
rb_recv_append(&c->rb, t);
|
||||
|
||||
if(rb_recv_is_full(&c->rb)) {
|
||||
ev_io_stop(c->loop, &c->read);
|
||||
if(c->error_callback) {
|
||||
c->error_callback(c, CL_READRBFULL_ERR);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if(c->hang_timer_enabled == 1) {
|
||||
ev_timer_stop(c->loop, &c->hang_timer);
|
||||
ev_timer_again(c->loop, &c->hang_timer);
|
||||
}
|
||||
|
||||
c->recv(c);
|
||||
|
||||
} else if(t == 0) {
|
||||
async_handle_socket_errno(c);
|
||||
if(c->error_callback) {
|
||||
c->error_callback(c, CL_READZERO_ERR);
|
||||
}
|
||||
} else {
|
||||
async_handle_socket_errno(c);
|
||||
if(c->error_callback) {
|
||||
c->error_callback(c, CL_READ_ERR);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void async_write(struct ev_loop *loop, ev_io *w, int revents)
|
||||
{
|
||||
(void)revents;
|
||||
int t;
|
||||
struct conn_client_s *c = (struct conn_client_s *)w->data;
|
||||
int fd = w->fd;
|
||||
int sz;
|
||||
|
||||
assert(c);
|
||||
|
||||
char *next = rb_send_next(&c->rb, &sz);
|
||||
|
||||
if(sz == 0) {
|
||||
ev_io_stop(loop, &c->write);
|
||||
return;
|
||||
}
|
||||
|
||||
t = send(fd, next, sz, MSG_NOSIGNAL);
|
||||
if(t > 0) {
|
||||
//NETDUMP(0, next, t)
|
||||
rb_send_skip(&c->rb, t);
|
||||
if(rb_send_is_empty(&c->rb)) {
|
||||
ev_io_stop(loop, &c->write);
|
||||
}
|
||||
} else {
|
||||
//assert(t == -1);
|
||||
async_handle_socket_errno(c);
|
||||
if(c->error_callback) {
|
||||
c->error_callback(c, CL_WRITE_ERR);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void timestring(char *b, const int nb)
|
||||
{
|
||||
char buf[128];
|
||||
time_t s;
|
||||
struct timespec spec;
|
||||
long long ms;
|
||||
struct tm ts;
|
||||
|
||||
clock_gettime(CLOCK_REALTIME, &spec);
|
||||
s = spec.tv_sec;
|
||||
ms = round(spec.tv_nsec / 1.0e6);
|
||||
|
||||
ts = *localtime(&s);
|
||||
strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", &ts);
|
||||
|
||||
snprintf(b, nb, "[%s.%03lld] ", buf, ms);
|
||||
}
|
||||
|
||||
int async_client(struct conn_client_s *client, const int monitor, const int hang)
|
||||
{
|
||||
struct sockaddr_in servaddr;
|
||||
char ip[32];
|
||||
|
||||
assert(client);
|
||||
|
||||
/** TCP & non-blocking */
|
||||
client->fd = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
|
||||
if(-1 == client->fd) {
|
||||
hm_log(LOG_ERR, client->log, "{Connector}: node init socket error: %d", client->fd);
|
||||
client->error_callback(client, CL_SOCKET_ERR);
|
||||
return -1;
|
||||
}
|
||||
|
||||
snprintf(ip, sizeof(ip), "%.*s", client->nip, client->ip);
|
||||
|
||||
setkeepalive(client->fd);
|
||||
memset(&servaddr, 0, sizeof(servaddr));
|
||||
servaddr.sin_family = AF_INET;
|
||||
servaddr.sin_addr.s_addr = inet_addr(ip);
|
||||
servaddr.sin_port = htons(client->port);
|
||||
|
||||
ev_io_init(&client->write, async_write, client->fd, EV_WRITE);
|
||||
ev_io_init(&client->read, async_read, client->fd, EV_READ);
|
||||
ev_init(&client->hang_timer, async_hang_timeout);
|
||||
|
||||
if(hang == 1) {
|
||||
client->hang_timer.repeat = ASYNC_HANG_TIMEOUT;
|
||||
client->hang_timer.data = client;
|
||||
client->hang_timer_enabled = 1;
|
||||
} else {
|
||||
client->hang_timer_enabled = 0;
|
||||
}
|
||||
client->read.data = client;
|
||||
client->write.data = client;
|
||||
timestring(client->date, sizeof(client->date));
|
||||
|
||||
if(monitor == 1) {
|
||||
connector_addclient(client->parent, client);
|
||||
client->monitor = 1;
|
||||
}
|
||||
|
||||
ev_io_start(client->loop, &client->read);
|
||||
ev_timer_again(client->loop, &client->hang_timer);
|
||||
if(connect(client->fd, (struct sockaddr *)&servaddr, sizeof(servaddr)) != -1 && errno != EINPROGRESS) {
|
||||
async_shutdown_client(client);
|
||||
hm_log(LOG_ERR, client->log, "{Connector}: connect() errno: %d", errno);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int async_client_accept(struct conn_client_s *client)
|
||||
{
|
||||
ev_io_init(&client->write, async_write, client->fd, EV_WRITE);
|
||||
ev_io_init(&client->read, async_read, client->fd, EV_READ);
|
||||
//ev_init(&client->hang_timer, async_hang_timeout);
|
||||
|
||||
client->hang_timer.repeat = ASYNC_HANG_TIMEOUT;
|
||||
//client->hang_timer.data = client;
|
||||
client->read.data = client;
|
||||
client->write.data = client;
|
||||
|
||||
ev_io_start(client->loop, &client->read);
|
||||
|
||||
timestring(client->date, sizeof(client->date));
|
||||
//ev_timer_again(client->loop, &client->hang_timer);
|
||||
|
||||
snprintf(client->client_index, sizeof(client->client_index), "%lld", client_index++);
|
||||
|
||||
#ifdef HM_GAMESERVER
|
||||
HT_ADD_WA(async_clients, client->client_index, strlen(client->client_index), client, sizeof(client), client->pool);
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,277 @@
|
||||
/*
|
||||
hm_base - hearthmod base library
|
||||
Copyright (C) 2016 Filip Pancik
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include <sys/types.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netdb.h>
|
||||
#include <sys/wait.h>
|
||||
#include <netinet/in.h>
|
||||
#include <netinet/tcp.h>
|
||||
#include <net/if.h>
|
||||
#include <arpa/inet.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/un.h>
|
||||
#include <sys/fcntl.h> // fcntl
|
||||
#include <sys/ioctl.h>
|
||||
#include <unistd.h> // close
|
||||
#include <ev.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
#include <hmbase.h>
|
||||
#include <connector.h>
|
||||
|
||||
#define CL(a, b) case a: hm_log(LOG_ERR, log, "{Connector}: fd:%d, "b, fd); break;
|
||||
|
||||
void connector_fd_close(int fd)
|
||||
{
|
||||
if(fd > STDERR_FILENO) {
|
||||
close(fd);
|
||||
}
|
||||
}
|
||||
|
||||
static int getsockerr(int fd, int name, void *optval, const int l, struct hm_log_s *log)
|
||||
{
|
||||
int r;
|
||||
socklen_t len = l;
|
||||
|
||||
r = getsockopt(fd, SOL_SOCKET, name, optval, &len);
|
||||
if(r < 0) {
|
||||
switch(errno) {
|
||||
CL(EBADF, "The argument sockfd is not a valid descriptor.");
|
||||
CL(EFAULT, "The address pointed to by optval is not in a valid part of the process address space.");
|
||||
CL(EINVAL, "optlen invalid in setsockopt()");
|
||||
CL(ENOPROTOOPT, "The option is unknown at the level indicated.");
|
||||
CL(ENOTSOCK, "The argument sockfd is a file, not a socket.");
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int getsoerror(int fd, struct hm_log_s *log)
|
||||
{
|
||||
int error = 0;
|
||||
|
||||
if(getsockerr(fd, SO_ERROR, &error, sizeof(int), log) < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
switch(error) {
|
||||
case 0: return 0; break;
|
||||
CL(EACCES, "For UNIX domain sockets, which are identified by pathname: Write permission is denied on the socket file, or search permission is denied for one of the directories in the path prefix. (See also path_resolution(7).)"); CL(EPERM, "The user tried to connect to a broadcast address without having the socket broadcast flag enabled or the connection request failed because of a local firewall rule.");
|
||||
CL(EADDRINUSE, "Local address is already in use.");
|
||||
CL(EAFNOSUPPORT, "The passed address didn't have the correct address family in its sa_family field.");
|
||||
CL(EAGAIN, "No more free local ports or insufficient entries in the routing cache. For AF_INET see the description of /proc/sys/net/ipv4/ip_local_port_range ip(7) for information on how to increase the number of local ports.");
|
||||
CL(EALREADY, "The socket is nonblocking and a previous connection attempt has not yet been completed.");
|
||||
CL(EBADF, "The file descriptor is not a valid index in the descriptor table.");
|
||||
CL(ECONNREFUSED, "No-one listening on the remote address.");
|
||||
CL(EFAULT, "The socket structure address is outside the user's address space.");
|
||||
CL(EINPROGRESS, "The socket is nonblocking and the connection cannot be completed immediately. It is possible to select(2) or poll(2) for completion by selecting the socket forwriting. After select(2) indicates writability, use getsockopt(2) to read the SO_ERROR option at level SOL_SOCKET to determine whether connect() completed successfully (SO_ERROR is zero) or unsuccessfully (SO_ERROR is one of the usual error codes listed here, explaining the reason for the failure).");
|
||||
CL(EINTR, "The system call was interrupted by a signal that was caught; see signal(7).");
|
||||
CL(EISCONN, "The socket is already connected.");
|
||||
CL(ENETUNREACH, "Network is unreachable.");
|
||||
CL(ENOTSOCK, "The file descriptor is not associated with a socket.");
|
||||
CL(ETIMEDOUT, "Timeout while attempting connection. The server may be too busy to accept new connections. Note that for IP sockets the timeout may be very long when syncookies are enabled on the server.");
|
||||
}
|
||||
return -2;
|
||||
}
|
||||
|
||||
static int islinger(struct conn_node_s *c)
|
||||
{
|
||||
struct linger l;
|
||||
|
||||
if(getsockerr(c->fd, SO_LINGER, &l, sizeof(l), c->log) < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return l.l_onoff;
|
||||
}
|
||||
|
||||
static void failed_cb(struct ev_loop *loop, ev_timer *w, int revents)
|
||||
{
|
||||
int r;
|
||||
struct conn_node_s *c = (struct conn_node_s *)w->data;
|
||||
#ifdef D_ASSERT
|
||||
assert(c != NULL);
|
||||
#endif
|
||||
|
||||
ev_io_stop(loop, &c->io);
|
||||
|
||||
r = islinger(c);
|
||||
if(r < 0) {
|
||||
hm_log(LOG_ERR, c->log, "{Connector}: error on socket %d", c->fd);
|
||||
} else if(r > 0) {
|
||||
hm_log(LOG_ERR, c->log, "{Connector}: SO_LINGER set on fd %d", c->fd);
|
||||
}
|
||||
connector_fd_close(c->fd);
|
||||
}
|
||||
|
||||
static void established_cb(struct ev_loop *loop, ev_io *w, int revents)
|
||||
{
|
||||
int r;
|
||||
struct conn_node_s *c = (struct conn_node_s *)w->data;
|
||||
|
||||
#ifdef D_ASSERT
|
||||
assert(c != NULL);
|
||||
#endif
|
||||
|
||||
ev_timer_stop(loop, &c->timer);
|
||||
ev_io_stop(loop, w);
|
||||
|
||||
r = getsoerror(c->fd, c->log);
|
||||
|
||||
if(r == 0) {
|
||||
c->status |= CONN_F_CONNECTED;
|
||||
|
||||
if(!(c->status & CONN_F_FDOPEN)) {
|
||||
r = islinger(c);
|
||||
if(r < 0) {
|
||||
hm_log(LOG_ERR, c->log, "{Connector}: error on socket %d", c->fd);
|
||||
} else if(r > 0) {
|
||||
hm_log(LOG_ERR, c->log, "{Connector}: SO_LINGER set on fd %d", c->fd);
|
||||
}
|
||||
connector_fd_close(c->fd);
|
||||
}
|
||||
|
||||
/**< return immediately when single check is performed or all nodes are connected */
|
||||
if(c->status & CONN_F_SINGLECHECK || ++(c->parent->established) == c->parent->size) {
|
||||
ev_timer_stop(loop, &c->parent->timer);
|
||||
c->parent->callback(c->parent);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static int check_connection(struct conn_data_s *cd, struct conn_node_s *c)
|
||||
{
|
||||
struct sockaddr_in servaddr;
|
||||
|
||||
#ifdef D_ASSERT
|
||||
assert(c != NULL && cd->loop != NULL);
|
||||
#endif
|
||||
if(strlen(c->host) < 1 || c->port < 1 || c->port > ((1<<16) - 1)) {
|
||||
hm_log(LOG_ERR, c->log, "{Connector}: incorrect hostname or port: [%s]:%d", c->host, c->port);
|
||||
return -1;
|
||||
}
|
||||
|
||||
c->fd = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
|
||||
if(-1 == c->fd) {
|
||||
hm_log(LOG_ERR, c->log, "{Connector}: FD = -1");
|
||||
return -1;
|
||||
}
|
||||
|
||||
// TODO: setsockopt depending on c->status
|
||||
|
||||
memset(&servaddr, 0, sizeof(servaddr));
|
||||
servaddr.sin_family = AF_INET;
|
||||
servaddr.sin_addr.s_addr=inet_addr(c->host);
|
||||
servaddr.sin_port=htons(c->port);
|
||||
|
||||
c->io.data = c;
|
||||
ev_io_init(&c->io, established_cb, c->fd, EV_WRITE);
|
||||
ev_io_start(cd->loop, &c->io);
|
||||
|
||||
c->timer.data = c;
|
||||
ev_timer_init(&c->timer, failed_cb, cd->timeout, 0);
|
||||
ev_timer_start(cd->loop, &c->timer);
|
||||
|
||||
hm_log(LOG_INFO, c->log, "{Connector}: connecting to %s:%d", c->host, c->port);
|
||||
connect(c->fd, (struct sockaddr *)&servaddr, sizeof(servaddr));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void terminator(struct ev_loop *loop, ev_timer *w, int revents)
|
||||
{
|
||||
int i;
|
||||
struct conn_data_s *c = (struct conn_data_s *)w->data;
|
||||
|
||||
#ifdef D_ASSERT
|
||||
assert(c != NULL);
|
||||
#endif
|
||||
/**< Terminate all connector events */
|
||||
for(i = 0; i < c->size; i++) {
|
||||
if(c && c->loop && c->src[i]) {
|
||||
ev_io_stop(c->loop, &c->src[i]->io);
|
||||
ev_timer_stop(c->loop, &c->src[i]->timer);
|
||||
}
|
||||
}
|
||||
|
||||
c->callback(c);
|
||||
}
|
||||
|
||||
int setkeepalive(int fd)
|
||||
{
|
||||
int optval;
|
||||
socklen_t optlen = sizeof(optval);
|
||||
|
||||
optval = 1;
|
||||
if(setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &optval, optlen) < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int connector(struct conn_data_s *c)
|
||||
{
|
||||
int i;
|
||||
|
||||
#ifdef D_ASSERT
|
||||
assert(c != NULL);
|
||||
#endif
|
||||
|
||||
c->established = 0;
|
||||
for(i = 0; i < c->size; i++) {
|
||||
c->src[i]->parent = c;
|
||||
c->src[i]->log = c->log;
|
||||
check_connection(c, c->src[i]);
|
||||
}
|
||||
|
||||
c->timer.data = c;
|
||||
|
||||
ev_timer_init(&c->timer, terminator, c->exptime, 0);
|
||||
ev_timer_start(c->loop, &c->timer);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void connector_free(struct conn_data_s *c)
|
||||
{
|
||||
if(!c) {
|
||||
return;
|
||||
}
|
||||
|
||||
int i;
|
||||
for(i = 0; i < c->size; i++) {
|
||||
ev_io_stop(c->loop, &c->src[i]->io);
|
||||
ev_timer_stop(c->loop, &c->src[i]->timer);
|
||||
connector_fd_close(c->src[i]->fd);
|
||||
hm_pfree(c->pool, c->src[i]);
|
||||
}
|
||||
|
||||
ev_timer_stop(c->loop, &c->timer);
|
||||
hm_pfree(c->pool, c->src);
|
||||
hm_pfree(c->pool, c);
|
||||
}
|
||||
@@ -0,0 +1,567 @@
|
||||
/*
|
||||
hm_base - hearthmod base library
|
||||
Copyright (C) 2016 Filip Pancik
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/time.h>
|
||||
#include <stdint.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/un.h>
|
||||
#include <errno.h>
|
||||
#include <libcouchbase/couchbase.h>
|
||||
#include <libcouchbase/views.h>
|
||||
#include <ev.h>
|
||||
|
||||
#include <hmbase.h>
|
||||
#include <connector.h>
|
||||
#include <hmcouchbase.h>
|
||||
|
||||
// TODO: even though these globals can stay here, move them elsewhere in the future
|
||||
static lcb_t *active_instance;
|
||||
static struct conn_node_s **c;
|
||||
static struct conn_data_s *cd;
|
||||
static int BUCKETS_PER_CLUSTER;
|
||||
static int HOSTS;
|
||||
static int grcount = 0; /**< groups counter */
|
||||
static void (*callback)(struct instance_s *) = NULL;
|
||||
static struct instance_s *instances = NULL;
|
||||
static struct connstr_s **groups = NULL;
|
||||
static struct hm_log_s *cb_log = NULL;
|
||||
|
||||
static void error_callback(lcb_t instance, struct cbop_s *cbop, lcb_error_t error);
|
||||
static int is_active_instance(lcb_t instance);
|
||||
static void switch_instances(lcb_t instance);
|
||||
static void bootstrap_callback(lcb_t instance, lcb_error_t err);
|
||||
static void store_callback(lcb_t instance, const void *cookie,
|
||||
lcb_storage_t operation, lcb_error_t error, const lcb_store_resp_t *resp);
|
||||
static void remove_callback(lcb_t instance, const void *cookie, lcb_error_t error, const lcb_remove_resp_t *resp);
|
||||
static void arithmetic_callback(lcb_t instance, const void *cookie, lcb_error_t error, const lcb_arithmetic_resp_t *resp);
|
||||
static void get_callback(lcb_t instance, const void *cookie, lcb_error_t error, const lcb_get_resp_t *resp);
|
||||
static int cb_get(struct cbop_s *cbop);
|
||||
static int cb_arithmetic(struct cbop_s *cbop);
|
||||
static int cb_remove(struct cbop_s *cbop);
|
||||
static int cb_store(struct cbop_s *cbop);
|
||||
static int cb_view(struct cbop_s *cbop);
|
||||
static int storage_init(struct conn_data_s *cd, struct conn_node_s *cn, struct connstr_s *cs, const int idx );
|
||||
static void connector_cb(struct conn_data_s *c);
|
||||
|
||||
static void error_callback(lcb_t instance, struct cbop_s *cbop, lcb_error_t error)
|
||||
{
|
||||
hm_log(LOG_ERR, cb_log, "{Couchbase}: error 0x%x", error);
|
||||
|
||||
// network related errors
|
||||
if(error == LCB_NETWORK_ERROR || error == LCB_ETIMEDOUT || error == LCB_ENETUNREACH) {
|
||||
if(is_active_instance(instance) == CB_ERR_OK) {
|
||||
// switch instances and try again
|
||||
// TODO: keep track of switching to avoid dead lock if all groups are offline
|
||||
switch_instances(instance);
|
||||
couchbase_operation(cbop);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#define TYPE(T)\
|
||||
if(resp->version == CB_QUERY_VER) {\
|
||||
cbop->lcb_error = error;\
|
||||
cbop->u.T.r = resp;\
|
||||
if(cbop->post) {\
|
||||
cbop->post(cbop);\
|
||||
}\
|
||||
}
|
||||
|
||||
static void store_callback(lcb_t instance, const void *cookie,
|
||||
lcb_storage_t operation, lcb_error_t error, const lcb_store_resp_t *resp)
|
||||
{
|
||||
struct cbop_s *cbop = (struct cbop_s *)cookie;
|
||||
|
||||
if(error == LCB_SUCCESS || error == LCB_KEY_ENOENT || error == LCB_KEY_EEXISTS || (error == LCB_ETMPFAIL && CBSQ(cas) != 0)) {
|
||||
TYPE(s)
|
||||
} else {
|
||||
error_callback(instance, cbop, error);
|
||||
}
|
||||
}
|
||||
|
||||
static void remove_callback(lcb_t instance, const void *cookie, lcb_error_t error, const lcb_remove_resp_t *resp)
|
||||
{
|
||||
struct cbop_s *cbop = (struct cbop_s *)cookie;
|
||||
|
||||
if(error == LCB_SUCCESS || error == LCB_KEY_ENOENT || (error == LCB_ETMPFAIL && CBRQ(cas) != 0)) {
|
||||
TYPE(r)
|
||||
} else {
|
||||
error_callback(instance, cbop, error);
|
||||
}
|
||||
}
|
||||
|
||||
static void arithmetic_callback(lcb_t instance, const void *cookie, lcb_error_t error, const lcb_arithmetic_resp_t *resp)
|
||||
{
|
||||
struct cbop_s *cbop = (struct cbop_s *)cookie;
|
||||
|
||||
if(error == LCB_SUCCESS || error == LCB_KEY_ENOENT) {
|
||||
TYPE(a)
|
||||
} else {
|
||||
error_callback(instance, cbop, error);
|
||||
}
|
||||
}
|
||||
|
||||
static void get_callback(lcb_t instance, const void *cookie, lcb_error_t error, const lcb_get_resp_t *resp)
|
||||
{
|
||||
struct cbop_s *cbop = (struct cbop_s *)cookie;
|
||||
|
||||
if(error == LCB_SUCCESS || error == LCB_KEY_ENOENT) {
|
||||
TYPE(g)
|
||||
} else {
|
||||
error_callback(instance, cbop, error);
|
||||
}
|
||||
}
|
||||
|
||||
static int cb_get(struct cbop_s *cbop)
|
||||
{
|
||||
lcb_error_t error;
|
||||
const lcb_get_cmd_t *commands[] = { &(cbop->u.g.q) };
|
||||
|
||||
if(0) hm_log(LOG_DEBUG, cb_log, "{Couchbase}: get [%.*s]", (int)commands[0]->v.v0.nkey, (char*)commands[0]->v.v0.key);
|
||||
error = lcb_get(active_instance[cbop->bucket], cbop, 1, commands);
|
||||
if(error != LCB_SUCCESS) {
|
||||
hm_log(LOG_ERR, cb_log, "{Couchbase}: couchbase operation failed with error: %d", error);
|
||||
return error;
|
||||
}
|
||||
|
||||
return CB_ERR_OK;
|
||||
}
|
||||
|
||||
static int cb_arithmetic(struct cbop_s *cbop)
|
||||
{
|
||||
lcb_error_t error;
|
||||
const lcb_arithmetic_cmd_t *cmds[] = { &(cbop->u.a.q) };
|
||||
|
||||
error = lcb_arithmetic(active_instance[cbop->bucket], cbop, 1, cmds);
|
||||
if(error != LCB_SUCCESS) {
|
||||
hm_log(LOG_ERR, cb_log, "{Couchbase}: couchbase operation failed with error: %d", error);
|
||||
return error;
|
||||
}
|
||||
|
||||
return CB_ERR_OK;
|
||||
}
|
||||
|
||||
static int cb_remove(struct cbop_s *cbop)
|
||||
{
|
||||
lcb_error_t error;
|
||||
const lcb_remove_cmd_t *cmds[] = { &(cbop->u.r.q) };
|
||||
|
||||
error = lcb_remove(active_instance[cbop->bucket], cbop, 1, cmds);
|
||||
if(error != LCB_SUCCESS) {
|
||||
hm_log(LOG_ERR, cb_log, "{Couchbase}: couchbase operation failed with error: %d", error);
|
||||
return error;
|
||||
}
|
||||
|
||||
return CB_ERR_OK;
|
||||
}
|
||||
|
||||
static int cb_store(struct cbop_s *cbop)
|
||||
{
|
||||
lcb_error_t error;
|
||||
const lcb_store_cmd_t *cmds[] = { &(cbop->u.s.q) };
|
||||
|
||||
if(1 == 1) hm_log(LOG_DEBUG, cb_log, "{Couchbase}: set [%.*s], cas=0x%lx [%.*s]", (int)cmds[0]->v.v0.nkey, (char*)cmds[0]->v.v0.key, cmds[0]->v.v0.cas, (int)cmds[0]->v.v0.nbytes, (char*)cmds[0]->v.v0.bytes);
|
||||
printf("bucket %p\n", active_instance[cbop->bucket]);
|
||||
error = lcb_store(active_instance[cbop->bucket], cbop, 1, cmds);
|
||||
|
||||
printf("bucket %p\n", active_instance[cbop->bucket]);
|
||||
if (error != LCB_SUCCESS) {
|
||||
hm_log(LOG_ERR, cb_log, "{Couchbase}: couchbase operation failed with error: %d", error);
|
||||
return error;
|
||||
}
|
||||
|
||||
printf("error %d\n", error);
|
||||
|
||||
return CB_ERR_OK;
|
||||
}
|
||||
|
||||
static int cb_view(struct cbop_s *cbop)
|
||||
{
|
||||
lcb_error_t error;
|
||||
|
||||
error = lcb_view_query(active_instance[cbop->bucket], cbop, &(cbop->u.v.q));
|
||||
if (error != LCB_SUCCESS) {
|
||||
hm_log(LOG_ERR, cb_log, "{Couchbase}: couchbase operation failed with error: %d", error);
|
||||
return error;
|
||||
}
|
||||
|
||||
return CB_ERR_OK;
|
||||
}
|
||||
|
||||
/**< @brief
|
||||
*
|
||||
* Natural selection based on network latency, whichever instance comes first, takes 0 index
|
||||
*/
|
||||
static void set_instance(lcb_t instance)
|
||||
{
|
||||
const void *cookie;
|
||||
struct instance_s *ins;
|
||||
|
||||
cookie = lcb_get_cookie(instance);
|
||||
ins = (struct instance_s *)cookie;
|
||||
|
||||
if(active_instance[ins->index] == NULL) {
|
||||
active_instance[ins->index] = instance;
|
||||
hm_log(LOG_DEBUG, ins->log, "{Couchbase}: New instance on index %d from group: %d : %p", ins->index, ins->group, instance);
|
||||
}
|
||||
}
|
||||
|
||||
// broken instance is set as active?
|
||||
static int is_active_instance(lcb_t instance)
|
||||
{
|
||||
int i;
|
||||
|
||||
for(i = 0; i < BUCKETS_PER_CLUSTER; i++) {
|
||||
if(active_instance[i] == instance) {
|
||||
return CB_ERR_OK;
|
||||
}
|
||||
}
|
||||
|
||||
return CB_ERR_NOACTIVE;
|
||||
}
|
||||
|
||||
static void switch_instances(lcb_t instance)
|
||||
{
|
||||
int i, j, k;
|
||||
const void *cookie;
|
||||
struct instance_s *ins;
|
||||
|
||||
cookie = lcb_get_cookie(instance);
|
||||
ins = (struct instance_s *)cookie;
|
||||
|
||||
for(i = 0; i < HOSTS; i++) {
|
||||
for(j = 0; j < BUCKETS_PER_CLUSTER; j++) {
|
||||
if(((struct cluster_s *)(cd->src[i]->data))->instance[j] != instance &&
|
||||
cd->src[i]->group != ins->group ) {
|
||||
for(k = 0; k < BUCKETS_PER_CLUSTER; k++) {
|
||||
hm_log(LOG_DEBUG, ins->log, "{Couchbase}: old instance: %p, new instance: %p, index: %d new host: %s", active_instance[k], ((struct cluster_s *)(cd->src[i]->data))->instance[k], k, cd->src[i]->host);
|
||||
active_instance[k] = ((struct cluster_s *)(cd->src[i]->data))->instance[k];
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void bootstrap_callback(lcb_t instance, lcb_error_t err)
|
||||
{
|
||||
static int counter = 0;
|
||||
const void *cookie;
|
||||
struct instance_s *ins;
|
||||
|
||||
cookie = lcb_get_cookie(instance);
|
||||
ins = (struct instance_s *)cookie;
|
||||
|
||||
if(err == LCB_SUCCESS) {
|
||||
ins->status = 1;
|
||||
set_instance(instance);
|
||||
} else {
|
||||
ins->status = 0;
|
||||
hm_log(LOG_ERR, ins->log, "{Couchbase}: Bootstrap error 0x%x", err);
|
||||
}
|
||||
|
||||
ins->next = instances;
|
||||
instances = ins;
|
||||
|
||||
if(++counter >= (BUCKETS_PER_CLUSTER * grcount) && callback) {
|
||||
callback(instances);
|
||||
}
|
||||
}
|
||||
|
||||
static void destroy_callback(const void *cookie)
|
||||
{
|
||||
struct instance_s *ins;
|
||||
|
||||
ins = (struct instance_s *)cookie;
|
||||
|
||||
///< this causes SIGSEGV
|
||||
//lcb_destroy_io_ops(ins->ioops);
|
||||
|
||||
hm_pfree(ins->pool, ins);
|
||||
}
|
||||
|
||||
static int storage_init(struct conn_data_s *cd, struct conn_node_s *cn, struct connstr_s *cs, const int idx)
|
||||
{
|
||||
lcb_error_t error;
|
||||
struct lcb_create_io_ops_st ciops;
|
||||
struct lcb_create_st copts;
|
||||
char connstr[CONNSTR_SIZE];
|
||||
struct instance_s *ins;
|
||||
struct cluster_s *cl = cn->data;
|
||||
int nconnstr;
|
||||
|
||||
memset(&ciops, 0, sizeof(ciops));
|
||||
memset(&copts, 0, sizeof(copts));
|
||||
ins = hm_palloc(cd->pool, sizeof(*ins));
|
||||
|
||||
ciops.v.v0.type = LCB_IO_OPS_LIBEV;
|
||||
ciops.v.v0.cookie = cd->loop;
|
||||
|
||||
assert(cl != NULL);
|
||||
|
||||
error = lcb_create_io_ops(&ins->ioops, &ciops);
|
||||
if(error != LCB_SUCCESS) {
|
||||
hm_pfree(cd->pool, ins);
|
||||
return CB_ERR_LCBCREATEIO;
|
||||
}
|
||||
|
||||
snprintf(connstr, sizeof(connstr), "couchbase://%.*s/%s", cs->len - 1, cs->host, cl->bucket[idx]);
|
||||
hm_log(LOG_DEBUG, cd->log, "{Connector}: connstr: [%s]", connstr);
|
||||
copts.version = 3;
|
||||
copts.v.v3.connstr = connstr;
|
||||
copts.v.v3.username = cl->bucket[idx];
|
||||
copts.v.v3.passwd = cl->passwd[idx];
|
||||
copts.v.v3.io = ins->ioops;
|
||||
|
||||
error = lcb_create(&cl->instance[idx], &copts);
|
||||
if(error != LCB_SUCCESS) {
|
||||
hm_pfree(cd->pool, ins);
|
||||
return CB_ERR_LCBCREATE;
|
||||
}
|
||||
|
||||
nconnstr = strlen(connstr);
|
||||
ins->group = cs->id;
|
||||
ins->index = idx;
|
||||
memcpy(ins->connstr, connstr, nconnstr);
|
||||
ins->connstr[nconnstr] = 0;
|
||||
ins->loop = cd->loop;
|
||||
ins->log = cd->log;
|
||||
ins->pool = cd->pool;
|
||||
lcb_set_cookie(cl->instance[idx], ins);
|
||||
|
||||
lcb_set_bootstrap_callback(cl->instance[idx], bootstrap_callback);
|
||||
lcb_set_get_callback(cl->instance[idx], get_callback);
|
||||
lcb_set_store_callback(cl->instance[idx], store_callback);
|
||||
lcb_set_remove_callback(cl->instance[idx], remove_callback);
|
||||
lcb_set_arithmetic_callback(cl->instance[idx], arithmetic_callback);
|
||||
lcb_set_destroy_callback(cl->instance[idx], destroy_callback);
|
||||
|
||||
if((error = lcb_connect(cl->instance[idx])) != LCB_SUCCESS) {
|
||||
lcb_destroy(cl->instance[idx]);
|
||||
hm_pfree(cd->pool, ins);
|
||||
return CB_ERR_LCBCONNECT;
|
||||
}
|
||||
|
||||
return CB_ERR_OK;
|
||||
}
|
||||
|
||||
static void buildconnstr(struct conn_data_s *c, struct connstr_s **groups, const char *host, const int group)
|
||||
{
|
||||
int len;
|
||||
|
||||
if(group >= MAX_GROUPS || group < 0) {
|
||||
hm_log(LOG_ERR, c->log, "{Couchbase}: groups count error: %d", group);
|
||||
return;
|
||||
}
|
||||
|
||||
if(groups[group] == NULL) {
|
||||
groups[group] = hm_palloc(c->pool, sizeof(struct connstr_s));
|
||||
groups[group]->host = hm_palloc(c->pool, CONNSTR_SIZE); /**< max size */
|
||||
groups[group]->len = strlen(host);
|
||||
groups[group]->id = group;
|
||||
|
||||
memcpy(groups[group]->host, host, groups[group]->len);
|
||||
groups[group]->host[groups[group]->len++] = ','; /**< connstr delimiter */
|
||||
|
||||
grcount++;
|
||||
} else {
|
||||
len = strlen(host);
|
||||
memcpy(&(groups[group]->host[groups[group]->len]), host, len);
|
||||
groups[group]->len += len;
|
||||
groups[group]->host[groups[group]->len++] = ','; /**< connstr delimiter */
|
||||
}
|
||||
}
|
||||
|
||||
static void connector_cb(struct conn_data_s *c)
|
||||
{
|
||||
int i, j, error, connected = 0;
|
||||
|
||||
for(i = 0; i < c->size; i++) {
|
||||
if(c->src[i] && (c->src[i]->status & CONN_F_CONNECTED)) {
|
||||
hm_log(LOG_DEBUG, c->log, "{Connector}: Connected to %s:%d", c->src[i]->host, c->src[i]->port);
|
||||
buildconnstr(c, groups, c->src[i]->host, c->src[i]->group);
|
||||
connected = 1;
|
||||
} else {
|
||||
hm_log(LOG_NOTICE, c->log, "{Connector}: Failed to connect to %s:%d", c->src[i]->host, c->src[i]->port);
|
||||
}
|
||||
}
|
||||
|
||||
/**< No available hosts found */
|
||||
if(connected != 1) {
|
||||
callback(NULL);
|
||||
return;
|
||||
}
|
||||
|
||||
for(i = 0; i < MAX_GROUPS; i++) {
|
||||
if(groups[i] != NULL) {
|
||||
for(j = 0; j < BUCKETS_PER_CLUSTER; j++) {
|
||||
error = storage_init(c, c->src[i], groups[i], j);
|
||||
if(error != CB_ERR_OK) {
|
||||
hm_log(LOG_ERR, c->log, "{Couchbase}: storage_init failed for %s:%d with code 0x%x", c->src[i]->host, c->src[i]->port, error);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int couchbase_init(struct couchbase_data_s *cbd)
|
||||
{
|
||||
int i, j;
|
||||
struct cluster_s *cl;
|
||||
|
||||
groups = hm_palloc(cbd->pool, sizeof(void *) * MAX_GROUPS);
|
||||
memset(groups, 0, sizeof(void *) * MAX_GROUPS);
|
||||
|
||||
BUCKETS_PER_CLUSTER = cbd->nbuckets;
|
||||
HOSTS = cbd->nhosts;
|
||||
|
||||
assert(cbd->pool != NULL && cbd->log != NULL);
|
||||
|
||||
cd = hm_palloc(cbd->pool, sizeof(*cd));
|
||||
cd->size = HOSTS;
|
||||
cd->loop = cbd->loop;
|
||||
cd->callback = connector_cb;
|
||||
cd->log = cbd->log;
|
||||
cd->pool = cbd->pool;
|
||||
cd->timeout = 1.0;
|
||||
cd->exptime = 2.0;
|
||||
callback = cbd->callback;
|
||||
cb_log = cbd->log;
|
||||
|
||||
active_instance = hm_palloc(cd->pool, BUCKETS_PER_CLUSTER * sizeof(lcb_t));
|
||||
for(i = 0; i < BUCKETS_PER_CLUSTER; i++) {
|
||||
active_instance[i] = NULL;
|
||||
}
|
||||
|
||||
c = hm_palloc(cd->pool, HOSTS * sizeof(void *));
|
||||
for( i = 0; i < HOSTS; i++ ) {
|
||||
c[i] = hm_palloc(cd->pool, sizeof(*(c[i])));
|
||||
bzero(c[i], sizeof(*c[i]));
|
||||
c[i]->port = 8091;
|
||||
c[i]->status = CONN_F_COUCHBASE; /**< Couchbase opens its own FD */
|
||||
c[i]->group = cbd->hgroups[i];
|
||||
|
||||
memcpy( c[i]->host, cbd->hosts[i], strlen(cbd->hosts[i])+1 );
|
||||
|
||||
cl = hm_palloc(cd->pool, sizeof(*cl));
|
||||
cl->bucket = hm_palloc(cd->pool, sizeof(void *) * BUCKETS_PER_CLUSTER);
|
||||
cl->passwd = hm_palloc(cd->pool, sizeof(void *) * BUCKETS_PER_CLUSTER);
|
||||
cl->instance = hm_palloc(cd->pool, sizeof(lcb_t) * BUCKETS_PER_CLUSTER);
|
||||
memset(cl->instance, 0, sizeof(lcb_t) * BUCKETS_PER_CLUSTER);
|
||||
|
||||
for( j = 0; j < BUCKETS_PER_CLUSTER; j++ ) {
|
||||
cl->bucket[j] = hm_palloc(cd->pool, strlen(cbd->buckets[j])+1);
|
||||
cl->passwd[j] = hm_palloc(cd->pool, strlen(cbd->bpasswd[j])+1);
|
||||
memcpy(cl->bucket[j], cbd->buckets[j], strlen(cbd->buckets[j])+1);
|
||||
memcpy(cl->passwd[j], cbd->bpasswd[j], strlen(cbd->bpasswd[j])+1);
|
||||
}
|
||||
|
||||
c[i]->data = cl;
|
||||
}
|
||||
|
||||
cd->src = c;
|
||||
connector( cd );
|
||||
|
||||
return CB_ERR_OK;
|
||||
}
|
||||
|
||||
int couchbase_bucket_index(const char *bucketName) {
|
||||
if(HOSTS == 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
struct cluster_s *cl = (struct cluster_s*)c[0]->data;
|
||||
int i;
|
||||
for(i=0; i<BUCKETS_PER_CLUSTER; i++) {
|
||||
if(strcmp(cl->bucket[i], bucketName) == 0) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int couchbase_operation(struct cbop_s *cbop)
|
||||
{
|
||||
// TODO: check if bucket index in range
|
||||
if(active_instance[cbop->bucket] == NULL) {
|
||||
hm_log(LOG_ERR, cb_log, "{Couchbase}: No active instance for bucket: %d", cbop->bucket);
|
||||
return CB_ERR_NOINSTANCE;
|
||||
}
|
||||
|
||||
switch(cbop->operation) {
|
||||
case CB_GET:
|
||||
return cb_get(cbop);
|
||||
break;
|
||||
case CB_STORE:
|
||||
return cb_store(cbop);
|
||||
break;
|
||||
case CB_REMOVE:
|
||||
return cb_remove(cbop);
|
||||
break;
|
||||
case CB_ARITH:
|
||||
return cb_arithmetic(cbop);
|
||||
break;
|
||||
case CB_VIEW:
|
||||
return cb_view(cbop);
|
||||
break;
|
||||
default:
|
||||
hm_log(LOG_ERR, cb_log, "{Couchbase}: Operation not supported: %d", cbop->operation);
|
||||
return CB_ERR_OPNOT;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void couchbase_deinit()
|
||||
{
|
||||
int i, j;
|
||||
//lcb_t instance;
|
||||
struct cluster_s *cl;
|
||||
const void *cookie;
|
||||
|
||||
for(i = 0; i < MAX_GROUPS; i++) {
|
||||
if(groups[i]) {
|
||||
hm_pfree(cd->pool, groups[i]->host);
|
||||
hm_pfree(cd->pool, groups[i]);
|
||||
}
|
||||
}
|
||||
hm_pfree(cd->pool, groups); /**< groups */
|
||||
hm_pfree(cd->pool, active_instance); /**< active instances */
|
||||
|
||||
for(i = 0; i < HOSTS; i++) {
|
||||
cl = cd->src[i]->data;
|
||||
|
||||
for(j = 0; j < BUCKETS_PER_CLUSTER; j++) {
|
||||
hm_pfree(cd->pool, cl->bucket[j]);
|
||||
hm_pfree(cd->pool, cl->passwd[j]);
|
||||
if(cl->instance[j]) {
|
||||
cookie = lcb_get_cookie(cl->instance[j]);
|
||||
lcb_destroy_async(cl->instance[j], cookie);
|
||||
//lcb_destroy_io_ops(((struct instance_s *)cookie)->ioops);
|
||||
}
|
||||
}
|
||||
hm_pfree(cd->pool, cl->bucket);
|
||||
hm_pfree(cd->pool, cl->passwd);
|
||||
hm_pfree(cd->pool, cl->instance);
|
||||
hm_pfree(cd->pool, cl);
|
||||
}
|
||||
|
||||
connector_free(cd);
|
||||
}
|
||||
@@ -0,0 +1,204 @@
|
||||
/*
|
||||
hm_base - hearthmod base library
|
||||
Copyright (C) 2016 Filip Pancik
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <memory.h>
|
||||
#include <assert.h>
|
||||
|
||||
#include <hmbase.h>
|
||||
|
||||
struct ht_s **ht_init(struct hm_pool_s *pool)
|
||||
{
|
||||
struct ht_s **ht;
|
||||
|
||||
ht = hm_palloc(pool, sizeof(void *) * HT_MAX);
|
||||
if(ht == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memset(ht, 0, sizeof(void *) * HT_MAX);
|
||||
|
||||
return ht;
|
||||
}
|
||||
|
||||
void ht_free(struct ht_s **ht, struct hm_pool_s *pool)
|
||||
{
|
||||
hm_pfree(pool, ht);
|
||||
}
|
||||
|
||||
inline static void ht_key(int *dst, const char *key, const int nkey)
|
||||
{
|
||||
int i;
|
||||
|
||||
for(i = 0; i < nkey; i++) {
|
||||
*dst += key[i];
|
||||
}
|
||||
|
||||
*dst %= HT_MAX;
|
||||
}
|
||||
|
||||
int ht_add(struct ht_s **ht, const char *key, const int nkey, const void *value, const int nvalue, const int alloc, struct hm_pool_s *pool)
|
||||
{
|
||||
struct ht_s *h;
|
||||
int index = 0;
|
||||
|
||||
assert(ht);
|
||||
|
||||
ht_key(&index, key, nkey);
|
||||
|
||||
for(h = ht[index]; h != NULL; h = h->next) {
|
||||
if(nkey == h->nk && memcmp(key, h->k, nkey) == 0) {
|
||||
/** first, free existing value */
|
||||
if(h->flag == HT_ALLOC) {
|
||||
hm_pfree(pool, h->s);
|
||||
}
|
||||
|
||||
/** then copy new value */
|
||||
if(alloc == HT_ALLOC) {
|
||||
h->s = hm_palloc(pool, nvalue);
|
||||
if(h->s == NULL) {
|
||||
return -1;
|
||||
}
|
||||
#ifdef POOL_STDLIB
|
||||
memcpy(h->s, value, nvalue);
|
||||
#else
|
||||
hm_memcpy(h->s, value, nvalue, h->s);
|
||||
#endif
|
||||
} else {
|
||||
h->s = (void *)value;
|
||||
}
|
||||
|
||||
h->flag = alloc;
|
||||
h->n = nvalue;
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
h = hm_palloc(pool, sizeof(*h));
|
||||
if(h == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
h->nk = nkey;
|
||||
h->k = hm_palloc(pool, nkey);
|
||||
if(h->k == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
h->n = nvalue;
|
||||
|
||||
/* always allocate a key */
|
||||
#ifdef POOL_STDLIB
|
||||
memcpy(h->k, key, nkey);
|
||||
#else
|
||||
hm_memcpy(h->k, key, nkey, h->k);
|
||||
#endif
|
||||
|
||||
if(alloc == HT_ALLOC) {
|
||||
h->flag = HT_ALLOC;
|
||||
h->s = hm_palloc(pool, nvalue);
|
||||
if(h->s == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
#ifdef POOL_STDLIB
|
||||
memcpy(h->s, value, nvalue);
|
||||
#else
|
||||
hm_memcpy(h->s, value, nvalue, h->s);
|
||||
#endif
|
||||
} else {
|
||||
h->flag = 0;
|
||||
h->s = (void *)value;
|
||||
}
|
||||
|
||||
h->next = ht[index];
|
||||
ht[index] = h;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ht_rem(struct ht_s **ht, const char *key, const int nkey, struct hm_pool_s *pool)
|
||||
{
|
||||
struct ht_s *h, *prev = NULL;
|
||||
int index = 0;
|
||||
|
||||
assert(ht);
|
||||
|
||||
ht_key(&index, key, nkey);
|
||||
|
||||
for(h = ht[index], prev = NULL; h != NULL; prev = h, h = h->next) {
|
||||
if(nkey == h->nk && memcmp(h->k, key, nkey) == 0) {
|
||||
if(prev == NULL) {
|
||||
ht[index] = h->next;
|
||||
} else {
|
||||
prev->next = h->next;
|
||||
}
|
||||
|
||||
if(h->flag == HT_ALLOC) {
|
||||
hm_pfree(pool, h->s);
|
||||
}
|
||||
hm_pfree(pool, h->k);
|
||||
|
||||
hm_pfree(pool, h);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
struct ht_s *ht_get(struct ht_s **ht, const char *key, const int nkey)
|
||||
{
|
||||
struct ht_s *h;
|
||||
int index = 0;
|
||||
|
||||
assert(ht);
|
||||
|
||||
ht_key(&index, key, nkey);
|
||||
|
||||
if(ht[index]) {
|
||||
/** fast access - only one key exists under ht index */
|
||||
if(ht[index] && ht[index]->next == NULL) {
|
||||
return ht[index];
|
||||
}
|
||||
|
||||
/** otherwise traverse compare against all existing keys */
|
||||
for(h = ht[index]; h != NULL; h = h->next) {
|
||||
if(h->nk == nkey && memcmp(h->k, key, nkey) == 0) {
|
||||
return h;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
} else {
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
void ht_dump_index(struct ht_s **ht, const char *key, const int nkey)
|
||||
{
|
||||
struct ht_s *h;
|
||||
int index = 0;
|
||||
|
||||
assert(ht);
|
||||
|
||||
ht_key(&index, key, nkey);
|
||||
|
||||
for(h = ht[index]; h != NULL; h = h->next) {
|
||||
printf("index [%d] with key [%.*s], value [%.*s]\n", index, h->nk, h->k, h->n, h->s);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,154 @@
|
||||
/*
|
||||
hm_base - hearthmod base library
|
||||
Copyright (C) 2016 Filip Pancik
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#ifndef HMCLIENT_H_
|
||||
#define HMCLIENT_H_
|
||||
|
||||
#define hm_send(c, conn_buf, conn_len)\
|
||||
/** Only respond if client is still active */\
|
||||
if(c) {\
|
||||
rb_send_init(&c->rb, (char *)conn_buf, conn_len, c->pool);\
|
||||
assert(c->loop);\
|
||||
ev_io_start(c->loop, &c->write);\
|
||||
}
|
||||
|
||||
struct conn_client_s;
|
||||
|
||||
enum clerr_e {
|
||||
CL_NOERROR = 1,
|
||||
CL_HANGTIMEOUT_ERR,
|
||||
CL_WANTSHUTDOWN_ERR,
|
||||
CL_READRBFULL_ERR,
|
||||
CL_READZERO_ERR,
|
||||
CL_READ_ERR,
|
||||
CL_WRITE_ERR,
|
||||
CL_BUFFERFULL_ERR,
|
||||
CL_PACKETLEN_ERR,
|
||||
CL_PACKETEXPECT_ERR,
|
||||
CL_SERVERSHUTDOWN_ERR,
|
||||
CL_SOCKET_ERR
|
||||
};
|
||||
|
||||
struct conn_client_holder_s {
|
||||
int signal_shutdown; ///< signal from client struct if it's already gone
|
||||
struct conn_client_s *client; ///< actual client
|
||||
struct conn_client_holder_s *next; ///< next client
|
||||
};
|
||||
|
||||
struct conn_server_s {
|
||||
struct ev_loop *loop; /**< An external event loop. */
|
||||
struct hm_pool_s *pool;
|
||||
struct hm_log_s *log;
|
||||
|
||||
struct conn_client_holder_s *clients_head;
|
||||
int clients;
|
||||
|
||||
struct ev_io listener;
|
||||
int fd;
|
||||
|
||||
const char *host;
|
||||
const char *port;
|
||||
|
||||
void (*recv)(struct conn_client_s *data, const char *buf, const int len);
|
||||
void (*client_dc)(void *data, const char *foreign_client_index, const char *hbs_id);
|
||||
void (*shutdown)();
|
||||
};
|
||||
|
||||
struct watcher_s {
|
||||
struct conn_client_s *p1;
|
||||
struct conn_client_s *p2;
|
||||
int counter;
|
||||
int target;
|
||||
};
|
||||
|
||||
struct player_deck_s {
|
||||
char code[16];
|
||||
int count;
|
||||
struct player_deck_s *next;
|
||||
};
|
||||
|
||||
struct conn_client_s {
|
||||
struct ev_loop *loop;
|
||||
struct hm_pool_s *pool;
|
||||
struct hm_log_s *log;
|
||||
|
||||
struct ev_io read;
|
||||
struct ev_io write;
|
||||
struct ev_timer hang_timer;
|
||||
|
||||
int hang_timer_enabled; ///< TODO: move this to flag
|
||||
int monitor; ///< TODO: move this to flag
|
||||
|
||||
unsigned long long logs; /**< log sequence number */
|
||||
|
||||
int fd;
|
||||
int *shutdown_signal_holder;
|
||||
|
||||
int want_shutdown;
|
||||
int type;
|
||||
|
||||
struct rb_s rb;
|
||||
|
||||
char net_buf[RB_SLOT_SIZE];
|
||||
int net_nbuf;
|
||||
int net_expect;
|
||||
|
||||
char client_index[32];
|
||||
char foreign_client_index[32];
|
||||
|
||||
char ip[64];
|
||||
int nip;
|
||||
int port;
|
||||
|
||||
char date[32];
|
||||
|
||||
void (*recv)(struct conn_client_s *client);
|
||||
void (*error_callback)(struct conn_client_s *client, enum clerr_e error);
|
||||
void (*client_dc)(void *data, const char *foregin_client_index, const char *hbs_id);
|
||||
|
||||
void *data;
|
||||
|
||||
struct conn_server_s *parent;
|
||||
struct conn_client_holder_s *holder;
|
||||
|
||||
char hbs_id[16];
|
||||
#ifdef HM_GAMESERVER
|
||||
struct player_deck_s *cards;
|
||||
int ncards;
|
||||
char hero[16];
|
||||
char hp[16];
|
||||
#elif defined HM_LOBBYSERVER
|
||||
char token[32];
|
||||
int ntoken;
|
||||
|
||||
struct {
|
||||
char user[32];
|
||||
char pass[32];
|
||||
char secret[32];
|
||||
} login;
|
||||
|
||||
int skip;
|
||||
#endif
|
||||
};
|
||||
|
||||
struct ht_s **async_clients; // ht of async clients
|
||||
|
||||
int async_shutdown_client(struct conn_client_s *c);
|
||||
void shutdown_server(struct conn_server_s *cs);
|
||||
int connector_server(struct conn_server_s *cs);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
hm_base - hearthmod base library
|
||||
Copyright (C) 2016 Filip Pancik
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#ifndef CONNECTOR_H_
|
||||
#define CONNECTOR_H_
|
||||
|
||||
#define CONN_F_CONNECTED 0x1 /**< connected */
|
||||
#define CONN_F_FDOPEN 0x2 /**< keep FD open */
|
||||
#define CONN_F_SINGLECHECK 0x4 /**< interrupt watcher when single client connection est */
|
||||
#define CONN_F_COUCHBASE 0x8 /**< couchbase connection */
|
||||
|
||||
struct conn_data_s;
|
||||
typedef void (*conn_cb_t)(struct conn_data_s *c);
|
||||
|
||||
struct conn_node_s {
|
||||
struct conn_data_s *parent;
|
||||
int group; /**< Group ID */
|
||||
char host[128]; /**< Hostname that must be recognized by inet_addr */
|
||||
int port; /**< Port number */
|
||||
|
||||
int fd; /**< File descriptor */
|
||||
|
||||
unsigned int status; /**< Hostname:port's flag status */
|
||||
|
||||
struct ev_io io; /**< Event that is pushed to an external event loop. */
|
||||
struct ev_timer timer; /**< Timer which stops 'io'. */
|
||||
|
||||
unsigned long long logs;
|
||||
|
||||
void *data; /**< Passed data */
|
||||
|
||||
struct hm_log_s *log;
|
||||
};
|
||||
|
||||
struct conn_data_s {
|
||||
struct ev_loop *loop; /**< An external event loop. */
|
||||
struct hm_pool_s *pool;
|
||||
struct hm_log_s *log;
|
||||
|
||||
unsigned long long logs;
|
||||
|
||||
struct conn_node_s **src; /**< An array of conn_node_s to be watched. */
|
||||
int size; /**< Number of connectors to watch. */
|
||||
int established;
|
||||
conn_cb_t callback; /**< Callback function to call when watcher finishes all jobs. */
|
||||
struct ev_timer timer; /**< Timer which finishes all jobs and invokes callback. */
|
||||
float exptime; /**< Expiration time which terminates all jobs */
|
||||
float timeout; /**< Timeout for specific host */
|
||||
};
|
||||
|
||||
int connector(struct conn_data_s *c);
|
||||
void connector_fd_close(int fd);
|
||||
void connector_free(struct conn_data_s *c);
|
||||
int setkeepalive(int fd) ;
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
hm_base - hearthmod base library
|
||||
Copyright (C) 2016 Filip Pancik
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#ifndef HASHTABLE_H_
|
||||
#define HASHTABLE_H_
|
||||
|
||||
#define HT_MAX (1<<12)
|
||||
#define HT_ALLOC 0x1
|
||||
|
||||
#define HT_ADD(ht, key, nkey, value, nvalue, pool) ht_add(ht, key, nkey, value, nvalue, HT_ALLOC, pool)
|
||||
#define HT_ADD_WA(ht, key, nkey, value, nvalue, pool) ht_add(ht, key, nkey, value, nvalue, 0, pool)
|
||||
#define HT_REM(ht, key, nkey, pool) ht_rem(ht, key, nkey, pool)
|
||||
|
||||
#define ht_item_clear(pool, dst)\
|
||||
if(dst && dst->n > 0) {\
|
||||
hm_pfree(pool, dst->s);\
|
||||
dst->s = NULL;\
|
||||
dst->n = 0;\
|
||||
}
|
||||
|
||||
struct ht_s {
|
||||
char *k;
|
||||
int nk;
|
||||
char *s; ///< value
|
||||
int n; ///< nvalue
|
||||
unsigned int flag;
|
||||
struct ht_s *next;
|
||||
};
|
||||
|
||||
struct ht_s **ht_init(struct hm_pool_s *pool);
|
||||
void ht_free(struct ht_s **ht, struct hm_pool_s *pool);
|
||||
int ht_add(struct ht_s **ht, const char *key, const int nkey, const void *value, const int nvalue, const int alloc, struct hm_pool_s *pool);
|
||||
int ht_rem(struct ht_s **ht, const char *key, const int nkey, struct hm_pool_s *pool);
|
||||
struct ht_s *ht_get(struct ht_s **ht, const char *key, const int nkey);
|
||||
void ht_dump_index(struct ht_s **ht, const char *key, const int nkey);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
hm_base - hearthmod base library
|
||||
Copyright (C) 2016 Filip Pancik
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#ifndef HMBASE_H_
|
||||
#define HMBASE_H_
|
||||
|
||||
#include <ev.h>
|
||||
|
||||
#include <utils.h>
|
||||
#include <log.h>
|
||||
#include <pool.h>
|
||||
#include <hashtable.h>
|
||||
#include <rb.h>
|
||||
#include <hmcouchbase.h>
|
||||
#include <connector.h>
|
||||
#include <async_client.h>
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,271 @@
|
||||
/*
|
||||
hm_base - hearthmod base library
|
||||
Copyright (C) 2016 Filip Pancik
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#ifndef HMCOUCHBASE_H_
|
||||
#define HMCOUCHBASE_H_
|
||||
|
||||
#include <libcouchbase/views.h>
|
||||
|
||||
#define CB_ERR_OK 0x0 /**< No error */
|
||||
#define CB_ERR_LCBCREATEIO 0x1 /**< Failed to lcb_create_io_ops() */
|
||||
#define CB_ERR_LCBCREATE 0x2 /**< Failed to lcb_create() */
|
||||
#define CB_ERR_LCBCONNECT 0x3 /**< Failed to lcb_connect() */
|
||||
#define CB_ERR_NOINSTANCE 0x4 /**< No active instance found */
|
||||
#define CB_ERR_NOACTIVE 0x5 /**< Instance is not active */
|
||||
#define CB_ERR_OPNOT 0x6 /**< Operation not supporeted */
|
||||
#define CB_ERR_OPERR 0x7 /**< Operation error */
|
||||
|
||||
#define CB_QUERY_VER 0
|
||||
|
||||
#define CBVQ(m) cbop->u.v.q.m // couchbase view query
|
||||
#define CBSQ(m) cbop->u.s.q.v.v0.m // couchbase store query
|
||||
#define CBRQ(m) cbop->u.r.q.v.v0.m // couchbase remove query
|
||||
#define CBGQ(m) cbop->u.g.q.v.v0.m // couchbase get query
|
||||
#define CBGR(m) cbop->u.g.r->v.v0.m // CouchBase Get Reply
|
||||
#define CBRR(m) cbop->u.r.r->v.v0.m // couchbase remove reply
|
||||
#define CBSR(m) cbop->u.s.r->v.v0.m // couchbase store reply
|
||||
#define CBAQ(m) cbop->u.a.q.v.v0.m // couchbase arithmetic query
|
||||
#define CBAR(m) cbop->u.a.r->v.v0.m // couchbase arithmetic reply
|
||||
|
||||
/**< CBSQ_V0 - CouchBase Store Query V0
|
||||
* bucket, operation, key, nkey, bytes, nbytes, callback, cas, exptime, datatype, flags
|
||||
* 'struct cbop_s *cbop' must exist within context
|
||||
*/
|
||||
#define CBSQ_V0(b, o, k, nk, bt, nb, f, c, e, d, flag)\
|
||||
cbop->bucket = b;\
|
||||
cbop->operation = CB_STORE;\
|
||||
cbop->u.s.q.version = CB_QUERY_VER;\
|
||||
CBSQ(operation) = o;\
|
||||
CBSQ(key) = k;\
|
||||
CBSQ(nkey) = nk;\
|
||||
CBSQ(bytes) = bt;\
|
||||
CBSQ(nbytes) = nb;\
|
||||
CBSQ(cas) = c;\
|
||||
CBSQ(exptime) = e;\
|
||||
CBSQ(datatype) = d;\
|
||||
CBSQ(flags) = flag;\
|
||||
cbop->post = f;\
|
||||
(void)couchbase_operation(cbop);
|
||||
/*if(couchbase_operation(cbop) != CB_ERR_OK) { */
|
||||
|
||||
|
||||
/**< CBGQ_V0 - CouchBase Get Query V0
|
||||
* bucket, key, nkey, callback, exptime, lock
|
||||
* 'struct cbop_s *cbop' must exist within context
|
||||
*/
|
||||
#define CBGQ_V0(b, k, nk, f, e, l)\
|
||||
cbop->bucket = b;\
|
||||
cbop->operation = CB_GET;\
|
||||
cbop->u.g.q.version = CB_QUERY_VER;\
|
||||
CBGQ(key) = k;\
|
||||
CBGQ(nkey) = nk;\
|
||||
CBGQ(exptime) = e;\
|
||||
CBGQ(lock) = l;\
|
||||
cbop->post = f;\
|
||||
(void)couchbase_operation(cbop);
|
||||
/*if(couchbase_operation(cbop) != CB_ERR_OK) { */
|
||||
|
||||
/**< CBRQ_V0 - CouchBase Remove Query V0
|
||||
* bucket, key, nkey, callback, cas
|
||||
* 'struct cbop_s *cbop' must exist within context
|
||||
*/
|
||||
#define CBRQ_V0(b, k, nk, f, c)\
|
||||
cbop->bucket = b;\
|
||||
cbop->operation = CB_REMOVE;\
|
||||
cbop->u.r.q.version = CB_QUERY_VER;\
|
||||
CBRQ(key) = k;\
|
||||
CBRQ(nkey) = nk;\
|
||||
CBRQ(cas) = c;\
|
||||
cbop->post = f;\
|
||||
(void)couchbase_operation(cbop);
|
||||
/*if(couchbase_operation(cbop) != CB_ERR_OK) { */
|
||||
|
||||
/**< CBAQ_V0 - CouchBase Arithmetic Query V0
|
||||
* bucket, key, nkey, callback, create, delta, initial
|
||||
* 'struct cbop_s *cbop' must exist within context
|
||||
*/
|
||||
#define CBAQ_V0(b, k, nk, f, c, d, i)\
|
||||
cbop->bucket = b;\
|
||||
cbop->operation = CB_ARITH;\
|
||||
cbop->u.a.q.version = CB_QUERY_VER;\
|
||||
CBAQ(key) = k;\
|
||||
CBAQ(nkey) = nk;\
|
||||
CBAQ(create) = c;\
|
||||
CBAQ(delta) = d;\
|
||||
CBAQ(initial) = i;\
|
||||
cbop->post = f;\
|
||||
(void)couchbase_operation(cbop);
|
||||
/*if(couchbase_operation(cbop) != CB_ERR_OK) { */
|
||||
|
||||
/**< CBVQ_V0 - CouchBase View Query V0
|
||||
* bucket, cmd, doc, ndoc, view, nview, options, noptions, post, npost, dcm, callback, handle
|
||||
* 'struct cbop_s *cbop' must exist within context
|
||||
*/
|
||||
#define CBVQ_V0(b, c, d, nd, v, nv, o, no, p, np, dcm, f, h)\
|
||||
cbop->bucket = b;\
|
||||
cbop->operation = CB_VIEW;\
|
||||
CBVQ(cmdflags) = c;\
|
||||
CBVQ(ddoc) = d;\
|
||||
CBVQ(nddoc) = nd;\
|
||||
CBVQ(view) = v;\
|
||||
CBVQ(nview) = nv;\
|
||||
CBVQ(optstr) = o;\
|
||||
CBVQ(noptstr) = no;\
|
||||
CBVQ(postdata) = p;\
|
||||
CBVQ(npostdata) = np;\
|
||||
CBVQ(docs_concurrent_max) = dcm;\
|
||||
CBVQ(callback) = f;\
|
||||
CBVQ(handle) = h;\
|
||||
(void)couchbase_operation(cbop);
|
||||
/*if(couchbase_operation(cbop) != CB_ERR_OK) { */
|
||||
|
||||
#define CONNSTR_SIZE 2048
|
||||
#define MAX_GROUPS 16 /**< max couchbase datacenters */
|
||||
|
||||
typedef enum {
|
||||
CB_GET = 0x1,
|
||||
CB_STORE = 0x2,
|
||||
CB_REMOVE = 0x3,
|
||||
CB_ARITH = 0x4,
|
||||
CB_VIEW = 0x5
|
||||
} cboperation_t;
|
||||
|
||||
/*
|
||||
typedef enum {
|
||||
LCB_ADD = 0x01,
|
||||
LCB_REPLACE = 0x02,
|
||||
LCB_SET = 0x03,
|
||||
LCB_APPEND = 0x04,
|
||||
LCB_PREPEND = 0x05
|
||||
} lcb_storage_t;
|
||||
*/
|
||||
|
||||
/*
|
||||
syslog priorities:
|
||||
LOG_EMERG system is unusable
|
||||
LOG_ALERT action must be taken immediately
|
||||
LOG_CRIT critical conditions
|
||||
LOG_ERR error conditions
|
||||
LOG_WARNING warning conditions
|
||||
LOG_NOTICE normal, but significant, condition
|
||||
LOG_INFO informational message
|
||||
LOG_DEBUG debug-level message
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief cbop_s structure is passed to every couchbase request
|
||||
*
|
||||
* User must allocate and deallocate space for this structure.
|
||||
*/
|
||||
struct cbop_s {
|
||||
int bucket; /**< Index of bucket */
|
||||
cboperation_t operation; /**< Couchbase operation, @see cboperation_t */
|
||||
void (*post)(struct cbop_s *cbop);
|
||||
lcb_error_t lcb_error; /**< Libcouchbase error code in case callback function needs it */
|
||||
|
||||
union {
|
||||
struct {
|
||||
lcb_get_cmd_t q; /**< Query */
|
||||
const lcb_get_resp_t *r; /**< Response */
|
||||
} g; /**< This is a Get procedure definition. */
|
||||
|
||||
struct {
|
||||
lcb_store_cmd_t q; /**< Query */
|
||||
const lcb_store_resp_t *r; /**< Response */
|
||||
} s; /**< This is a Store procedure definition. */
|
||||
|
||||
struct {
|
||||
lcb_remove_cmd_t q; /**< Query */
|
||||
const lcb_remove_resp_t *r; /**< Response */
|
||||
} r; /**< This is a Remove procedure definition. */
|
||||
|
||||
struct {
|
||||
lcb_arithmetic_cmd_t q; /**< Query */
|
||||
const lcb_arithmetic_resp_t *r; /**< Response */
|
||||
} a; /**< This is an Arithmetic procedure definition. */
|
||||
|
||||
struct {
|
||||
lcb_CMDVIEWQUERY q; /**< Query */
|
||||
struct lcb_RESPVIEW_st r; /**< Response */
|
||||
} v; /**< This is a View procedure definition. */
|
||||
} u;
|
||||
|
||||
void *g_child; /** grunt's child */
|
||||
void *data; /**< Some other client's data. */
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief struct cluster_s
|
||||
*
|
||||
* Every bucket has its own instance.
|
||||
*/
|
||||
struct cluster_s {
|
||||
char **bucket;
|
||||
char **passwd;
|
||||
lcb_t *instance;
|
||||
|
||||
};
|
||||
|
||||
struct couchbase_data_s;
|
||||
/**
|
||||
* @brief struct instance_s
|
||||
*
|
||||
* Every cluster has its own group.
|
||||
*/
|
||||
struct instance_s {
|
||||
struct ev_loop *loop;
|
||||
struct hm_log_s *log;
|
||||
struct hm_pool_s *pool;
|
||||
char connstr[CONNSTR_SIZE];
|
||||
int status;
|
||||
int group;
|
||||
int index;
|
||||
lcb_io_opt_t ioops;
|
||||
struct instance_s *next;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief struct couchbase_data_s
|
||||
*
|
||||
*/
|
||||
struct couchbase_data_s {
|
||||
struct ev_loop *loop;
|
||||
struct hm_log_s *log;
|
||||
struct hm_pool_s *pool;
|
||||
void (*callback)(struct instance_s *data);
|
||||
|
||||
const char **hosts;
|
||||
const int *hgroups;
|
||||
int nhosts;
|
||||
const char **buckets;
|
||||
const char **bpasswd;
|
||||
int nbuckets;
|
||||
};
|
||||
|
||||
struct connstr_s {
|
||||
int id;
|
||||
char *host;
|
||||
int len;
|
||||
};
|
||||
|
||||
int couchbase_init(struct couchbase_data_s *cbd);
|
||||
int couchbase_bucket_index(const char *bucketName); ///< Returns the bucket index, or -1 if there is no such bucket
|
||||
int couchbase_operation(struct cbop_s *cbop);
|
||||
void couchbase_deinit();
|
||||
extern int couchbase_deinit_async;
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
hm_base - hearthmod base library
|
||||
Copyright (C) 2016 Filip Pancik
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#ifndef HMLOG_H_
|
||||
#define HMLOG_H_
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
enum errors_e {
|
||||
LOG_EMERG = 0, ///< system is unusable
|
||||
LOG_ALERT, ///< action must be taken immediately
|
||||
LOG_CRIT, ///< critical conditions
|
||||
LOG_ERR, ///< error conditions
|
||||
LOG_WARNING, ///< warning conditions
|
||||
LOG_NOTICE, //< normal, but significant, condition
|
||||
LOG_INFO, //< informational message
|
||||
LOG_DEBUG, ///< debug-level message
|
||||
LOG_MEMORY, ///< memory-level message
|
||||
};
|
||||
|
||||
struct hm_log_s {
|
||||
const char *name;
|
||||
int fd;
|
||||
FILE *file;
|
||||
void *data;
|
||||
int priority;
|
||||
};
|
||||
|
||||
#define hm_log(t, l, fmt...)\
|
||||
hm_log_impl(0, t, l, __FILE__, __LINE__, __FUNCTION__, fmt)
|
||||
|
||||
int hm_log_impl(unsigned long long seq_nb, int priority, struct hm_log_s *log, const char *file, int line, const char *func, const char *fmt, ...) __attribute__ ((format (printf, 7, 8)));
|
||||
int hm_log_open(struct hm_log_s *l, const char *filename, const int priority);
|
||||
int hm_log_close(struct hm_log_s *l);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
hm_base - hearthmod base library
|
||||
Copyright (C) 2016 Filip Pancik
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#ifndef HMPOOL_H_
|
||||
#define HMPOOL_H_
|
||||
|
||||
#define POOL_DEBUG
|
||||
#define POOL_STDLIB
|
||||
|
||||
struct pool_bucket_s {
|
||||
void *memory_region;
|
||||
void *nodes;
|
||||
struct pool_bucket_s *next;
|
||||
};
|
||||
|
||||
struct hm_pool_s {
|
||||
int size;
|
||||
int used;
|
||||
struct hm_log_s *log;
|
||||
struct pool_node_s *freenode;
|
||||
struct pool_bucket_s *buckets;
|
||||
struct hm_pool_s *next;
|
||||
};
|
||||
|
||||
struct pool_node_s {
|
||||
void *ptr;
|
||||
int size;
|
||||
int realsize;
|
||||
int used;
|
||||
struct hm_pool_s *pool;
|
||||
struct pool_node_s *next;
|
||||
};
|
||||
|
||||
struct hm_pool_s *hm_create_pool();
|
||||
void *hm_palloc(struct hm_pool_s *pool, int size);
|
||||
void *hm_prealloc(struct hm_pool_s *pool, void *ptr, const int size);
|
||||
void *hm_memcpy(void *dst, const void *src, const int n, void *start);
|
||||
int hm_pfree(struct hm_pool_s *pool, void *ptr);
|
||||
int hm_destroy_pool(struct hm_pool_s *pool);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
hm_base - hearthmod base library
|
||||
Copyright (C) 2016 Filip Pancik
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#ifndef RB_H
|
||||
#define RB_H
|
||||
|
||||
#define RB_SLOT_SIZE (16 * 1024)
|
||||
|
||||
|
||||
struct rb_slot_s {
|
||||
void *buf;
|
||||
int len;
|
||||
int sent;
|
||||
struct rb_slot_s *next;
|
||||
};
|
||||
|
||||
struct rb_s {
|
||||
struct {
|
||||
char slot[RB_SLOT_SIZE];
|
||||
int len;
|
||||
} recv;
|
||||
|
||||
struct rb_slot_s *send, *tail;
|
||||
};
|
||||
|
||||
char *rb_send_next(struct rb_s *rb, int *size);
|
||||
void rb_send_skip(struct rb_s *rb, int offset);
|
||||
int rb_send_is_empty(struct rb_s *rb);
|
||||
void rb_send_pop(struct rb_s *rb, struct hm_pool_s *pool);
|
||||
int rb_send_init(struct rb_s *rb, char *buf, const int len, struct hm_pool_s *pool);
|
||||
char *rb_recv_ptr(struct rb_s *rb, int *used);
|
||||
void rb_recv_append(struct rb_s *rb, const int len);
|
||||
char *rb_recv_read(struct rb_s *rb, int *size);
|
||||
void rb_recv_pop(struct rb_s *rb);
|
||||
int rb_recv_is_full(struct rb_s *rb);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,139 @@
|
||||
/*
|
||||
hm_base - hearthmod base library
|
||||
Copyright (C) 2016 Filip Pancik
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#ifndef UTILS_H_
|
||||
#define UTILS_H_
|
||||
|
||||
#include <time.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/time.h>
|
||||
#include <memory.h>
|
||||
|
||||
#define EQFLAG(d, f)\
|
||||
((d & f) == f)
|
||||
|
||||
/** N memory compare */
|
||||
#define nmc(dst, ndst, src, nsrc) (ndst == nsrc && memcmp(dst, src, ndst) == 0)
|
||||
|
||||
inline static void to_lower(char *bytes, const int nbytes)
|
||||
{
|
||||
int i;
|
||||
|
||||
for(i = 0; i < nbytes; i++) {
|
||||
if(bytes[i] >= 65 && bytes[i] <= 90) {
|
||||
bytes[i] += 32;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
inline static void mac_tonum(char *dst, const int ndst, char *src, const int nsrc)
|
||||
{
|
||||
char *s, *e;
|
||||
int i;
|
||||
|
||||
if(nsrc != 17 || ndst < 17) {
|
||||
return;
|
||||
}
|
||||
|
||||
for(i = 0, s = src, e = src + nsrc; s < e; s++) {
|
||||
if(*s == ':' && s - 2 >= src) {
|
||||
memcpy(&dst[i], s - 2, 2);
|
||||
i += 2;
|
||||
}
|
||||
else if(i == 10 && s + 2 <= e) {
|
||||
memcpy(&dst[i], s, 2);
|
||||
i += 2;
|
||||
}
|
||||
}
|
||||
|
||||
dst[i] = '\0';
|
||||
}
|
||||
|
||||
inline static void replace_space(char *dst, const int ndst, const char src)
|
||||
{
|
||||
char *s, *e;
|
||||
|
||||
for(s = dst, e = dst + ndst; s < e; s++) {
|
||||
if(*s == ' ') {
|
||||
*s = src;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
inline static void num_tomac(char *dst, const int ndst, char *src, const int nsrc)
|
||||
{
|
||||
int i = 0;
|
||||
char *ds, *de;
|
||||
char *ss, *se;
|
||||
|
||||
// 17 + 1 term
|
||||
if(ndst < 18) {
|
||||
return;
|
||||
}
|
||||
|
||||
for(i = 0,
|
||||
ss = src, se = src + nsrc,
|
||||
ds = dst, de = dst + ndst;
|
||||
(ss < se && (ds + i)< de);
|
||||
/* void */) {
|
||||
memcpy(&dst[i], ss, 2);
|
||||
i += 2;
|
||||
dst[i++] = ':';
|
||||
ss += 2;
|
||||
if(i >= 16) {
|
||||
dst[--i] = '\0';
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
inline static int is_valid_mac(char *buf, const int len)
|
||||
{
|
||||
char *s, *e;
|
||||
|
||||
if(len < 17) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
/** buf must point to first char of mac */
|
||||
for(s = buf, e = buf + len; s < e; s++) {
|
||||
/** we're looking for a mac address xx:xx:xx:xx:xx:xx
|
||||
* ( 1 - : ) || ( A - Z ) || ( a - z )
|
||||
*/
|
||||
if(!((*s >= 48 && *s <= 58) || (*s >= 65 && *s <= 90) || (*s >= 97 && *s <= 122))) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if(s - buf == 16) {
|
||||
return (s - buf) + 1;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
inline static int random_number(int max)
|
||||
{
|
||||
struct timespec ts;
|
||||
|
||||
clock_gettime(CLOCK_REALTIME, &ts);
|
||||
srand(ts.tv_nsec);
|
||||
|
||||
return (rand() % max);
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,133 @@
|
||||
/*
|
||||
hm_base - hearthmod base library
|
||||
Copyright (C) 2016 Filip Pancik
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdarg.h>
|
||||
#include <unistd.h>
|
||||
#include <memory.h>
|
||||
#include <assert.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
|
||||
#include <hmbase.h>
|
||||
|
||||
int hm_log_impl(unsigned long long seq_nb, int priority, struct hm_log_s *log, const char *file, const int line, const char *func, const char *msg, ...)
|
||||
{
|
||||
size_t len = 0;
|
||||
char out[8192], buf[128];
|
||||
time_t s;
|
||||
struct timespec spec;
|
||||
long long ms;
|
||||
struct tm ts;
|
||||
|
||||
assert(log);
|
||||
|
||||
/** only display messages user asked for */
|
||||
if(priority > log->priority) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
//if(log->fd == STDERR_FILENO)
|
||||
{
|
||||
const char *colour;
|
||||
switch(priority) {
|
||||
case LOG_EMERG: //< system is unusable
|
||||
colour = "(%llu) \33[1;31;41mLOG_EMERG\33[m";
|
||||
break;
|
||||
case LOG_ALERT: //< action must be taken immediately
|
||||
colour = "(%llu) \33[1;33;41mLOG_ALERT\33[m";
|
||||
break;
|
||||
case LOG_CRIT: //< critical conditions
|
||||
colour = "(%llu) \33[1;31;40mLOG_CRIT\33[m";
|
||||
break;
|
||||
case LOG_ERR: //< error conditions
|
||||
colour = "(%llu) \33[1;34;41mLOG_ERR\33[m";
|
||||
break;
|
||||
case LOG_WARNING: //< warning conditions
|
||||
colour = "(%llu) \33[1;37;43mLOG_WARNING\33[m";
|
||||
break;
|
||||
case LOG_NOTICE: //< normal, but significant, condition
|
||||
colour = "(%llu) \33[1;34;47mLOG_NOTICE\33[m";
|
||||
break;
|
||||
case LOG_INFO: //< informational message
|
||||
colour = "(%llu) \33[1;37;42mLOG_INFO\33[m";
|
||||
break;
|
||||
case LOG_DEBUG: //< debug-level message
|
||||
colour = "(%llu) \33[1;32;40mLOG_DEBUG\33[m";
|
||||
break;
|
||||
case LOG_MEMORY: //< memory message
|
||||
colour = "(%llu) \33[1;35;34mLOG_MEMORY\33[m";
|
||||
break;
|
||||
default:
|
||||
colour = NULL;
|
||||
}
|
||||
if(colour) {
|
||||
len += snprintf(out, sizeof(out), colour, seq_nb);
|
||||
}
|
||||
}
|
||||
|
||||
clock_gettime(CLOCK_REALTIME, &spec);
|
||||
s = spec.tv_sec;
|
||||
ms = round(spec.tv_nsec / 1.0e6);
|
||||
|
||||
ts = *localtime(&s);
|
||||
strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", &ts);
|
||||
|
||||
len += snprintf(out + len, sizeof(out) - len, "[%s.%03lld] ", buf, ms);
|
||||
|
||||
va_list args;
|
||||
va_start(args, msg);
|
||||
len += vsnprintf(out+len, 3*sizeof(out)/4-len, msg, args);
|
||||
va_end(args);
|
||||
if(len >= 3*sizeof(out)/4) {
|
||||
len += snprintf(out + len, sizeof(out) - len, "...");
|
||||
}
|
||||
|
||||
len += snprintf(out+len, sizeof(out)-len, ", %s:%d(%s)\n", file, line, func);
|
||||
|
||||
ssize_t nwritten = write(log->fd, out, len);
|
||||
return (nwritten == len) ? 0 : -1;
|
||||
}
|
||||
|
||||
int hm_log_open(struct hm_log_s *l, const char *filename, const int priority)
|
||||
{
|
||||
if(filename != NULL) {
|
||||
l->file = fopen(filename, "a");
|
||||
if(l->file == NULL) {
|
||||
return -1;
|
||||
}
|
||||
l->fd = fileno(l->file);
|
||||
} else {
|
||||
l->file = stderr;
|
||||
l->fd = STDERR_FILENO;
|
||||
}
|
||||
|
||||
l->priority = priority;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int hm_log_close(struct hm_log_s *l)
|
||||
{
|
||||
if(l->file && l->file != stderr) {
|
||||
fclose(l->file);
|
||||
return 0;
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,368 @@
|
||||
/*
|
||||
hm_base - hearthmod base library
|
||||
Copyright (C) 2016 Filip Pancik
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <assert.h>
|
||||
#include <memory.h>
|
||||
|
||||
#include <hmbase.h>
|
||||
|
||||
#define BUCKET_MAX 2
|
||||
#define ROUND16(dst) (((dst) + 15) & ~15)
|
||||
|
||||
#define get_meta(m_ptr) (*(struct pool_node_s **)((struct pool_node_s ***)(m_ptr - sizeof(void *))))
|
||||
|
||||
static int pool_create_bucket(struct hm_pool_s *pool);
|
||||
|
||||
int hm_pfree(struct hm_pool_s *pool, void *ptr)
|
||||
{
|
||||
#ifdef POOL_STDLIB
|
||||
free(ptr);
|
||||
return 0;
|
||||
#endif
|
||||
|
||||
struct hm_pool_s *p;
|
||||
struct pool_node_s *node;
|
||||
|
||||
if(ptr == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
/** get to metadata */
|
||||
node = get_meta(ptr);
|
||||
|
||||
for(p = pool; p != NULL; p = p->next) {
|
||||
if(node && node->used && node->size == p->size) {
|
||||
#ifdef POOL_DEBUG
|
||||
hm_log(LOG_MEMORY, pool->log, "{Pool}: old freenode: %p/%d, new freenode: %p/%d from pool:%p/%d", p->freenode, p->freenode->realsize, node, node->realsize, p, p->size);
|
||||
#endif
|
||||
node->used = 0;
|
||||
node->next = p->freenode;
|
||||
p->freenode = node;
|
||||
|
||||
--p->used;
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
static void *pool_get_node(struct hm_pool_s *pool, const int realsize)
|
||||
{
|
||||
struct pool_node_s *node = NULL;
|
||||
|
||||
node = pool->freenode;
|
||||
assert(node);
|
||||
pool->freenode = node->next;
|
||||
|
||||
if(node->next == NULL) {
|
||||
#ifdef POOL_DEBUG
|
||||
hm_log(LOG_MEMORY, pool->log, "no more freenodes remaining in pool: %p/%d, creating new bucket", pool, pool->size);
|
||||
#endif
|
||||
if(pool_create_bucket(pool) != 0) {
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef POOL_DEBUG
|
||||
hm_log(LOG_MEMORY, pool->log, "{Pool}: returning node: %p/%d, new freenode: %p from pool: %p/%d", node, realsize, pool->freenode, pool, pool->size);
|
||||
#endif
|
||||
|
||||
/** increment used nodes */
|
||||
++pool->used;
|
||||
|
||||
/** real size of block */
|
||||
node->realsize = realsize;
|
||||
|
||||
node->used = 1;
|
||||
|
||||
/** metadata offset */
|
||||
return (node->ptr + sizeof(void *));
|
||||
}
|
||||
|
||||
/** don't do anything but creating a zero valued holder */
|
||||
struct hm_pool_s *hm_create_pool(struct hm_log_s *log)
|
||||
{
|
||||
struct hm_pool_s *pool;
|
||||
|
||||
pool = malloc(sizeof(*pool));
|
||||
|
||||
if(pool == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memset(pool, 0, sizeof(*pool));
|
||||
pool->log = log;
|
||||
|
||||
#ifdef POOL_DEBUG
|
||||
hm_log(LOG_MEMORY, pool->log, "{Pool}: pool created %p", pool);
|
||||
#endif
|
||||
return pool;
|
||||
}
|
||||
|
||||
static int pool_create_bucket(struct hm_pool_s *pool)
|
||||
{
|
||||
int i;
|
||||
void *nodes, *region;
|
||||
struct pool_bucket_s *b;
|
||||
struct pool_node_s *node;
|
||||
|
||||
/** holder of buckets */
|
||||
b = malloc(sizeof(*b));
|
||||
|
||||
if(b == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
region = malloc((pool->size + sizeof(void *)) * BUCKET_MAX);
|
||||
|
||||
/** nodes holders */
|
||||
nodes = malloc(BUCKET_MAX * sizeof(struct pool_node_s));
|
||||
|
||||
if(nodes == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
b->memory_region = region;
|
||||
b->nodes = nodes;
|
||||
|
||||
for(i = 0; BUCKET_MAX > i; i++) {
|
||||
node = (struct pool_node_s *)(nodes + (i * sizeof(struct pool_node_s)));
|
||||
/** node's offset set by i * (metadata + size)*/
|
||||
node->ptr = (void *)(region + i * (sizeof(void *) + pool->size));
|
||||
/** copy metadata to offset + 0 */
|
||||
memcpy(node->ptr, &node, sizeof(node));
|
||||
|
||||
#ifdef POOL_DEBUG
|
||||
hm_log(LOG_MEMORY, pool->log, "new node: %p in pool: %p old freenode: %p pool size: %d node ptr: %p reg: %p", node, pool, pool->freenode, pool->size, node->ptr, *(void **)node->ptr);
|
||||
|
||||
/*
|
||||
for(j = 0; j < 32; j++) {
|
||||
printf("%d|", ((char *)(node->ptr))[j]);
|
||||
}
|
||||
*/
|
||||
#endif
|
||||
|
||||
node->size = pool->size;
|
||||
node->next = pool->freenode;
|
||||
node->pool = pool;
|
||||
node->realsize = 0;
|
||||
pool->freenode = node;
|
||||
}
|
||||
|
||||
b->next = pool->buckets;
|
||||
pool->buckets = b;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void *hm_memcpy(void *dst, const void *src, const int n, void *start)
|
||||
{
|
||||
int diff;
|
||||
struct pool_node_s *node;
|
||||
|
||||
node = get_meta(start);
|
||||
|
||||
diff = dst - (node->ptr + sizeof(void *));
|
||||
|
||||
//printf("memcpy diff %d\n", node->size);
|
||||
|
||||
if(n + diff > node->size) {
|
||||
hm_log(LOG_ERR, node->pool->log, "{Pool}: illegal memcpy(), overlapping dst of %d with %d bytes", node->size, diff + n);
|
||||
return NULL;
|
||||
} else {
|
||||
return memcpy(dst, src, n);
|
||||
}
|
||||
}
|
||||
|
||||
static struct hm_pool_s *pool_create_append(struct hm_pool_s *pool, const int size)
|
||||
{
|
||||
struct hm_pool_s *p, *tp;
|
||||
|
||||
p = malloc(sizeof(*p));
|
||||
|
||||
if(p == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/** set pool size */
|
||||
p->size = size;
|
||||
p->next = NULL;
|
||||
p->freenode = NULL;
|
||||
p->buckets = NULL;
|
||||
p->used = 0;
|
||||
p->log = pool->log;
|
||||
|
||||
if(pool_create_bucket(p) != 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
for(tp = pool; tp != NULL; tp = tp->next) {
|
||||
if(tp->next == NULL) {
|
||||
tp->next = p;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
/**
|
||||
The realloc() function changes the size of the memory block pointed to by ptr to size bytes. The contents
|
||||
will be unchanged in the range from the start of the region up to the minimum of the old and new sizes. If
|
||||
the new size is larger than the old size, the added memory will not be initialized. If ptr is NULL, then the
|
||||
call is equivalent to malloc(size), for all values of size; if size is equal to zero, and ptr is not NULL,
|
||||
then the call is equivalent to free(ptr). Unless ptr is NULL, it must have been returned by an earlier call
|
||||
to malloc(), calloc() or realloc(). If the area pointed to was moved, a free(ptr) is done.
|
||||
*/
|
||||
void *hm_prealloc(struct hm_pool_s *pool, void *ptr, const int size)
|
||||
{
|
||||
#ifdef POOL_STDLIB
|
||||
return realloc(ptr, size);
|
||||
#endif
|
||||
|
||||
struct pool_node_s *node = NULL;
|
||||
void *dst;
|
||||
|
||||
if(ptr != NULL) {
|
||||
node = get_meta(ptr);
|
||||
}
|
||||
|
||||
if(ptr == NULL && size == 0) {
|
||||
#ifdef POOL_DEBUG
|
||||
hm_log(LOG_MEMORY, pool->log, "{Pool}: doing nothing");
|
||||
#endif
|
||||
/** do nothing */
|
||||
return NULL;
|
||||
} else if(ptr == NULL && size != 0) {
|
||||
#ifdef POOL_DEBUG
|
||||
hm_log(LOG_MEMORY, pool->log, "{Pool}: malloc() size: %d", size);
|
||||
#endif
|
||||
/** malloc() */
|
||||
return hm_palloc(pool, size);
|
||||
} else if(ptr != NULL && size == 0) {
|
||||
#ifdef POOL_DEBUG
|
||||
hm_log(LOG_MEMORY, pool->log, "{Pool}: free() size: %d", size);
|
||||
#endif
|
||||
/** free() */
|
||||
if(hm_pfree(pool, ptr) != 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
} else if(ptr != NULL && node != NULL && node->size == size) {
|
||||
#ifdef POOL_DEBUG
|
||||
hm_log(LOG_MEMORY, pool->log, "{Pool}: status quo for size: %d", size);
|
||||
#endif
|
||||
/** nothing needs to be changed - return exactly the same pointer */
|
||||
return ptr;
|
||||
} else if(ptr != NULL && size > 0 && node != NULL && node->size != size) {
|
||||
#ifdef POOL_DEBUG
|
||||
hm_log(LOG_MEMORY, pool->log, "{Pool}: realloc for old size: %d new size: %d", node->size, size);
|
||||
#endif
|
||||
/** realloc() */
|
||||
|
||||
/** first allocate new dst */
|
||||
dst = hm_palloc(pool, size);
|
||||
|
||||
if(dst == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/** copy src to dst */
|
||||
if(node->realsize <= size) {
|
||||
memcpy(dst, ptr, node->realsize);
|
||||
} else {
|
||||
memcpy(dst, ptr, size);
|
||||
}
|
||||
|
||||
/** free src */
|
||||
hm_pfree(pool, ptr);
|
||||
|
||||
return dst;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void *hm_palloc(struct hm_pool_s *pool, int size)
|
||||
{
|
||||
#ifdef POOL_STDLIB
|
||||
return malloc(size);
|
||||
#endif
|
||||
|
||||
struct hm_pool_s *p;
|
||||
|
||||
assert(pool);
|
||||
|
||||
for(p = pool; p != NULL; p = p->next) {
|
||||
/** do we match an existing pool */
|
||||
if(ROUND16(size) == p->size) {
|
||||
#ifdef POOL_DEBUG
|
||||
hm_log(LOG_MEMORY, pool->log, "{Pool}: found existing pool: %p/%d", p, p->size);
|
||||
#endif
|
||||
return pool_get_node(p, size);
|
||||
}
|
||||
}
|
||||
|
||||
p = pool_create_append(pool, ROUND16(size));
|
||||
|
||||
if(p == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
#ifdef POOL_DEBUG
|
||||
hm_log(LOG_MEMORY, pool->log, "{Pool}: creating new pool with parent pool: %p/%d", p, p->size);
|
||||
#endif
|
||||
|
||||
return pool_get_node(p, size);
|
||||
}
|
||||
|
||||
int hm_destroy_pool(struct hm_pool_s *pool)
|
||||
{
|
||||
struct hm_pool_s *p, *pd;
|
||||
struct pool_bucket_s *b, *bd;
|
||||
|
||||
for(p = pool; p != NULL; ) {
|
||||
for(b = p->buckets; b != NULL; ) {
|
||||
free(b->memory_region);
|
||||
free(b->nodes);
|
||||
|
||||
bd = b;
|
||||
b = b->next;
|
||||
free(bd);
|
||||
}
|
||||
pd = p;
|
||||
p = p->next;
|
||||
free(pd);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void pool_info(struct hm_pool_s *pool)
|
||||
{
|
||||
struct hm_pool_s *p;
|
||||
|
||||
for(p = pool; p != NULL; p = p->next) {
|
||||
printf("pool size: %d used: %d\n", p->size, p->used);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,147 @@
|
||||
/*
|
||||
hm_base - hearthmod base library
|
||||
Copyright (C) 2016 Filip Pancik
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include <assert.h>
|
||||
#include <memory.h>
|
||||
#include <stdio.h>
|
||||
#include <malloc.h>
|
||||
|
||||
#include <hmbase.h>
|
||||
|
||||
/** Send functions */
|
||||
char *rb_send_next(struct rb_s *rb, int *size)
|
||||
{
|
||||
assert(rb);
|
||||
|
||||
if(rb->send == NULL) {
|
||||
*size = 0;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
*size = rb->send->len - rb->send->sent;
|
||||
// test
|
||||
/*
|
||||
if(*size >= 10) {
|
||||
*size = 10;
|
||||
}
|
||||
*/
|
||||
|
||||
return (char *)(rb->send->buf + rb->send->sent);
|
||||
}
|
||||
|
||||
static void rb_next(struct rb_s *rb)
|
||||
{
|
||||
struct rb_slot_s *next;
|
||||
|
||||
if(rb && rb->send && rb->send->sent == rb->send->len) {
|
||||
next = rb->send->next;
|
||||
free(rb->send->buf);
|
||||
free(rb->send);
|
||||
rb->send = next;
|
||||
if(rb->send == NULL) {
|
||||
rb->tail = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void rb_send_skip(struct rb_s *rb, int offset)
|
||||
{
|
||||
assert(rb);
|
||||
assert(rb->send);
|
||||
rb->send->sent += offset;
|
||||
|
||||
rb_next(rb);
|
||||
}
|
||||
|
||||
int rb_send_is_empty(struct rb_s *rb)
|
||||
{
|
||||
assert(rb);
|
||||
return (rb->send == NULL);
|
||||
}
|
||||
|
||||
void rb_send_pop(struct rb_s *rb, struct hm_pool_s *pool)
|
||||
{
|
||||
rb_next(rb);
|
||||
}
|
||||
|
||||
int rb_send_init(struct rb_s *rb, char *buf, const int len, struct hm_pool_s *pool)
|
||||
{
|
||||
assert(rb);
|
||||
struct rb_slot_s *slot;
|
||||
|
||||
slot = malloc(sizeof(*slot));
|
||||
if(slot == NULL) {
|
||||
return -1;
|
||||
}
|
||||
slot->buf = malloc(len);
|
||||
if(slot->buf == NULL) {
|
||||
free(slot);
|
||||
return -1;
|
||||
}
|
||||
|
||||
memcpy(slot->buf, buf, len);
|
||||
slot->len = len;
|
||||
slot->sent = 0;
|
||||
slot->next = NULL;
|
||||
|
||||
if(rb->send == NULL && rb->tail == NULL) {
|
||||
//printf("added to head send init\n");
|
||||
rb->send = rb->tail = slot;
|
||||
} else {
|
||||
|
||||
//printf("added to tail send init\n");
|
||||
assert(rb->tail);
|
||||
rb->tail->next = slot;
|
||||
rb->tail = slot;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/** Receive functions */
|
||||
char *rb_recv_ptr(struct rb_s *rb, int *used)
|
||||
{
|
||||
assert(rb && used);
|
||||
*used = rb->recv.len;
|
||||
return (rb->recv.slot + rb->recv.len);
|
||||
}
|
||||
|
||||
void rb_recv_append(struct rb_s *rb, const int len)
|
||||
{
|
||||
assert(rb);
|
||||
rb->recv.len += len;
|
||||
}
|
||||
|
||||
char *rb_recv_read(struct rb_s *rb, int *size)
|
||||
{
|
||||
assert(rb && size);
|
||||
*size = rb->recv.len;
|
||||
return rb->recv.slot;
|
||||
}
|
||||
|
||||
void rb_recv_pop(struct rb_s *rb)
|
||||
{
|
||||
assert(rb);
|
||||
rb->recv.len = 0;
|
||||
}
|
||||
|
||||
int rb_recv_is_full(struct rb_s *rb)
|
||||
{
|
||||
assert(rb);
|
||||
return (rb->recv.len >= RB_SLOT_SIZE);
|
||||
}
|
||||
Reference in New Issue
Block a user