Skip to content

Commit

Permalink
优化共享内存
Browse files Browse the repository at this point in the history
  • Loading branch information
xgugeng committed Jul 26, 2014
1 parent f169b78 commit 71d2d29
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 83 deletions.
4 changes: 4 additions & 0 deletions tbus/include/tbus.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ extern "C" {

#include <stdint.h>
#include <sys/uio.h>
#include <sys/types.h>


#define TBUS_VERSION "0.0.1"
Expand Down Expand Up @@ -41,6 +42,9 @@ typedef struct tbus_s
#define tbus_size(packet_size, packet_num) (TLIBC_OFFSET_OF(tbus_t, buff) + (packet_size + sizeof(tbus_header_t)) * packet_num)
void tbus_init(tbus_t *tb, size_t size, size_t number);

tbus_t *tbus_at(key_t key);
void tbus_dt(tbus_t *tb);

tbus_atomic_size_t tbus_send_begin(tbus_t *tb, char** buf);
void tbus_send_end(tbus_t *tb, tbus_atomic_size_t len);

Expand Down
23 changes: 23 additions & 0 deletions tbus/source/tbus.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@


#include <string.h>
#include <sys/types.h>
#include <sys/shm.h>


void tbus_init(tbus_t *tb, size_t size, size_t number)
Expand All @@ -13,6 +15,27 @@ void tbus_init(tbus_t *tb, size_t size, size_t number)
tb->size = (tbus_atomic_size_t)(tb->packet_size * number);
}

tbus_t *tbus_at(key_t key)
{
tbus_t *ret = NULL;
int id = shmget(key, 0, 0666);
if(id == -1)
{
return NULL;
}
ret = shmat(id, NULL, 0);
if((ssize_t)ret == -1)
{
return NULL;
}
return ret;
}

void tbus_dt(tbus_t *tb)
{
shmdt(tb);
}

