-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/chinabin/learn_skynet
- Loading branch information
Showing
102 changed files
with
8,980 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
.PHONY : all clean | ||
|
||
CFLAGS = -g -Wall | ||
SHARED = -fPIC --shared | ||
|
||
all : \ | ||
skynet \ | ||
service/snlua.so \ | ||
service/logger.so \ | ||
lualib/skynet.so \ | ||
service/gate.so \ | ||
service/client.so \ | ||
service/connection.so \ | ||
client \ | ||
lualib/socket.so \ | ||
lualib/int64.so \ | ||
service/master.so \ | ||
service/multicast.so \ | ||
service/tunnel.so \ | ||
service/harbor.so | ||
|
||
skynet : \ | ||
skynet-src/skynet_main.c \ | ||
skynet-src/skynet_handle.c \ | ||
skynet-src/skynet_module.c \ | ||
skynet-src/skynet_mq.c \ | ||
skynet-src/skynet_server.c \ | ||
skynet-src/skynet_start.c \ | ||
skynet-src/skynet_timer.c \ | ||
skynet-src/skynet_error.c \ | ||
skynet-src/skynet_harbor.c \ | ||
skynet-src/skynet_multicast.c \ | ||
skynet-src/skynet_group.c \ | ||
skynet-src/skynet_env.c | ||
gcc $(CFLAGS) -Wl,-E -o $@ $^ -Iskynet-src -lpthread -ldl -lrt -Wl,-E -llua -lm | ||
|
||
service/tunnel.so : service-src/service_tunnel.c | ||
gcc $(CFLAGS) $(SHARED) $^ -o $@ -Iskynet-src | ||
|
||
service/multicast.so : service-src/service_multicast.c | ||
gcc $(CFLAGS) $(SHARED) $^ -o $@ -Iskynet-src | ||
|
||
service/master.so : service-src/service_master.c | ||
gcc $(CFLAGS) $(SHARED) $^ -o $@ -Iskynet-src | ||
|
||
service/harbor.so : service-src/service_harbor.c | ||
gcc $(CFLAGS) $(SHARED) $^ -o $@ -Iskynet-src | ||
|
||
service/logger.so : skynet-src/skynet_logger.c | ||
gcc $(CFLAGS) $(SHARED) $^ -o $@ -Iskynet-src | ||
|
||
service/snlua.so : service-src/service_lua.c | ||
gcc $(CFLAGS) $(SHARED) $^ -o $@ -Iskynet-src | ||
|
||
service/gate.so : gate/mread.c gate/ringbuffer.c gate/main.c | ||
gcc $(CFLAGS) $(SHARED) $^ -o $@ -Igate -Iskynet-src | ||
|
||
lualib/skynet.so : lualib-src/lua-skynet.c lualib-src/lua-seri.c lualib-src/lua-remoteobj.c | ||
gcc $(CFLAGS) $(SHARED) $^ -o $@ -Iskynet-src | ||
|
||
service/client.so : service-src/service_client.c | ||
gcc $(CFLAGS) $(SHARED) $^ -o $@ -Iskynet-src | ||
|
||
service/connection.so : connection/connection.c connection/main.c | ||
gcc $(CFLAGS) $(SHARED) $^ -o $@ -Iskynet-src -Iconnection | ||
|
||
lualib/socket.so : connection/lua-socket.c | ||
gcc $(CFLAGS) $(SHARED) $^ -o $@ -Iskynet-src -Iconnection | ||
|
||
lualib/int64.so : lua-int64/int64.c | ||
gcc $(CFLAGS) $(SHARED) -O2 $^ -o $@ | ||
|
||
client : client-src/client.c | ||
gcc $(CFLAGS) $^ -o $@ -lpthread | ||
|
||
clean : | ||
rm skynet client lualib/*.so service/*.so | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
## Build | ||
|
||
Install lua 5.2 first. | ||
|
||
``` | ||
make | ||
``` | ||
|
||
## Test | ||
|
||
Run these in different console | ||
|
||
``` | ||
./skynet config # Launch first skynet node (Gate server) and a skynet-master (see config for standalone option) | ||
./skynet config_log # Launch second skynet node (Global logger server) | ||
./client 127.0.0.1 8888 # Launch a client, and try to input some words. | ||
``` | ||
|
||
## Blog (in Chinese) | ||
|
||
* http://blog.codingnow.com/2012/09/the_design_of_skynet.html | ||
* http://blog.codingnow.com/2012/08/skynet.html | ||
* http://blog.codingnow.com/2012/08/skynet_harbor_rpc.html |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
#include <pthread.h> | ||
#include <sys/types.h> | ||
#include <sys/socket.h> | ||
#include <arpa/inet.h> | ||
#include <unistd.h> | ||
|
||
#include <stdlib.h> | ||
#include <stdio.h> | ||
#include <stdint.h> | ||
#include <string.h> | ||
#include <errno.h> | ||
|
||
struct args { | ||
int fd; | ||
}; | ||
|
||
static int | ||
readall(int fd, void * buffer, size_t sz) { | ||
for (;;) { | ||
int err = recv(fd , buffer, sz, MSG_WAITALL); | ||
if (err < 0) { | ||
if (errno == EAGAIN || errno == EINTR) | ||
continue; | ||
break; | ||
} | ||
return err; | ||
} | ||
perror("Socket error"); | ||
exit(1); | ||
} | ||
|
||
static void * | ||
_read(void *ud) { | ||
struct args *p = ud; | ||
int fd = p->fd; | ||
fflush(stdout); | ||
for (;;) { | ||
uint8_t header[2]; | ||
fflush(stdout); | ||
if (readall(fd, header, 2) == 0) | ||
break; | ||
size_t len = header[0] << 8 | header[1]; | ||
if (len>0) { | ||
char tmp[len+1]; | ||
readall(fd, tmp, len); | ||
tmp[len]='\0'; | ||
printf("%s\n",tmp); | ||
} | ||
} | ||
return NULL; | ||
} | ||
|
||
static void * | ||
test(void *ud) { | ||
struct args *p = ud; | ||
int fd = p->fd; | ||
|
||
char tmp[1024]; | ||
while (!feof(stdin)) { | ||
fgets(tmp,sizeof(tmp),stdin); | ||
int n = strlen(tmp) -1; | ||
uint8_t head[2]; | ||
head[0] = (n >> 8) & 0xff; | ||
head[1] = n & 0xff; | ||
int r; | ||
r = send(fd, head, 2, 0); | ||
if (r<0) { | ||
perror("send head"); | ||
} | ||
r = send(fd, tmp , n, 0); | ||
if (r<0) { | ||
perror("send data"); | ||
} | ||
} | ||
return NULL; | ||
} | ||
|
||
int | ||
main(int argc, char * argv[]) { | ||
if (argc < 3) { | ||
printf("connect address port\n"); | ||
return 1; | ||
} | ||
|
||
int fd = socket(AF_INET,SOCK_STREAM,0); | ||
struct sockaddr_in my_addr; | ||
|
||
my_addr.sin_addr.s_addr=inet_addr(argv[1]); | ||
my_addr.sin_family=AF_INET; | ||
my_addr.sin_port=htons(strtol(argv[2],NULL,10)); | ||
|
||
int r = connect(fd,(struct sockaddr *)&my_addr,sizeof(struct sockaddr_in)); | ||
|
||
if (r == -1) { | ||
perror("Connect failed:"); | ||
return 1; | ||
} | ||
|
||
struct args arg = { fd }; | ||
pthread_t pid ; | ||
pthread_create(&pid, NULL, _read, &arg); | ||
pthread_t pid_stdin; | ||
pthread_create(&pid_stdin, NULL, test, &arg); | ||
|
||
pthread_join(pid, NULL); | ||
|
||
close(fd); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
root = "./" | ||
thread = 8 | ||
mqueue = 256 | ||
logger = nil | ||
harbor = 1 | ||
address = "127.0.0.1:2525" | ||
master = "127.0.0.1:2012" | ||
start = "main" | ||
standalone = "0.0.0.0:2012" | ||
luaservice = root.."service/?.lua;"..root.."service/?/init.lua" | ||
cpath = root.."service/?.so" | ||
protopath = root.."proto" | ||
redis = root .. "redisconf" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
thread = 8 | ||
mqueue = 256 | ||
cpath = "./service/?.so" | ||
logger = nil | ||
harbor = 2 | ||
address = "127.0.0.1:2526" | ||
master = "127.0.0.1:2012" | ||
start = "main_log" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
#include "connection.h" | ||
|
||
#include <stdio.h> | ||
#include <sys/epoll.h> | ||
#include <errno.h> | ||
#include <stdlib.h> | ||
#include <unistd.h> | ||
|
||
#define EPOLLQUEUE 32 | ||
|
||
struct connection_pool { | ||
int epoll_fd; | ||
int queue_len; | ||
int queue_head; | ||
struct epoll_event ev[EPOLLQUEUE]; | ||
}; | ||
|
||
struct connection_pool * | ||
connection_newpool(int max) { | ||
int epoll_fd = epoll_create(max); | ||
if (epoll_fd == -1) { | ||
return NULL; | ||
} | ||
struct connection_pool * pool = malloc(sizeof(*pool)); | ||
pool->epoll_fd = epoll_fd; | ||
pool->queue_len = 0; | ||
pool->queue_head = 0; | ||
|
||
return pool; | ||
} | ||
|
||
void | ||
connection_deletepool(struct connection_pool * pool) { | ||
close(pool->epoll_fd); | ||
free(pool); | ||
} | ||
|
||
int | ||
connection_add(struct connection_pool * pool, int fd, void *ud) { | ||
struct epoll_event ev; | ||
ev.events = EPOLLIN; | ||
ev.data.ptr = ud; | ||
|
||
if (epoll_ctl(pool->epoll_fd, EPOLL_CTL_ADD, fd, &ev) == -1) { | ||
return 1; | ||
} | ||
return 0; | ||
} | ||
|
||
void | ||
connection_del(struct connection_pool * pool, int fd) { | ||
epoll_ctl(pool->epoll_fd, EPOLL_CTL_DEL, fd , NULL); | ||
close(fd); | ||
} | ||
|
||
static int | ||
_read_queue(struct connection_pool * pool, int timeout) { | ||
pool->queue_head = 0; | ||
int n = epoll_wait(pool->epoll_fd , pool->ev, EPOLLQUEUE, timeout); | ||
if (n == -1) { | ||
pool->queue_len = 0; | ||
return -1; | ||
} | ||
pool->queue_len = n; | ||
return n; | ||
} | ||
|
||
void * | ||
connection_poll(struct connection_pool * pool, int timeout) { | ||
if (pool->queue_head >= pool->queue_len) { | ||
if (_read_queue(pool, timeout) <= 0) { | ||
return NULL; | ||
} | ||
} | ||
return pool->ev[pool->queue_head ++].data.ptr; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#ifndef SKYNET_CONNECTION_H | ||
#define SKYNET_CONNECTION_H | ||
|
||
#include <stddef.h> | ||
|
||
struct connection_pool; | ||
|
||
struct connection_pool * connection_newpool(int max); | ||
void connection_deletepool(struct connection_pool *); | ||
|
||
int connection_add(struct connection_pool *, int fd, void *ud); | ||
void connection_del(struct connection_pool *, int fd); | ||
|
||
void * connection_poll(struct connection_pool *, int timeout); | ||
|
||
#endif |
Oops, something went wrong.