Skip to content

Commit

Permalink
* Doppelte Kompression-Start-Pakete ignorieren
Browse files Browse the repository at this point in the history
 * Kicken ueberarbeitet


git-svn-id: http://infon.googlecode.com/svn/trunk@52 8171fb75-e542-0410-96e4-03d5dd800671
  • Loading branch information
dividuum committed Aug 6, 2006
1 parent 5e524bc commit 90a3382
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 52 deletions.
20 changes: 9 additions & 11 deletions client.c
Expand Up @@ -66,7 +66,10 @@ int client_is_connected();

static void client_read_handshake(packet_t *packet) {
uint8_t serverprotocol;
if (!packet_read08(packet, &serverprotocol)) PROTOCOL_ERROR();

if (!packet_read08(packet, &serverprotocol))
PROTOCOL_ERROR();

if (serverprotocol != PROTOCOL_VERSION) {
die("server has %s protocol version %d. I have %d.\n"
"visit the infon homepage for more information.",
Expand All @@ -76,6 +79,9 @@ static void client_read_handshake(packet_t *packet) {
}

static void client_start_compression() {
if (compression)
PROTOCOL_ERROR();

strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = NULL;
Expand Down Expand Up @@ -226,9 +232,8 @@ void client_destroy(char *reason) {
evbuffer_free(in_buf);
evbuffer_free(out_buf);
evbuffer_free(packet_buf);
if (compression) {
if (compression)
inflateEnd(&strm);
}
event_del(&rd_event);
event_del(&wr_event);
close(clientfd);
Expand All @@ -255,14 +260,7 @@ void client_init(char *addr) {
int port = 1234;
struct hostent *host;

#ifdef WIN32
char *colon = addr;
while (*colon && *colon != ':')
colon++;
if (!*colon) colon = NULL;
#else
char *colon = index(addr, ':');
#endif
char *colon = strchr(addr, ':');
if (colon) {
*colon = '\0';
port = atoi(colon + 1);
Expand Down
2 changes: 2 additions & 0 deletions gui_creature.c
Expand Up @@ -104,6 +104,8 @@ void gui_creature_draw() {
video_line(x + 6, y + 6, X_TO_SCREENX(target->x) + 3 + xoff, Y_TO_SCREENY(target->y) + 3 + yoff);
}
}
//gui_world_center_set(creature->x * SPRITE_TILE_SIZE / TILE_WIDTH,
// creature->y * SPRITE_TILE_SIZE / TILE_HEIGHT);
}
}

Expand Down
2 changes: 1 addition & 1 deletion gui_creature.h
Expand Up @@ -62,7 +62,7 @@ void gui_creature_draw();
void gui_creature_move(int delta);

/* Network */
void gui_creature_from_network(packet_t *packet);
void gui_creature_from_network(packet_t *packet);

void gui_creature_init();
void gui_creature_shutdown();
Expand Down
9 changes: 7 additions & 2 deletions gui_world.c
Expand Up @@ -139,7 +139,12 @@ void gui_world_center_change(int dx, int dy) {
center_y += dy;
}

void gui_world_center() {
void gui_world_center_set(int x, int y) {
center_x = x;
center_y = y;
}

void gui_world_recenter() {
center_x = world_w * SPRITE_TILE_SIZE / 2;
center_y = world_h * SPRITE_TILE_SIZE / 2;
}
Expand Down Expand Up @@ -199,7 +204,7 @@ void gui_world_info_from_network(packet_t *packet) {
}
}

gui_world_center();
gui_world_recenter();
initialized = 1;
}

Expand Down
3 changes: 2 additions & 1 deletion gui_world.h
Expand Up @@ -25,8 +25,9 @@
#include "common_world.h"

void gui_world_draw();
void gui_world_center();
void gui_world_recenter();
void gui_world_center_change(int dx, int dy);
void gui_world_center_set(int x, int y);

int gui_world_x_offset();
int gui_world_y_offset();
Expand Down
2 changes: 1 addition & 1 deletion infon.c
Expand Up @@ -129,7 +129,7 @@ int main(int argc, char *argv[]) {
case SDLK_4: video_resize(1280, 1024); break;
case SDLK_5: video_resize(1600, 1200); break;
case SDLK_c:
gui_world_center();
gui_world_recenter();
break;
case SDLK_ESCAPE:
running = 0;
Expand Down
48 changes: 39 additions & 9 deletions server.c
Expand Up @@ -115,7 +115,8 @@ int server_accept(int fd, struct sockaddr_in *peer) {
clients[fd].out_buf = evbuffer_new();

clients[fd].compress = 0;


clients[fd].kill_me = NULL;
clients[fd].player = NULL;

clients[fd].next = NULL;
Expand All @@ -141,6 +142,12 @@ static void server_readable(int fd, short event, void *arg) {
struct event *cb_event = arg;
client_t *client = &clients[fd];

// Der Client wurde 'extern' gekickt, allerdings noch
// nicht entfernt. Dann wird dieser Readcallback aufgerufen,
// sollte allerdings nichts mehr machen.
if (client->kill_me)
return;

int ret = evbuffer_read(client->in_buf, fd, 128);
if (ret < 0) {
server_destroy(client, strerror(errno));
Expand All @@ -157,18 +164,17 @@ static void server_readable(int fd, short event, void *arg) {
lua_pushstring(L, line);
free(line);

if (lua_pcall(L, 2, 1, 0) != 0) {
if (lua_pcall(L, 2, 0, 0) != 0) {
fprintf(stderr, "error calling on_client_input: %s\n", lua_tostring(L, -1));
server_writeto(client, lua_tostring(L, -1), lua_strlen(L, -1));
}

if (!lua_toboolean(L, -1)) {
lua_pop(L, 1);
server_destroy(client, "user closed");
// Kill Me Flag waehrend Aufruf von on_client_input
// gesetzt? Direkt rausschmeissen!
if (client->kill_me) {
server_destroy(client, client->kill_me);
return;
}

lua_pop(L, 1);
}
event_add(cb_event, NULL);
}
Expand Down Expand Up @@ -295,9 +301,11 @@ void server_destroy(client_t *client, char *reason) {
evbuffer_free(client->in_buf);
evbuffer_free(client->out_buf);

if (client->compress) {
if (client->kill_me)
free(client->kill_me);

if (client->compress)
deflateEnd(&client->strm);
}

event_del(&client->rd_event);
event_del(&client->wr_event);
Expand Down Expand Up @@ -434,6 +442,15 @@ static int luaScrollerAdd(lua_State *L) {
return 0;
}

static int luaClientDisconnect(lua_State *L) {
client_t *client = client_get_checked_lua(L, luaL_checklong(L, 1));
const char *reason = luaL_checkstring(L, 2);
if (client->kill_me)
free(client->kill_me);
client->kill_me = strdup(reason);
return 0;
}

void server_tick() {
lua_pushliteral(L, "server_tick");
lua_rawget(L, LUA_GLOBALSINDEX);
Expand All @@ -443,6 +460,18 @@ void server_tick() {
}

event_loop(EVLOOP_NONBLOCK);

// Ungefaehr jede Sekunde alle zu kickenden Clients entfernen.
static int kicktick = 0;
if (++kicktick % 10 == 0) {
int clientno;
for (clientno = 0; clientno < MAXCLIENTS; clientno++) {
if (!CLIENT_USED(&clients[clientno]))
continue;
if (clients[clientno].kill_me)
server_destroy(&clients[clientno], clients[clientno].kill_me);
}
}
}

void server_init() {
Expand All @@ -465,6 +494,7 @@ void server_init() {
lua_register(L, "client_make_guiclient", luaClientMakeGuiClient);
lua_register(L, "client_is_gui_client", luaClientIsGuiClient);
lua_register(L, "client_player_number", luaClientPlayerNumber);
lua_register(L, "client_disconnect", luaClientDisconnect);

lua_register(L, "scroller_add", luaScrollerAdd);

Expand Down
2 changes: 2 additions & 0 deletions server.h
Expand Up @@ -48,6 +48,8 @@ typedef struct client_s {
int compress;
z_stream strm;

char *kill_me;

// Verbindung von: 1-player <-> N-client
struct player_s *player;
struct client_s *next;
Expand Down
12 changes: 6 additions & 6 deletions server.lua
Expand Up @@ -173,14 +173,14 @@ function Client:killmenu()
end

function Client:shell()
if debugpass == "" then
self:writeln("password must be set in config.lua")
return
end
local ok = false
if self.authorized then
ok = true
else
if debugpass == "" then
self:writeln("password must be set in config.lua")
return
end
self:write("password: ")
ok = self:readln() == debugpass
end
Expand Down Expand Up @@ -236,7 +236,7 @@ function Client:mainmenu()
self:write(prompt)
local input = self:readln()
if input == "q" then
self:disconnect()
self:disconnect("quitting")
break
elseif input == "j" then
self:joinmenu()
Expand Down Expand Up @@ -299,6 +299,7 @@ end

function Client:handler()
if self.fd == 0 then -- XXX: HACK, stdin client
self.authorized = true
self:writeln("")
else
self:welcome("Press <enter>")
Expand Down Expand Up @@ -347,4 +348,3 @@ function ServerMain()
coroutine.yield()
end
end

30 changes: 9 additions & 21 deletions serverlib.lua
Expand Up @@ -52,13 +52,12 @@ end

function Client:on_new_client(addr)
self.addr = addr
self.finished = false
print(self.addr .. " accepted")
scroller_add(self.addr .. " joined")
self.thread = coroutine.create(self.handler)
local ok, msg = coroutine.resume(self.thread, self)
if not ok then
self:writeln(msg)
self:disconnect(msg)
end
end

Expand All @@ -76,15 +75,15 @@ function Client:on_input(line)
self.thread = coroutine.create(self.handler)
local ok, msg = coroutine.resume(self.thread, self)
if not ok then
self:writeln(msg)
return false
self:disconnect(msg)
end
end
return not self.finished
end

function Client:disconnect()
self.finished = true
function Client:disconnect(reason)
if self.fd ~= 0 then
client_disconnect(self.fd, reason)
end
end

function Client:write(data)
Expand Down Expand Up @@ -209,19 +208,14 @@ function on_client_accepted(fd, addr)
end

function on_client_input(fd, line)
if clients[fd].kill_me then
return false
else
return clients[fd]:on_input(line)
end
clients[fd]:on_input(line)
end

function on_client_close(fd, reason)
clients[fd]:on_destroy(reason)
clients[fd] = nil
end


function server_tick()
server_tick = coroutine.wrap(ServerMain)
end
Expand Down Expand Up @@ -253,13 +247,7 @@ function clientlist(adminfd)
end

function kick(fd, msg)
-- XXX: momentan bis zur naechsten eingabe delayed...
if client_is_gui_client(fd) then
-- XXX: TODO
else
client_write(fd, msg .. "\n")
end
clients[fd].kill_me = true
clients[fd]:disconnect(msg)
end

function killall()
Expand All @@ -278,6 +266,6 @@ end

function kickall()
table.foreach(clients, function (fd, obj)
clients[fd].kill_me = true
clients[fd]:disconnect("kicked")
end)
end

0 comments on commit 90a3382

Please sign in to comment.