tbus_atomic_size_t tbus_send_begin(tbus_t *tb, char** buf)
{
tbus_atomic_size_t write_size;
Expand Down
7 changes: 1 addition & 6 deletions tbusapi/include/tbusapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ typedef void (*tbusapi_on_recviov_func)(tbusapi_t *self, struct iovec *iov, uint
typedef tbus_atomic_size_t (*tbusapi_encode_func)(char *dst, size_t dst_len, const char *src, size_t src_len);
struct tbusapi_s
{
int itb_id;
tbus_t *itb;

int otb_id;
tbus_t *otb;

tbusapi_on_recviov_func on_recviov;
Expand All @@ -32,9 +29,7 @@ struct tbusapi_s
size_t iov_num;
};

tlibc_error_code_t tbusapi_init(tbusapi_t *self, key_t input_tbuskey, key_t output_tbuskey);

void tbusapi_fini(tbusapi_t *self);
void tbusapi_init(tbusapi_t *self, tbus_t *itb, tbus_t *otb);

void tbusapi_send(tbusapi_t *self, const char *packet, size_t packet_len);

Expand Down
71 changes: 4 additions & 67 deletions tbusapi/source/tbusapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,65 +16,15 @@ static tbus_atomic_size_t encode(char *dst, size_t dst_len, const char *src, siz
return (tbus_atomic_size_t)src_len;
}

tlibc_error_code_t tbusapi_init(tbusapi_t *self, key_t input_tbuskey, key_t output_tbuskey)
{
tlibc_error_code_t ret = E_TLIBC_NOERROR;

if(input_tbuskey == 0)
{
self->itb_id = 0;
self->itb = NULL;
}
else
{
self->itb_id = shmget(input_tbuskey, 0, 0666);
if(self->itb_id == -1)
{
ret = E_TLIBC_ERRNO;
goto done;
}
self->itb = shmat(self->itb_id, NULL, 0);
if((ssize_t)self->itb == -1)
{
ret = E_TLIBC_ERRNO;
goto done;
}
}

if(output_tbuskey == 0)
{
self->otb_id = 0;
self->otb = NULL;
}
else
{
self->otb_id = shmget(output_tbuskey, 0, 0666);
if(self->otb_id == -1)
{
ret = E_TLIBC_ERRNO;
goto shmdt_itb;
}

self->otb = shmat(self->otb_id, NULL, 0);
if((ssize_t)self->otb == -1)
{
ret = E_TLIBC_ERRNO;
goto shmdt_itb;
}
}

void tbusapi_init(tbusapi_t *self, tbus_t *itb, tbus_t *otb)
{
self->itb = itb;
self->otb = otb;
self->encode = encode;
self->on_recv = NULL;
self->on_recviov = NULL;
self->iov_num = 1;
done:
return ret;
shmdt_itb:
if(self->itb)
{
shmdt(self->itb);
}
return ret;
}

tlibc_error_code_t tbusapi_process(tbusapi_t *self)
Expand Down Expand Up @@ -128,16 +78,3 @@ void tbusapi_send(tbusapi_t *self, const char *packet, size_t packet_len)
tbus_send_end(self->otb, code_size);
}
}

void tbusapi_fini(tbusapi_t *self)
{
if(self->itb)
{
shmdt(self->itb);
}
if(self->otb)
{
shmdt(self->otb);
}
}

4 changes: 4 additions & 0 deletions tconnapi/include/tconnapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ typedef void (*tconnapi_on_recv_func)(tconnapi_t *self, const sip_cid_t *cid, co

struct tconnapi_s
{
tbus_t *itb;
tbus_t *otb;
tbusapi_t tbusapi;

encode_t encode;
Expand All @@ -38,6 +40,8 @@ void tconnapi_close(tconnapi_t *self, const sip_cid_t *cid_vec, uint16_t cid_vec

tlibc_error_code_t tconnapi_process(tconnapi_t *self);

void tconnapi_fini(tconnapi_t *self);

#ifdef __cplusplus
}
#endif
Expand Down
22 changes: 18 additions & 4 deletions tconnapi/source/tconnapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,20 @@ tlibc_error_code_t tconnapi_process(tconnapi_t *self)
tlibc_error_code_t tconnapi_init(tconnapi_t *self, key_t ikey, key_t okey, encode_t encode)
{
tlibc_error_code_t ret = E_TLIBC_NOERROR;

ret = tbusapi_init(&self->tbusapi, ikey, okey);
if(ret != E_TLIBC_NOERROR)
self->itb = tbus_at(ikey);
self->otb = tbus_at(okey);
if(self->itb == NULL)
{
goto done;
ret = E_TLIBC_ERRNO;
goto done;
}
if(self->itb == NULL)
{
ret = E_TLIBC_ERRNO;
goto done;
}

tbusapi_init(&self->tbusapi, self->itb, self->otb);

self->tbusapi.on_recv = tconnapi_on_recv;
self->tbusapi.encode = tconnapi_encode;
Expand All @@ -180,3 +188,9 @@ tlibc_error_code_t tconnapi_init(tconnapi_t *self, key_t ikey, key_t okey, encod
return ret;
}

void tconnapi_fini(tconnapi_t *self)
{
tbus_dt(self->itb);
tbus_dt(self->otb);
}

10 changes: 7 additions & 3 deletions tlogd/source/tlogd.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ tlogd_config_t g_config;
tlog_message_t g_message;
tlibc_binary_reader_t g_binary_reader;
tbusapi_t g_tbusapi;
tbus_t *g_itb = NULL;
tlog_t g_tlog;


Expand All @@ -56,12 +57,15 @@ static void on_recv(tbusapi_t *self, const char *buf, size_t buf_len)

static tlibc_error_code_t init()
{
if(tbusapi_init(&g_tbusapi, g_config.input_tbuskey, 0) != E_TLIBC_NOERROR)
g_itb = tbus_at(g_config.input_tbuskey);
if(g_itb == NULL)
{
ERROR_PRINT("tbusapi_init failed.");
ERROR_PRINT("tbus_at failed.");
goto error_ret;
}

tbusapi_init(&g_tbusapi, g_itb, NULL);

if(tlog_init(&g_tlog, &g_config.tlog_config) != E_TLIBC_NOERROR)
{
ERROR_PRINT("tlog_init failed.");
Expand All @@ -78,7 +82,7 @@ static tlibc_error_code_t init()

static void fini()
{
tbusapi_fini(&g_tbusapi);
tbus_dt(g_itb);
tlog_fini(&g_tlog);
}

Expand Down
6 changes: 5 additions & 1 deletion tutorials/tbus/tbus_client/source/tbus_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,19 @@
#define MAX_MESSAGE_LENGTH 1024

tbusapi_t g_tbusapi;
tbus_t *g_otb;

int main()
{
uint32_t i;

if(tbusapi_init(&g_tbusapi, 0, SHM_KEY) != E_TLIBC_NOERROR)
g_otb = tbus_at(SHM_KEY);
if(g_otb == NULL)
{
fprintf(stderr, "tbusapi_init failed.\n");
exit(1);
}
tbusapi_init(&g_tbusapi, 0, g_otb);

for(i = 0;i < 10;++i)
{
Expand All @@ -39,6 +42,7 @@ int main()
//任何时刻都不能让tbus堆满消息!
// sleep(1);
}
tbus_dt(g_otb);
return 0;
}

8 changes: 6 additions & 2 deletions tutorials/tbus/tbus_server/source/tbus_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,27 @@ static void on_recv(tbusapi_t *self, const char *buf, size_t buf_len)
}

tbusapi_t g_tbusapi;
tbus_t *g_itb;

int main(int argc, char *argv[])
{
tlibc_error_code_t ret;

if(tbusapi_init(&g_tbusapi, SHM_KEY, 0) != E_TLIBC_NOERROR)
g_itb = tbus_at(SHM_KEY);
if(g_itb == NULL)
{
fprintf(stderr, "tbusapi_init failed.\n");
exit(1);
}

tbusapi_init(&g_tbusapi, g_itb, 0);

g_tbusapi.on_recv = on_recv;
ret = tapp_loop(TAPP_IDLE_USEC, TAPP_IDLE_LIMIT, NULL, NULL, NULL, NULL
, tbusapi_process, &g_tbusapi
, NULL, NULL);

tbusapi_fini(&g_tbusapi);
tbus_dt(g_itb);

if(ret == E_TLIBC_NOERROR)
{
Expand Down

0 comments on commit 71d2d29

Please sign in to comment.