mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-24 12:20:41 +00:00
stand alone skynet without skynet-master
This commit is contained in:
8
Makefile
8
Makefile
@@ -1,7 +1,7 @@
|
|||||||
all : skynet snlua.so logger.so skynet.so gate.so client.so connection.so client skynet-master
|
all : skynet snlua.so logger.so skynet.so gate.so client.so connection.so client skynet-master
|
||||||
|
|
||||||
skynet : skynet_main.c skynet_handle.c skynet_module.c skynet_mq.c skynet_server.c skynet_start.c skynet_timer.c skynet_error.c skynet_harbor.c
|
skynet : skynet_main.c skynet_handle.c skynet_module.c skynet_mq.c skynet_server.c skynet_start.c skynet_timer.c skynet_error.c skynet_harbor.c master/master.c
|
||||||
gcc -Wall -g -Wl,-E -o $@ $^ -lpthread -ldl -lrt -Wl,-E -llua -lm -lzmq
|
gcc -Wall -g -Wl,-E -o $@ $^ -Imaster -lpthread -ldl -lrt -Wl,-E -llua -lm -lzmq
|
||||||
|
|
||||||
logger.so : skynet_logger.c
|
logger.so : skynet_logger.c
|
||||||
gcc -Wall -g -fPIC --shared $^ -o $@
|
gcc -Wall -g -fPIC --shared $^ -o $@
|
||||||
@@ -24,8 +24,8 @@ connection.so : connection/connection.c connection/main.c
|
|||||||
client : client.c
|
client : client.c
|
||||||
gcc -Wall -g $^ -o $@ -lpthread
|
gcc -Wall -g $^ -o $@ -lpthread
|
||||||
|
|
||||||
skynet-master : master/master.c
|
skynet-master : master/master.c master/main.c
|
||||||
gcc -g -Wall -o $@ $^ -lzmq
|
gcc -g -Wall -Imaster -o $@ $^ -lzmq
|
||||||
|
|
||||||
clean :
|
clean :
|
||||||
rm skynet client skynet-master *.so
|
rm skynet client skynet-master *.so
|
||||||
|
|||||||
@@ -11,8 +11,7 @@ make
|
|||||||
Run these in different console
|
Run these in different console
|
||||||
|
|
||||||
```
|
```
|
||||||
./skynet-master # Launch master server first
|
./skynet config # Launch first skynet node (Gate server) and a skynet-master (see config for standalone option)
|
||||||
./skynet config # Launch first skynet node (Gate server)
|
|
||||||
./skynet config_log # Launch second skynet node (Global logger server)
|
./skynet config_log # Launch second skynet node (Global logger server)
|
||||||
./clinet 127.0.0.1 8888 # Launch a client, and try to input some words.
|
./clinet 127.0.0.1 8888 # Launch a client, and try to input some words.
|
||||||
```
|
```
|
||||||
@@ -20,8 +19,7 @@ Run these in different console
|
|||||||
## Test connection server
|
## Test connection server
|
||||||
|
|
||||||
```
|
```
|
||||||
./skynet-master # Launch master server first
|
./skynet # Launch skynet
|
||||||
./skynet # Launch first skynet node
|
|
||||||
./nc -l 8000 # Listen on port 8000
|
./nc -l 8000 # Listen on port 8000
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
1
config
1
config
@@ -6,3 +6,4 @@ harbor = 1
|
|||||||
address = "tcp://127.0.0.1:2525"
|
address = "tcp://127.0.0.1:2525"
|
||||||
master = "tcp://127.0.0.1:2012"
|
master = "tcp://127.0.0.1:2012"
|
||||||
start = "main.lua"
|
start = "main.lua"
|
||||||
|
standalone = true
|
||||||
|
|||||||
@@ -1,7 +1,10 @@
|
|||||||
all : master test
|
all : master test
|
||||||
|
|
||||||
master : master.c
|
master : master.c main.c
|
||||||
gcc -g -Wall -o $@ $^ -lzmq
|
gcc -g -Wall -o $@ $^ -lzmq
|
||||||
|
|
||||||
test : test.c
|
test : test.c
|
||||||
gcc -g -Wall -o $@ $^ -lzmq
|
gcc -g -Wall -o $@ $^ -lzmq
|
||||||
|
|
||||||
|
clean :
|
||||||
|
rm master test
|
||||||
|
|||||||
17
master/main.c
Normal file
17
master/main.c
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
#include "skynet_master.h"
|
||||||
|
#include <zmq.h>
|
||||||
|
|
||||||
|
int
|
||||||
|
main (int argc, char * argv[]) {
|
||||||
|
const char * default_port = "tcp://127.0.0.1:2012";
|
||||||
|
if (argc > 1) {
|
||||||
|
default_port = argv[2];
|
||||||
|
}
|
||||||
|
|
||||||
|
void *context = zmq_init (1);
|
||||||
|
|
||||||
|
skynet_master(context, default_port);
|
||||||
|
|
||||||
|
zmq_term (context);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -217,22 +217,16 @@ update(void * responder, struct hashmap *map, zmq_msg_t * msg) {
|
|||||||
query(responder, map, key);
|
query(responder, map, key);
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
void
|
||||||
main (int argc, char * argv[]) {
|
skynet_master(void * context, const char * port) {
|
||||||
const char * default_port = "tcp://127.0.0.1:2012";
|
|
||||||
if (argc > 1) {
|
|
||||||
default_port = argv[2];
|
|
||||||
}
|
|
||||||
|
|
||||||
void *context = zmq_init (1);
|
|
||||||
void *responder = zmq_socket (context, ZMQ_REP);
|
void *responder = zmq_socket (context, ZMQ_REP);
|
||||||
|
|
||||||
int r = zmq_bind(responder, default_port);
|
int r = zmq_bind(responder, port);
|
||||||
if (r < 0) {
|
if (r < 0) {
|
||||||
fprintf(stderr, "Can't bind to %s\n",default_port);
|
fprintf(stderr, "Can't bind to %s\n",port);
|
||||||
return 1;
|
exit(1);
|
||||||
}
|
}
|
||||||
printf("Start master on %s\n",default_port);
|
printf("Start master on %s\n", port);
|
||||||
|
|
||||||
struct hashmap *map = _hash_new(context);
|
struct hashmap *map = _hash_new(context);
|
||||||
|
|
||||||
@@ -245,6 +239,5 @@ main (int argc, char * argv[]) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
zmq_close (responder);
|
zmq_close (responder);
|
||||||
zmq_term (context);
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
6
master/skynet_master.h
Normal file
6
master/skynet_master.h
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
#ifndef SKYNET_MASTER_H
|
||||||
|
#define SKYNET_MASTER_H
|
||||||
|
|
||||||
|
void skynet_master(void * context, const char * port);
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -617,12 +617,11 @@ register_harbor(void *request, const char *local, int harbor) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
skynet_harbor_init(const char * master, const char *local, int harbor) {
|
skynet_harbor_init(void * context, const char * master, const char *local, int harbor) {
|
||||||
if (harbor <=0 || harbor>255 || strlen(local) > 512) {
|
if (harbor <=0 || harbor>255 || strlen(local) > 512) {
|
||||||
fprintf(stderr,"Invalid harbor id\n");
|
fprintf(stderr,"Invalid harbor id\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
void *context = zmq_init (1);
|
|
||||||
void *request = zmq_socket (context, ZMQ_REQ);
|
void *request = zmq_socket (context, ZMQ_REQ);
|
||||||
int r = zmq_connect(request, master);
|
int r = zmq_connect(request, master);
|
||||||
if (r<0) {
|
if (r<0) {
|
||||||
|
|||||||
@@ -16,6 +16,6 @@ void skynet_harbor_message_close(struct skynet_message * message);
|
|||||||
|
|
||||||
// harbor worker thread
|
// harbor worker thread
|
||||||
void * skynet_harbor_dispatch_thread(void *ud);
|
void * skynet_harbor_dispatch_thread(void *ud);
|
||||||
void skynet_harbor_init(const char * master, const char *local, int harbor);
|
void skynet_harbor_init(void * context, const char * master, const char *local, int harbor);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ struct skynet_config {
|
|||||||
const char * master;
|
const char * master;
|
||||||
const char * local;
|
const char * local;
|
||||||
const char * start;
|
const char * start;
|
||||||
|
int standalone;
|
||||||
};
|
};
|
||||||
|
|
||||||
void skynet_start(struct skynet_config * config);
|
void skynet_start(struct skynet_config * config);
|
||||||
|
|||||||
@@ -17,6 +17,16 @@ optint(lua_State *L, const char *key, int opt) {
|
|||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
optboolean(lua_State *L, const char *key, int opt) {
|
||||||
|
lua_getglobal(L, key);
|
||||||
|
int n = lua_toboolean(L,-1);
|
||||||
|
lua_pop(L,1);
|
||||||
|
if (n==0)
|
||||||
|
return opt;
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
static const char *
|
static const char *
|
||||||
optstring(lua_State *L, const char *key,const char * opt) {
|
optstring(lua_State *L, const char *key,const char * opt) {
|
||||||
lua_getglobal(L, key);
|
lua_getglobal(L, key);
|
||||||
@@ -61,6 +71,7 @@ main(int argc, char *argv[]) {
|
|||||||
config.master = optstring(L,"master","tcp://127.0.0.1:2012");
|
config.master = optstring(L,"master","tcp://127.0.0.1:2012");
|
||||||
config.start = optstring(L,"start","main.lua");
|
config.start = optstring(L,"start","main.lua");
|
||||||
config.local = optstring(L,"address","tcp://127.0.0.1:2525");
|
config.local = optstring(L,"address","tcp://127.0.0.1:2525");
|
||||||
|
config.standalone = optboolean(L,"standalone",0);
|
||||||
lua_close(L);
|
lua_close(L);
|
||||||
|
|
||||||
skynet_start(&config);
|
skynet_start(&config);
|
||||||
|
|||||||
@@ -5,10 +5,12 @@
|
|||||||
#include "skynet_module.h"
|
#include "skynet_module.h"
|
||||||
#include "skynet_timer.h"
|
#include "skynet_timer.h"
|
||||||
#include "skynet_harbor.h"
|
#include "skynet_harbor.h"
|
||||||
|
#include "skynet_master.h"
|
||||||
|
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
#include <zmq.h>
|
||||||
|
|
||||||
static void *
|
static void *
|
||||||
_timer(void *p) {
|
_timer(void *p) {
|
||||||
@@ -46,10 +48,36 @@ _start(int thread) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct master_arg {
|
||||||
|
void * context;
|
||||||
|
const char * port;
|
||||||
|
};
|
||||||
|
|
||||||
|
static void *
|
||||||
|
_master_thread(void *ud) {
|
||||||
|
struct master_arg * args = ud;
|
||||||
|
skynet_master(args->context, args->port);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
_start_master(void * context, const char * port) {
|
||||||
|
pthread_t pid;
|
||||||
|
struct master_arg args;
|
||||||
|
args.context = context;
|
||||||
|
args.port = port;
|
||||||
|
pthread_create(&pid, NULL, _master_thread, &args);
|
||||||
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
skynet_start(struct skynet_config * config) {
|
skynet_start(struct skynet_config * config) {
|
||||||
|
void *context = zmq_init (1);
|
||||||
|
assert(context);
|
||||||
|
if (config->standalone) {
|
||||||
|
_start_master(context, config->master);
|
||||||
|
}
|
||||||
// harbor must be init first
|
// harbor must be init first
|
||||||
skynet_harbor_init(config->master , config->local, config->harbor);
|
skynet_harbor_init(context, config->master , config->local, config->harbor);
|
||||||
skynet_handle_init(config->harbor);
|
skynet_handle_init(config->harbor);
|
||||||
skynet_mq_init(config->mqueue_size);
|
skynet_mq_init(config->mqueue_size);
|
||||||
skynet_module_init(config->module_path);
|
skynet_module_init(config->module_path);
|
||||||
|
|||||||
Reference in New Issue
Block a user