Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug fix, refers to mread.c event_add() and return value. #30

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ all : \
luaclib/mcast.so \
luaclib/bson.so \
luaclib/mongo.so \
luaclib/dataswap.so \
client

skynet : \
Expand Down Expand Up @@ -83,7 +84,7 @@ luaclib/skynet.so : lualib-src/lua-skynet.c lualib-src/lua-seri.c lualib-src/lua
gcc $(CFLAGS) $(SHARED) -Iluacompat $^ -o $@ -Iskynet-src -Iservice-src -Ilualib-src

service/client.so : service-src/service_client.c
gcc $(CFLAGS) $(SHARED) $^ -o $@ -Iskynet-src
gcc $(CFLAGS) $(SHARED) $^ -o $@ -Iskynet-src -Ilualib-src

service/socket.so : service-src/service_socket.c
gcc $(CFLAGS) $(SHARED) $^ -o $@ -Iskynet-src
Expand All @@ -103,9 +104,11 @@ luaclib/bson.so : lualib-src/lua-bson.c | luaclib
luaclib/mongo.so : lualib-src/lua-mongo.c | luaclib
gcc $(CFLAGS) $(SHARED) -Iluacompat $^ -o $@

luaclib/dataswap.so : lualib-src/lua-dataswap.c | luaclib
gcc $(CFLAGS) $(SHARED) -Iluacompat $^ -o $@

client : client-src/client.c
gcc $(CFLAGS) $^ -o $@ -lpthread

clean :
rm skynet client service/*.so luaclib/*.so

42 changes: 40 additions & 2 deletions client-src/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@
#include <string.h>
#include <errno.h>

#define _SEND_DATASWAP_SERVICE_EXAMPLE

#ifdef _SEND_DATASWAP_SERVICE_EXAMPLE
struct s_mydata {
int a;
int b;
int c;
int d;
};
#endif

struct args {
int fd;
};
Expand Down Expand Up @@ -44,8 +55,13 @@ _read(void *ud) {
if (len>0) {
char tmp[len+1];
readall(fd, tmp, len);
#ifndef _SEND_DATASWAP_SERVICE_EXAMPLE
tmp[len]='\0';
printf("%s\n",tmp);
#else
struct s_mydata *data = (struct s_mydata*)tmp;
printf("recv: %d %d %d %d\n", data->a, data->b, data->c, data->d);
#endif
}
}
return NULL;
Expand All @@ -57,6 +73,8 @@ test(void *ud) {
int fd = p->fd;

char tmp[1024];

#ifndef _SEND_DATASWAP_SERVICE_EXAMPLE
while (!feof(stdin)) {
fgets(tmp,sizeof(tmp),stdin);
int n = strlen(tmp) -1;
Expand All @@ -72,7 +90,27 @@ test(void *ud) {
if (r<0) {
perror("send data");
}
}
}
#else
int i = 0;
uint8_t head[2] = {0, 0};
for (i = 0; i < 128; i+=4) {

sleep(1);

struct s_mydata *data = (struct s_mydata*)tmp;

data->a = i;
data->b = i + 1;
data->c = i + 2;
data->d = i + 3;

head[1] = sizeof(*data);
send(fd, head, 2, 0);
send(fd, tmp , (int)head[1], 0);
printf("send svr sz %d, cnt %d %d %d %d, ", (int)head[1], data->a, data->b, data->c, data->d);
}
#endif
return NULL;
}

Expand Down Expand Up @@ -108,4 +146,4 @@ main(int argc, char * argv[]) {
close(fd);

return 0;
}
}
67 changes: 67 additions & 0 deletions lualib-src/lua-dataswap.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/* lua-dataswap.c ---
*
* Copyright (c) 2013 chale.suu@gmail.com
*
* Author: lalawue
* Maintainer:
*
* Created: 2013/08/15 12:24
* Last-Updated: 2013/08/15 23:50
*
*/

/* Commentary:
*
*
*/

/* Code: */

#include <lua.h>
#include <lauxlib.h>
#include "luacompat52.h"

#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "lua-dataswap.h"

static int
_op_swap(lua_State *L) {
void *msg = lua_touserdata(L, 1);
/* size_t sz = luaL_checkinteger(L, 2); */

struct s_mydata *data = (struct s_mydata*)msg;

printf("mydata recv %d %d %d %d\n", data->a, data->b, data->c, data->d);

int tmp = data->a;
data->a = data->d;
data->d = tmp;

tmp = data->b;
data->b = data->c;
data->c = tmp;

lua_pushlightuserdata(L, data);
lua_pushinteger(L, sizeof(*data));

return 2;
}


int
luaopen_dataswap_c(lua_State *L) {
luaL_checkversion(L);
luaL_Reg l[] = {
{ "swap", _op_swap },
{ NULL, NULL },
};

luaL_newlib(L, l);
return 1;
}

