change source dir

This commit is contained in:
云风
2012-08-09 15:36:56 +08:00
parent d23c4aa905
commit eed9c42fd4
46 changed files with 284 additions and 118 deletions

View File

@@ -0,0 +1,100 @@
#include "skynet.h"
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <stdio.h>
#define DEFAULT_NUMBER 16
#define LAUNCHER ".launcher"
struct worker {
int init;
char * name;
};
struct broker {
int init;
int id;
char * name;
struct worker w[DEFAULT_NUMBER];
};
struct broker *
broker_create(void) {
struct broker *b = malloc(sizeof(*b));
memset(b,0,sizeof(*b));
return b;
}
void
broker_release(struct broker * b) {
int i;
for (i=0;i<DEFAULT_NUMBER;i++) {
free(b->w[i].name);
}
free(b->name);
free(b);
}
static void
_init(struct broker *b, int session, const char * msg, size_t sz) {
assert(session > 0 && session <= DEFAULT_NUMBER);
assert(msg);
int id = session - 1;
assert(b->w[id].init == 0);
b->w[id].name = malloc(sz+1);
memcpy(b->w[id].name, msg, sz);
b->w[id].name[sz] = '\0';
b->w[id].init = 1;
++b->init;
}
static void
_forward(struct broker *b, struct skynet_context * context) {
skynet_forward(context, b->w[b->id].name);
b->id = (b->id + 1) % DEFAULT_NUMBER;
}
static void
_cb(struct skynet_context * context, void * ud, int session, const char * addr, const void * msg, size_t sz) {
struct broker * b = ud;
if (b->init < DEFAULT_NUMBER) {
_init(b, session, msg, sz);
if (b->init == DEFAULT_NUMBER) {
skynet_command(context, "REG", b->name);
skynet_send(context, LAUNCHER, 0, NULL, 0, 0);
}
} else {
_forward(b, context);
}
}
int
broker_init(struct broker *b, struct skynet_context *ctx, const char * args) {
char * service = strchr(args,' ');
if (service == NULL) {
return 1;
}
int len = service - args;
if (len>0) {
b->name = malloc(len +1);
memcpy(b->name, args, len);
b->name[len] = '\0';
}
service++;
int i;
len = strlen(service);
if (len == 0)
return 1;
for (i=0;i<DEFAULT_NUMBER;i++) {
int id = skynet_send(ctx, LAUNCHER , -1, service , len, 0);
assert(id > 0 && id <= DEFAULT_NUMBER);
}
skynet_callback(ctx, b, _cb);
return 0;
}

View File

@@ -0,0 +1,45 @@
#include "skynet.h"
#include <sys/uio.h>
#include <errno.h>
#include <stdlib.h>
#include <stdint.h>
#include <assert.h>
#include <stdio.h>
#include <string.h>
static void
_cb(struct skynet_context * context, void * ud, int session, const char * addr, const void * msg, size_t sz) {
assert(session == 0);
assert(sz <= 65535);
int fd = (int)(intptr_t)ud;
struct iovec buffer[2];
// send big-endian header
uint8_t head[2] = { sz >> 8 & 0xff , sz & 0xff };
buffer[0].iov_base = head;
buffer[0].iov_len = 2;
buffer[1].iov_base = (void *)msg;
buffer[1].iov_len = sz;
for (;;) {
int err = writev(fd, buffer, 2);
if (err < 0) {
switch (errno) {
case EAGAIN:
case EINTR:
continue;
}
}
assert(err == sz +2);
return;
}
}
int
client_init(void * dummy, struct skynet_context *ctx, const char * args) {
int fd = strtol(args, NULL, 10);
skynet_callback(ctx, (void*)(intptr_t)fd, _cb);
return 0;
}

104
service-src/service_lua.c Normal file
View File

@@ -0,0 +1,104 @@
#include "skynet.h"
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#include <string.h>
#include <stdlib.h>
lua_State *
snlua_create(void) {
return luaL_newstate();
}
static int
_load(lua_State *L, char ** filename) {
const char * name = strsep(filename, " \r\n");
const char * path = skynet_command(NULL, "GETENV", "luaservice");
int namesz = strlen(name);
int sz = strlen(path) + namesz;
char tmp[sz];
int i;
for (i=0;path[i]!='?' && path[i]!='\0';i++) {
tmp[i] = path[i];
}
memcpy(tmp+i,name,namesz);
if (path[i] == '?') {
strcpy(tmp+i+namesz,path+i+1);
} else {
fprintf(stderr,"snlua : Invalid lua service path\n");
exit(1);
}
int r = luaL_loadfile(L,tmp);
return r != LUA_OK;
}
static int
traceback (lua_State *L) {
const char *msg = lua_tostring(L, 1);
if (msg)
luaL_traceback(L, L, msg, 1);
else {
lua_pushliteral(L, "(no error message)");
}
return 1;
}
int
snlua_init(lua_State *L, struct skynet_context *ctx, const char * args) {
lua_gc(L, LUA_GCSTOP, 0);
luaL_openlibs(L);
lua_pushlightuserdata(L, ctx);
lua_setfield(L, LUA_REGISTRYINDEX, "skynet_context");
lua_gc(L, LUA_GCRESTART, 0);
char tmp[strlen(args)+1];
char *parm = tmp;
strcpy(parm,args);
lua_pushcfunction(L, traceback);
int traceback_index = lua_gettop(L);
const char * filename = parm;
int r = _load(L, &parm);
if (r) {
skynet_error(ctx, "lua parser [%s] error : %s", filename, lua_tostring(L,-1));
return 1;
}
int n=0;
while(parm) {
const char * arg = strsep(&parm, " \r\n");
if (arg && arg[0]!='\0') {
lua_pushstring(L, arg);
++n;
}
}
r = lua_pcall(L,n,0,traceback_index);
switch (r) {
case LUA_OK:
return 0;
case LUA_ERRRUN:
skynet_error(ctx, "lua do [%s] error : %s", filename, lua_tostring(L,-1));
break;
case LUA_ERRMEM:
skynet_error(ctx, "lua memory error : %s",filename);
break;
case LUA_ERRERR:
skynet_error(ctx, "lua message error : %s",filename);
break;
case LUA_ERRGCMM:
skynet_error(ctx, "lua gc error : %s",filename);
break;
};
lua_pop(L,1);
return 1;
}
void
snlua_release(lua_State *L) {
lua_close(L);
}