Skip to content

Commit

Permalink
Increase message size 16M(24bit) limit to 56bit on 64bit arch
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudwu committed Aug 5, 2015
1 parent b5244b9 commit aff73cb
Show file tree
Hide file tree
Showing 11 changed files with 31 additions and 24 deletions.
5 changes: 1 addition & 4 deletions examples/config.userlog
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@ thread = 8
logger = "userlog"
logservice = "snlua"
logpath = "."
harbor = 1
address = "127.0.0.1:2526"
master = "127.0.0.1:2013"
harbor = 0
start = "main" -- main script
bootstrap = "snlua bootstrap" -- The service for bootstrap
standalone = "0.0.0.0:2013"
luaservice = root.."service/?.lua;"..root.."test/?.lua;"..root.."examples/?.lua"
lualoader = "lualib/loader.lua"
-- preload = "./examples/preload.lua" -- run preload.lua before every lua service run
Expand Down
9 changes: 7 additions & 2 deletions service-src/service_harbor.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "skynet.h"
#include "skynet_harbor.h"
#include "skynet_socket.h"
#include "skynet_handle.h"

/*
harbor listen the PTYPE_HARBOR (in text)
Expand Down Expand Up @@ -323,9 +324,13 @@ forward_local_messsage(struct harbor *h, void *msg, int sz) {

static void
send_remote(struct skynet_context * ctx, int fd, const char * buffer, size_t sz, struct remote_message_header * cookie) {
uint32_t sz_header = sz+sizeof(*cookie);
size_t sz_header = sz+sizeof(*cookie);
if (sz_header > UINT32_MAX) {
skynet_error(ctx, "remote message from :%08x to :%08x is too large.", cookie->source, cookie->destination);
return;
}
uint8_t * sendbuf = skynet_malloc(sz_header+4);
to_bigendian(sendbuf, sz_header);
to_bigendian(sendbuf, (uint32_t)sz_header);
memcpy(sendbuf+4, buffer, sz);
header_to_message(cookie, sendbuf+4+sz);

Expand Down
2 changes: 1 addition & 1 deletion skynet-src/skynet_error.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ skynet_error(struct skynet_context * context, const char *msg, ...) {
}
smsg.session = 0;
smsg.data = data;
smsg.sz = len | (PTYPE_TEXT << HANDLE_REMOTE_SHIFT);
smsg.sz = len | ((size_t)PTYPE_TEXT << MESSAGE_TYPE_SHIFT);
skynet_context_push(logger, &smsg);
}

4 changes: 3 additions & 1 deletion skynet-src/skynet_handle.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

#include <stdint.h>

#include "skynet_harbor.h"
// reserve high 8 bits for remote id
#define HANDLE_MASK 0xffffff
#define HANDLE_REMOTE_SHIFT 24

struct skynet_context;

Expand Down
6 changes: 4 additions & 2 deletions skynet-src/skynet_harbor.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "skynet.h"
#include "skynet_harbor.h"
#include "skynet_server.h"
#include "skynet_mq.h"
#include "skynet_handle.h"

#include <string.h>
#include <stdio.h>
Expand All @@ -11,8 +13,8 @@ static unsigned int HARBOR = ~0;

void
skynet_harbor_send(struct remote_message *rmsg, uint32_t source, int session) {
int type = rmsg->sz >> HANDLE_REMOTE_SHIFT;
rmsg->sz &= HANDLE_MASK;
int type = rmsg->sz >> MESSAGE_TYPE_SHIFT;
rmsg->sz &= MESSAGE_TYPE_MASK;
assert(type != PTYPE_SYSTEM && type != PTYPE_HARBOR && REMOTE);
skynet_context_send(REMOTE, rmsg, sizeof(*rmsg) , source, type , session);
}
Expand Down
4 changes: 0 additions & 4 deletions skynet-src/skynet_harbor.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@
#define GLOBALNAME_LENGTH 16
#define REMOTE_MAX 256

// reserve high 8 bits for remote id
#define HANDLE_MASK 0xffffff
#define HANDLE_REMOTE_SHIFT 24

struct remote_name {
char name[GLOBALNAME_LENGTH];
uint32_t handle;
Expand Down
4 changes: 4 additions & 0 deletions skynet-src/skynet_mq.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ struct skynet_message {
size_t sz;
};

// type is encoding in skynet_message.sz high 8bit
#define MESSAGE_TYPE_MASK (SIZE_MAX >> 8)
#define MESSAGE_TYPE_SHIFT ((sizeof(size_t)-1) * 8)

struct message_queue;

void skynet_globalmq_push(struct message_queue * queue);
Expand Down
12 changes: 6 additions & 6 deletions skynet-src/skynet_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,8 @@ dispatch_message(struct skynet_context *ctx, struct skynet_message *msg) {
assert(ctx->init);
CHECKCALLING_BEGIN(ctx)
pthread_setspecific(G_NODE.handle_key, (void *)(uintptr_t)(ctx->handle));
int type = msg->sz >> HANDLE_REMOTE_SHIFT;
size_t sz = msg->sz & HANDLE_MASK;
int type = msg->sz >> MESSAGE_TYPE_SHIFT;
size_t sz = msg->sz & MESSAGE_TYPE_MASK;
if (ctx->logfile) {
skynet_log_output(ctx->logfile, msg->source, type, msg->session, msg->data, sz);
}
Expand Down Expand Up @@ -662,13 +662,13 @@ _filter_args(struct skynet_context * context, int type, int *session, void ** da
*data = msg;
}

*sz |= type << HANDLE_REMOTE_SHIFT;
*sz |= (size_t)type << MESSAGE_TYPE_SHIFT;
}

int
skynet_send(struct skynet_context * context, uint32_t source, uint32_t destination , int type, int session, void * data, size_t sz) {
if ((sz & HANDLE_MASK) != sz) {
skynet_error(context, "The message to %x is too large (sz = %lu)", destination, sz);
if ((sz & MESSAGE_TYPE_MASK) != sz) {
skynet_error(context, "The message to %x is too large", destination);
skynet_free(data);
return -1;
}
Expand Down Expand Up @@ -751,7 +751,7 @@ skynet_context_send(struct skynet_context * ctx, void * msg, size_t sz, uint32_t
smsg.source = source;
smsg.session = session;
smsg.data = msg;
smsg.sz = sz | type << HANDLE_REMOTE_SHIFT;
smsg.sz = sz | (size_t)type << MESSAGE_TYPE_SHIFT;

skynet_mq_push(ctx->queue, &smsg);
}
Expand Down
4 changes: 2 additions & 2 deletions skynet-src/skynet_socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ skynet_socket_free() {
static void
forward_message(int type, bool padding, struct socket_message * result) {
struct skynet_socket_message *sm;
int sz = sizeof(*sm);
size_t sz = sizeof(*sm);
if (padding) {
if (result->data) {
sz += strlen(result->data);
Expand All @@ -56,7 +56,7 @@ forward_message(int type, bool padding, struct socket_message * result) {
message.source = 0;
message.session = 0;
message.data = sm;
message.sz = sz | PTYPE_SOCKET << HANDLE_REMOTE_SHIFT;
message.sz = sz | ((size_t)PTYPE_SOCKET << MESSAGE_TYPE_SHIFT);

if (skynet_context_push((uint32_t)result->opaque, &message)) {
// todo: report somewhere to close socket
Expand Down
1 change: 1 addition & 0 deletions skynet-src/skynet_start.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "skynet_monitor.h"
#include "skynet_socket.h"
#include "skynet_daemon.h"
#include "skynet_harbor.h"

#include <pthread.h>
#include <unistd.h>
Expand Down
4 changes: 2 additions & 2 deletions skynet-src/skynet_timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ dispatch_list(struct timer_node *current) {
message.source = 0;
message.session = event->session;
message.data = NULL;
message.sz = PTYPE_RESPONSE << HANDLE_REMOTE_SHIFT;
message.sz = (size_t)MESSAGE_TYPE_SHIFT << MESSAGE_TYPE_SHIFT;

skynet_context_push(event->handle, &message);

Expand Down Expand Up @@ -214,7 +214,7 @@ skynet_timeout(uint32_t handle, int time, int session) {
message.source = 0;
message.session = session;
message.data = NULL;
message.sz = PTYPE_RESPONSE << HANDLE_REMOTE_SHIFT;
message.sz = (size_t)PTYPE_RESPONSE << MESSAGE_TYPE_SHIFT;

if (skynet_context_push(handle, &message)) {
return -1;
Expand Down

0 comments on commit aff73cb

Please sign in to comment.