/* lua-dataswap.c ends here */
32 changes: 32 additions & 0 deletions lualib-src/lua-dataswap.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/* lua-dataswap.h ---
*
* Copyright (c) 2013 chale.suu@gmail.com
*
* Author: lalawue
* Maintainer:
*
* Created: 2013/08/15 12:34
* Last-Updated: 2013/08/15 23:50
*
*/

/* Commentary:
*
*
*/

/* Code: */

#ifndef LUA_MYDATA_H
#define LUA_MYDATA_H

struct s_mydata {
int a;
int b;
int c;
int d;
};

#endif

/* lua-dataswap.h ends here */
4 changes: 2 additions & 2 deletions service-src/event.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ event_add(int efd, int sock, void *ud) {
ev.events = EPOLLIN;
ev.data.ptr = ud;
if (epoll_ctl(efd, EPOLL_CTL_ADD, sock, &ev) == -1) {
return 1;
return -1;
}
return 0;
}
Expand Down Expand Up @@ -93,7 +93,7 @@ event_add(int efd, int sock, void *ud) {
struct kevent ke;
EV_SET(&ke, sock, EVFILT_READ, EV_ADD, 0, 0, ud);
if (kevent(efd, &ke, 1, NULL, 0, NULL) == -1) {
return 1;
return -1;
}
return 0;
}
Expand Down
55 changes: 40 additions & 15 deletions service/agent.lua
Original file line number Diff line number Diff line change
@@ -1,19 +1,44 @@
local skynet = require "skynet"
local client = ...

skynet.register_protocol {
name = "client",
id = 3,
pack = function(...) return ... end,
unpack = skynet.tostring,
dispatch = function (session, address, text)
-- It's client, there is no session
skynet.send("LOG", "text", "client message :" .. text)
local result = skynet.call("SIMPLEDB", "text", text)
skynet.ret(result)
end
}
local dataswap_example = true

skynet.start(function()
skynet.send(client,"text","Welcome to skynet")
end)
if not dataswap_example then
print("---- SIMPLEDB agent ----")
skynet.register_protocol {
name = "client",
id = 3,
pack = function(...) return ... end,
unpack = skynet.tostring,
dispatch = function (session, address, text)
-- It's client, there is no session
skynet.send("LOG", "text", "client message :" .. text)
local result = skynet.call("SIMPLEDB", "text", text)
skynet.ret(result)
end
}

skynet.start(function()
skynet.send(client,"text","Welcome to skynet")
end)
else
print("---- DATASWAP agent ----")
skynet.register_protocol {
name = "client",
id = 3,
pack = function(...) return ... end,
unpack = function(...) return ... end,
dispatch =
function (session, address, msg, sz)
-- It's client, there is no session
skynet.send("LOG", "text", "dataswap message type :", type(msg), sz)
local result = skynet.rawcall("DATASWAP", "client", msg, sz)
skynet.ret(result, sz)
end
}

skynet.start(
function()
skynet.send(client,"text","Welcome to skynet")
end)
end
18 changes: 18 additions & 0 deletions service/dataswap.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
local skynet = require "skynet"
local dataswap = require "dataswap.c"

skynet.register_protocol {
name = "client",
id = 3,
pack = function(...) return ... end,
unpack = function(...) return ... end
}

skynet.start(function()
skynet.dispatch(
"client",
function(session, address, message, sz)
skynet.ret(dataswap.swap(message), sz)
end)
skynet.register "DATASWAP"
end)
3 changes: 2 additions & 1 deletion service/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ skynet.start(function()
local console = skynet.newservice("console")
local remoteroot = skynet.newservice("remote_root")
local watchdog = skynet.newservice("watchdog","8888 4 0")
local db = skynet.newservice("simpledb")
-- local db = skynet.newservice("simpledb")
local db = skynet.newservice("dataswap")
-- skynet.launch("snlua","testgroup")

skynet.exit()
Expand Down
9 changes: 5 additions & 4 deletions skynet-src/skynet_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ skynet_forward(struct skynet_context * context, uint32_t destination) {

static void
_filter_args(struct skynet_context * context, int type, int *session, void ** data, size_t * sz) {
int dontcopy = type & PTYPE_TAG_DONTCOPY;
/* int dontcopy = type & PTYPE_TAG_DONTCOPY; */
int allocsession = type & PTYPE_TAG_ALLOCSESSION;
type &= 0xff;

Expand All @@ -567,9 +567,10 @@ _filter_args(struct skynet_context * context, int type, int *session, void ** da
}

char * msg;
if (dontcopy || *data == NULL) {
msg = *data;
} else {
/* if (dontcopy || *data == NULL) { */
/* msg = *data; */
/* } else */
{
msg = malloc(*sz+1);
memcpy(msg, *data, *sz);
msg[*sz] = '\0';
Expand Down